[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> functions.bb-meta.php (source)

   1  <?php
   2  
   3  /* Options/Meta */
   4  
   5  /* Internal */
   6  
   7  function bb_sanitize_meta_key( $key )
   8  {
   9      return preg_replace( '|[^a-z0-9_]|i', '', $key );
  10  }
  11  
  12  /**
  13   * Adds and updates meta data in the database
  14   *
  15   * @internal
  16   */
  17  function bb_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false )
  18  {
  19      global $bbdb;
  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 = 'bb_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          case 'forum' :
  38              $object_type = 'bb_forum';
  39              break;
  40          case 'topic' :
  41              $object_type = 'bb_topic';
  42              break;
  43          case 'post' :
  44              $object_type = 'bb_post';
  45              break;
  46          default :
  47              $object_type = $type;
  48              break;
  49      }
  50  
  51      $meta_key = bb_sanitize_meta_key( $meta_key );
  52  
  53      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
  54      $meta_tuple = apply_filters( 'bb_update_meta', $meta_tuple );
  55      extract( $meta_tuple, EXTR_OVERWRITE );
  56  
  57      $meta_value = $_meta_value = maybe_serialize( $meta_value );
  58      $meta_value = maybe_unserialize( $meta_value );
  59  
  60      $cur = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) );
  61      if ( !$cur ) {
  62          $bbdb->query( $bbdb->prepare(
  63              "INSERT INTO `$bbdb->meta` ( `object_type`, `object_id`, `meta_key`, `meta_value` ) VALUES( %s, %d, %s, %s )
  64              ON DUPLICATE KEY UPDATE `meta_value` = VALUES( `meta_value` )",
  65              $object_type, $object_id, $meta_key, $_meta_value
  66          ) );
  67      } elseif ( $cur->meta_value != $meta_value ) {
  68          $bbdb->update( $bbdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
  69      }
  70  
  71      if ( $object_type === 'bb_option' ) {
  72          $cache_object_id = $meta_key;
  73          wp_cache_delete( $cache_object_id, 'bb_option_not_set' );
  74      }
  75      wp_cache_delete( $cache_object_id, $object_type );
  76  
  77      if ( !$cur ) {
  78          return true;
  79      }
  80  }
  81  
  82  /**
  83   * Deletes meta data from the database
  84   *
  85   * @internal
  86   */
  87  function bb_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false )
  88  {
  89      global $bbdb;
  90      if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
  91          return false;
  92      }
  93      $cache_object_id = $object_id = (int) $object_id;
  94      switch ( $type ) {
  95          case 'option':
  96              $object_type = 'bb_option';
  97              break;
  98          case 'user':
  99              global $wp_users_object;
 100              $id = $object_id;
 101              return $wp_users_object->delete_meta( compact( 'id', 'meta_key', 'meta_value' ) );
 102              break;
 103          case 'forum':
 104              $object_type = 'bb_forum';
 105              break;
 106          case 'topic':
 107              $object_type = 'bb_topic';
 108              break;
 109          case 'post':
 110              $object_type = 'bb_post';
 111              break;
 112          default:
 113              $object_type = $type;
 114              break;
 115      }
 116  
 117      $meta_key = bb_sanitize_meta_key( $meta_key );
 118  
 119      $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
 120      $meta_tuple = apply_filters( 'bb_delete_meta', $meta_tuple );
 121      extract( $meta_tuple, EXTR_OVERWRITE );
 122  
 123      $meta_value = maybe_serialize( $meta_value );
 124  
 125      if ( empty( $meta_value ) ) {
 126          $meta_sql = $bbdb->prepare( "SELECT `meta_id` FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key );
 127      } else {
 128          $meta_sql = $bbdb->prepare( "SELECT `meta_id` FROM `$bbdb->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 );
 129      }
 130  
 131      if ( !$meta_id = $bbdb->get_var( $meta_sql ) ) {
 132          return false;
 133      }
 134  
 135      $bbdb->query( $bbdb->prepare( "DELETE FROM `$bbdb->meta` WHERE `meta_id` = %d", $meta_id ) );
 136  
 137      if ( $object_type == 'bb_option' ) {
 138          $cache_object_id = $meta_key;
 139          wp_cache_delete( $cache_object_id, 'bb_option_not_set' );
 140      }
 141      wp_cache_delete( $cache_object_id, $object_type );
 142      return true;
 143  }
 144  
 145  /**
 146   * Adds an objects meta data to the object
 147   *
 148   * This is the only function that should add to user / topic - NOT bbdb::prepared
 149   *
 150   * @internal
 151   */
 152  function bb_append_meta( $object, $type )
 153  {
 154      global $bbdb;
 155      switch ( $type ) {
 156          case 'user':
 157              global $wp_users_object;
 158              return $wp_users_object->append_meta( $object );
 159              break;
 160          case 'forum':
 161              $object_id_column = 'forum_id';
 162              $object_type = 'bb_forum';
 163              $slug = 'forum_slug';
 164              break;
 165          case 'topic':
 166              $object_id_column = 'topic_id';
 167              $object_type = 'bb_topic';
 168              $slug = 'topic_slug';
 169              break;
 170          case 'post':
 171              $object_id_column = 'post_id';
 172              $object_type = 'bb_post';
 173              $slug = false;
 174              break;
 175      }
 176  
 177      if ( is_array( $object ) && count( $object ) ) {
 178          $trans = array();
 179          foreach ( array_keys( $object ) as $i ) {
 180              $trans[$object[$i]->$object_id_column] =& $object[$i];
 181          }
 182          $ids = array_map( 'intval', array_keys( $trans ) );
 183          $query_ids = array();
 184          $cached_objects = array();
 185          $position = 0;
 186          foreach ( $ids as $_id ) {
 187              if ( false !== $_cached_object = wp_cache_get( $_id, $object_type ) ) {
 188                  $cached_objects[$position] = $_cached_object;
 189              } else {
 190                  $query_ids[$position] = $_id;
 191              }
 192              $position++;
 193          }
 194          if ( !count( $query_ids ) ) {
 195              return $cached_objects;
 196          }
 197  
 198          $_query_ids = $query_ids;
 199          sort( $_query_ids );
 200          $_query_ids = join( ',', $_query_ids );
 201  
 202          if ( $metas = $bbdb->get_results( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` IN ($_query_ids) /* bb_append_meta */" ) ) {
 203              usort( $metas, '_bb_append_meta_sort' );
 204              foreach ( $metas as $meta ) {
 205                  $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
 206                  if ( strpos($meta->meta_key, $bbdb->prefix) === 0 ) {
 207                      $trans[$meta->object_id]->{substr($meta->meta_key, strlen($bbdb->prefix))} = maybe_unserialize( $meta->meta_value );
 208                  }
 209              }
 210          }
 211          foreach ( $query_ids as $position => $i ) {
 212              $cached_objects[$position] = $trans[$i];
 213              wp_cache_add( $i, $trans[$i], $object_type );
 214              if ( $slug ) {
 215                  wp_cache_add( $trans[$i]->$slug, $i, 'bb_' . $slug );
 216              }
 217          }
 218          if ( !count( $cached_objects ) ) {
 219              return $object;
 220          }
 221          ksort( $cached_objects );
 222  
 223          return $cached_objects;
 224      } elseif ( $object ) {
 225          if ( false !== $cached_object = wp_cache_get( $object->$object_id_column, $object_type ) ) {
 226              return $cached_object;
 227          }
 228          if ( $metas = $bbdb->get_results( $bbdb->prepare( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` = %d /* bb_append_meta */", $object->$object_id_column ) ) ) {
 229              usort( $metas, '_bb_append_meta_sort' );
 230              foreach ( $metas as $meta ) {
 231                  $object->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
 232                  if ( strpos( $meta->meta_key, $bbdb->prefix ) === 0 ) {
 233                      $object->{substr( $meta->meta_key, strlen( $bbdb->prefix ) )} = $object->{$meta->meta_key};
 234                  }
 235              }
 236          }
 237          if ( $object->$object_id_column ) {
 238              wp_cache_add( $object->$object_id_column, $object, $object_type );
 239              if ( $slug ) {
 240                  wp_cache_add( $object->$slug, $object->$object_id_column, 'bb_' . $slug );
 241              }
 242          }
 243          return $object;
 244      }
 245  }
 246  
 247  /**
 248   * Sorts meta keys by length to ensure $appended_object->{$bbdb->prefix} key overwrites $appended_object->key as desired
 249   *
 250   * @internal
 251   */
 252  function _bb_append_meta_sort( $a, $b )
 253  {
 254      return strlen( $a->meta_key ) - strlen( $b->meta_key );
 255  }
 256  
 257  
 258  
 259  /* Options */
 260  
 261  /**
 262   * Echoes the requested bbPress option by calling bb_get_option()
 263   *
 264   * @param string The option to be echoed
 265   * @return void
 266   */
 267  function bb_option( $option )
 268  {
 269      echo apply_filters( 'bb_option_' . $option, bb_get_option( $option ) );
 270  }
 271  
 272  /**
 273   * Returns the requested bbPress option from the meta table or the $bb object
 274   *
 275   * @param string The option to be echoed
 276   * @return mixed The value of the option
 277   */
 278  function bb_get_option( $option )
 279  {
 280      // Allow plugins to short-circuit options.
 281      if ( false !== $r = apply_filters( 'bb_pre_get_option_' . $option, false, $option ) ) {
 282          return $r;
 283      }
 284  
 285      global $bb;
 286  
 287      switch ( $option ) {
 288          case 'site_id':
 289              if ( isset( $bb->site_id ) && is_numeric( $bb->site_id ) ) {
 290                  $r = (int) $bb->site_id;
 291              } else {
 292                  $r = 1;
 293              }
 294              break;
 295          case 'language':
 296              $r = str_replace( '_', '-', bb_get_locale() );
 297              break;
 298          case 'text_direction':
 299              global $bb_locale;
 300              $r = $bb_locale->text_direction;
 301              break;
 302          case 'version':
 303              return '1.1-alpha-2539'; // Don't filter
 304              break;
 305          case 'bb_db_version' :
 306              return '2471'; // Don't filter
 307              break;
 308          case 'html_type':
 309              $r = 'text/html';
 310              break;
 311          case 'charset':
 312              $r = 'UTF-8';
 313              break;
 314          case 'bb_table_prefix':
 315          case 'table_prefix':
 316              global $bbdb;
 317              return $bbdb->prefix; // Don't filter;
 318              break;
 319          case 'url':
 320              $option = 'uri';
 321          default:
 322              if ( isset( $bb->$option ) ) {
 323                  $r = $bb->$option;
 324                  if ( $option === 'mod_rewrite' ) {
 325                      if ( is_bool( $r ) ) {
 326                          $r = (int) $r;
 327                      }
 328                  }
 329                  break;
 330              }
 331  
 332              $r = bb_get_option_from_db( $option );
 333  
 334              if ( !$r ) {
 335                  switch ( $option ) {
 336                      case 'name':
 337                          $r = __( 'Please give me a name!' );
 338                          break;
 339                      case 'wp_table_prefix' :
 340                          global $wp_table_prefix; // This global is deprecated
 341                          return $wp_table_prefix; // Don't filter;
 342                          break;
 343                      case 'mod_rewrite':
 344                          $r = 0;
 345                          break;
 346                      case 'page_topics':
 347                          $r = 30;
 348                          break;
 349                      case 'edit_lock':
 350                          $r = 60;
 351                          break;
 352                      case 'gmt_offset':
 353                          $r = 0;
 354                          break;
 355                      case 'uri_ssl':
 356                          $r = preg_replace( '|^http://|i', 'https://', bb_get_option( 'uri' ) );
 357                          break;
 358                      case 'throttle_time':
 359                          $r = 30;
 360                          break;
 361                      case 'email_login':
 362                          $r = false;
 363                          break;
 364                  }
 365              }
 366              break;
 367      }
 368  
 369      return apply_filters( 'bb_get_option_' . $option, $r, $option );
 370  }
 371  
 372  /**
 373   * Retrieves and returns the requested bbPress option from the meta table
 374   *
 375   * @param string The option to be echoed
 376   * @return void
 377   */
 378  function bb_get_option_from_db( $option )
 379  {
 380      global $bbdb;
 381      $option = bb_sanitize_meta_key( $option );
 382  
 383      if ( wp_cache_get( $option, 'bb_option_not_set' ) ) {
 384          $r = null;
 385      } elseif ( false !== $_r = wp_cache_get( $option, 'bb_option' ) ) {
 386          $r = $_r;
 387      } else {
 388          if ( BB_INSTALLING ) {
 389              $bbdb->suppress_errors();
 390          }
 391          $row = $bbdb->get_row( $bbdb->prepare( "SELECT `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option' AND `meta_key` = %s", $option ) );
 392          if ( BB_INSTALLING ) {
 393              $bbdb->suppress_errors( false );
 394          }
 395  
 396          if ( is_object( $row ) ) {
 397              $r = maybe_unserialize( $row->meta_value );
 398          } else {
 399              $r = null;
 400          }
 401      }
 402  
 403      if ( $r === null ) {
 404          wp_cache_set( $option, true, 'bb_option_not_set' );
 405      } else {
 406          wp_cache_set( $option, $r, 'bb_option' );
 407      }
 408  
 409      return apply_filters( 'bb_get_option_from_db_' . $option, $r, $option );
 410  }
 411  
 412  function bb_form_option( $option )
 413  {
 414      echo bb_get_form_option( $option );
 415  }
 416  
 417  function bb_get_form_option( $option )
 418  {
 419      return esc_attr( bb_get_option( $option ) );
 420  }
 421  
 422  // Don't use the return value; use the API. Only returns options stored in DB.
 423  function bb_cache_all_options()
 424  {
 425      $base_options = array(
 426          'site_id',
 427          'bb_db_version',
 428          'name',
 429          'description',
 430          'uri_ssl',
 431          'from_email',
 432          'bb_auth_salt',
 433          'bb_secure_auth_salt',
 434          'bb_logged_in_salt',
 435          'bb_nonce_salt',
 436          'page_topics',
 437          'edit_lock',
 438          'bb_active_theme',
 439          'active_plugins',
 440          'mod_rewrite',
 441          'datetime_format',
 442          'date_format',
 443          'avatars_show',
 444          'avatars_default',
 445          'avatars_rating',
 446          'wp_table_prefix',
 447          'user_bbdb_name',
 448          'user_bbdb_user',
 449          'user_bbdb_password',
 450          'user_bbdb_host',
 451          'user_bbdb_charset',
 452          'user_bbdb_collate',
 453          'custom_user_table',
 454          'custom_user_meta_table',
 455          'wp_siteurl',
 456          'wp_home',
 457          'cookiedomain',
 458          'usercookie',
 459          'passcookie',
 460          'authcookie',
 461          'cookiepath',
 462          'sitecookiepath',
 463          'secure_auth_cookie',
 464          'logged_in_cookie',
 465          'admin_cookie_path',
 466          'core_plugins_cookie_path',
 467          'user_plugins_cookie_path',
 468          'wp_admin_cookie_path',
 469          'wp_plugins_cookie_path',
 470          'wordpress_mu_primary_blog_id',
 471          'enable_loginless',
 472          'enable_subscriptions',
 473          'enable_xmlrpc',
 474          'enable_pingback',
 475          'throttle_time',
 476          'bb_xmlrpc_allow_user_switching',
 477          'bp_bbpress_cron',
 478          'email_login',
 479          'static_title',
 480          'plugin_cookie_paths',
 481          'wp_roles_map',
 482          'gmt_offset',
 483          'timezone_string',
 484          'name_link_profile',
 485          'bp_bbpress_cron_check',
 486      );
 487  
 488      // Check that these aren't already in the cache
 489      $query_options = array();
 490      foreach ( $base_options as $base_option ) {
 491          if ( isset( $bb->$base_option ) ) {
 492              continue;
 493          }
 494          if ( wp_cache_get( $base_option, 'bb_option_not_set' ) ) {
 495              continue;
 496          }
 497          if ( false === wp_cache_get( $base_option, 'bb_option' ) ) {
 498              $query_options[] = $base_option;
 499              wp_cache_set( $base_option, true, 'bb_option_not_set' );
 500          }
 501      }
 502      if ( !count( $query_options ) ) {
 503          // It's all in cache
 504          return true;
 505      }
 506  
 507      $query_keys = "('" . join( "','", $query_options ) . "')";
 508  
 509      global $bbdb;
 510      $results = $bbdb->get_results( "SELECT `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option' AND `meta_key` IN $query_keys;" );
 511  
 512      if ( count( $base_options ) === count( $query_options ) && ( !$results || !is_array( $results ) || !count( $results ) ) ) {
 513          // Let's assume that the options haven't been populated from the old topicmeta table
 514          if ( !BB_INSTALLING && ( !defined( 'BB_DO_NOT_UPGRADE_TOPICMETA' ) || !BB_DO_NOT_UPGRADE_TOPICMETA ) ) {
 515              $topicmeta_exists = $bbdb->query( "SELECT * FROM $bbdb->topicmeta LIMIT 1" );
 516              if ($topicmeta_exists) {
 517                  require_once ( BB_PATH . 'bb-admin/includes/defaults.bb-schema.php' );
 518                  // Create the meta table
 519                  $bbdb->query( $bb_queries['meta'] );
 520                  // Copy options
 521                  $bbdb->query( "INSERT INTO `$bbdb->meta` (`meta_key`, `meta_value`) SELECT `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` = 0;" );
 522                  // Copy topic meta
 523                  $bbdb->query( "INSERT INTO `$bbdb->meta` (`object_id`, `meta_key`, `meta_value`) SELECT `topic_id`, `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` != 0;" );
 524                  // Entries with an object_id are topic meta at this stage
 525                  $bbdb->query( "UPDATE `$bbdb->meta` SET `object_type` = 'bb_topic' WHERE `object_id` != 0" );
 526              }
 527              unset( $topicmeta_exists );
 528  
 529              return bb_cache_all_options();
 530          }
 531  
 532          return false;
 533      } else {
 534          foreach ( $results as $options ) {
 535              wp_cache_delete( $options->meta_key, 'bb_option_not_set' );
 536              wp_cache_set( $options->meta_key, maybe_unserialize( $options->meta_value ), 'bb_option' );
 537          }
 538      }
 539  
 540      return true;
 541  }
 542  
 543  // Can store anything but NULL.
 544  function bb_update_option( $option, $value )
 545  {
 546      return bb_update_meta( 0, $option, $value, 'option', true );
 547  }
 548  
 549  function bb_delete_option( $option, $value = '' )
 550  {
 551      return bb_delete_meta( 0, $option, $value, 'option', true );
 552  }
 553  
 554  /**
 555   * Delete a transient
 556   *
 557   * @since 1.0
 558   * @package bbPress
 559   * @subpackage Transient
 560   *
 561   * @param string $transient Transient name. Expected to not be SQL-escaped
 562   * @return bool true if successful, false otherwise
 563   */
 564  function bb_delete_transient( $transient )
 565  {
 566      global $_bb_using_ext_object_cache, $bbdb;
 567  
 568      if ( $_bb_using_ext_object_cache ) {
 569          return wp_cache_delete( $transient, 'transient' );
 570      } else {
 571          $transient = '_transient_' . $bbdb->escape( $transient );
 572          return bb_delete_option( $transient );
 573      }
 574  }
 575  
 576  /**
 577   * Get the value of a transient
 578   *
 579   * If the transient does not exist or does not have a value, then the return value
 580   * will be false.
 581   * 
 582   * @since 1.0
 583   * @package bbPress
 584   * @subpackage Transient
 585   *
 586   * @param string $transient Transient name. Expected to not be SQL-escaped
 587   * @return mixed Value of transient
 588   */
 589  function bb_get_transient( $transient )
 590  {
 591      global $_bb_using_ext_object_cache, $bbdb;
 592  
 593      if ( $_bb_using_ext_object_cache ) {
 594          $value = wp_cache_get( $transient, 'transient' );
 595      } else {
 596          $transient_option = '_transient_' . $bbdb->escape( $transient );
 597          $transient_timeout = '_transient_timeout_' . $bbdb->escape( $transient );
 598          $timeout = bb_get_option( $transient_timeout );
 599          if ( $timeout && $timeout < time() ) {
 600              bb_delete_option( $transient_option );
 601              bb_delete_option( $transient_timeout );
 602              return false;
 603          }
 604  
 605          $value = bb_get_option( $transient_option );
 606      }
 607  
 608      return apply_filters( 'transient_' . $transient, $value );
 609  }
 610  
 611  /**
 612   * Set/update the value of a transient
 613   *
 614   * You do not need to serialize values, if the value needs to be serialize, then
 615   * it will be serialized before it is set.
 616   *
 617   * @since 1.0
 618   * @package bbPress
 619   * @subpackage Transient
 620   *
 621   * @param string $transient Transient name. Expected to not be SQL-escaped
 622   * @param mixed $value Transient value.
 623   * @param int $expiration Time until expiration in seconds, default 0
 624   * @return bool False if value was not set and true if value was set.
 625   */
 626  function bb_set_transient( $transient, $value, $expiration = 0 )
 627  {
 628      global $_bb_using_ext_object_cache, $bbdb;
 629  
 630      if ( $_bb_using_ext_object_cache ) {
 631          return wp_cache_set( $transient, $value, 'transient', $expiration );
 632      } else {
 633          $transient_timeout = '_transient_timeout_' . $bbdb->escape( $transient );
 634          $transient = '_transient_' . $bbdb->escape( $transient );
 635          if ( 0 != $expiration ) {
 636              bb_update_option( $transient_timeout, time() + $expiration );
 637          }
 638          return bb_update_option( $transient, $value );
 639      }
 640  }
 641  
 642  
 643  
 644  /* User meta */
 645  
 646  function bb_get_usermeta( $user_id, $meta_key )
 647  {
 648      if ( !$user = bb_get_user( $user_id ) ) {
 649          return;
 650      }
 651  
 652      $meta_key = bb_sanitize_meta_key( $meta_key );
 653      if ( !isset( $user->$meta_key ) ) {
 654          return;
 655      }
 656      return $user->$meta_key;
 657  }
 658  
 659  function bb_update_usermeta( $user_id, $meta_key, $meta_value )
 660  {
 661      return bb_update_meta( $user_id, $meta_key, $meta_value, 'user' );
 662  }
 663  
 664  function bb_delete_usermeta( $user_id, $meta_key, $meta_value = '' )
 665  {
 666      return bb_delete_meta( $user_id, $meta_key, $meta_value, 'user' );
 667  }
 668  
 669  /**
 670   * Saves and restores user interface settings stored in a cookie.
 671   *
 672   * Checks if the current user-settings cookie is updated and stores it. When no
 673   * cookie exists (different browser used), adds the last saved cookie restoring
 674   * the settings.
 675   *
 676   * @package bbPress
 677   * @subpackage Meta
 678   * @since 1.0
 679   */
 680  function bb_user_settings()
 681  {
 682      if ( !defined( 'BB_IS_ADMIN' ) || !BB_IS_ADMIN ) {
 683          return;
 684      }
 685  
 686      if ( defined( 'DOING_AJAX' ) ) {
 687          return;
 688      }
 689  
 690      if ( !$user = bb_get_current_user() ) {
 691          return;
 692      }
 693  
 694      $settings = bb_get_usermeta( $user->ID, 'bb_user_settings' );
 695  
 696      if ( isset( $_COOKIE['bb-user-settings-' . $user->ID] ) ) {
 697          $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['bb-user-settings-' . $user->ID] );
 698  
 699          if ( !empty( $cookie ) && strpos( $cookie, '=' ) ) {
 700              if ( $cookie == $settings ) {
 701                  return;
 702              }
 703  
 704              $last_time = (int) bb_get_usermeta( $user->ID, 'bb_user_settings_time' );
 705              $saved = isset( $_COOKIE['bb-user-settings-time-' . $user->ID] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE['bb-user-settings-time-' . $user->ID] ) : 0;
 706  
 707              if ( $saved > $last_time ) {
 708                  bb_update_usermeta( $user->ID, 'bb_user_settings', $cookie );
 709                  bb_update_usermeta( $user->ID, 'bb_user_settings_time', time() - 5 );
 710                  return;
 711              }
 712          }
 713      }
 714  
 715      setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath );
 716      setcookie( 'bb-user-settings-time-' . $user->ID, time(), time() + 31536000, $bb->cookiepath );
 717  }
 718  
 719  /**
 720   * Retrieve user interface setting value based on setting name.
 721   *
 722   * @package bbPress
 723   * @subpackage Meta
 724   * @since 1.0
 725   *
 726   * @param string $name The name of the setting.
 727   * @param string $default Optional default value to return when $name is not set.
 728   * @return mixed the last saved user setting or the default value/false if it doesn't exist.
 729   */
 730  function bb_get_user_setting( $name, $default = false )
 731  {
 732      $arr = bb_get_all_user_settings();
 733  
 734      return isset( $arr[$name] ) ? $arr[$name] : $default;
 735  }
 736  
 737  /**
 738   * Adds or updates a user interface setting value based on setting name.
 739   *
 740   * @package bbPress
 741   * @subpackage Meta
 742   * @since 1.0
 743   *
 744   * @param string $name The name of the setting.
 745   * @param string $value The value of the setting.
 746   */
 747  function bb_update_user_setting( $name, $value )
 748  {
 749      if ( is_null( $value ) || $value === '' ) {
 750          return;
 751      }
 752  
 753      if ( !$user = bb_get_current_user() ) {
 754          return;
 755      }
 756  
 757      $name = (string) $name;
 758      $name = preg_replace( '/[^A-Za-z0-9_]/', '', $name );
 759  
 760      $value = (string) $value;
 761      $value = preg_replace( '/[^A-Za-z0-9_]/', '', $value );
 762  
 763      $arr = bb_get_all_user_settings();
 764      $arr[$name] = $value;
 765  
 766      foreach ( $arr as $k => $v ) {
 767          $settings .= $k . '=' . $v . '&';
 768      }
 769      $settings = rtrim( $settings, '&' );
 770  
 771      bb_update_usermeta( $user->ID, 'bb_user_settings', $settings );
 772      setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath );
 773  }
 774  
 775  /**
 776   * Delete user interface settings.
 777   *
 778   * Deleting settings would reset them to the defaults.
 779   *
 780   * @package bbPress
 781   * @subpackage Meta
 782   * @since 1.0
 783   *
 784   * @param mixed $names The name or array of names of the setting to be deleted.
 785   */
 786  function bb_delete_user_setting( $names )
 787  {
 788      $arr = bb_get_all_user_settings();
 789      $names = (array) $names;
 790  
 791      if ( !$user = bb_get_current_user() ) {
 792          return;
 793      }
 794  
 795      foreach ( $names as $name ) {
 796          if ( isset( $arr[$name] ) ) {
 797              unset( $arr[$name] );
 798              $settings = '';
 799          }
 800      }
 801  
 802      if ( isset( $settings ) ) {
 803          foreach ( $arr as $k => $v ) {
 804              $settings .= $k . '=' . $v . '&';
 805          }
 806          $settings = rtrim( $settings, '&' );
 807  
 808          bb_update_usermeta( $user->ID, 'bb_user_settings', $settings );
 809          setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath );
 810      }
 811  }
 812  
 813  /**
 814   * Retrieve all user interface settings.
 815   *
 816   * @package bbPress
 817   * @subpackage Meta
 818   * @since 1.0
 819   *
 820   * @return array the last saved user settings or empty array.
 821   */
 822  function bb_get_all_user_settings()
 823  {
 824      if ( ! $user = bb_get_current_user() ) {
 825          return array();
 826      }
 827  
 828      $arr = array();
 829      if ( isset( $_COOKIE['bb-user-settings-' . $user->ID] ) ) {
 830          $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['bb-user-settings-' . $user->ID] );
 831  
 832          if ( $cookie && strpos( $cookie, '=' ) ) { // the '=' cannot be 1st char
 833              parse_str( $cookie, $arr );
 834          }
 835      } elseif ( isset( $user->bb_user_settings ) && is_string( $user->bb_user_settings ) ) {
 836          parse_str( $user->bb_user_settings, $arr );
 837      }
 838  
 839      return $arr;
 840  }
 841  
 842  /**
 843   * Delete the user settings of the current user.
 844   *
 845   * @package bbPress
 846   * @subpackage Meta
 847   * @since 1.0
 848   */
 849  function bb_delete_all_user_settings()
 850  {
 851      if ( !$user = bb_get_current_user() ) {
 852          return;
 853      }
 854  
 855      bb_delete_usermeta( $user->ID, 'bb_user_settings' );
 856      setcookie( 'bb-user-settings-' . $user->ID, ' ', time() - 31536000, $bb->cookiepath );
 857  }
 858  
 859  
 860  
 861  
 862  /* Forum meta */
 863  
 864  function bb_get_forummeta( $forum_id, $meta_key )
 865  {
 866      if ( !$forum = bb_get_forum( $forum_id ) ) {
 867          return;
 868      }
 869  
 870      $meta_key = bb_sanitize_meta_key( $meta_key );
 871      if ( !isset( $forum->$meta_key ) ) {
 872          return;
 873      }
 874      return $forum->$meta_key;
 875  }
 876  
 877  function bb_update_forummeta( $forum_id, $meta_key, $meta_value )
 878  {
 879      return bb_update_meta( $forum_id, $meta_key, $meta_value, 'forum' );
 880  }
 881  
 882  function bb_delete_forummeta( $forum_id, $meta_key, $meta_value = '' )
 883  {
 884      return bb_delete_meta( $forum_id, $meta_key, $meta_value, 'forum' );
 885  }
 886  
 887  
 888  
 889  /* Topic meta */
 890  
 891  function bb_get_topicmeta( $topic_id, $meta_key )
 892  {
 893      if ( !$topic = get_topic( $topic_id ) ) {
 894          return;
 895      }
 896  
 897      $meta_key = bb_sanitize_meta_key( $meta_key );
 898      if ( !isset($topic->$meta_key) ) {
 899          return;
 900      }
 901      return $topic->$meta_key;
 902  }
 903  
 904  function bb_update_topicmeta( $topic_id, $meta_key, $meta_value )
 905  {
 906      return bb_update_meta( $topic_id, $meta_key, $meta_value, 'topic' );
 907  }
 908  
 909  function bb_delete_topicmeta( $topic_id, $meta_key, $meta_value = '' )
 910  {
 911      return bb_delete_meta( $topic_id, $meta_key, $meta_value, 'topic' );
 912  }
 913  
 914  
 915  
 916  /* Post meta */
 917  
 918  function bb_get_postmeta( $post_id, $meta_key )
 919  {
 920      if ( !$post = bb_get_post( $post_id ) ) {
 921          return;
 922      }
 923  
 924      $meta_key = bb_sanitize_meta_key( $meta_key );
 925      if ( !isset( $post->$meta_key ) ) {
 926          return;
 927      }
 928      return $post->$meta_key;
 929  }
 930  
 931  function bb_update_postmeta( $post_id, $meta_key, $meta_value )
 932  {
 933      return bb_update_meta( $post_id, $meta_key, $meta_value, 'post' );
 934  }
 935  
 936  function bb_delete_postmeta( $post_id, $meta_key, $meta_value = '' )
 937  {
 938      return bb_delete_meta( $post_id, $meta_key, $meta_value, 'post' );
 939  }


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7