From 0949e1e7086a86b444f4423927d6b01543c713e4 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Thu, 2 Jan 2025 12:46:11 +0530 Subject: [PATCH 01/10] New function to retrieve page IDs/Permalinks/Titles based on template file name Queries pages using a specified template file and returns an array of IDs, permalinks, or titles based on the provided field parameter. --- src/wp-includes/post.php | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3c3a422430324..69e9cea080c23 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8470,3 +8470,45 @@ function wp_create_initial_post_meta() { ) ); } + +/** + * Retrieves the ID or permalink of a page based on its template file name. + * + * This function performs a query to find pages using a specific template + * and returns the ID or permalink of the first matching page. + * + * @param string $template The name of the template file to search for. + * @param string $field The field to return: 'ID' for the page ID, 'permalink' for the page permalink. + * @return string|int The ID or permalink of the page, or null if no page is found. + */ + +function get_page_by_template( $template, $field = 'ID' ) { + global $post; + + $query = new WP_Query( + array( + 'post_type' => 'page', + 'meta_query' => array( + array( + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==', + ), + ), + 'fields' => 'ids', + ) + ); + + if ( $query->have_posts() ) { + $query->the_post(); + if ( 'ID' === $field ) { + return $query->posts; + } elseif ( 'title' === $field ) { + return array_combine($query->posts, array_map('get_the_title', $query->posts) ); + } elseif ( 'permalink' === $field ) { + return array_combine($query->posts, array_map('get_permalink', $query->posts) ); + } + } + + return null; +} From 85f837571ca87232c067a34453e9c6312fd418fb Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Thu, 2 Jan 2025 12:54:05 +0530 Subject: [PATCH 02/10] updated code as per coding standard --- src/wp-includes/post.php | 72 +++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 69e9cea080c23..8ad2b2948ce11 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8472,43 +8472,47 @@ function wp_create_initial_post_meta() { } /** - * Retrieves the ID or permalink of a page based on its template file name. + * Retrieves page IDs, permalinks, or titles based on a template file name. * - * This function performs a query to find pages using a specific template - * and returns the ID or permalink of the first matching page. + * Queries pages using a specified template file and returns an array of + * IDs, permalinks, or titles based on the provided field parameter. * - * @param string $template The name of the template file to search for. - * @param string $field The field to return: 'ID' for the page ID, 'permalink' for the page permalink. - * @return string|int The ID or permalink of the page, or null if no page is found. + * @param string $template The template file name to search for. + * @param string $field The field to return: 'ID' for page IDs, 'permalink' for page permalinks, or 'title' for page titles. + * @return array|null An array of IDs, permalinks, or titles of matching pages, or null if no pages are found. */ function get_page_by_template( $template, $field = 'ID' ) { - global $post; - - $query = new WP_Query( - array( - 'post_type' => 'page', - 'meta_query' => array( - array( - 'key' => '_wp_page_template', - 'value' => $template, - 'compare' => '==', - ), - ), - 'fields' => 'ids', - ) - ); - - if ( $query->have_posts() ) { - $query->the_post(); - if ( 'ID' === $field ) { - return $query->posts; - } elseif ( 'title' === $field ) { - return array_combine($query->posts, array_map('get_the_title', $query->posts) ); - } elseif ( 'permalink' === $field ) { - return array_combine($query->posts, array_map('get_permalink', $query->posts) ); - } - } - - return null; + // Query pages by template + $query = new WP_Query( + array( + 'post_type' => 'page', + 'meta_query' => array( + array( + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==', + ), + ), + 'fields' => 'ids', + ) + ); + + if ( $query->have_posts() ) { + $template_page_ids = $query->posts; + + if ( 'ID' === $field ) { + wp_reset_postdata(); + return $template_page_ids; + } elseif ( 'title' === $field ) { + wp_reset_postdata(); + return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); + } elseif ( 'permalink' === $field ) { + wp_reset_postdata(); + return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); + } + } + + wp_reset_postdata(); + return null; } From 787498764dcc17d6f8a9d4c7c6807732b9be1a16 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Thu, 2 Jan 2025 12:57:07 +0530 Subject: [PATCH 03/10] Updated as per the PHPCS --- src/wp-includes/post.php | 63 ++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 8ad2b2948ce11..5bfb28ff7cb1f 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8483,36 +8483,35 @@ function wp_create_initial_post_meta() { */ function get_page_by_template( $template, $field = 'ID' ) { - // Query pages by template - $query = new WP_Query( - array( - 'post_type' => 'page', - 'meta_query' => array( - array( - 'key' => '_wp_page_template', - 'value' => $template, - 'compare' => '==', - ), - ), - 'fields' => 'ids', - ) - ); - - if ( $query->have_posts() ) { - $template_page_ids = $query->posts; - - if ( 'ID' === $field ) { - wp_reset_postdata(); - return $template_page_ids; - } elseif ( 'title' === $field ) { - wp_reset_postdata(); - return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); - } elseif ( 'permalink' === $field ) { - wp_reset_postdata(); - return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); - } - } - - wp_reset_postdata(); - return null; + $query = new WP_Query( + array( + 'post_type' => 'page', + 'meta_query' => array( + array( + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==', + ), + ), + 'fields' => 'ids', + ) + ); + + if ( $query->have_posts() ) { + $template_page_ids = $query->posts; + + if ( 'ID' === $field ) { + wp_reset_postdata(); + return $template_page_ids; + } elseif ( 'title' === $field ) { + wp_reset_postdata(); + return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); + } elseif ( 'permalink' === $field ) { + wp_reset_postdata(); + return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); + } + } + + wp_reset_postdata(); + return null; } From 3473943432e2b3ab090cd539ccca34a1105b9af9 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Thu, 2 Jan 2025 15:17:09 +0530 Subject: [PATCH 04/10] updated code as suggested --- src/wp-includes/post.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 5bfb28ff7cb1f..ec742b571a687 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8474,7 +8474,7 @@ function wp_create_initial_post_meta() { /** * Retrieves page IDs, permalinks, or titles based on a template file name. * - * Queries pages using a specified template file and returns an array of + * Queries pages using a specified template file and returns an array of * IDs, permalinks, or titles based on the provided field parameter. * * @param string $template The template file name to search for. @@ -8499,7 +8499,6 @@ function get_page_by_template( $template, $field = 'ID' ) { if ( $query->have_posts() ) { $template_page_ids = $query->posts; - if ( 'ID' === $field ) { wp_reset_postdata(); return $template_page_ids; From fd899087d562c96251baaa5547d46673c8020715 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 10:45:42 +0530 Subject: [PATCH 05/10] Added condition to check capabilities Added condition to prevent the fatal error on missing capabilities in array. --- src/wp-includes/class-wp-user-query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index fd35182eab9f5..3d2134452bf85 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -482,7 +482,7 @@ public function prepare_query( $query = array() ) { $caps_with_roles = array(); foreach ( $available_roles as $role => $role_data ) { - $role_caps = array_keys( array_filter( $role_data['capabilities'] ) ); + $role_caps = (isset($role_data['capabilities'])) ? array_keys( array_filter( $role_data['capabilities'] ) ) : array(); foreach ( $capabilities as $cap ) { if ( in_array( $cap, $role_caps, true ) ) { From 546515e9960951f909050efb88d22e636be9fd68 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 16:42:48 +0530 Subject: [PATCH 06/10] Added space as expected after parenthesis Expected 1 space after open parenthesis as per the PHPCS standard. --- src/wp-includes/post.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 648387f01d83d..485a0fdb25efc 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8497,16 +8497,14 @@ function get_page_by_template( $template, $field = 'ID' ) { ) ); - if ( $query->have_posts() ) { + if ( $query->have_posts() ) { $template_page_ids = $query->posts; - if ( 'ID' === $field ) { - wp_reset_postdata(); + wp_reset_postdata(); + if ( 'ID' === $field ) { return $template_page_ids; - } elseif ( 'title' === $field ) { - wp_reset_postdata(); + } elseif ( 'title' === $field ) { return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); - } elseif ( 'permalink' === $field ) { - wp_reset_postdata(); + } elseif ( 'permalink' === $field ) { return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); } } From e078dcb437acfea34c256563d3ed238b312f058a Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 16:52:14 +0530 Subject: [PATCH 07/10] Added space as expected after parenthesis Expected 1 space after open parenthesis as per the PHPCS standard. --- src/wp-includes/post.php | 58 +++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 485a0fdb25efc..b34d5a7168cdb 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8483,32 +8483,34 @@ function wp_create_initial_post_meta() { */ function get_page_by_template( $template, $field = 'ID' ) { - $query = new WP_Query( - array( - 'post_type' => 'page', - 'meta_query' => array( - array( - 'key' => '_wp_page_template', - 'value' => $template, - 'compare' => '==', - ), - ), - 'fields' => 'ids', - ) - ); - - if ( $query->have_posts() ) { - $template_page_ids = $query->posts; - wp_reset_postdata(); - if ( 'ID' === $field ) { - return $template_page_ids; - } elseif ( 'title' === $field ) { - return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); - } elseif ( 'permalink' === $field ) { - return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); - } - } - - wp_reset_postdata(); - return null; + $query = new WP_Query( + array( + 'post_type' => 'page', + 'meta_query' => array( + array( + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==', + ), + ), + 'fields' => 'ids', + ) + ); + + if ( $query->have_posts() ) { + $template_page_ids = $query->posts; + wp_reset_postdata(); + + if ( 'ID' === $field ) { + return $template_page_ids; + } elseif ( 'title' === $field ) { + return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); + } elseif ( 'permalink' === $field ) { + return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); + } + } + + wp_reset_postdata(); + return null; } + From ca1dd5db2a00c5c9750d3ba633072e2a4ad14e3e Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 16:55:35 +0530 Subject: [PATCH 08/10] Added space as expected after parenthesis Expected 1 space after open parenthesis as per the PHPCS standard. --- src/wp-includes/post.php | 59 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index b34d5a7168cdb..de4c2268c4442 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8483,34 +8483,33 @@ function wp_create_initial_post_meta() { */ function get_page_by_template( $template, $field = 'ID' ) { - $query = new WP_Query( - array( - 'post_type' => 'page', - 'meta_query' => array( - array( - 'key' => '_wp_page_template', - 'value' => $template, - 'compare' => '==', - ), - ), - 'fields' => 'ids', - ) - ); - - if ( $query->have_posts() ) { - $template_page_ids = $query->posts; - wp_reset_postdata(); - - if ( 'ID' === $field ) { - return $template_page_ids; - } elseif ( 'title' === $field ) { - return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); - } elseif ( 'permalink' === $field ) { - return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); - } - } - - wp_reset_postdata(); - return null; -} + $query = new WP_Query( + array( + 'post_type' => 'page', + 'meta_query' => array( + array( + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==', + ), + ), + 'fields' => 'ids', + ) + ); + if ( $query->have_posts() ) { + $template_page_ids = $query->posts; + wp_reset_postdata(); + + if ( 'ID' === $field ) { + return $template_page_ids; + } elseif ( 'title' === $field ) { + return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); + } elseif ( 'permalink' === $field ) { + return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); + } + } + + wp_reset_postdata(); + return null; +} From 43d377234ab9741251d9d1864dfeaa9d4c654e89 Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 16:58:34 +0530 Subject: [PATCH 09/10] Fixing coding standard error Fixing coding standard error as per the PHPCS --- src/wp-includes/post.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index de4c2268c4442..b6959f12a3abc 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8500,12 +8500,11 @@ function get_page_by_template( $template, $field = 'ID' ) { if ( $query->have_posts() ) { $template_page_ids = $query->posts; wp_reset_postdata(); - - if ( 'ID' === $field ) { + if ( 'ID' === $field ) { return $template_page_ids; - } elseif ( 'title' === $field ) { + } elseif ( 'title' === $field ) { return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); - } elseif ( 'permalink' === $field ) { + } elseif ( 'permalink' === $field ) { return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); } } From 216591232aca1438a49a1566afb07ec0398a3d2e Mon Sep 17 00:00:00 2001 From: Aakif Kadiwala Date: Fri, 3 Jan 2025 17:01:07 +0530 Subject: [PATCH 10/10] Fixing space issue removed Whitespace --- src/wp-includes/post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index b6959f12a3abc..1b1081e317ac5 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8497,7 +8497,7 @@ function get_page_by_template( $template, $field = 'ID' ) { ) ); - if ( $query->have_posts() ) { + if ( $query->have_posts() ) { $template_page_ids = $query->posts; wp_reset_postdata(); if ( 'ID' === $field ) {