wpSocket - Connecting WordPress People
  • Services
    • WordPress Recovery & Repair Service
    • WordPress Malware Removal Service
    • WordPress Speed Optimization Service
    • WordPress Migration Service
  • Resources
    • WP Infractucture
      • WordPress CDN
      • WordPress WAF
      • WordPress SSL
      • WordPress GeoDNS
      • WordPress Load Balancer
      • WordPress Redis
    • WP Themes
      • Most Rated WordPress Themes
      • Most Reviewed WordPress Themes
      • Most Downloaded WordPress Themes
      • Most Active Installed WordPress Themes
      • Recently Updated WordPress Themes
      • Latest WordPress Themes
    • WP Plugins
      • Most Rated WordPress Plugins
      • Most Reviewed WordPress Plugins
      • Most Downloaded WordPress Plugins
      • Most Active Installed WordPress Plugins
      • Recently Updated WordPress Plugins
      • Latest WordPress Plugins
    • WP Referrence
      • Functions
      • Classes
      • Methods
  • About
    • About wpSocket
    • Write for wpSocket
    • Advertise with wpSocket
    • Contact wpSocket
  • BLOG
    • Read wpSocket Blogs
  • WP career
    • Learn with WP
    • Earn with WP
  • Contact
  1. Home
  2. wpref
  3. class
  4. wp_metadata_lazyloader

wp_metadata_lazyloader

class
  • _WP_Dependency
  • _WP_Editors
  • AMFReader
  • AMFStream
  • AtomEntry
  • AtomFeed
  • AtomParser
  • AVCSequenceParameterSetReader
  • Error
  • getid3_ac3
  • getid3_apetag
  • getid3_asf
  • getid3_dts
  • getid3_exception
  • getid3_flac
  • getid3_flv
  • getid3_handler
  • getid3_id3v1
  • getid3_id3v2
  • getid3_lib
  • getid3_lyrics3
  • getid3_matroska
  • getid3_mp3
  • getid3_ogg
  • getid3_quicktime
  • getid3_riff
  • getID3
  • Gettext_Translations
  • IXR_Base64
  • IXR_Client
  • IXR_ClientMulticall
  • IXR_Date
  • IXR_Error
  • IXR_IntrospectionServer
  • IXR_Message
  • IXR_Request
  • IXR_Server
  • IXR_Value
  • MagpieRSS
  • MO
  • NOOP_Translations
  • ParagonIE_Sodium_Compat
  • ParagonIE_Sodium_Core_BLAKE2b
  • ParagonIE_Sodium_Core_ChaCha20_Ctx
  • ParagonIE_Sodium_Core_ChaCha20_IetfCtx
  • ParagonIE_Sodium_Core_ChaCha20
  • ParagonIE_Sodium_Core_Curve25519_Fe
  • ParagonIE_Sodium_Core_Curve25519_Ge_Cached
  • ParagonIE_Sodium_Core_Curve25519_Ge_P1p1
  • ParagonIE_Sodium_Core_Curve25519_Ge_P2
  • ParagonIE_Sodium_Core_Curve25519_Ge_P3
  • ParagonIE_Sodium_Core_Curve25519_Ge_Precomp
  • ParagonIE_Sodium_Core_Curve25519_H
  • ParagonIE_Sodium_Core_Curve25519
  • ParagonIE_Sodium_Core_Ed25519
  • ParagonIE_Sodium_Core_HChaCha20
  • ParagonIE_Sodium_Core_HSalsa20
  • ParagonIE_Sodium_Core_Poly1305_State
  • ParagonIE_Sodium_Core_Poly1305
  • ParagonIE_Sodium_Core_Salsa20
  • ParagonIE_Sodium_Core_SipHash
  • ParagonIE_Sodium_Core_Util
  • ParagonIE_Sodium_Core_X25519
  • ParagonIE_Sodium_Core_XChaCha20
  • ParagonIE_Sodium_Core_XSalsa20
  • ParagonIE_Sodium_Core32_BLAKE2b
  • ParagonIE_Sodium_Core32_ChaCha20_Ctx
  • ParagonIE_Sodium_Core32_ChaCha20_IetfCtx
  • ParagonIE_Sodium_Core32_ChaCha20
  • ParagonIE_Sodium_Core32_Curve25519_Fe
  • ParagonIE_Sodium_Core32_Curve25519_Ge_Cached
  • ParagonIE_Sodium_Core32_Curve25519_Ge_P1p1
  • ParagonIE_Sodium_Core32_Curve25519_Ge_P2
  • ParagonIE_Sodium_Core32_Curve25519_Ge_P3
  • ParagonIE_Sodium_Core32_Curve25519_Ge_Precomp
  • ParagonIE_Sodium_Core32_Curve25519_H
  • ParagonIE_Sodium_Core32_Curve25519
  • ParagonIE_Sodium_Core32_Ed25519
  • ParagonIE_Sodium_Core32_HChaCha20
  • ParagonIE_Sodium_Core32_HSalsa20
  • ParagonIE_Sodium_Core32_Int32
  • ParagonIE_Sodium_Core32_Int64
  • ParagonIE_Sodium_Core32_Poly1305_State
  • ParagonIE_Sodium_Core32_Poly1305
  • ParagonIE_Sodium_Core32_Salsa20
  • ParagonIE_Sodium_Core32_SipHash
  • ParagonIE_Sodium_Core32_Util
  • ParagonIE_Sodium_Core32_X25519
  • ParagonIE_Sodium_Core32_XChaCha20
  • ParagonIE_Sodium_Core32_XSalsa20
  • ParagonIE_Sodium_Crypto
  • ParagonIE_Sodium_Crypto32
  • ParagonIE_Sodium_File
  • Compat
  • BLAKE2b
  • Ctx
  • IetfCtx
  • ChaCha20
  • Fe
  • Cached
  • P1p1
  • P2
  • P3
  • Precomp
  • H
  • Curve25519
  • Ed25519
  • HChaCha20
  • HSalsa20
  • State
  • Poly1305
  • Salsa20
  • SipHash
  • Util
  • X25519
  • XChaCha20
  • Xsalsa20
  • Crypto
  • File
  • PasswordHash
  • PHPMailer
  • phpmailerException
  • Plural_Forms
  • PO
  • POMO_CachedFileReader
  • POMO_CachedIntFileReader
  • POMO_FileReader
  • POMO_Reader
  • POMO_StringReader
  • POP3
  • Requests_Auth_Basic
  • Requests_Cookie_Jar
  • Requests_Cookie
  • Requests_Exception_HTTP_304
  • Requests_Exception_HTTP_305
  • Requests_Exception_HTTP_306
  • Requests_Exception_HTTP_400
  • Requests_Exception_HTTP_401
  • Requests_Exception_HTTP_402
  • Requests_Exception_HTTP_403
  • Requests_Exception_HTTP_404
  • Requests_Exception_HTTP_405
  • Requests_Exception_HTTP_406
  • Requests_Exception_HTTP_407
  • Requests_Exception_HTTP_408
  • Requests_Exception_HTTP_409
  • Requests_Exception_HTTP_410
  • Requests_Exception_HTTP_411
  • Requests_Exception_HTTP_412
  • Requests_Exception_HTTP_413
  • Requests_Exception_HTTP_414
  • Requests_Exception_HTTP_415
  • Requests_Exception_HTTP_416
  • Requests_Exception_HTTP_417
  • Requests_Exception_HTTP_418
  • Requests_Exception_HTTP_428
  • Requests_Exception_HTTP_429
  • Requests_Exception_HTTP_431
  • Requests_Exception_HTTP_500
  • Requests_Exception_HTTP_501
  • Requests_Exception_HTTP_502
  • Requests_Exception_HTTP_503
  • Requests_Exception_HTTP_504
  • Requests_Exception_HTTP_505
  • Requests_Exception_HTTP_511
  • Requests_Exception_HTTP_Unknown
  • Requests_Exception_HTTP
  • Requests_Exception_Transport_cURL
  • Requests_Exception_Transport
  • Requests_Exception
  • Requests_Hooks
  • Requests_IDNAEncoder
  • Requests_IPv6
  • Requests_IRI
  • Requests_Proxy_HTTP
  • Requests_Response_Headers
  • Requests_Response
  • Requests_Session
  • Requests_SSL
  • Requests_Transport_cURL
  • Requests_Transport_fsockopen
  • Requests_Utility_CaseInsensitiveDictionary
  • Requests_Utility_FilteredIterator
  • Requests
  • RSSCache
  • bar
  • SimplePie_Author
  • SimplePie_Cache_DB
  • SimplePie_Cache_File
  • SimplePie_Cache_Memcache
  • SimplePie_Cache_MySQL
  • SimplePie_Cache
  • SimplePie_Caption
  • SimplePie_Category
  • SimplePie_Content_Type_Sniffer
  • SimplePie_Copyright
  • SimplePie_Core
  • SimplePie_Credit
  • SimplePie_Decode_HTML_Entities
  • SimplePie_Enclosure
  • SimplePie_Exception
  • SimplePie_File
  • SimplePie_gzdecode
  • SimplePie_HTTP_Parser
  • SimplePie_IRI
  • SimplePie_Item
  • SimplePie_Locator
  • SimplePie_Misc
  • SimplePie_Net_IPv6
  • SimplePie_Parse_Date
  • SimplePie_Parser
  • SimplePie_Rating
  • SimplePie_Registry
  • SimplePie_Restriction
  • SimplePie_Sanitize
  • SimplePie_Source
  • SimplePie_XML_Declaration_Parser
  • SimplePie
  • SMTP
  • Snoopy
  • SodiumException
  • Text_Diff_Engine_native
  • Text_Diff_Engine_shell
  • Text_Diff_Engine_string
  • Text_Diff_Engine_xdiff
  • Text_Diff_Op_add
  • Text_Diff_Op_change
  • Text_Diff_Op_copy
  • Text_Diff_Op_delete
  • Text_Diff_Op
  • Text_Diff_Renderer_inline
  • Text_Diff_Renderer
  • Text_Diff
  • Text_MappedDiff
  • Translation_Entry
  • Translations
  • Walker_Category
  • Walker_CategoryDropdown
  • Walker_Comment
  • Walker_Nav_Menu
  • Walker_Page
  • Walker_PageDropdown
  • Walker
  • WP_Admin_Bar
  • WP_Ajax_Response
  • wp_atom_server
  • WP_Block_Parser_Block
  • WP_Block_Parser_Frame
  • WP_Block_Parser
  • WP_Block_Styles_Registry
  • WP_Block_Type_Registry
  • WP_Block_Type
  • WP_Comment_Query
  • WP_Comment
  • WP_Customize_Background_Image_Control
  • WP_Customize_Background_Image_Setting
  • WP_Customize_Background_Position_Control
  • WP_Customize_Code_Editor_Control
  • WP_Customize_Color_Control
  • WP_Customize_Control
  • WP_Customize_Cropped_Image_Control
  • WP_Customize_Custom_CSS_Setting
  • WP_Customize_Date_Time_Control
  • WP_Customize_Filter_Setting
  • WP_Customize_Header_Image_Control
  • WP_Customize_Header_Image_Setting
  • WP_Customize_Image_Control
  • WP_Customize_Manager
  • WP_Customize_Media_Control
  • WP_Customize_Nav_Menu_Auto_Add_Control
  • WP_Customize_Nav_Menu_Control
  • WP_Customize_Nav_Menu_Item_Control
  • WP_Customize_Nav_Menu_Item_Setting
  • WP_Customize_Nav_Menu_Location_Control
  • WP_Customize_Nav_Menu_Locations_Control
  • WP_Customize_Nav_Menu_Name_Control
  • WP_Customize_Nav_Menu_Section
  • WP_Customize_Nav_Menu_Setting
  • WP_Customize_Nav_Menus_Panel
  • WP_Customize_Nav_Menus
  • WP_Customize_New_Menu_Control
  • WP_Customize_New_Menu_Section
  • WP_Customize_Panel
  • WP_Customize_Partial
  • WP_Customize_Section
  • WP_Customize_Selective_Refresh
  • WP_Customize_Setting
  • WP_Customize_Sidebar_Section
  • WP_Customize_Site_Icon_Control
  • WP_Customize_Theme_Control
  • WP_Customize_Themes_Panel
  • WP_Customize_Themes_Section
  • WP_Customize_Upload_Control
  • WP_Customize_Widgets
  • WP_Date_Query
  • WP_Dependencies
  • WP_Embed
  • WP_Error
  • WP_Fatal_Error_Handler
  • WP_Feed_Cache_Transient
  • WP_Feed_Cache
  • WP_Hook
  • WP_Http_Cookie
  • WP_Http_Curl
  • WP_Http_Encoding
  • WP_HTTP_Fsockopen
  • WP_HTTP_IXR_Client
  • WP_HTTP_Proxy
  • WP_HTTP_Requests_Hooks
  • WP_HTTP_Requests_Response
  • WP_HTTP_Response
  • WP_Http_Streams
  • WP_Http
  • WP_Image_Editor_GD
  • WP_Image_Editor_Imagick
  • WP_Image_Editor
  • WP_List_Util
  • WP_Locale_Switcher
  • WP_Locale
  • WP_MatchesMapRegex
  • WP_Meta_Query
  • WP_Metadata_Lazyloader
  • WP_Nav_Menu_Widget
  • WP_Network_Query
  • WP_Network
  • WP_Object_Cache
  • WP_oEmbed_Controller
  • WP_oEmbed
  • WP_Paused_Extensions_Storage
  • WP_Post_Type
  • WP_Post
  • WP_Query
  • WP_Recovery_Mode_Cookie_Service
  • WP_Recovery_Mode_Email_Service
  • WP_Recovery_Mode_Key_Service
  • WP_Recovery_Mode_Link_Service
  • WP_Recovery_Mode
  • WP_REST_Attachments_Controller
  • WP_REST_Autosaves_Controller
  • WP_REST_Block_Renderer_Controller
  • WP_REST_Blocks_Controller
  • WP_REST_Comment_Meta_Fields
  • WP_REST_Comments_Controller
  • WP_REST_Controller
  • WP_REST_Meta_Fields
  • WP_REST_Post_Meta_Fields
  • WP_REST_Post_Search_Handler
  • WP_REST_Post_Statuses_Controller
  • WP_REST_Post_Types_Controller
  • WP_REST_Posts_Controller
  • WP_REST_Request
  • WP_REST_Response
  • WP_REST_Revisions_Controller
  • WP_REST_Search_Controller
  • WP_REST_Search_Handler
  • WP_REST_Server
  • WP_REST_Settings_Controller
  • WP_REST_Taxonomies_Controller
  • WP_REST_Term_Meta_Fields
  • WP_REST_Terms_Controller
  • WP_REST_Themes_Controller
  • WP_REST_User_Meta_Fields
  • WP_REST_Users_Controller
  • WP_Rewrite
  • WP_Role
  • WP_Roles
  • WP_Scripts
  • WP_Session_Tokens
  • WP_SimplePie_File
  • WP_SimplePie_Sanitize_KSES
  • WP_Site_Query
  • WP_Site
  • WP_Styles
  • WP_Tax_Query
  • WP_Taxonomy
  • WP_Term_Query
  • WP_Term
  • WP_Text_Diff_Renderer_inline
  • WP_Text_Diff_Renderer_Table
  • WP_Theme
  • WP_User_Meta_Session_Tokens
  • WP_User_Query
  • WP_User_Request
  • WP_User
  • WP_Widget_Archives
  • WP_Widget_Area_Customize_Control
  • WP_Widget_Calendar
  • WP_Widget_Categories
  • WP_Widget_Custom_HTML
  • WP_Widget_Factory
  • WP_Widget_Form_Customize_Control
  • WP_Widget_Links
  • WP_Widget_Media_Audio
  • WP_Widget_Media_Gallery
  • WP_Widget_Media_Image
  • WP_Widget_Media_Video
  • WP_Widget_Media
  • WP_Widget_Meta
  • WP_Widget_Pages
  • WP_Widget_Recent_Comments
  • WP_Widget_Recent_Posts
  • WP_Widget_RSS
  • WP_Widget_Search
  • WP_Widget_Tag_Cloud
  • WP_Widget_Text
  • WP_Widget
  • wp_xmlrpc_server
  • WP
  • wpdb

WordPress Web Application Development

$300 worth free guest posting on WPSocket.com!
Get Started With Free Trial

Explore the offer details: Discover it now



WordPress Themes


WordPress Plugins

WP_Metadata_Lazyloader

Core class used for lazy-loading object metadata.

Contents

  • Description
    • Source
    • Changelog
  • Methods
  • User Contributed Notes

Description #Description

When loading many objects of a given type, such as posts in a WP_Query loop, it often makes sense to prime various metadata caches at the beginning of the loop. This means fetching all relevant metadata with a single database query, a technique that has the potential to improve performance dramatically in some cases.

In cases where the given metadata may not even be used in the loop, we can improve performance even more by only priming the metadata cache for affected items the first time a piece of metadata is requested – ie, by lazy-loading it. So, for example, comment meta may not be loaded into the cache in the comments section of a post until the first time get_comment_meta() is called in the context of the comment loop.

WP uses the WP_Metadata_Lazyloader class to queue objects for metadata cache priming. The class then detects the relevant get_*_meta() function call, and queries the metadata of all queued objects.

Do not access this class directly. Use the wp_metadata_lazyloader() function.


Source #Source

File: wp-includes/class-wp-metadata-lazyloader.php

class WP_Metadata_Lazyloader {
	/**
	 * Pending objects queue.
	 *
	 * @since 4.5.0
	 * @var array
	 */
	protected $pending_objects;

	/**
	 * Settings for supported object types.
	 *
	 * @since 4.5.0
	 * @var array
	 */
	protected $settings = array();

	/**
	 * Constructor.
	 *
	 * @since 4.5.0
	 */
	public function __construct() {
		$this->settings = array(
			'term'    => array(
				'filter'   => 'get_term_metadata',
				'callback' => array( $this, 'lazyload_term_meta' ),
			),
			'comment' => array(
				'filter'   => 'get_comment_metadata',
				'callback' => array( $this, 'lazyload_comment_meta' ),
			),
		);
	}

	/**
	 * Adds objects to the metadata lazy-load queue.
	 *
	 * @since 4.5.0
	 *
	 * @param string $object_type Type of object whose meta is to be lazy-loaded. Accepts 'term' or 'comment'.
	 * @param array  $object_ids  Array of object IDs.
	 * @return bool|WP_Error True on success, WP_Error on failure.
	 */
	public function queue_objects( $object_type, $object_ids ) {
		if ( ! isset( $this->settings[ $object_type ] ) ) {
			return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
		}

		$type_settings = $this->settings[ $object_type ];

		if ( ! isset( $this->pending_objects[ $object_type ] ) ) {
			$this->pending_objects[ $object_type ] = array();
		}

		foreach ( $object_ids as $object_id ) {
			// Keyed by ID for faster lookup.
			if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
				$this->pending_objects[ $object_type ][ $object_id ] = 1;
			}
		}

		add_filter( $type_settings['filter'], $type_settings['callback'] );

		/**
		 * Fires after objects are added to the metadata lazy-load queue.
		 *
		 * @since 4.5.0
		 *
		 * @param array                  $object_ids  Array of object IDs.
		 * @param string                 $object_type Type of object being queued.
		 * @param WP_Metadata_Lazyloader $lazyloader  The lazy-loader object.
		 */
		do_action( 'metadata_lazyloader_queued_objects', $object_ids, $object_type, $this );
	}

	/**
	 * Resets lazy-load queue for a given object type.
	 *
	 * @since 4.5.0
	 *
	 * @param string $object_type Object type. Accepts 'comment' or 'term'.
	 * @return bool|WP_Error True on success, WP_Error on failure.
	 */
	public function reset_queue( $object_type ) {
		if ( ! isset( $this->settings[ $object_type ] ) ) {
			return new WP_Error( 'invalid_object_type', __( 'Invalid object type' ) );
		}

		$type_settings = $this->settings[ $object_type ];

		$this->pending_objects[ $object_type ] = array();
		remove_filter( $type_settings['filter'], $type_settings['callback'] );
	}

	/**
	 * Lazy-loads term meta for queued terms.
	 *
	 * This method is public so that it can be used as a filter callback. As a rule, there
	 * is no need to invoke it directly.
	 *
	 * @since 4.5.0
	 *
	 * @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
	 * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
	 *               another value if filtered by a plugin.
	 */
	public function lazyload_term_meta( $check ) {
		if ( ! empty( $this->pending_objects['term'] ) ) {
			update_termmeta_cache( array_keys( $this->pending_objects['term'] ) );

			// No need to run again for this set of terms.
			$this->reset_queue( 'term' );
		}

		return $check;
	}

	/**
	 * Lazy-loads comment meta for queued comments.
	 *
	 * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
	 * directly, from either inside or outside the `WP_Query` object.
	 *
	 * @since 4.5.0
	 *
	 * @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook.
	 * @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
	 */
	public function lazyload_comment_meta( $check ) {
		if ( ! empty( $this->pending_objects['comment'] ) ) {
			update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) );

			// No need to run again for this set of comments.
			$this->reset_queue( 'comment' );
		}

		return $check;
	}
}

Expand full source code Collapse full source code View on Trac


Top ↑

Changelog #Changelog

Changelog
Version Description
4.5.0 Introduced.

Top ↑

Methods #Methods

  • __construct — Constructor.
  • lazyload_comment_meta — Lazy-loads comment meta for queued comments.
  • lazyload_term_meta — Lazy-loads term meta for queued terms.
  • queue_objects — Adds objects to the metadata lazy-load queue.
  • reset_queue — Resets lazy-load queue for a given object type.

Top ↑

User Contributed Notes #User Contributed Notes

You must log in before being able to contribute a note or feedback.

Keep Leading Your Followers!
Share it for them.

About

  • About wpSocket
  • Write for wpSocket
  • Advertise with wpSocket
  • Contact wpSocket
  • Privacy Policy
  • Terms of Use

Services

  • WordPress CDN
  • WordPress WAF
  • WordPress SSL
  • WordPress GeoDNS
  • WordPress Load Balancer
  • WordPress Redis

WP Infractucture

  • WordPress CDN
  • WordPress WAF
  • WordPress SSL
  • WordPress GeoDNS
  • WordPress Load Balancer
  • WordPress Redis

WP Themes

  • Most Rated WordPress Themes
  • Most Reviewed WordPress Themes
  • Most Downloaded WordPress Themes
  • Most Active Installed WordPress Themes
  • Recently Updated WordPress Themes
  • Latest WordPress Themes

WP Plugins

  • Most Rated WordPress Plugins
  • Most Reviewed WordPress Plugins
  • Most Downloaded WordPress Plugins
  • Most Active Installed WordPress Plugins
  • Recently Updated WordPress Plugins
  • Latest WordPress Plugins

WP Referrence

  • Functions
  • Classes
  • Methods
wpsocket logo

© WpSocket. 2023 All rights reserved.