Skip to content

Commit

Permalink
Code cleanup (II)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed May 21, 2024
1 parent 17a73eb commit 1c13545
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 666 deletions.
95 changes: 51 additions & 44 deletions lib/class-wp-block-templates-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ final class WP_Block_Templates_Registry {
* Registered block templates, as `$name => $instance` pairs.
*
* @since 6.6.0
* @var WP_Block_Type[]
* @var array $registered_block_templates {
* @type WP_Block_Template[] $wp_template Registered block templates.
* @type WP_Block_Template[] $wp_template_part Registered block template parts.
* }
*/
private $registered_block_templates = array(
'wp_template' => array(),
Expand Down Expand Up @@ -83,7 +86,7 @@ public function register( $template_name, $template_type, $args = array() ) {
if ( ! preg_match( $name_matcher, $template_name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Block template names must contain a namespace prefix. Example: my-plugin/my-custom-template', 'gutenberg' ),
__( 'Block template names must contain a namespace prefix. Example: my-plugin//my-custom-template', 'gutenberg' ),
'6.6.0'
);
return false;
Expand All @@ -101,56 +104,61 @@ public function register( $template_name, $template_type, $args = array() ) {

if ( ! $template ) {
$theme_name = get_stylesheet();
$slug = array_key_exists( 'slug', $args ) ? $args['slug'] : explode( '//', $template_name )[1];
$slug = isset( $args['slug'] ) ? $args['slug'] : explode( '//', $template_name )[1];

$template = new WP_Block_Template();
$template->id = $theme_name . '//' . $slug;
$template->theme = $theme_name; // @todo If not attached to the theme, this should be the plugin URI.
$template->plugin = array_key_exists( 'plugin', $args ) ? $args['plugin'] : '';
$template->theme = $theme_name;
$template->plugin = isset( $args['plugin'] ) ? $args['plugin'] : '';
$template->author = null;
$template->content = array_key_exists( 'path', $args ) ? file_get_contents( $args['path'] ) : '';
$template->content = isset( $args['path'] ) ? file_get_contents( $args['path'] ) : '';
$template->source = 'plugin';
$template->slug = $slug;
$template->type = $template_type;
$template->title = array_key_exists( 'title', $args ) ? $args['title'] : '';
$template->description = array_key_exists( 'description', $args ) ? $args['description'] : '';
$template->title = isset( $args['title'] ) ? $args['title'] : '';
$template->description = isset( $args['description'] ) ? $args['description'] : '';
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = 'plugin';
$template->is_custom = true;
$template->post_types = array_key_exists( 'post_types', $args ) ? $args['post_types'] : '';
$template->area = 'wp_template_part' === $template_type && array_key_exists( 'area', $args ) ? $args['area'] : '';
$template->post_types = 'wp_template' === $template_type && isset( $args['post_types'] ) ? $args['post_types'] : '';
$template->area = 'wp_template_part' === $template_type && isset( $args['area'] ) ? $args['area'] : '';
}

$this->registered_block_templates[ $template_type ][ $template_name ] = $template;

return $template;
}

public function get_by_slug( $template_type, $template_slug ) {
$all_templates = $this->get_all_registered( $template_type );

if ( ! $all_templates ) {
return null;
}

foreach ( $all_templates as $template ) {
if ( $template->slug === $template_slug ) {
return $template;
}
/**
* Retrieves all registered block templates by type.
*
* @since 6.6.0
*
* @param string $template_type Template type, either `wp_template` or `wp_template_part`.
* @return WP_Block_Template[]|false Associative array of `$block_template_name => $block_template` pairs.
*/
public function get_all_registered( $template_type ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
_doing_it_wrong(
__METHOD__,
__( 'Only valid block template types are `wp_template` and `wp_template_part`.', 'gutenberg' ),
'6.6.0'
);
return false;
}

return null;
return $this->registered_block_templates[ $template_type ];
}

/**
* Retrieves a registered template.
* Retrieves a registered template by its type and name.
*
* @since 6.6.0
*
* @param string $template_type Template type, either `wp_template` or `wp_template_part`.
* @param string $template_name Block type name including namespace.
* @return WP_Block_Type|null The registered block type, or null if it is not registered.
* @param string $template_name Block template name including namespace.
* @return WP_Block_Template|null|false The registered block template, or null if it is not registered.
*/
public function get_registered( $template_type, $template_name ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
Expand All @@ -170,24 +178,28 @@ public function get_registered( $template_type, $template_name ) {
}

/**
* Retrieves all registered block templates by type.
* Retrieves a registered template by its type and slug.
*
* @since 6.6.0
*
* @param string $template_type Template type, either `wp_template` or `wp_template_part`.
* @return WP_Block_Template[] Associative array of `$block_type_name => $block_type` pairs.
* @param string $template_slug Slug of the template.
* @return WP_Block_Template|null The registered block template, or null if it is not registered.
*/
public function get_all_registered( $template_type ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
_doing_it_wrong(
__METHOD__,
__( 'Only valid block template types are `wp_template` and `wp_template_part`.', 'gutenberg' ),
'6.6.0'
);
return false;
public function get_by_slug( $template_type, $template_slug ) {
$all_templates = $this->get_all_registered( $template_type );

if ( ! $all_templates ) {
return null;
}

return $this->registered_block_templates[ $template_type ];
foreach ( $all_templates as $template ) {
if ( $template->slug === $template_slug ) {
return $template;
}
}

return null;
}

/**
Expand Down Expand Up @@ -230,7 +242,7 @@ public function get_by_query( $template_type, $query = array() ) {
unset( $all_templates[ $template_name ] );
}

if ( ! empty( $post_type ) && ! in_array( $post_type, $template->post_types, true ) ) {
if ( 'wp_template' === $template_type && ! empty( $post_type ) && ! in_array( $post_type, $template->post_types, true ) ) {
unset( $all_templates[ $template_name ] );
}
}
Expand All @@ -244,7 +256,7 @@ public function get_by_query( $template_type, $query = array() ) {
* @since 6.6.0
*
* @param string $template_type Template type, either `wp_template` or `wp_template_part`.
* @param string $template_name Block type name including namespace.
* @param string $template_name Block template name including namespace.
* @return bool True if the template is registered, false otherwise.
*/
public function is_registered( $template_type, $template_name ) {
Expand All @@ -266,15 +278,10 @@ public function is_registered( $template_type, $template_name ) {
* @since 6.6.0
*
* @param string $template_type Template type, either `wp_template` or `wp_template_part`.
* @param string|WP_Block_Template $name Block template name including namespace, or alternatively
* a complete WP_Block_Template instance.
* @param string $name Block template name including namespace.
* @return WP_Block_Template|false The unregistered block template on success, or false on failure.
*/
public function unregister( $template_type, $template_name ) {
if ( $template_name instanceof WP_Block_Type ) {
$template_name = $template_name->name;
}

if ( ! $this->is_registered( $template_type, $template_name ) ) {
_doing_it_wrong(
__METHOD__,
Expand Down
111 changes: 2 additions & 109 deletions lib/compat/wordpress-6.6/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,6 @@ function _gutenberg_get_block_templates_files( $template_type, $query = array()
return array_values( $template_files );
}

function _gutenberg_add_template_details_from_registration( $template_type, $template_object_from_file ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_type, $template_object_from_file->slug );
if ( $registered_template ) {
$template_object_from_file->plugin = $registered_template->plugin;
}
return $template_object_from_file;
}

/**
* Retrieves a list of unified template objects based on a query.
*
Expand Down Expand Up @@ -335,39 +327,19 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
continue;
}

$query_result[] = _gutenberg_add_template_details_from_registration( $template_type, $template );
$query_result[] = $template;
}

if ( ! isset( $query['wp_id'] ) ) {

/*
* If the query has found some use templates, those have priority
* over the theme-provided ones, so we skip querying and building them.
*/
$query['slug__not_in'] = wp_list_pluck( $query_result, 'slug' );
$template_files = _gutenberg_get_block_templates_files( $template_type, $query );
foreach ( $template_files as $template_file ) {
$template_object_from_file = _build_block_template_result_from_file( $template_file, $template_type );

$query_result[] = _gutenberg_add_template_details_from_registration( $template_type, $template_object_from_file );
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
}

/*
* Add templates registered in the template registry. Filtering out the ones which have a theme file.
*/
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $template_type, $query );
$matching_registered_templates = array_filter(
$registered_templates,
function ( $registered_template ) use ( $template_files ) {
foreach ( $template_files as $template_file ) {
if ( $template_file['slug'] === $registered_template->slug ) {
return false;
}
}
return true;
}
);
$query_result = array_merge( $query_result, $matching_registered_templates );
}

/**
Expand All @@ -388,82 +360,3 @@ function ( $registered_template ) use ( $template_files ) {
*/
return apply_filters( 'get_block_templates', $query_result, $query, $template_type );
}

/**
* Retrieves a single unified template object using its id.
*
* @since 5.8.0
*
* @param string $id Template unique identifier (example: 'theme_slug//template_slug').
* @param string $template_type Optional. Template type. Either 'wp_template' or 'wp_template_part'.
* Default 'wp_template'.
* @return WP_Block_Template|null Template.
*/
function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
/**
* Filters the block template object before the query takes place.
*
* Return a non-null value to bypass the WordPress queries.
*
* @since 5.9.0
*
* @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query,
* or null to allow WP to run its normal queries.
* @param string $id Template unique identifier (example: 'theme_slug//template_slug').
* @param string $template_type Template type. Either '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;
}
list( $theme, $slug ) = $parts;
$wp_query_args = array(
'post_name__in' => array( $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
'no_found_rows' => true,
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
);
$template_query = new WP_Query( $wp_query_args );
$posts = $template_query->posts;

if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] );

if ( ! is_wp_error( $template ) ) {
return $template;
}
}

$block_template = get_block_file_template( $id, $template_type );
// @core-merge: This code will go into Core's 'get_block_template' function.
if ( ! $block_template ) {
$block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_type, $slug );
}
// @core-merge: End of the code that will go into Core.

$block_template = _gutenberg_add_template_details_from_registration( $template_type, $block_template );

/**
* Filters the queried block template object after it's been fetched.
*
* @since 5.9.0
*
* @param WP_Block_Template|null $block_template The found block template, or null if there isn't one.
* @param string $id Template unique identifier (example: 'theme_slug//template_slug').
* @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'.
*/
return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}
Loading

0 comments on commit 1c13545

Please sign in to comment.