Make WordPress Core

Changeset 55845


Ignore:
Timestamp:
05/22/2023 10:01:07 AM (3 years ago)
Author:
spacedmonkey
Message:

Posts, Post Types: Add a new filter for query arguments in get_pages.

In [55569] get_pages was converted to use WP_Query internally. But for plugins that were extending the get_pages filters and filter WP_Query query arguments, this could result in a conflict. Add a filter get_pages_query_args to allow developers to change arguments passed to WP_Query but also have the context of the original arguments passed to the get_pages function.

This change also expands test coverage of get_pages to ensure no breakages in the future.

Props spacedmonkey, westonruter, costdev, flixos90, kenwins, marianne38.
See #12821.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r55743 r55845  
    60326032                                        $post_author = $post_author->ID;
    60336033                                }
    6034                                 $query_args['author__in'][] = $post_author;
     6034                                $query_args['author__in'][] = (int) $post_author;
    60356035                        }
    60366036                }
     
    60606060                $query_args['posts_per_page'] = $number;
    60616061        }
     6062
     6063        /**
     6064         * Filters query arguments passed to WP_Query in get_pages.
     6065         *
     6066         * @since 6.3.0
     6067         *
     6068         * @param array $query_args  Array of arguments passed to WP_Query.
     6069         * @param array $parsed_args Array of get_pages() arguments.
     6070         */
     6071        $query_args = apply_filters( 'get_pages_query_args', $query_args, $parsed_args );
    60626072
    60636073        $query = new WP_Query( $query_args );
  • trunk/tests/phpunit/tests/post/getPages.php

    r55745 r55845  
    324324                sort( $exc_result );
    325325                $this->assertSame( $inc, $exc_result );
     326        }
     327
     328        /**
     329         * @ticket 12821
     330         * @covers ::get_pages
     331         */
     332        public function test_get_pages_test_filter() {
     333                register_post_type( 'wptests_pt', array( 'hierarchical' => true ) );
     334
     335                $posts              = self::factory()->post->create_many(
     336                        2,
     337                        array(
     338                                'post_type' => 'wptests_pt',
     339                        )
     340                );
     341                $query_args_values  = array();
     342                $parsed_args_values = array();
     343
     344                // Filter the query to return the wptests_pt post type.
     345                add_filter(
     346                        'get_pages_query_args',
     347                        static function( $query_args, $parsed_args ) use ( &$query_args_values, &$parsed_args_values ) {
     348                                $query_args['post_type'] = 'wptests_pt';
     349                                $query_args_values       = $query_args;
     350                                $parsed_args_values      = $parsed_args;
     351                                return $query_args;
     352                        },
     353                        10,
     354                        2
     355                );
     356
     357                $pages    = get_pages();
     358                $page_ids = wp_list_pluck( $pages, 'ID' );
     359                $this->assertSameSets( $posts, $page_ids, 'The return post ids should match the post type wptests_pt.' );
     360
     361                $query_args = array(
     362                        'orderby'                => array( 'post_title' => 'ASC' ),
     363                        'order'                  => 'ASC',
     364                        'post__not_in'           => array(),
     365                        'meta_key'               => '',
     366                        'meta_value'             => '',
     367                        'posts_per_page'         => -1,
     368                        'offset'                 => 0,
     369                        'post_type'              => 'wptests_pt',
     370                        'post_status'            => array( 'publish' ),
     371                        'update_post_term_cache' => false,
     372                        'update_post_meta_cache' => false,
     373                        'ignore_sticky_posts'    => true,
     374                        'no_found_rows'          => true,
     375                );
     376
     377                $this->assertSameSets( $query_args, $query_args_values, 'Query arguments should match expected values' );
     378
     379                $parsed_args = array(
     380                        'child_of'     => 0,
     381                        'sort_order'   => 'ASC',
     382                        'sort_column'  => 'post_title',
     383                        'hierarchical' => 1,
     384                        'exclude'      => array(),
     385                        'include'      => array(),
     386                        'meta_key'     => '',
     387                        'meta_value'   => '',
     388                        'authors'      => '',
     389                        'parent'       => -1,
     390                        'exclude_tree' => array(),
     391                        'number'       => '',
     392                        'offset'       => 0,
     393                        'post_type'    => 'page',
     394                        'post_status'  => 'publish',
     395                );
     396
     397                $this->assertSameSets( $parsed_args, $parsed_args_values, 'Parsed arguments should match expected values' );
     398        }
     399
     400        /**
     401         * @ticket 12821
     402         * @covers ::get_pages
     403         * @dataProvider data_get_pages_args
     404         */
     405        public function test_get_pages_args_test_filter( $args, $expected_query_args ) {
     406                $filter = new MockAction();
     407                add_filter( 'get_pages_query_args', array( $filter, 'filter' ), 10, 2 );
     408
     409                $results = get_pages( $args );
     410
     411                $this->assertIsArray( $results, 'get_pages should result an array' );
     412
     413                $filter_args = $filter->get_args();
     414
     415                $default_args = array(
     416                        'orderby'                => array( 'post_title' => 'ASC' ),
     417                        'order'                  => 'ASC',
     418                        'post__not_in'           => array(),
     419                        'meta_key'               => '',
     420                        'meta_value'             => '',
     421                        'posts_per_page'         => -1,
     422                        'offset'                 => 0,
     423                        'post_type'              => 'page',
     424                        'post_status'            => array( 'publish' ),
     425                        'update_post_term_cache' => false,
     426                        'update_post_meta_cache' => false,
     427                        'ignore_sticky_posts'    => true,
     428                        'no_found_rows'          => true,
     429                );
     430
     431                $query_args = wp_parse_args( $expected_query_args, $default_args );
     432
     433                $this->assertSameSets( $query_args, $filter_args[0][0], 'Unexpected $query_args for get_pages_query_args filter' );
     434
     435                $defaults = array(
     436                        'child_of'     => 0,
     437                        'sort_order'   => 'ASC',
     438                        'sort_column'  => 'post_title',
     439                        'hierarchical' => 1,
     440                        'exclude'      => array(),
     441                        'include'      => array(),
     442                        'meta_key'     => '',
     443                        'meta_value'   => '',
     444                        'authors'      => '',
     445                        'parent'       => -1,
     446                        'exclude_tree' => array(),
     447                        'number'       => '',
     448                        'offset'       => 0,
     449                        'post_type'    => 'page',
     450                        'post_status'  => 'publish',
     451                );
     452
     453                $parsed_args = wp_parse_args( $args, $defaults );
     454                $this->assertSameSets( $parsed_args, $filter_args[0][1], 'Unexpected $parsed_args for get_pages_query_args filter' );
     455        }
     456
     457        public function data_get_pages_args() {
     458                return array(
     459                        'default'            => array(
     460                                'args'                => array(),
     461                                'expected_query_args' => array(),
     462                        ),
     463                        'exclude'            => array(
     464                                'args'                => array( 'exclude' => array( 1, 2, 4 ) ),
     465                                'expected_query_args' => array( 'post__not_in' => array( 1, 2, 4 ) ),
     466                        ),
     467                        'post status'        => array(
     468                                'args'                => array( 'post_status' => 'draft' ),
     469                                'expected_query_args' => array( 'post_status' => array( 'draft' ) ),
     470                        ),
     471                        'number'             => array(
     472                                'args'                => array( 'number' => 99 ),
     473                                'expected_query_args' => array( 'posts_per_page' => 99 ),
     474                        ),
     475                        'meta query'         => array(
     476                                'args'                => array(
     477                                        'meta_key'   => 'foo',
     478                                        'meta_value' => 'bar',
     479                                ),
     480                                'expected_query_args' => array(
     481                                        'meta_key'   => 'foo',
     482                                        'meta_value' => 'bar',
     483                                ),
     484                        ),
     485                        'post parent number' => array(
     486                                'args'                => array( 'parent' => 5 ),
     487                                'expected_query_args' => array( 'post_parent' => 5 ),
     488                        ),
     489                        'post parent array'  => array(
     490                                'args'                => array( 'parent' => array( 5 ) ),
     491                                'expected_query_args' => array( 'post_parent__in' => array( 5 ) ),
     492                        ),
     493                        'offset'             => array(
     494                                'args'                => array( 'offset' => 2 ),
     495                                'expected_query_args' => array( 'offset' => 2 ),
     496                        ),
     497                        'authors'            => array(
     498                                'args'                => array( 'authors' => 2 ),
     499                                'expected_query_args' => array( 'author__in' => array( 2 ) ),
     500                        ),
     501                        'sort order'         => array(
     502                                'args'                => array( 'sort_order' => 'DESC' ),
     503                                'expected_query_args' => array(
     504                                        'order'   => 'DESC',
     505                                        'orderby' => array( 'post_title' => 'DESC' ),
     506                                ),
     507                        ),
     508                );
    326509        }
    327510
Note: See TracChangeset for help on using the changeset viewer.