/svntrunk/bp-core/bp-core-cache.php

https://bitbucket.org/simplemediacode/bptrunk · PHP · 130 lines · 72 code · 20 blank · 38 comment · 14 complexity · 245dc2b4655e96dc98f247e2476af583 MD5 · raw file

  1. <?php
  2. /**
  3. * Caching functions handle the clearing of cached objects and pages on specific
  4. * actions throughout BuddyPress.
  5. */
  6. // Exit if accessed directly
  7. if ( !defined( 'ABSPATH' ) ) exit;
  8. /**
  9. * REQUIRES WP-SUPER-CACHE
  10. *
  11. * When wp-super-cache is installed this function will clear cached pages
  12. * so that success/error messages are not cached, or time sensitive content.
  13. *
  14. * @package BuddyPress Core
  15. */
  16. function bp_core_clear_cache() {
  17. global $cache_path;
  18. if ( function_exists( 'prune_super_cache' ) ) {
  19. do_action( 'bp_core_clear_cache' );
  20. return prune_super_cache( $cache_path, true );
  21. }
  22. }
  23. /**
  24. * Add's 'bp' to global group of network wide cachable objects
  25. *
  26. * @package BuddyPress Core
  27. */
  28. function bp_core_add_global_group() {
  29. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  30. wp_cache_add_global_groups( array( 'bp' ) );
  31. }
  32. }
  33. add_action( 'bp_loaded', 'bp_core_add_global_group' );
  34. /**
  35. * Clears all cached objects for a user, or a user is part of.
  36. *
  37. * @package BuddyPress Core
  38. */
  39. function bp_core_clear_user_object_cache( $user_id ) {
  40. wp_cache_delete( 'bp_user_' . $user_id, 'bp' );
  41. }
  42. /**
  43. * Clears member count caches and transients
  44. */
  45. function bp_core_clear_member_count_caches() {
  46. wp_cache_delete( 'bp_total_member_count', 'bp' );
  47. delete_transient( 'bp_active_member_count' );
  48. }
  49. add_action( 'bp_core_activated_user', 'bp_core_clear_member_count_caches' );
  50. add_action( 'bp_core_process_spammer_status', 'bp_core_clear_member_count_caches' );
  51. add_action( 'bp_core_deleted_account', 'bp_core_clear_member_count_caches' );
  52. add_action( 'bp_first_activity_for_member', 'bp_core_clear_member_count_caches' );
  53. add_action( 'deleted_user', 'bp_core_clear_member_count_caches' );
  54. /**
  55. * Update the metadata cache for the specified objects.
  56. *
  57. * @since BuddyPress (1.6)
  58. * @global $wpdb WordPress database object for queries.
  59. * @param array $args See $defaults definition for more details
  60. * @return mixed Metadata cache for the specified objects, or false on failure.
  61. */
  62. function bp_update_meta_cache( $args = array() ) {
  63. global $wpdb;
  64. $defaults = array(
  65. 'object_ids' => array(), // Comma-separated list or array of item ids
  66. 'object_type' => '', // Canonical component id: groups, members, etc
  67. 'meta_table' => '', // Name of the table containing the metadata
  68. 'object_column' => '', // DB column for the object ids (group_id, etc)
  69. 'cache_key_prefix' => '' // Prefix to use when creating cache key names. Eg
  70. // 'bp_groups_groupmeta'
  71. );
  72. $r = wp_parse_args( $args, $defaults );
  73. extract( $r );
  74. if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) ) {
  75. return false;
  76. }
  77. if ( empty( $cache_key_prefix ) ) {
  78. $cache_key_prefix = $meta_table;
  79. }
  80. if ( empty( $object_column ) ) {
  81. $object_column = $object_type . '_id';
  82. }
  83. $object_ids = wp_parse_id_list( $object_ids );
  84. $cache = array();
  85. // Get meta info
  86. $id_list = join( ',', $object_ids );
  87. $meta_list = $wpdb->get_results( $wpdb->prepare( "SELECT {$object_column}, meta_key, meta_value FROM {$meta_table} WHERE {$object_column} IN ($id_list)", $object_type ), ARRAY_A );
  88. if ( !empty( $meta_list ) ) {
  89. foreach ( $meta_list as $metarow ) {
  90. $mpid = intval( $metarow[$object_column] );
  91. $mkey = $metarow['meta_key'];
  92. $mval = $metarow['meta_value'];
  93. // Force subkeys to be array type:
  94. if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
  95. $cache[$mpid] = array();
  96. if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
  97. $cache[$mpid][$mkey] = array();
  98. // Add a value to the current pid/key:
  99. $cache[$mpid][$mkey][] = $mval;
  100. }
  101. }
  102. foreach ( $object_ids as $id ) {
  103. if ( ! isset($cache[$id]) )
  104. $cache[$id] = array();
  105. foreach( $cache[$id] as $meta_key => $meta_value ) {
  106. wp_cache_set( $cache_key_prefix . '_' . $id . '_' . $meta_key, $meta_value, 'bp' );
  107. }
  108. }
  109. return $cache;
  110. }