From 6d9701a6f0b44e6dbe8d1bf9a0dcf5e15a98e9d5 Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Mon, 10 Oct 2022 15:15:31 +0000 Subject: [PATCH] Editor: Dynamic site editor template names performance improvements. This change updates `get_(posts|terms)()` to direct `WP_Query` calls and adds additional parameters to each query to improve performance. Follow-up to [54280], [54333], [54370], and [54388]. Props spacedmonkey, peterwilsoncc. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54445 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 63 +++++++++++++++--------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 543cdaf629..59d76d48d3 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -546,13 +546,22 @@ function _build_block_template_result_from_file( $template_file, $template_type function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) { $post_type_object = get_post_type_object( $post_type ); - $posts = get_posts( - array( - 'name' => $slug, - 'post_type' => $post_type, - ) + $default_args = array( + 'post_type' => $post_type, + 'post_status' => 'publish', + 'posts_per_page' => 1, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'ignore_sticky_posts' => true, + 'no_found_rows' => true, ); + $args = array( + 'name' => $slug, + ); + $args = wp_parse_args( $args, $default_args ); + $posts = new WP_Query( $args ); + if ( empty( $posts ) ) { $template->title = sprintf( /* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */ @@ -579,13 +588,12 @@ function _wp_build_title_and_description_for_single_post_type_block_template( $p $post_title ); - $posts_with_same_title = get_posts( - array( - 'title' => $post_title, - 'post_type' => $post_type, - 'post_status' => 'publish', - ) + $args = array( + 'title' => $post_title, ); + $args = wp_parse_args( $args, $default_args ); + + $posts_with_same_title = new WP_Query( $args ); if ( count( $posts_with_same_title ) > 1 ) { $template->title = sprintf( @@ -615,14 +623,21 @@ function _wp_build_title_and_description_for_single_post_type_block_template( $p function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) { $taxonomy_object = get_taxonomy( $taxonomy ); - $terms = get_terms( - array( - 'taxonomy' => $taxonomy, - 'hide_empty' => false, - 'slug' => $slug, - ) + $default_args = array( + 'taxonomy' => $taxonomy, + 'hide_empty' => false, + 'update_term_meta_cache' => false, ); + $term_query = new WP_Term_Query(); + + $args = array( + 'number' => 1, + 'slug' => $slug, + ); + $args = wp_parse_args( $args, $default_args ); + $terms = $term_query->query( $args ); + if ( empty( $terms ) ) { $template->title = sprintf( /* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */ @@ -648,13 +663,15 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $term_title ); - $terms_with_same_title = get_terms( - array( - 'taxonomy' => $taxonomy, - 'hide_empty' => false, - 'name' => $term_title, - ) + $term_query = new WP_Term_Query(); + + $args = array( + 'number' => 2, + 'name' => $term_title, ); + $args = wp_parse_args( $args, $default_args ); + + $terms_with_same_title = $term_query->query( $args ); if ( count( $terms_with_same_title ) > 1 ) { $template->title = sprintf(