Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filters to get block templates functions. #31806

Merged
merged 13 commits into from
Jun 23, 2021
Merged
Changes from 5 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
114 changes: 104 additions & 10 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ function _gutenberg_get_template_paths( $base_directory ) {
* @access private
* @internal
*
* @param array $template_type wp_template or wp_template_part.
* @param string $template_type wp_template or wp_template_part.
* @param string $slug template slug.
*
* @return array Template.
* @return array|null Template.
*/
function _gutenberg_get_template_file( $template_type, $slug ) {
$template_base_paths = array(
Expand Down Expand Up @@ -297,6 +297,28 @@ function _gutenberg_build_template_result_from_post( $post ) {
* @return array Templates.
*/
function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_template' ) {
/**
* Filters the block templates array before the query takes place.
*
* Return a non-null value to bypass the WordPress quries.
*
* @since 5.8
*
* @param WP_Block_Template[]|null $block_templates Return an array of block templates to short-circuit the default query,
* or null to allow WP to run it's normal queries.
* @param array $query {
* Optional. Arguments to retrieve templates.
*
* @type array $slug__in List of slugs to include.
* @type int $wp_id Post ID of customized template.
* }
* @param array $template_type wp_template or wp_template_part.
*/
$templates = apply_filters( 'pre_get_block_templates', null, $query, $template_type );
if ( ! is_null( $templates ) ) {
return $templates;
}

$wp_query_args = array(
'post_status' => array( 'auto-draft', 'draft', 'publish' ),
'post_type' => $template_type,
Expand Down Expand Up @@ -360,7 +382,21 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
}
}

return $query_result;
/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 5.8
*
* @param WP_Block_Template[] $query_result Array of found block templates.
* @param array $query {
* Optional. Arguments to retrieve templates.
*
* @type array $slug__in List of slugs to include.
* @type int $wp_id Post ID of customized template.
* }
* @param array $template_type wp_template or wp_template_part.
*/
return apply_filters( 'queried_block_templates', $query_result, $query, $template_type );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name here should correspond to the same name as the pre_* filter above. So we would have pre_get_block_templates and get_block_templates (similar to as you did in the functions below).

Also, im not completely certain, but I wonder if we should be adding the gutenberg prefix to these filters the same as they are in the functions they are contained in? Like pre_gutenberg_get_block_templates / gutenberg_get_block_templates etc? It would be a little more specific to their use and hopefully less potential of conflict 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I'm also a bit unsure about the names of these filter names, and this one the most.

Searching the project for filters with a gutenberg_ prefix I only found 1. Blocks templates themself are pretty new territory, so I doubt there would be conflicts.

I'll change it to get_block_templates for at least now, if anybody has a better suggestion (for any of the filters) I'll change it again.

}

/**
Expand All @@ -372,6 +408,23 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
* @return WP_Block_Template|null Template.
*/
function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
/**
* Filters the block templates array before the query takes place.
*
* Return a non-null value to bypass the WordPress quries.
*
* @since 5.8
*
* @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
* or null to allow WP to run it's normal queries.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*/
$block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type );
if ( ! is_null( $block_template ) ) {
return $block_template;
}

$parts = explode( '//', $id, 2 );
if ( count( $parts ) < 2 ) {
return null;
Expand Down Expand Up @@ -402,7 +455,18 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
}
}

return gutenberg_get_block_file_template( $id, $template_type );
$block_template = gutenberg_get_block_file_template( $id, $template_type );

/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 5.8
*
* @param WP_Block_Template $block_template The found block template.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*/
return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}

/**
Expand All @@ -415,20 +479,50 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
* @return WP_Block_Template|null File template.
*/
function gutenberg_get_block_file_template( $id, $template_type = 'wp_template' ) {
/**
* Filters the block templates array before the query takes place.
*
* Return a non-null value to bypass the WordPress quries.
*
* @since 5.8
*
* @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
* or null to allow WP to run it's normal queries.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*/
$block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type );
if ( ! is_null( $block_template ) ) {
return $block_template;
}

$parts = explode( '//', $id, 2 );
if ( count( $parts ) < 2 ) {
return null;
}
list( $theme, $slug ) = $parts;

if ( wp_get_theme()->get_stylesheet() === $theme ) {
$template_file = _gutenberg_get_template_file( $template_type, $slug );
if ( null !== $template_file ) {
return _gutenberg_build_template_result_from_file( $template_file, $template_type );
}
if ( wp_get_theme()->get_stylesheet() !== $theme ) {
return null;
}

return null;
$template_file = _gutenberg_get_template_file( $template_type, $slug );
if ( null === $template_file ) {
return null;
}

$block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type );
janw-me marked this conversation as resolved.
Show resolved Hide resolved

/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 5.8
*
* @param WP_Block_Template $block_template The found block template.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*/
return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
}

/**
Expand Down