Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wp-admin/includes/class-wp-posts-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,7 @@ public function inline_edit() {
'show_option_none' => __( 'Main Page (no parent)' ),
'option_none_value' => 0,
'sort_column' => 'menu_order, post_title',
'fields' => 'dropdown',
);

if ( $bulk ) {
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/includes/meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ function page_attributes_meta_box( $post ) {
'show_option_none' => __( '(no parent)' ),
'sort_column' => 'menu_order, post_title',
'echo' => 0,
'fields' => 'dropdown',
);

/**
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/options-privacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ static function ( $body_class ) {
'option_none_value' => '0',
'selected' => $privacy_policy_page_id,
'post_status' => array( 'draft', 'publish' ),
'fields' => 'dropdown',
)
);

Expand Down
17 changes: 16 additions & 1 deletion src/wp-admin/options-reading.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,22 @@
if ( ! is_utf8_charset() ) {
add_settings_field( 'blog_charset', __( 'Encoding for pages and feeds' ), 'options_reading_blog_charset', 'reading', 'default', array( 'label_for' => 'blog_charset' ) );
}

/*
* Check for one published top-level page instead of loading every page.
* The hierarchical get_pages() result is only non-empty when such a page
* exists, and the homepage dropdowns below offer the same pages.
*/
$has_pages = (bool) get_pages(
array(
'number' => 1,
'parent' => 0,
'hierarchical' => false,
)
);
?>

<?php if ( ! get_pages() ) : ?>
<?php if ( ! $has_pages ) : ?>
<input name="show_on_front" type="hidden" value="posts" />
<table class="form-table" role="presentation">
<?php
Expand Down Expand Up @@ -120,6 +133,7 @@
'show_option_none' => __( '&mdash; Select &mdash;' ),
'option_none_value' => '0',
'selected' => get_option( 'page_on_front' ),
'fields' => 'dropdown',
)
)
);
Expand All @@ -137,6 +151,7 @@
'show_option_none' => __( '&mdash; Select &mdash;' ),
'option_none_value' => '0',
'selected' => get_option( 'page_for_posts' ),
'fields' => 'dropdown',
)
)
);
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/class-wp-customize-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ protected function render_content() {
'show_option_none' => $show_option_none,
'option_none_value' => $option_none_value,
'selected' => $this->value(),
'fields' => 'dropdown',
)
);
if ( empty( $dropdown ) ) {
Expand Down
70 changes: 70 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6755,6 +6755,7 @@ function get_page_uri( $page = 0 ) {
*
* @since 1.5.0
* @since 6.3.0 Use WP_Query internally.
* @since 7.3.0 Added the `$fields` argument.
*
* @param array|string $args {
* Optional. Array or string of arguments to retrieve pages.
Expand Down Expand Up @@ -6788,6 +6789,13 @@ function get_page_uri( $page = 0 ) {
* @type string $post_type The post type to query. Default 'page'.
* @type string|array $post_status A comma-separated list or array of post statuses to include.
* Default 'publish'.
* @type string $fields Which fields to load. Accepts 'all' for complete page objects, or
* 'dropdown' for page objects with only ID, post_author, post_date,
* post_date_gmt, post_title, post_status, post_name, post_parent,
* menu_order, and post_type. With 'dropdown', other fields such as
* post_content are empty, including in callbacks on the 'get_pages'
* filter, and the pages are not added to the post cache.
* Default 'all'.
* }
* @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the
* specified post type is not hierarchical or the specified status is not
Expand All @@ -6814,6 +6822,9 @@ function get_pages( $args = array() ) {

$parsed_args = wp_parse_args( $args, $defaults );

// Not in $defaults, so that the arguments passed to the filters below are unchanged for existing callers.
$dropdown_fields = isset( $parsed_args['fields'] ) && 'dropdown' === $parsed_args['fields'];

$number = (int) $parsed_args['number'];
$offset = (int) $parsed_args['offset'];
$child_of = (int) $parsed_args['child_of'];
Expand Down Expand Up @@ -6926,6 +6937,10 @@ static function ( $orderby_field ) {
$query_args['posts_per_page'] = $number;
}

if ( $dropdown_fields ) {
$query_args['fields'] = 'ids';
}

/**
* Filters query arguments passed to WP_Query in get_pages().
*
Expand All @@ -6939,6 +6954,11 @@ static function ( $orderby_field ) {
$pages = new WP_Query();
$pages = $pages->query( $query_args );

// A 'get_pages_query_args' callback may have removed 'fields', in which case the pages are complete.
if ( $dropdown_fields && isset( $query_args['fields'] ) && 'ids' === $query_args['fields'] ) {
$pages = _get_pages_dropdown_fields( $pages );
}

if ( $child_of || $hierarchical ) {
$pages = get_page_children( $child_of, $pages );
}
Expand Down Expand Up @@ -6971,6 +6991,56 @@ static function ( $orderby_field ) {
return apply_filters( 'get_pages', $pages, $parsed_args );
}

/**
* Loads pages with only the fields a page dropdown needs.
*
* Used by get_pages() when `$fields` is 'dropdown'. The rows are cached on their own and never
* added to the post cache, so a later get_post() call still loads the complete page.
*
* @since 7.3.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int[] $page_ids Page IDs, in the order to return them.
* @return WP_Post[] Pages with only the dropdown fields set, in the order of `$page_ids`.
*/
function _get_pages_dropdown_fields( $page_ids ) {
global $wpdb;

$page_ids = array_map( 'intval', $page_ids );
if ( empty( $page_ids ) ) {
return array();
}

$last_changed = wp_cache_get_last_changed( 'posts' );
$cache_key = 'get_pages_dropdown_fields:' . md5( implode( ',', $page_ids ) );
$rows = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );

if ( false === $rows ) {
$rows = array();
foreach ( array_chunk( $page_ids, 1000 ) as $chunk ) {
$ids = implode( ',', $chunk );
$rows += $wpdb->get_results(
"SELECT ID, post_author, post_date, post_date_gmt, post_title, post_status, post_name, post_parent, menu_order, post_type
FROM $wpdb->posts WHERE ID IN ($ids)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $ids holds integers only.
OBJECT_K
);
}
wp_cache_set_salted( $cache_key, $rows, 'post-queries', $last_changed );
}

$pages = array();
foreach ( $page_ids as $page_id ) {
if ( isset( $rows[ $page_id ] ) ) {
// Clone so that get_post() sanitizing the row doesn't change the cached copy.
$pages[] = get_post( clone $rows[ $page_id ] );
}
}

return $pages;
}

//
// Attachment functions.
//
Expand Down
135 changes: 135 additions & 0 deletions tests/phpunit/tests/post/getPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -1204,4 +1204,139 @@ public function test_get_pages_order_by_post_modified_gmt() {
'Check that ORDER is post modified when using modified_gmt.'
);
}

/**
* @ticket 41714
*
* @dataProvider data_dropdown_fields_args
*
* @param array $args Arguments for get_pages(), with `true` standing for the parent page ID.
*/
public function test_dropdown_fields_return_the_same_pages_in_the_same_order( $args ) {
$parent_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Parent',
'menu_order' => 2,
)
);
$child_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Child',
'post_parent' => $parent_id,
)
);
self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Grandchild',
'post_parent' => $child_id,
)
);
self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Another',
'menu_order' => 1,
)
);
foreach ( array( 'child_of', 'exclude_tree' ) as $key ) {
if ( isset( $args[ $key ] ) ) {
$args[ $key ] = 'child_of' === $key ? $parent_id : $child_id;
}
}

$full = get_pages( $args );
$light = get_pages( array_merge( $args, array( 'fields' => 'dropdown' ) ) );

$this->assertNotEmpty( $full );
$this->assertSame( wp_list_pluck( $full, 'ID' ), wp_list_pluck( $light, 'ID' ) );
$this->assertSame( wp_list_pluck( $full, 'post_title' ), wp_list_pluck( $light, 'post_title' ) );
$this->assertSame( wp_list_pluck( $full, 'post_parent' ), wp_list_pluck( $light, 'post_parent' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_dropdown_fields_args() {
return array(
'defaults' => array( array() ),
'flat' => array( array( 'hierarchical' => false ) ),
'menu order' => array( array( 'sort_column' => 'menu_order, post_title' ) ),
'descending' => array( array( 'sort_order' => 'DESC' ) ),
'child of' => array( array( 'child_of' => true ) ),
'exclude tree' => array( array( 'exclude_tree' => true ) ),
'number' => array(
array(
'number' => 2,
'hierarchical' => false,
),
),
);
}

/**
* @ticket 41714
*/
public function test_dropdown_fields_leave_out_content_and_the_post_cache() {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'A page',
'post_content' => 'Long content',
)
);
clean_post_cache( $page_id );

$pages = get_pages( array( 'fields' => 'dropdown' ) );

$this->assertSame( array( $page_id ), wp_list_pluck( $pages, 'ID' ) );
$this->assertSame( 'A page', $pages[0]->post_title );
$this->assertSame( '', $pages[0]->post_content );
$this->assertFalse( wp_cache_get( $page_id, 'posts' ), 'A partial page must not be cached as the full page.' );
$this->assertSame( 'Long content', get_post( $page_id )->post_content );
}

/**
* @ticket 41714
*/
public function test_dropdown_fields_are_cached_until_pages_change() {
self::factory()->post->create( array( 'post_type' => 'page' ) );

$first = get_pages( array( 'fields' => 'dropdown' ) );
$num_queries = get_num_queries();
$second = get_pages( array( 'fields' => 'dropdown' ) );

$this->assertSame( $num_queries, get_num_queries(), 'The second call should be served from the cache.' );
$this->assertSame( wp_list_pluck( $first, 'post_title' ), wp_list_pluck( $second, 'post_title' ) );

$new_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$this->assertContains( $new_id, wp_list_pluck( get_pages( array( 'fields' => 'dropdown' ) ), 'ID' ) );
}

/**
* @ticket 41714
*/
public function test_dropdown_fields_return_full_pages_when_a_filter_removes_the_ids_query() {
self::factory()->post->create(
array(
'post_type' => 'page',
'post_content' => 'Long content',
)
);
add_filter(
'get_pages_query_args',
static function ( $query_args ) {
unset( $query_args['fields'] );
return $query_args;
}
);

$pages = get_pages( array( 'fields' => 'dropdown' ) );

$this->assertSame( 'Long content', $pages[0]->post_content );
}
}
48 changes: 48 additions & 0 deletions tests/phpunit/tests/post/wpDropdownPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,52 @@ public function test_wp_dropdown_pages_should_obey_class_parameter() {

$this->assertMatchesRegularExpression( '/<select[^>]+class=\'bar\'/', $found );
}

/**
* @ticket 41714
*/
public function test_wp_dropdown_pages_with_dropdown_fields_renders_the_same_options_without_loading_content() {
global $wpdb;

$parent_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Parent',
'post_content' => 'Long content',
)
);
$child_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => 'Child',
'post_parent' => $parent_id,
)
);
$bump = '&nbsp;&nbsp;&nbsp;';
$expected = "<select name='page_id' id='page_id'>\n";
$expected .= "\t<option value=\"0\">None</option>\n";
$expected .= "\t<option class=\"level-0\" value=\"$parent_id\">Parent</option>\n";
$expected .= "\t<option class=\"level-1\" value=\"$child_id\" selected=\"selected\">{$bump}Child</option>\n";
$expected .= "</select>\n";

$queries = array();
$collect = static function ( $query ) use ( &$queries ) {
$queries[] = $query;
return $query;
};
add_filter( 'query', $collect );
$found = wp_dropdown_pages(
array(
'echo' => 0,
'selected' => $child_id,
'show_option_none' => 'None',
'option_none_value' => '0',
'fields' => 'dropdown',
)
);
remove_filter( 'query', $collect );

$this->assertSame( $expected, $found );
$this->assertStringNotContainsString( "{$wpdb->posts}.*", implode( "\n", $queries ) );
}
}
Loading