WP_Metadata_Lazyloader

Core class used for lazy-loading object metadata.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-metadata-lazyloader.php  
  1. class WP_Metadata_Lazyloader { 
  2. /** 
  3. * Pending objects queue. 
  4. * @since 4.5.0 
  5. * @access protected 
  6. * @var array 
  7. */ 
  8. protected $pending_objects; 
  9.  
  10. /** 
  11. * Settings for supported object types. 
  12. * @since 4.5.0 
  13. * @access protected 
  14. * @var array 
  15. */ 
  16. protected $settings = array(); 
  17.  
  18. /** 
  19. * Constructor. 
  20. * @since 4.5.0 
  21. * @access public 
  22. */ 
  23. public function __construct() { 
  24. $this->settings = array( 
  25. 'term' => array( 
  26. 'filter' => 'get_term_metadata',  
  27. 'callback' => array( $this, 'lazyload_term_meta' ),  
  28. ),  
  29. 'comment' => array( 
  30. 'filter' => 'get_comment_metadata',  
  31. 'callback' => array( $this, 'lazyload_comment_meta' ),  
  32. ),  
  33. ); 
  34.  
  35. /** 
  36. * Adds objects to the metadata lazy-load queue. 
  37. * @since 4.5.0 
  38. * @access public 
  39. * @param string $object_type Type of object whose meta is to be lazy-loaded. Accepts 'term' or 'comment'. 
  40. * @param array $object_ids Array of object IDs. 
  41. * @return bool|WP_Error True on success, WP_Error on failure. 
  42. */ 
  43. public function queue_objects( $object_type, $object_ids ) { 
  44. if ( ! isset( $this->settings[ $object_type ] ) ) { 
  45. return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) ); 
  46.  
  47. $type_settings = $this->settings[ $object_type ]; 
  48.  
  49. if ( ! isset( $this->pending_objects[ $object_type ] ) ) { 
  50. $this->pending_objects[ $object_type ] = array(); 
  51.  
  52. foreach ( $object_ids as $object_id ) { 
  53. // Keyed by ID for faster lookup. 
  54. if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) { 
  55. $this->pending_objects[ $object_type ][ $object_id ] = 1; 
  56.  
  57. add_filter( $type_settings['filter'], $type_settings['callback'] ); 
  58.  
  59. /** 
  60. * Fires after objects are added to the metadata lazy-load queue. 
  61. * @since 4.5.0 
  62. * @param array $object_ids Object IDs. 
  63. * @param string $object_type Type of object being queued. 
  64. * @param WP_Metadata_Lazyloader $lazyloader The lazy-loader object. 
  65. */ 
  66. do_action( 'metadata_lazyloader_queued_objects', $object_ids, $object_type, $this ); 
  67.  
  68. /** 
  69. * Resets lazy-load queue for a given object type. 
  70. * @since 4.5.0 
  71. * @access public 
  72. * @param string $object_type Object type. Accepts 'comment' or 'term'. 
  73. * @return bool|WP_Error True on success, WP_Error on failure. 
  74. */ 
  75. public function reset_queue( $object_type ) { 
  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.  
  81. $this->pending_objects[ $object_type ] = array(); 
  82. remove_filter( $type_settings['filter'], $type_settings['callback'] ); 
  83.  
  84. /** 
  85. * Lazy-loads term meta for queued terms. 
  86. * This method is public so that it can be used as a filter callback. As a rule, there 
  87. * is no need to invoke it directly. 
  88. * @since 4.5.0 
  89. * @access public 
  90. * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook. 
  91. * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be 
  92. * another value if filtered by a plugin. 
  93. */ 
  94. public function lazyload_term_meta( $check ) { 
  95. if ( ! empty( $this->pending_objects['term'] ) ) { 
  96. update_termmeta_cache( array_keys( $this->pending_objects['term'] ) ); 
  97.  
  98. // No need to run again for this set of terms. 
  99. $this->reset_queue( 'term' ); 
  100.  
  101. return $check; 
  102.  
  103. /** 
  104. * Lazy-loads comment meta for queued comments. 
  105. * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it 
  106. * directly, from either inside or outside the `WP_Query` object. 
  107. * @since 4.5.0 
  108. * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook. 
  109. * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`. 
  110. */ 
  111. public function lazyload_comment_meta( $check ) { 
  112. if ( ! empty( $this->pending_objects['comment'] ) ) { 
  113. update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) ); 
  114.  
  115. // No need to run again for this set of comments. 
  116. $this->reset_queue( 'comment' ); 
  117.  
  118. return $check;