You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

177 lines
5.4 KiB

  1. <?php
  2. /**
  3. * Meta API: WP_Metadata_Lazyloader class
  4. *
  5. * @package WordPress
  6. * @subpackage Meta
  7. * @since 4.5.0
  8. */
  9. /**
  10. * Core class used for lazy-loading object metadata.
  11. *
  12. * When loading many objects of a given type, such as posts in a WP_Query loop, it often makes
  13. * sense to prime various metadata caches at the beginning of the loop. This means fetching all
  14. * relevant metadata with a single database query, a technique that has the potential to improve
  15. * performance dramatically in some cases.
  16. *
  17. * In cases where the given metadata may not even be used in the loop, we can improve performance
  18. * even more by only priming the metadata cache for affected items the first time a piece of metadata
  19. * is requested - ie, by lazy-loading it. So, for example, comment meta may not be loaded into the
  20. * cache in the comments section of a post until the first time get_comment_meta() is called in the
  21. * context of the comment loop.
  22. *
  23. * WP uses the WP_Metadata_Lazyloader class to queue objects for metadata cache priming. The class
  24. * then detects the relevant get_*_meta() function call, and queries the metadata of all queued objects.
  25. *
  26. * Do not access this class directly. Use the wp_metadata_lazyloader() function.
  27. *
  28. * @since 4.5.0
  29. */
  30. class WP_Metadata_Lazyloader {
  31. /**
  32. * Pending objects queue.
  33. *
  34. * @since 4.5.0
  35. * @access protected
  36. * @var array
  37. */
  38. protected $pending_objects;
  39. /**
  40. * Settings for supported object types.
  41. *
  42. * @since 4.5.0
  43. * @access protected
  44. * @var array
  45. */
  46. protected $settings = array();
  47. /**
  48. * Constructor.
  49. *
  50. * @since 4.5.0
  51. * @access public
  52. */
  53. public function __construct() {
  54. $this->settings = array(
  55. 'term' => array(
  56. 'filter' => 'get_term_metadata',
  57. 'callback' => array( $this, 'lazyload_term_meta' ),
  58. ),
  59. 'comment' => array(
  60. 'filter' => 'get_comment_metadata',
  61. 'callback' => array( $this, 'lazyload_comment_meta' ),
  62. ),
  63. );
  64. }
  65. /**
  66. * Adds objects to the metadata lazy-load queue.
  67. *
  68. * @since 4.5.0
  69. * @access public
  70. *
  71. * @param string $object_type Type of object whose meta is to be lazy-loaded. Accepts 'term' or 'comment'.
  72. * @param array $object_ids Array of object IDs.
  73. * @return bool|WP_Error True on success, WP_Error on failure.
  74. */
  75. public function queue_objects( $object_type, $object_ids ) {
  76. if ( ! isset( $this->settings[ $object_type ] ) ) {
  77. return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
  78. }
  79. $type_settings = $this->settings[ $object_type ];
  80. if ( ! isset( $this->pending_objects[ $object_type ] ) ) {
  81. $this->pending_objects[ $object_type ] = array();
  82. }
  83. foreach ( $object_ids as $object_id ) {
  84. // Keyed by ID for faster lookup.
  85. if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
  86. $this->pending_objects[ $object_type ][ $object_id ] = 1;
  87. }
  88. }
  89. add_filter( $type_settings['filter'], $type_settings['callback'] );
  90. /**
  91. * Fires after objects are added to the metadata lazy-load queue.
  92. *
  93. * @since 4.5.0
  94. *
  95. * @param array $object_ids Object IDs.
  96. * @param string $object_type Type of object being queued.
  97. * @param WP_Metadata_Lazyloader $lazyloader The lazy-loader object.
  98. */
  99. do_action( 'metadata_lazyloader_queued_objects', $object_ids, $object_type, $this );
  100. }
  101. /**
  102. * Resets lazy-load queue for a given object type.
  103. *
  104. * @since 4.5.0
  105. * @access public
  106. *
  107. * @param string $object_type Object type. Accepts 'comment' or 'term'.
  108. * @return bool|WP_Error True on success, WP_Error on failure.
  109. */
  110. public function reset_queue( $object_type ) {
  111. if ( ! isset( $this->settings[ $object_type ] ) ) {
  112. return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
  113. }
  114. $type_settings = $this->settings[ $object_type ];
  115. $this->pending_objects[ $object_type ] = array();
  116. remove_filter( $type_settings['filter'], $type_settings['callback'] );
  117. }
  118. /**
  119. * Lazy-loads term meta for queued terms.
  120. *
  121. * This method is public so that it can be used as a filter callback. As a rule, there
  122. * is no need to invoke it directly.
  123. *
  124. * @since 4.5.0
  125. * @access public
  126. *
  127. * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
  128. * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
  129. * another value if filtered by a plugin.
  130. */
  131. public function lazyload_term_meta( $check ) {
  132. if ( ! empty( $this->pending_objects['term'] ) ) {
  133. update_termmeta_cache( array_keys( $this->pending_objects['term'] ) );
  134. // No need to run again for this set of terms.
  135. $this->reset_queue( 'term' );
  136. }
  137. return $check;
  138. }
  139. /**
  140. * Lazy-loads comment meta for queued comments.
  141. *
  142. * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
  143. * directly, from either inside or outside the `WP_Query` object.
  144. *
  145. * @since 4.5.0
  146. *
  147. * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook.
  148. * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
  149. */
  150. public function lazyload_comment_meta( $check ) {
  151. if ( ! empty( $this->pending_objects['comment'] ) ) {
  152. update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) );
  153. // No need to run again for this set of comments.
  154. $this->reset_queue( 'comment' );
  155. }
  156. return $check;
  157. }
  158. }