Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename gutenberg_get_block_file_template
  • Loading branch information
ntsekouras committed Nov 4, 2021
commit 679bd49706039907383ff7dd42230139f5869324
63 changes: 62 additions & 1 deletion lib/compat/wordpress-5.9/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,67 @@ function _build_block_template_result_from_post( $post ) {
}
}

if ( ! function_exists( 'get_block_file_template' ) ) {
/**
* Retrieves a single unified template object using its id.
* Retrieves the file template.
*
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*
* @return WP_Block_Template|null File template.
*/
function 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 queries.
*
* @since 10.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 ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}
list( $theme, $slug ) = $parts;

if ( wp_get_theme()->get_stylesheet() !== $theme ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}

$template_file = _get_block_template_file( $template_type, $slug );
if ( null === $template_file ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}

$block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type );

/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 10.8
*
* @param null|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 );
}
}

if ( ! function_exists( 'get_block_template' ) ) {
/**
* Retrieves a single unified template object using its id.
Expand Down Expand Up @@ -414,7 +475,7 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
}
}

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

/**
* Filters the array of queried block templates array after they've been fetched.
Expand Down
59 changes: 0 additions & 59 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,65 +259,6 @@ function gutenberg_get_block_templates( $query = array(), $template_type = 'wp_t
return apply_filters( 'get_block_templates', $query_result, $query, $template_type );
}

/**
* Retrieves a single unified template object using its id.
* Retrieves the file template.
*
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*
* @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 queries.
*
* @since 10.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 ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}
list( $theme, $slug ) = $parts;

if ( wp_get_theme()->get_stylesheet() !== $theme ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}

$template_file = _get_block_template_file( $template_type, $slug );
if ( null === $template_file ) {
/** This filter is documented at the end of this function */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}

$block_template = _gutenberg_build_template_result_from_file( $template_file, $template_type );

/**
* Filters the array of queried block templates array after they've been fetched.
*
* @since 10.8
*
* @param null|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 );
}

/**
* Generates a unique slug for templates or template parts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function get_item_permissions_check( $request ) {
*/
public function get_item( $request ) {
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
$template = gutenberg_get_block_file_template( $request['id'], $this->post_type );
$template = get_block_file_template( $request['id'], $this->post_type );
} else {
$template = get_block_template( $request['id'], $this->post_type );
}
Expand Down Expand Up @@ -209,7 +209,7 @@ public function update_item( $request ) {

if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
wp_delete_post( $template->wp_id, true );
return $this->prepare_item_for_response( gutenberg_get_block_file_template( $request['id'], $this->post_type ), $request );
return $this->prepare_item_for_response( get_block_file_template( $request['id'], $this->post_type ), $request );
}

$changes = $this->prepare_item_for_database( $request );
Expand Down