[ 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   * @supackage 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      $defaults = array(
 203          'object_ids'        => array(), // Comma-separated list or array of item ids.
 204          'object_type'        => '',      // Canonical component id: groups, members, etc.
 205          'cache_group'      => '',      // Cache group.
 206          'meta_table'        => '',      // Name of the table containing the metadata.
 207          'object_column'    => '',      // DB column for the object ids (group_id, etc).
 208          'cache_key_prefix' => ''       // Prefix to use when creating cache key names. Eg 'bp_groups_groupmeta'.
 209      );
 210      $r = wp_parse_args( $args, $defaults );
 211      extract( $r );
 212  
 213      if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) || empty( $cache_group ) ) {
 214          return false;
 215      }
 216  
 217      if ( empty( $cache_key_prefix ) ) {
 218          $cache_key_prefix = $meta_table;
 219      }
 220  
 221      if ( empty( $object_column ) ) {
 222          $object_column = $object_type . '_id';
 223      }
 224  
 225      if ( ! $cache_group ) {
 226          return false;
 227      }
 228  
 229      $object_ids   = wp_parse_id_list( $object_ids );
 230      $uncached_ids = bp_get_non_cached_ids( $object_ids, $cache_group );
 231  
 232      $cache = array();
 233  
 234      // Get meta info.
 235      if ( ! empty( $uncached_ids ) ) {
 236          $id_list   = join( ',', wp_parse_id_list( $uncached_ids ) );
 237          $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 );
 238  
 239          if ( ! empty( $meta_list ) ) {
 240              foreach ( $meta_list as $metarow ) {
 241                  $mpid = intval( $metarow[$object_column] );
 242                  $mkey = $metarow['meta_key'];
 243                  $mval = $metarow['meta_value'];
 244  
 245                  // Force subkeys to be array type.
 246                  if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
 247                      $cache[$mpid] = array();
 248                  if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
 249                      $cache[$mpid][$mkey] = array();
 250  
 251                  // Add a value to the current pid/key.
 252                  $cache[$mpid][$mkey][] = $mval;
 253              }
 254          }
 255  
 256          foreach ( $uncached_ids as $uncached_id ) {
 257              // Cache empty values as well.
 258              if ( ! isset( $cache[ $uncached_id ] ) ) {
 259                  $cache[ $uncached_id ] = array();
 260              }
 261  
 262              wp_cache_set( $uncached_id, $cache[ $uncached_id ], $cache_group );
 263          }
 264      }
 265  
 266      return $cache;
 267  }
 268  
 269  /**
 270   * Gets a value that has been cached using an incremented key.
 271   *
 272   * A utility function for use by query methods like BP_Activity_Activity::get().
 273   *
 274   * @since 2.7.0
 275   * @see bp_core_set_incremented_cache()
 276   *
 277   * @param string $key   Unique key for the query. Usually a SQL string.
 278   * @param string $group Cache group. Eg 'bp_activity'.
 279   * @return array|bool False if no cached values are found, otherwise an array of IDs.
 280   */
 281  function bp_core_get_incremented_cache( $key, $group ) {
 282      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 283      return wp_cache_get( $cache_key, $group );
 284  }
 285  
 286  /**
 287   * Caches a value using an incremented key.
 288   *
 289   * An "incremented key" is a cache key that is hashed with a unique incrementor,
 290   * allowing for bulk invalidation.
 291   *
 292   * Use this method when caching data that should be invalidated whenever any
 293   * object of a given type is created, updated, or deleted. This usually means
 294   * data related to object queries, which can only reliably cached until the
 295   * underlying set of objects has been modified. See, eg, BP_Activity_Activity::get().
 296   *
 297   * @since 2.7.0
 298   *
 299   * @param string $key   Unique key for the query. Usually a SQL string.
 300   * @param string $group Cache group. Eg 'bp_activity'.
 301   * @param array  $ids   Array of IDs.
 302   * @return bool
 303   */
 304  function bp_core_set_incremented_cache( $key, $group, $ids ) {
 305      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 306      return wp_cache_set( $cache_key, $ids, $group );
 307  }
 308  
 309  /**
 310   * Delete a value that has been cached using an incremented key.
 311   *
 312   * A utility function for use by query methods like BP_Activity_Activity::get().
 313   *
 314   * @since 3.0.0
 315   * @see bp_core_set_incremented_cache()
 316   *
 317   * @param string $key   Unique key for the query. Usually a SQL string.
 318   * @param string $group Cache group. Eg 'bp_activity'.
 319   * @return bool True on successful removal, false on failure.
 320   */
 321  function bp_core_delete_incremented_cache( $key, $group ) {
 322      $cache_key = bp_core_get_incremented_cache_key( $key, $group );
 323      return wp_cache_delete( $cache_key, $group );
 324  }
 325  
 326  /**
 327   * Gets the key to be used when caching a value using an incremented cache key.
 328   *
 329   * The $key is hashed with a component-specific incrementor, which is used to
 330   * invalidate multiple caches at once.
 331  
 332   * @since 2.7.0
 333   *
 334   * @param string $key   Unique key for the query. Usually a SQL string.
 335   * @param string $group Cache group. Eg 'bp_activity'.
 336   * @return string
 337   */
 338  function bp_core_get_incremented_cache_key( $key, $group ) {
 339      $incrementor = bp_core_get_incrementor( $group );
 340      $cache_key = md5( $key . $incrementor );
 341      return $cache_key;
 342  }
 343  
 344  /**
 345   * Gets a group-specific cache incrementor.
 346   *
 347   * The incrementor is paired with query identifiers (like SQL strings) to
 348   * create cache keys that can be invalidated en masse.
 349   *
 350   * If an incrementor does not yet exist for the given `$group`, one will
 351   * be created.
 352   *
 353   * @since 2.7.0
 354   *
 355   * @param string $group Cache group. Eg 'bp_activity'.
 356   * @return string
 357   */
 358  function bp_core_get_incrementor( $group ) {
 359      $incrementor = wp_cache_get( 'incrementor', $group );
 360      if ( ! $incrementor ) {
 361          $incrementor = microtime();
 362          wp_cache_set( 'incrementor', $incrementor, $group );
 363      }
 364  
 365      return $incrementor;
 366  }
 367  
 368  /**
 369   * Reset a group-specific cache incrementor.
 370   *
 371   * Call this function when all incrementor-based caches associated with a given
 372   * cache group should be invalidated.
 373   *
 374   * @since 2.7.0
 375   *
 376   * @param string $group Cache group. Eg 'bp_activity'.
 377   * @return bool True on success, false on failure.
 378   */
 379  function bp_core_reset_incrementor( $group ) {
 380      return wp_cache_delete( 'incrementor', $group );
 381  }
 382  
 383  /**
 384   * Resets all incremented bp_invitations caches.
 385   *
 386   * @since 5.0.0
 387   */
 388  function bp_invitations_reset_cache_incrementor() {
 389      bp_core_reset_incrementor( 'bp_invitations' );
 390  }
 391  add_action( 'bp_invitation_after_save', 'bp_invitations_reset_cache_incrementor' );
 392  add_action( 'bp_invitation_after_delete', 'bp_invitations_reset_cache_incrementor' );


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1