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..65fc6c3ac76eb 100644
--- a/tests/phpunit/tests/customize/control.php
+++ b/tests/phpunit/tests/customize/control.php
@@ -181,6 +181,58 @@ 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() {
+ 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',
+ )
+ );
+
+ update_option( 'page_on_front', $selected_page_id );
+
+ do_action( 'customize_register', $this->wp_customize );
+ $control = $this->wp_customize->get_control( 'page_on_front' );
+
+ $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.
*/