[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> meta.php (source)

   1  <?php
   2  /**
   3   * Meta and options functions
   4   */
   5  
   6  
   7  /* Internal */
   8  
   9  function gp_sanitize_meta_key( $key ) {
  10      return preg_replace( '|[^a-z0-9_]|i', '', $key );
  11  }
  12  
  13  /**
  14   * Adds and updates meta data in the database
  15   *
  16   * @internal
  17   */
  18  function gp_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
  19      global $gpdb;
  20      if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
  21          return false;
  22      }
  23      $cache_object_id = $object_id = (int) $object_id;
  24      switch ( $type ) {
  25          case 'option':
  26              $object_type = 'gp_option';
  27              break;
  28          case 'user' :
  29              global $wp_users_object;
  30              $id = $object_id;
  31              $return = $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) );
  32              if ( is_wp_error( $return ) ) {
  33                  return false;
  34              }
  35              return $return;
  36              break;
  37          default :
  38              $object_type = $type;
  39              break;
  40      }
  41  
  42      $meta_key = gp_sanitize_meta_key( $meta_key );
  43  
  44      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
  45      $meta_tuple = apply_filters( 'gp_update_meta', $meta_tuple );
  46      extract( $meta_tuple, EXTR_OVERWRITE );
  47  
  48      $meta_value = $_meta_value = maybe_serialize( $meta_value );
  49      $meta_value = maybe_unserialize( $meta_value );
  50  
  51      $cur = $gpdb->get_row( $gpdb->prepare( "SELECT * FROM `$gpdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) );
  52      if ( !$cur ) {
  53          $gpdb->insert( $gpdb->meta, array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $_meta_value ) );
  54      } elseif ( $cur->meta_value != $meta_value ) {
  55          $gpdb->update( $gpdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
  56      }
  57  
  58      if ( $object_type === 'gp_option' ) {
  59          $cache_object_id = $meta_key;
  60          wp_cache_delete( $cache_object_id, 'gp_option_not_set' );
  61      }
  62      wp_cache_delete( $cache_object_id, $object_type );
  63  
  64      if ( !$cur ) {
  65          return true;
  66      }
  67  }
  68  
  69  /**
  70   * Deletes meta data from the database
  71   *
  72   * @internal
  73   */
  74  function gp_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false )
  75  {
  76      global $gpdb;
  77      if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
  78          return false;
  79      }
  80      $cache_object_id = $object_id = (int) $object_id;
  81      switch ( $type ) {
  82          case 'option':
  83              $object_type = 'gp_option';
  84              break;
  85          case 'user':
  86              global $wp_users_object;
  87              $id = $object_id;
  88              return $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) );
  89              break;
  90          default:
  91              $object_type = $type;
  92              break;
  93      }
  94  
  95      $meta_key = gp_sanitize_meta_key( $meta_key );
  96  
  97      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
  98      $meta_tuple = apply_filters( 'gp_delete_meta', $meta_tuple );
  99      extract( $meta_tuple, EXTR_OVERWRITE );
 100  
 101      $meta_value = maybe_serialize( $meta_value );
 102  
 103      if ( empty( $meta_value ) ) {
 104          $meta_sql = $gpdb->prepare( "SELECT `meta_id` FROM `$gpdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key );
 105      } else {
 106          $meta_sql = $gpdb->prepare( "SELECT `meta_id` FROM `$gpdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s AND `meta_value` = %s", $object_type, $object_id, $meta_key, $meta_value );
 107      }
 108  
 109      if ( !$meta_id = $gpdb->get_var( $meta_sql ) ) {
 110          return false;
 111      }
 112  
 113      $gpdb->query( $gpdb->prepare( "DELETE FROM `$gpdb->meta` WHERE `meta_id` = %d", $meta_id ) );
 114  
 115      if ( $object_type == 'gp_option' ) {
 116          $cache_object_id = $meta_key;
 117          wp_cache_delete( $cache_object_id, 'gp_option_not_set' );
 118      }
 119      wp_cache_delete( $cache_object_id, $object_type );
 120      return true;
 121  }
 122  
 123  /**
 124   * Adds an objects meta data to the object
 125   *
 126   * This is the only function that should add to user / topic - NOT gpdb::prepared
 127   *
 128   * @internal
 129   */
 130  function gp_append_meta( $object, $type )
 131  {
 132      global $gpdb;
 133      switch ( $type ) {
 134          case 'user':
 135              global $wp_users_object;
 136              return $wp_users_object->append_meta( $object );
 137              break;
 138      }
 139  
 140      if ( is_array( $object ) && $object ) {
 141          $trans = array();
 142          foreach ( array_keys( $object ) as $i ) {
 143              $trans[$object[$i]->$object_id_column] =& $object[$i];
 144          }
 145          $ids = join( ',', array_map( 'intval', array_keys( $trans ) ) );
 146          if ( $metas = $gpdb->get_results( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$gpdb->meta` WHERE `object_type` = '$object_type' AND `object_id` IN ($ids) /* gp_append_meta */" ) ) {
 147              usort( $metas, '_gp_append_meta_sort' );
 148              foreach ( $metas as $meta ) {
 149                  $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
 150                  if ( strpos($meta->meta_key, $gpdb->prefix) === 0 ) {
 151                      $trans[$meta->object_id]->{substr($meta->meta_key, strlen($gpdb->prefix))} = maybe_unserialize( $meta->meta_value );
 152                  }
 153              }
 154          }
 155          foreach ( array_keys( $trans ) as $i ) {
 156              wp_cache_add( $i, $trans[$i], $object_type );
 157              if ( $slug ) {
 158                  wp_cache_add( $trans[$i]->$slug, $i, 'gp_' . $slug );
 159              }
 160          }
 161          return $object;
 162      } elseif ( $object ) {
 163          if ( $metas = $gpdb->get_results( $gpdb->prepare( "SELECT `meta_key`, `meta_value` FROM `$gpdb->meta` WHERE `object_type` = '$object_type' AND `object_id` = %d /* gp_append_meta */", $object->$object_id_column ) ) ) {
 164              usort( $metas, '_gp_append_meta_sort' );
 165              foreach ( $metas as $meta ) {
 166                  $object->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
 167                  if ( strpos( $meta->meta_key, $gpdb->prefix ) === 0 ) {
 168                      $object->{substr( $meta->meta_key, strlen( $gpdb->prefix ) )} = $object->{$meta->meta_key};
 169                  }
 170              }
 171          }
 172          if ( $object->$object_id_column ) {
 173              wp_cache_set( $object->$object_id_column, $object, $object_type );
 174              if ( $slug ) {
 175                  wp_cache_add( $object->$slug, $object->$object_id_column, 'gp_' . $slug );
 176              }
 177          }
 178          return $object;
 179      }
 180  }
 181  
 182  /**
 183   * Sorts meta keys by length to ensure $appended_object->{$gpdb->prefix} key overwrites $appended_object->key as desired
 184   *
 185   * @internal
 186   */
 187  function _gp_append_meta_sort( $a, $b )
 188  {
 189      return strlen( $a->meta_key ) - strlen( $b->meta_key );
 190  }
 191  
 192  
 193  
 194  /* Options */
 195  
 196  /**
 197   * Echoes the requested bbPress option by calling gp_get_option()
 198   *
 199   * @param string The option to be echoed
 200   * @return void
 201   */
 202  function gp_option( $option )
 203  {
 204      echo apply_filters( 'gp_option_' . $option, gp_get_option( $option ) );
 205  }
 206  
 207  /**
 208   * Returns the requested bbPress option from the meta table or the $bb object
 209   *
 210   * @param string The option to be echoed
 211   * @return mixed The value of the option
 212   */
 213  function gp_get_option( $option ) {
 214      global $bb;
 215  
 216      switch ( $option ) {
 217          case 'language':
 218              $r = str_replace( '_', '-', get_locale() );
 219              break;
 220          case 'text_direction':
 221              global $gp_locale;
 222              $r = $gp_locale->text_direction;
 223              break;
 224          case 'version':
 225              return '0.1'; // Don't filter
 226              break;
 227          case 'gp_db_version' :
 228              return '569'; // Don't filter
 229              break;
 230          case 'html_type':
 231              $r = 'text/html';
 232              break;
 233          case 'charset':
 234              $r = 'UTF-8';
 235              break;
 236          case 'gp_table_prefix':
 237          case 'table_prefix':
 238              global $gpdb;
 239              return $gpdb->prefix; // Don't filter;
 240              break;
 241          case 'url':
 242              $option = 'uri';
 243          default:
 244              $r = gp_get_option_from_db( $option );
 245              break;
 246      }
 247  
 248      return apply_filters( 'gp_get_option_' . $option, $r, $option );
 249  }
 250  
 251  /**
 252   * Retrieves and returns the requested bbPress option from the meta table
 253   *
 254   * @param string The option to be echoed
 255   * @return void
 256   */
 257  function gp_get_option_from_db( $option ) {
 258      global $gpdb;
 259      $option = gp_sanitize_meta_key( $option );
 260  
 261      if ( wp_cache_get( $option, 'gp_option_not_set' ) ) {
 262          $r = null;
 263      } elseif ( false !== $_r = wp_cache_get( $option, 'gp_option' ) ) {
 264          $r = $_r;
 265      } else {
 266          if ( defined( 'GP_INSTALLING' ) && GP_INSTALLING ) {
 267              $gpdb->suppress_errors();
 268          }
 269          $row = $gpdb->get_row( $gpdb->prepare( "SELECT `meta_value` FROM `$gpdb->meta` WHERE `object_type` = 'gp_option' AND `meta_key` = %s", $option ) );
 270          if ( defined( 'GP_INSTALLING' ) && GP_INSTALLING ) {
 271              $gpdb->suppress_errors( false );
 272          }
 273  
 274          if ( is_object( $row ) ) {
 275              $r = maybe_unserialize( $row->meta_value );
 276          } else {
 277              $r = null;
 278          }
 279      }
 280  
 281      if ( $r === null ) {
 282          wp_cache_set( $option, true, 'gp_option_not_set' );
 283      } else {
 284          wp_cache_set( $option, $r, 'gp_option' );
 285      }
 286  
 287      return apply_filters( 'gp_get_option_from_db_' . $option, $r, $option );
 288  }
 289  
 290  // Don't use the return value; use the API. Only returns options stored in DB.
 291  function gp_cache_all_options()
 292  {
 293      global $gpdb;
 294      $results = $gpdb->get_results( "SELECT `meta_key`, `meta_value` FROM `$gpdb->meta` WHERE `object_type` = 'gp_option'" );
 295  
 296      if ( !$results || !is_array( $results ) || !count( $results ) ) {
 297          return false;
 298      } else {
 299          foreach ( $results as $options ) {
 300              wp_cache_set( $options->meta_key, maybe_unserialize( $options->meta_value ), 'gp_option' );
 301          }
 302      }
 303  
 304      // TODO: leave only the GlotPress options here
 305      $base_options = array(
 306          'gp_db_version',
 307          'name',
 308          'description',
 309          'uri_ssl',
 310          'from_email',
 311          'gp_auth_salt',
 312          'gp_secure_auth_salt',
 313          'gp_logged_in_salt',
 314          'gp_nonce_salt',
 315          'page_topics',
 316          'edit_lock',
 317          'gp_active_theme',
 318          'active_plugins',
 319          'mod_rewrite',
 320          'datetime_format',
 321          'date_format',
 322          'avatars_show',
 323          'avatars_default',
 324          'avatars_rating',
 325          'wp_table_prefix',
 326          'user_gpdb_name',
 327          'user_gpdb_user',
 328          'user_gpdb_password',
 329          'user_gpdb_host',
 330          'user_gpdb_charset',
 331          'user_gpdb_collate',
 332          'custom_user_table',
 333          'custom_user_meta_table',
 334          'wp_siteurl',
 335          'wp_home',
 336          'cookiedomain',
 337          'usercookie',
 338          'passcookie',
 339          'authcookie',
 340          'cookiepath',
 341          'sitecookiepath',
 342          'secure_auth_cookie',
 343          'logged_in_cookie',
 344          'admin_cookie_path',
 345          'core_plugins_cookie_path',
 346          'user_plugins_cookie_path',
 347          'wp_admin_cookie_path',
 348          'wp_plugins_cookie_path',
 349          'enable_xmlrpc',
 350          'enable_pingback',
 351          'throttle_time',
 352          'gp_xmlrpc_allow_user_switching',
 353          'bp_bbpress_cron'
 354      );
 355  
 356      foreach ( $base_options as $base_option ) {
 357          if ( false === wp_cache_get( $base_option, 'gp_option' ) ) {
 358              wp_cache_set( $base_option, true, 'gp_option_not_set' );
 359          }
 360      }
 361  
 362      return true;
 363  }
 364  
 365  // Can store anything but NULL.
 366  function gp_update_option( $option, $value ) {
 367      return gp_update_meta( 0, $option, $value, 'option', true );
 368  }
 369  
 370  function gp_delete_option( $option, $value = '' ) {
 371      return gp_delete_meta( 0, $option, $value, 'option', true );
 372  }


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.