WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.45k stars 4.17k forks source link

Add get_pages() args filter to core/page-list block #65897

Open koskinenaa opened 1 week ago

koskinenaa commented 1 week ago

What problem does this address?

This ticket was inspired by the question "How do you add a filter to wp-block-page-list?" at WordPress StackExchange.

Currently the core/page-list block doesn't provide any filters for modifying the pages query. The user has to either use the get_pages_query_args or get_pages filters, should they want to filter the query. However, targeting the correct get_pages() call is difficult as the filters are not aware of in which context they are used - in a block or some other place.

The user can get creative and wrap the default block rendering callback in a helper function to include logic to try to determine the correct block, which query needs to be filtered. This is demonstrated in an answer written by me to the above WPSE question.

What is your proposed solution?

If the core/page-list block provided a query args filter, the query modification would be a lot easier and could be done in a more accurate and reliable way.

This could be done for example by extracting the pages query from the block's rendering callback into a helper function. This helper would then contain the query args filter and return an array of pages to be used in the rendering callback. The helper would receive the block attributes and the block instance as parameters to provide context for the filter.

This kind of filter would also mimic the widget_pages_args filter the WP_Widget_Pages uses.

The extracted query helper function,

/**
 * Query pages for `core/page-list` block
 *
 * @param array    $attributes The block attributes.
 * @param WP_Block $block      The parsed block.
 *
 * @return array Returns array of pages
 */
function block_core_page_list_pages_query( $attributes, $block ) {

    /**
     * Filters the arguments for the `core/page-list` block
     *
     * @see get_pages()
     *
     * @param array    $args       An array of arguments to query the pages array.
     * @param array    $attributes The block attributes.
     * @param WP_Block $block      The parsed block.
     */
    $args = apply_filters(
        'block_core_page_list_pages_query_args',
        array(
            'sort_column' => 'menu_order,post_title',
            'order'       => 'asc',
        ),
        $attributes,
        $block
    );

    $all_pages = get_pages( $args );

    return is_array( $all_pages ) ? $all_pages : array();
}

The above helper function used in the rendering callback,

function render_block_core_page_list( $attributes, $content, $block ) {

    // code..

    $all_pages = block_core_page_list_pages_query( $attributes, $block );

    // code..

}
Vrishabhsk commented 3 days ago

Hi @koskinenaa 👋

Let me know your thoughts on this. Thanks