Automattic\WooCommerce\Blocks\Utils
BlockTemplateUtils::get_block_templates_from_db
Gets the templates saved in the database.
Метод класса: BlockTemplateUtils{}
Хуков нет.
Возвращает
\WP_Block_Template[]. An array of found templates.
Использование
$result = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type );
- $slugs(массив)
- An array of slugs to retrieve templates for.
По умолчанию:array() - $template_type(строка)
- wp_template or wp_template_part.
По умолчанию:'wp_template'
Код BlockTemplateUtils::get_block_templates_from_db() BlockTemplateUtils::get block templates from db WC 11.0.1
public static function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
static $request_level_cache = array();
// Optimization note: first query, which optimized for fetching IDs, to minimize temporary/filesort overhead.
$theme = get_stylesheet();
$ids = wp_cache_get( $template_type . '-ids', 'woocommerce_blocks' );
if ( ! isset( $ids[ $theme ] ) ) {
$ids = false === $ids ? array() : $ids;
// 'post__not_in' directs the query to use the `type_status_date` index on the posts table. Omitting this may
// impact index usage on some systems and result in `Using join buffer (flat, BNL join)`.
// As the table grows, the number of template type entries stays small, which helps maintain strong query performance.
$ids[ $theme ] = ( new \WP_Query(
array(
'post_type' => $template_type,
'post__not_in' => array( 0 ),
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => array( self::DEPRECATED_PLUGIN_SLUG, self::PLUGIN_SLUG, $theme ),
),
),
)
) )->posts;
// 12 hours is half of default nonce lifetime, if out of sync, templates operating with nonce will keep running.
wp_cache_set( $template_type . '-ids', $ids, 'woocommerce_blocks', 12 * HOUR_IN_SECONDS );
$request_level_cache[ $template_type ][ $theme ] = null;
}
// Optimization note: second query, which optimized for fetching templates data with caches priming API.
if ( null === ( $request_level_cache[ $template_type ][ $theme ] ?? null ) ) {
$request_level_cache[ $template_type ][ $theme ] = array();
if ( ! empty( $ids[ $theme ] ) ) {
// Prime caches to reduce future queries.
_prime_post_caches( $ids[ $theme ], false, false );
$request_level_cache[ $template_type ][ $theme ] = array_filter( array_map( 'get_post', $ids[ $theme ] ) );
}
}
// Optimization note: populate template objects; optimized for subsequent calls, without spawning consequent SQLs.
$saved_templates = $request_level_cache[ $template_type ][ $theme ];
if ( ! empty( $saved_templates ) && is_array( $slugs ) && array() !== $slugs ) {
$slugs = array_map( 'sanitize_title', $slugs );
$saved_templates = array_filter( $saved_templates, fn( $template ) => in_array( $template->post_name, $slugs, true ) );
}
if ( ! empty( $saved_templates ) ) {
$block_templates = array_map( fn( $template ) => self::build_template_result_from_post( $template ), $saved_templates );
$block_templates = array_values( array_filter( $block_templates, fn( $template ) => $template instanceof \WP_Block_Template ) );
return $block_templates;
}
return array();
}