[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/ -> bp-core-cache.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Core Caching Functions.
   4   *
   5   * Caching functions handle the clearing of cached objects and pages on specific
   6   * actions throughout BuddyPress.
   7   *
   8   * @package BuddyPress
   9   * @subpackage Cache
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Prune the WP Super Cache.
  18   *
  19   * When WP Super Cache is installed, this function will clear cached pages
  20   * so that success/error messages or time-sensitive content are not cached.
  21   *
  22   * @since 1.0.0
  23   *
  24   * @see prune_super_cache()
  25   *
  26   * @return int
  27   */
  28  function bp_core_clear_cache() {
  29      global $cache_path;
  30  
  31      if ( function_exists( 'prune_super_cache' ) ) {
  32  
  33          /**
  34           * Fires before the pruning of WP Super Cache.
  35           *
  36           * @since 1.0.0
  37           */
  38          do_action( 'bp_core_clear_cache' );
  39          return prune_super_cache( $cache_path, true );
  40      }
  41  }
  42  
  43  /**
  44   * Clear all cached objects for a user, or those that a user is part of.
  45   *
  46   * @since 1.0.0
  47   *
  48   * @param string $user_id User ID to delete cache for.
  49   */
  50  function bp_core_clear_user_object_cache( $user_id ) {
  51      wp_cache_delete( 'bp_user_' . $user_id, 'bp' );
  52  }
  53  
  54  /**
  55   * Clear member count caches and transients.
  56   *
  57   * @since 1.6.0
  58   */
  59  function bp_core_clear_member_count_caches() {
  60      wp_cache_delete( 'bp_total_member_count', 'bp' );
  61      delete_transient( 'bp_active_member_count' );
  62  }
  63  add_action( 'bp_core_activated_user',         'bp_core_clear_member_count_caches' );
  64  add_action( 'bp_core_process_spammer_status', 'bp_core_clear_member_count_caches' );
  65  add_action( 'bp_core_deleted_account',        'bp_core_clear_member_count_caches' );
  66  add_action( 'bp_first_activity_for_member',   'bp_core_clear_member_count_caches' );
  67  add_action( 'deleted_user',                   'bp_core_clear_member_count_caches' );
  68  
  69  /**
  70   * Clear the directory_pages cache when one of the pages is updated.
  71   *
  72   * @since 2.0.0
  73   *
  74   * @param int $post_id ID of the page that was saved.
  75   */
  76  function bp_core_clear_directory_pages_cache_page_edit( $post_id = 0 ) {
  77  
  78      // Bail if BP is not defined here.
  79      if ( ! buddypress() ) {
  80          return;
  81      }
  82  
  83      // Bail if not on the root blog.
  84      if ( ! bp_is_root_blog() ) {
  85          return;
  86      }
  87  
  88      $page_ids = bp_core_get_directory_page_ids( 'all' );
  89  
  90      // Bail if post ID is not a directory page.
  91      if ( ! in_array( $post_id, $page_ids ) ) {
  92          return;
  93      }
  94  
  95      wp_cache_delete( 'directory_pages', 'bp_pages' );
  96  }
  97  add_action( 'save_post', 'bp_core_clear_directory_pages_cache_page_edit' );
  98  
  99  /**
 100   * Clear the directory_pages cache when the bp-pages option is updated.
 101   *
 102   * @since 2.0.0
 103   *
 104   * @param string $option Option name.
 105   */
 106  function bp_core_clear_directory_pages_cache_settings_edit( $option ) {
 107      if ( 'bp-pages' === $option ) {
 108          wp_cache_delete( 'directory_pages', 'bp_pages' );
 109      }
 110  }
 111  add_action( 'update_option', 'bp_core_clear_directory_pages_cache_settings_edit' );
 112  
 113  /**
 114   * Clear the root_blog_options cache when any of its options are updated.
 115   *
 116   * @since 2.0.0
 117   *
 118   * @param string $option Option name.
 119   */
 120  function bp_core_clear_root_options_cache( $option ) {
 121      foreach ( array( 'add_option', 'add_site_option', 'update_option', 'update_site_option' ) as $action ) {
 122          remove_action( $action, 'bp_core_clear_root_options_cache' );
 123      }
 124  
 125      // Surrounding code prevents infinite loops on WP < 4.4.
 126      $keys = array_keys( bp_get_default_options() );
 127  
 128      foreach ( array( 'add_option', 'add_site_option', 'update_option', 'update_site_option' ) as $action ) {
 129          add_action( $action, 'bp_core_clear_root_options_cache' );
 130      }
 131  
 132      $keys = array_merge( $keys, array(
 133          'registration',
 134          'avatar_default',
 135          'tags_blog_id',
 136          'sitewide_tags_blog',
 137          'registration',
 138          'fileupload_mask',
 139      ) );
 140  
 141      if ( in_array( $option, $keys ) ) {
 142          wp_cache_delete( 'root_blog_options', 'bp' );
 143      }
 144  }
 145  add_action( 'update_option', 'bp_core_clear_root_options_cache' );
 146  add_action( 'update_site_option', 'bp_core_clear_root_options_cache' );
 147  add_action( 'add_option', 'bp_core_clear_root_options_cache' );
 148  add_action( 'add_site_option', 'bp_core_clear_root_options_cache' );
 149  
 150  /**
 151   * Determine which items from a list do not have cached values.
 152   *
 153   * @since 2.0.0
 154   *
 155   * @param array  $item_ids    ID list.
 156   * @param string $cache_group The cache group to check against.
 157   * @return array
 158   */
 159  function bp_get_non_cached_ids( $item_ids, $cache_group ) {
 160      $uncached = array();
 161  
 162      foreach ( $item_ids as $item_id ) {
 163          $item_id = (int) $item_id;
 164          if ( false === wp_cache_get( $item_id, $cache_group ) ) {
 165              $uncached[] = $item_id;
 166          }
 167      }
 168  
 169      return $uncached;
 170  }
 171  
 172  /**
 173   * Update the metadata cache for the specified objects.
 174   *
 175   * Based on WordPress's {@link update_meta_cache()}, this function primes the
 176   * cache with metadata related to a set of objects. This is typically done when
 177   * querying for a loop of objects; pre-fetching metadata for each queried
 178   * object can lead to dramatic performance improvements when using metadata
 179   * in the context of template loops.
 180   *
 181   * @since 1.6.0
 182   *
 183   * @global object $wpdb WordPress database object for queries..
 184   *
 185   * @param array $args {
 186   *     Array of arguments.
 187   *     @type array|string $object_ids       List of object IDs to fetch metadata for.
 188   *                                          Accepts an array or a comma-separated list of numeric IDs.
 189   *     @type string       $object_type      The type of object, eg 'groups' or 'activity'.
 190   *     @type string       $meta_table       The name of the metadata table being queried.
 191   *     @type string       $object_column    Optional. The name of the database column where IDs
 192   *                                          (those provided by $object_ids) are found. Eg, 'group_id'
 193   *                                          for the groups metadata tables. Default: $object_type . '_id'.
 194   *     @type string       $cache_key_prefix Optional. The prefix to use when creating
 195   *                                          cache key names. Default: the value of $meta_table.
 196   * }
 197   * @return false|array Metadata cache for the specified objects, or false on failure.
 198   */
 199  function bp_update_meta_cache( $args = array() ) {
 200      global $wpdb;
 201  
 202      $r = bp_parse_args(
 203          $args,
 204          array(
 205              'object_ids'        => array(), // Comma-separated list or array of item ids.
 206              'object_type'        => '',      // Canonical component id: groups, members, etc.
 207              'cache_group'      => '',      // Cache group.
 208              'meta_table'        => '',      // Name of the table containing the metadata.
 209              'object_column'    => '',      // DB column for the object ids (group_id, etc).
 210              'cache_key_prefix' => '',      // Prefix to use when creating cache key names. Eg 'bp_groups_groupmeta'.
 211          )
 212      );
 213  
 214      extract( $r );
 215  
 216      if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) || empty( $cache_group ) ) {
 217          return false;
 218      }
 219  
 220      if ( empty( $cache_key_prefix ) ) {
 221          $cache_key_prefix = $meta_table;
 222      }
 223  
 224      if ( empty( $object_column ) ) {
 225          $object_column = $object_type . '_id';
 226      }
 227  
 228      if ( ! $cache_group ) {
 229          return false;
 230      }
 231  
 232      $object_ids   = wp_parse_id_list( $object_ids );
 233      $uncached_ids = bp_get_non_cached_ids( $object_ids, $cache_group );
 234  
 235      $cache = array();
 236  
 237      // Get meta info.
 238      if ( ! empty( $uncached_ids ) ) {
 239          $id_list   = join( ',', wp_parse_id_list( $uncached_ids ) );
 240          $meta_list = $wpdb->get_results( esc_sql( "SELECT {$object_column}, meta_key, meta_value FROM {$meta_table} WHERE {$object_column} IN ({$id_list})" ), ARRAY_A );
 241  
 242          if ( ! empty( $meta_list ) ) {
 243              foreach ( $meta_list as $metarow ) {
 244                  $mpid = intval( $metarow[$object_column] );
 245                  $mkey = $metarow['meta_key'];
 246                  $mval = $metarow['meta_value'];
 247  
 248                  // Force subkeys to be array type.
 249                  if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
 250                      $cache[$mpid] = array();
 251                  if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
 252                      $cache[$mpid][$mkey] = array();
 253  
 254                  // Add a value to the current pid/key.
 255                  $cache[$mpid][$mkey][] = $mval;
 256              }
 257          }
 258  
 259          foreach ( $uncached_ids as $uncached_id ) {
 260              // Cache empty values as well.
 261              if ( ! isset( $cache[ $uncached_id ] ) ) {
 262                  $cache[ $uncached_id ] = array();
 263              }
 264  
 265              wp_cache_set( $uncached_id, $cache[ $uncached_id ], $cache_group );
 266          }
 267      }
 268  
 269      return $cache;
 270  }
 271  
 272  /**
 273   * Gets a value that has been cached using an incremented key.
 274   *
 275   * A utility function for use by query methods like BP_Activity_Activity::get().
 276   *
 277   * @since 2.7.0
 278   * @see bp_core_set_incremented_cache()
 279   *
 280   * @param string $key   Unique key for the query. Usually a SQL string.
 281   * @param string $group Cache group. Eg 'bp_activity'.
 282   * @return array|bool False if no cached values are found, otherwise an array of IDs.
 283   */
 284  function bp_core_get_incremented_cache( $key, $group ) {
 285      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 286      return wp_cache_get( $cache_key, $group );
 287  }
 288  
 289  /**
 290   * Caches a value using an incremented key.
 291   *
 292   * An "incremented key" is a cache key that is hashed with a unique incrementor,
 293   * allowing for bulk invalidation.
 294   *
 295   * Use this method when caching data that should be invalidated whenever any
 296   * object of a given type is created, updated, or deleted. This usually means
 297   * data related to object queries, which can only reliably cached until the
 298   * underlying set of objects has been modified. See, eg, BP_Activity_Activity::get().
 299   *
 300   * @since 2.7.0
 301   *
 302   * @param string $key   Unique key for the query. Usually a SQL string.
 303   * @param string $group Cache group. Eg 'bp_activity'.
 304   * @param array  $ids   Array of IDs.
 305   * @return bool
 306   */
 307  function bp_core_set_incremented_cache( $key, $group, $ids ) {
 308      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 309      return wp_cache_set( $cache_key, $ids, $group );
 310  }
 311  
 312  /**
 313   * Delete a value that has been cached using an incremented key.
 314   *
 315   * A utility function for use by query methods like BP_Activity_Activity::get().
 316   *
 317   * @since 3.0.0
 318   * @see bp_core_set_incremented_cache()
 319   *
 320   * @param string $key   Unique key for the query. Usually a SQL string.
 321   * @param string $group Cache group. Eg 'bp_activity'.
 322   * @return bool True on successful removal, false on failure.
 323   */
 324  function bp_core_delete_incremented_cache( $key, $group ) {
 325      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 326      return wp_cache_delete( $cache_key, $group );
 327  }
 328  
 329  /**
 330   * Gets the key to be used when caching a value using an incremented cache key.
 331   *
 332   * The $key is hashed with a component-specific incrementor, which is used to
 333   * invalidate multiple caches at once.
 334  
 335   * @since 2.7.0
 336   *
 337   * @param string $key   Unique key for the query. Usually a SQL string.
 338   * @param string $group Cache group. Eg 'bp_activity'.
 339   * @return string
 340   */
 341  function bp_core_get_incremented_cache_key( $key, $group ) {
 342      $incrementor = bp_core_get_incrementor( $group );
 343      $cache_key = md5( $key . $incrementor );
 344      return $cache_key;
 345  }
 346  
 347  /**
 348   * Gets a group-specific cache incrementor.
 349   *
 350   * The incrementor is paired with query identifiers (like SQL strings) to
 351   * create cache keys that can be invalidated en masse.
 352   *
 353   * If an incrementor does not yet exist for the given `$group`, one will
 354   * be created.
 355   *
 356   * @since 2.7.0
 357   *
 358   * @param string $group Cache group. Eg 'bp_activity'.
 359   * @return string
 360   */
 361  function bp_core_get_incrementor( $group ) {
 362      $incrementor = wp_cache_get( 'incrementor', $group );
 363      if ( ! $incrementor ) {
 364          $incrementor = microtime();
 365          wp_cache_set( 'incrementor', $incrementor, $group );
 366      }
 367  
 368      return $incrementor;
 369  }
 370  
 371  /**
 372   * Reset a group-specific cache incrementor.
 373   *
 374   * Call this function when all incrementor-based caches associated with a given
 375   * cache group should be invalidated.
 376   *
 377   * @since 2.7.0
 378   *
 379   * @param string $group Cache group. Eg 'bp_activity'.
 380   * @return bool True on success, false on failure.
 381   */
 382  function bp_core_reset_incrementor( $group ) {
 383      return wp_cache_delete( 'incrementor', $group );
 384  }
 385  
 386  /**
 387   * Resets all incremented bp_invitations caches.
 388   *
 389   * @since 5.0.0
 390   */
 391  function bp_invitations_reset_cache_incrementor() {
 392      bp_core_reset_incrementor( 'bp_invitations' );
 393  }
 394  add_action( 'bp_invitation_after_save', 'bp_invitations_reset_cache_incrementor' );
 395  add_action( 'bp_invitation_after_delete', 'bp_invitations_reset_cache_incrementor' );
 396  
 397  /**
 398   * Add a cache group for Database object types.
 399   *
 400   * @since 7.0.0
 401   */
 402  function bp_set_object_type_terms_cache_group() {
 403      wp_cache_add_global_groups( 'bp_object_terms' );
 404  }
 405  add_action( 'bp_setup_cache_groups', 'bp_set_object_type_terms_cache_group' );
 406  
 407  /**
 408   * Clear the Database object types cache.
 409   *
 410   * @since 7.0.0
 411   *
 412   * @param int $type_id The Type's term ID.
 413   * @param string $taxonomy The Type's taxonomy name.
 414   */
 415  function bp_clear_object_type_terms_cache( $type_id = 0, $taxonomy = '' ) {
 416      wp_cache_delete( $taxonomy, 'bp_object_terms' );
 417  }
 418  add_action( 'bp_type_inserted', 'bp_clear_object_type_terms_cache' );
 419  add_action( 'bp_type_updated', 'bp_clear_object_type_terms_cache' );
 420  add_action( 'bp_type_deleted', 'bp_clear_object_type_terms_cache' );
 421  
 422  /**
 423   * Resets all incremented bp_optout caches.
 424   *
 425   * @since 8.0.0
 426   */
 427  function bp_optouts_reset_cache_incrementor() {
 428      bp_core_reset_incrementor( 'bp_optouts' );
 429  }
 430  add_action( 'bp_optout_after_save', 'bp_optouts_reset_cache_incrementor' );
 431  add_action( 'bp_optout_after_delete', 'bp_optouts_reset_cache_incrementor' );


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1