From 2d0ac0756f49c47432e7e473c18aa16bce1fe4e6 Mon Sep 17 00:00:00 2001 From: gohelkunjan Date: Fri, 27 Nov 2026 00:08:10 +0530 Subject: [PATCH 1/2] Customize: Bound dropdown-pages queries to prevent memory exhaustion. Trac ticket: https://core.trac.wordpress.org/ticket/66775 --- .../class-wp-customize-control.php | 56 ++++++++++++--- tests/phpunit/tests/customize/control.php | 69 +++++++++++++++++++ 2 files changed, 117 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-customize-control.php b/src/wp-includes/class-wp-customize-control.php index 859958a2f6838..5793c7e279c81 100644 --- a/src/wp-includes/class-wp-customize-control.php +++ b/src/wp-includes/class-wp-customize-control.php @@ -597,19 +597,59 @@ protected function render_content() { $dropdown_name = '_customize-dropdown-pages-' . $this->id; $show_option_none = __( '— Select —' ); $option_none_value = '0'; - $dropdown = wp_dropdown_pages( - array( - 'name' => $dropdown_name, - 'echo' => 0, - 'show_option_none' => $show_option_none, - 'option_none_value' => $option_none_value, - 'selected' => $this->value(), - ) + $selected = $this->value(); + $dropdown_args = array( + 'name' => $dropdown_name, + 'echo' => 0, + 'show_option_none' => $show_option_none, + 'option_none_value' => $option_none_value, + 'selected' => $selected, ); + + /** + * Filters the maximum number of pages to load in Customizer dropdown-pages controls. + * + * Bounding the query prevents PHP memory exhaustion on sites with large numbers of pages. + * Return 0 or a negative integer to disable the limit. + * + * @since 7.3.0 + * + * @param int $limit Maximum number of pages to query. Default 500. + * @param WP_Customize_Control $control Current Customizer control instance. + */ + $max_pages = (int) apply_filters( 'customize_dropdown_pages_limit', 500, $this ); + $page_counts = wp_count_posts( 'page' ); + $is_limited = $max_pages > 0 && isset( $page_counts->publish ) && (int) $page_counts->publish > $max_pages; + + if ( $is_limited ) { + $dropdown_args['number'] = $max_pages; + $dropdown_args['hierarchical'] = 0; + } + + $dropdown = wp_dropdown_pages( $dropdown_args ); if ( empty( $dropdown ) ) { $dropdown = sprintf( ''; + } elseif ( $is_limited && (int) $selected > 0 && false === strpos( $dropdown, 'value="' . (int) $selected . '"' ) ) { + $selected_page = get_post( (int) $selected ); + if ( $selected_page instanceof WP_Post && 'page' === $selected_page->post_type ) { + $selected_title = $selected_page->post_title; + if ( '' === $selected_title ) { + /* translators: %d: ID of a post. */ + $selected_title = sprintf( __( '#%d (no title)' ), $selected_page->ID ); + } + + /** This filter is documented in wp-includes/class-walker-page-dropdown.php */ + $selected_title = apply_filters( 'list_pages', $selected_title, $selected_page ); + + $selected_option = sprintf( + "\t\n", + esc_attr( $selected_page->ID ), + esc_html( $selected_title ) + ); + $dropdown = str_replace( '', $selected_option . '', $dropdown ); + } } // Hackily add in the data link parameter. diff --git a/tests/phpunit/tests/customize/control.php b/tests/phpunit/tests/customize/control.php index ea3311e15c192..2e258e9012c70 100644 --- a/tests/phpunit/tests/customize/control.php +++ b/tests/phpunit/tests/customize/control.php @@ -181,6 +181,75 @@ public function test_dropdown_pages() { $this->assertStringNotContainsString( 'Orphan Auto Draft Page', $content ); } + /** + * Test that dropdown-pages controls bound the query and preserve the selected page. + * + * @ticket 66775 + * + * @covers WP_Customize_Control::render_content + */ + public function test_dropdown_pages_limit_preserves_selected_page() { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + foreach ( array( 'Page A', 'Page B', 'Page C', 'Page D' ) as $title ) { + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => $title, + ) + ); + } + + $selected_page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page Z', + ) + ); + + $this->wp_customize->add_setting( + 'page_on_front', + array( + 'type' => 'option', + 'capability' => 'manage_options', + 'default' => $selected_page_id, + ) + ); + + $control = new WP_Customize_Control( + $this->wp_customize, + 'page_on_front', + array( + 'label' => 'Homepage', + 'section' => 'static_front_page', + 'type' => 'dropdown-pages', + ) + ); + $this->wp_customize->add_control( $control ); + + $limit_callback = static function () { + return 2; + }; + add_filter( 'customize_dropdown_pages_limit', $limit_callback ); + + ob_start(); + $control->maybe_render(); + $content = ob_get_clean(); + + remove_filter( 'customize_dropdown_pages_limit', $limit_callback ); + + $this->assertStringContainsString( 'Page A', $content ); + $this->assertStringContainsString( 'Page B', $content ); + $this->assertStringNotContainsString( 'Page C', $content ); + $this->assertStringNotContainsString( 'Page D', $content ); + $this->assertStringContainsString( + sprintf( '', $selected_page_id ), + $content + ); + } + /** * Tear down. */ From 4cb454e3537580e1085062f36016171838542d63 Mon Sep 17 00:00:00 2001 From: gohelkunjan Date: Fri, 27 Nov 2026 00:25:33 +0530 Subject: [PATCH 2/2] Tests: Register Customize controls in dropdown-pages limit test. --- tests/phpunit/tests/customize/control.php | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/tests/phpunit/tests/customize/control.php b/tests/phpunit/tests/customize/control.php index 2e258e9012c70..65fc6c3ac76eb 100644 --- a/tests/phpunit/tests/customize/control.php +++ b/tests/phpunit/tests/customize/control.php @@ -189,8 +189,6 @@ public function test_dropdown_pages() { * @covers WP_Customize_Control::render_content */ public function test_dropdown_pages_limit_preserves_selected_page() { - wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); - foreach ( array( 'Page A', 'Page B', 'Page C', 'Page D' ) as $title ) { self::factory()->post->create( array( @@ -209,25 +207,10 @@ public function test_dropdown_pages_limit_preserves_selected_page() { ) ); - $this->wp_customize->add_setting( - 'page_on_front', - array( - 'type' => 'option', - 'capability' => 'manage_options', - 'default' => $selected_page_id, - ) - ); + update_option( 'page_on_front', $selected_page_id ); - $control = new WP_Customize_Control( - $this->wp_customize, - 'page_on_front', - array( - 'label' => 'Homepage', - 'section' => 'static_front_page', - 'type' => 'dropdown-pages', - ) - ); - $this->wp_customize->add_control( $control ); + do_action( 'customize_register', $this->wp_customize ); + $control = $this->wp_customize->get_control( 'page_on_front' ); $limit_callback = static function () { return 2;