wp/wp-includes/class-wp-metadata-lazyloader.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    26  *
    26  *
    27  * Do not access this class directly. Use the wp_metadata_lazyloader() function.
    27  * Do not access this class directly. Use the wp_metadata_lazyloader() function.
    28  *
    28  *
    29  * @since 4.5.0
    29  * @since 4.5.0
    30  */
    30  */
       
    31 #[AllowDynamicProperties]
    31 class WP_Metadata_Lazyloader {
    32 class WP_Metadata_Lazyloader {
    32 	/**
    33 	/**
    33 	 * Pending objects queue.
    34 	 * Pending objects queue.
    34 	 *
    35 	 *
    35 	 * @since 4.5.0
    36 	 * @since 4.5.0
    52 	 */
    53 	 */
    53 	public function __construct() {
    54 	public function __construct() {
    54 		$this->settings = array(
    55 		$this->settings = array(
    55 			'term'    => array(
    56 			'term'    => array(
    56 				'filter'   => 'get_term_metadata',
    57 				'filter'   => 'get_term_metadata',
    57 				'callback' => array( $this, 'lazyload_term_meta' ),
    58 				'callback' => array( $this, 'lazyload_meta_callback' ),
    58 			),
    59 			),
    59 			'comment' => array(
    60 			'comment' => array(
    60 				'filter'   => 'get_comment_metadata',
    61 				'filter'   => 'get_comment_metadata',
    61 				'callback' => array( $this, 'lazyload_comment_meta' ),
    62 				'callback' => array( $this, 'lazyload_meta_callback' ),
       
    63 			),
       
    64 			'blog'    => array(
       
    65 				'filter'   => 'get_blog_metadata',
       
    66 				'callback' => array( $this, 'lazyload_meta_callback' ),
    62 			),
    67 			),
    63 		);
    68 		);
    64 	}
    69 	}
    65 
    70 
    66 	/**
    71 	/**
    88 			if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
    93 			if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
    89 				$this->pending_objects[ $object_type ][ $object_id ] = 1;
    94 				$this->pending_objects[ $object_type ][ $object_id ] = 1;
    90 			}
    95 			}
    91 		}
    96 		}
    92 
    97 
    93 		add_filter( $type_settings['filter'], $type_settings['callback'] );
    98 		add_filter( $type_settings['filter'], $type_settings['callback'], 10, 5 );
    94 
    99 
    95 		/**
   100 		/**
    96 		 * Fires after objects are added to the metadata lazy-load queue.
   101 		 * Fires after objects are added to the metadata lazy-load queue.
    97 		 *
   102 		 *
    98 		 * @since 4.5.0
   103 		 * @since 4.5.0
   128 	 *
   133 	 *
   129 	 * This method is public so that it can be used as a filter callback. As a rule, there
   134 	 * This method is public so that it can be used as a filter callback. As a rule, there
   130 	 * is no need to invoke it directly.
   135 	 * is no need to invoke it directly.
   131 	 *
   136 	 *
   132 	 * @since 4.5.0
   137 	 * @since 4.5.0
       
   138 	 * @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead.
   133 	 *
   139 	 *
   134 	 * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
   140 	 * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
   135 	 * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
   141 	 * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
   136 	 *               another value if filtered by a plugin.
   142 	 *               another value if filtered by a plugin.
   137 	 */
   143 	 */
   138 	public function lazyload_term_meta( $check ) {
   144 	public function lazyload_term_meta( $check ) {
   139 		if ( ! empty( $this->pending_objects['term'] ) ) {
   145 		_deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' );
   140 			update_termmeta_cache( array_keys( $this->pending_objects['term'] ) );
   146 		return $this->lazyload_meta_callback( $check, 0, '', false, 'term' );
   141 
       
   142 			// No need to run again for this set of terms.
       
   143 			$this->reset_queue( 'term' );
       
   144 		}
       
   145 
       
   146 		return $check;
       
   147 	}
   147 	}
   148 
   148 
   149 	/**
   149 	/**
   150 	 * Lazy-loads comment meta for queued comments.
   150 	 * Lazy-loads comment meta for queued comments.
   151 	 *
   151 	 *
   152 	 * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
   152 	 * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
   153 	 * directly, from either inside or outside the `WP_Query` object.
   153 	 * directly, from either inside or outside the `WP_Query` object.
   154 	 *
   154 	 *
   155 	 * @since 4.5.0
   155 	 * @since 4.5.0
       
   156 	 * @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead.
   156 	 *
   157 	 *
   157 	 * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook.
   158 	 * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook.
   158 	 * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
   159 	 * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
   159 	 */
   160 	 */
   160 	public function lazyload_comment_meta( $check ) {
   161 	public function lazyload_comment_meta( $check ) {
   161 		if ( ! empty( $this->pending_objects['comment'] ) ) {
   162 		_deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' );
   162 			update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) );
   163 		return $this->lazyload_meta_callback( $check, 0, '', false, 'comment' );
   163 
   164 	}
   164 			// No need to run again for this set of comments.
   165 
   165 			$this->reset_queue( 'comment' );
   166 	/**
   166 		}
   167 	 * Lazy-loads meta for queued objects.
       
   168 	 *
       
   169 	 * This method is public so that it can be used as a filter callback. As a rule, there
       
   170 	 * is no need to invoke it directly.
       
   171 	 *
       
   172 	 * @since 6.3.0
       
   173 	 *
       
   174 	 * @param mixed  $check     The `$check` param passed from the 'get_*_metadata' hook.
       
   175 	 * @param int    $object_id ID of the object metadata is for.
       
   176 	 * @param string $meta_key  Unused.
       
   177 	 * @param bool   $single    Unused.
       
   178 	 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   179 	 *                          or any other object type with an associated meta table.
       
   180 	 * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
       
   181 	 *               another value if filtered by a plugin.
       
   182 	 */
       
   183 	public function lazyload_meta_callback( $check, $object_id, $meta_key, $single, $meta_type ) {
       
   184 		if ( empty( $this->pending_objects[ $meta_type ] ) ) {
       
   185 			return $check;
       
   186 		}
       
   187 
       
   188 		$object_ids = array_keys( $this->pending_objects[ $meta_type ] );
       
   189 		if ( $object_id && ! in_array( $object_id, $object_ids, true ) ) {
       
   190 			$object_ids[] = $object_id;
       
   191 		}
       
   192 
       
   193 		update_meta_cache( $meta_type, $object_ids );
       
   194 
       
   195 		// No need to run again for this set of objects.
       
   196 		$this->reset_queue( $meta_type );
   167 
   197 
   168 		return $check;
   198 		return $check;
   169 	}
   199 	}
   170 }
   200 }