Create update_template_data util and formatting improvements (#46408)
* Create update_template_data util * Fix comment typo * Formatting * Remove unnecessary use of BlockTemplateUtils * Add changelog file * Create update_template_data util (II)
This commit is contained in:
parent
573fe6e612
commit
c59dcbe925
|
@ -406,6 +406,8 @@ export class EditorUtils {
|
|||
}
|
||||
|
||||
async revertTemplateCreation( templateName: string ) {
|
||||
await this.page.getByPlaceholder( 'Search' ).fill( templateName );
|
||||
|
||||
const templateRow = this.page.getByRole( 'row' ).filter( {
|
||||
has: this.page.getByRole( 'link', {
|
||||
name: templateName,
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
Significance: patch
|
||||
Type: tweak
|
||||
Comment: Create update_template_data util and formatting improvements
|
||||
|
||||
|
|
@ -308,19 +308,7 @@ class BlockTemplatesController {
|
|||
* @return WP_Block_Template|null
|
||||
*/
|
||||
public function add_block_template_details( $block_template, $id, $template_type ) {
|
||||
if ( ! $block_template ) {
|
||||
return $block_template;
|
||||
}
|
||||
if ( ! BlockTemplateUtils::template_has_title( $block_template ) ) {
|
||||
$block_template->title = BlockTemplateUtils::get_block_template_title( $block_template->slug );
|
||||
}
|
||||
if ( ! $block_template->description ) {
|
||||
$block_template->description = BlockTemplateUtils::get_block_template_description( $block_template->slug );
|
||||
}
|
||||
if ( ! $block_template->area || 'uncategorized' === $block_template->area ) {
|
||||
$block_template->area = BlockTemplateUtils::get_block_template_area( $block_template->slug, $template_type );
|
||||
}
|
||||
return $block_template;
|
||||
return BlockTemplateUtils::update_template_data( $block_template, $template_type );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -401,17 +389,7 @@ class BlockTemplatesController {
|
|||
*/
|
||||
$query_result = array_map(
|
||||
function ( $template ) use ( $template_type ) {
|
||||
if ( ! BlockTemplateUtils::template_has_title( $template ) ) {
|
||||
$template->title = BlockTemplateUtils::get_block_template_title( $template->slug );
|
||||
}
|
||||
if ( ! $template->description ) {
|
||||
$template->description = BlockTemplateUtils::get_block_template_description( $template->slug );
|
||||
}
|
||||
if ( ! $template->area || 'uncategorized' === $template->area ) {
|
||||
$template->area = BlockTemplateUtils::get_block_template_area( $template->slug, $template_type );
|
||||
}
|
||||
|
||||
return $template;
|
||||
return BlockTemplateUtils::update_template_data( $template, $template_type );
|
||||
},
|
||||
$query_result
|
||||
);
|
||||
|
|
|
@ -78,7 +78,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
|||
|
||||
$block_hooks = array_filter(
|
||||
$this->hook_data,
|
||||
function( $hook ) use ( $block_name ) {
|
||||
function ( $hook ) use ( $block_name ) {
|
||||
return in_array( $block_name, $hook['block_names'], true );
|
||||
}
|
||||
);
|
||||
|
@ -98,9 +98,9 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
|||
}
|
||||
|
||||
$supported_blocks = array_merge(
|
||||
[],
|
||||
array(),
|
||||
...array_map(
|
||||
function( $hook ) {
|
||||
function ( $hook ) {
|
||||
return $hook['block_names'];
|
||||
},
|
||||
array_values( $this->hook_data )
|
||||
|
@ -312,7 +312,7 @@ class ArchiveProductTemplatesCompatibility extends AbstractTemplateCompatibility
|
|||
continue;
|
||||
}
|
||||
foreach ( $data['hooked'] as $callback => $priority ) {
|
||||
if ( ! in_array( $callback, $data['permanently_removed_actions'] ?? [], true ) ) {
|
||||
if ( ! in_array( $callback, $data['permanently_removed_actions'] ?? array(), true ) ) {
|
||||
add_action( $hook, $callback, $priority );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\Templates;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
|
||||
|
||||
/**
|
||||
* CartTemplate class.
|
||||
*
|
||||
|
@ -33,7 +31,6 @@ class CartTemplate extends AbstractPageTemplate {
|
|||
*/
|
||||
public function get_template_title() {
|
||||
return _x( 'Page: Cart', 'Template name', 'woocommerce' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,7 +40,6 @@ class CartTemplate extends AbstractPageTemplate {
|
|||
*/
|
||||
public function get_template_description() {
|
||||
return __( 'The Cart template displays the items selected by the user for purchase, including quantities, prices, and discounts. It allows users to review their choices before proceeding to checkout.', 'woocommerce' );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\Templates;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
|
||||
|
||||
/**
|
||||
* CheckoutTemplate class.
|
||||
*
|
||||
|
|
|
@ -54,13 +54,13 @@ class MiniCartTemplate extends AbstractTemplatePart {
|
|||
* @return array The supported template part areas including the Mini-Cart one.
|
||||
*/
|
||||
public function register_mini_cart_template_part_area( $default_area_definitions ) {
|
||||
$mini_cart_template_part_area = [
|
||||
$mini_cart_template_part_area = array(
|
||||
'area' => 'mini-cart',
|
||||
'label' => __( 'Mini-Cart', 'woocommerce' ),
|
||||
'description' => __( 'The Mini-Cart template allows shoppers to see their cart items and provides access to the Cart and Checkout pages.', 'woocommerce' ),
|
||||
'icon' => 'mini-cart',
|
||||
'area_tag' => 'mini-cart',
|
||||
];
|
||||
return array_merge( $default_area_definitions, [ $mini_cart_template_part_area ] );
|
||||
);
|
||||
return array_merge( $default_area_definitions, array( $mini_cart_template_part_area ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use Automattic\WooCommerce\Blocks\Templates\SingleProductTemplateCompatibility;
|
|||
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
|
||||
|
||||
/**
|
||||
* SingleProductTemplae class.
|
||||
* SingleProductTemplate class.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
|
@ -51,7 +51,7 @@ class SingleProductTemplate extends AbstractTemplate {
|
|||
if ( ! is_embed() && is_singular( 'product' ) ) {
|
||||
global $post;
|
||||
|
||||
$valid_slugs = [ self::SLUG ];
|
||||
$valid_slugs = array( self::SLUG );
|
||||
if ( 'product' === $post->post_type && $post->post_name ) {
|
||||
$valid_slugs[] = 'single-product-' . $post->post_name;
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ class SingleProductTemplate extends AbstractTemplate {
|
|||
*/
|
||||
public function update_single_product_content( $query_result, $query, $template_type ) {
|
||||
$query_result = array_map(
|
||||
function( $template ) {
|
||||
function ( $template ) {
|
||||
if ( str_contains( $template->slug, self::SLUG ) ) {
|
||||
// We don't want to add the compatibility layer on the Editor Side.
|
||||
// The second condition is necessary to not apply the compatibility layer on the REST API. Gutenberg uses the REST API to clone the template.
|
||||
|
@ -84,7 +84,7 @@ class SingleProductTemplate extends AbstractTemplate {
|
|||
// Add the product class to the body. We should move this to a more appropriate place.
|
||||
add_filter(
|
||||
'body_class',
|
||||
function( $classes ) {
|
||||
function ( $classes ) {
|
||||
return array_merge( $classes, wc_get_product_class() );
|
||||
}
|
||||
);
|
||||
|
@ -125,7 +125,7 @@ class SingleProductTemplate extends AbstractTemplate {
|
|||
$single_product_template_blocks = array( 'woocommerce/product-image-gallery', 'woocommerce/product-details', 'woocommerce/add-to-cart-form', 'woocommerce/product-meta', 'woocommerce/product-rating', 'woocommerce/product-price', 'woocommerce/related-products' );
|
||||
return array_reduce(
|
||||
$parsed_blocks,
|
||||
function( $carry, $block ) use ( $single_product_template_blocks ) {
|
||||
function ( $carry, $block ) use ( $single_product_template_blocks ) {
|
||||
if ( in_array( $block['blockName'], $single_product_template_blocks, true ) ) {
|
||||
if ( $carry['is_already_replaced'] ) {
|
||||
return array(
|
||||
|
|
|
@ -31,7 +31,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
|
||||
$block_hooks = array_filter(
|
||||
$this->hook_data,
|
||||
function( $hook ) use ( $block_name ) {
|
||||
function ( $hook ) use ( $block_name ) {
|
||||
return in_array( $block_name, $hook['block_names'], true );
|
||||
}
|
||||
);
|
||||
|
@ -279,7 +279,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
$grouped_blocks = self::group_blocks( $parsed_blocks );
|
||||
|
||||
$wrapped_blocks = array_map(
|
||||
function( $blocks ) {
|
||||
function ( $blocks ) {
|
||||
if ( 'core/template-part' === $blocks[0]['blockName'] ) {
|
||||
return $blocks;
|
||||
}
|
||||
|
@ -306,7 +306,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
private static function inject_custom_attributes_to_first_and_last_block_single_product_template( $wrapped_blocks ) {
|
||||
$template_with_custom_attributes = array_reduce(
|
||||
$wrapped_blocks,
|
||||
function( $carry, $item ) {
|
||||
function ( $carry, $item ) {
|
||||
|
||||
$index = $carry['index'];
|
||||
$carry['index'] = $carry['index'] + 1;
|
||||
|
@ -379,7 +379,6 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
$new_block['innerBlocks'] = $blocks;
|
||||
|
||||
return $new_block;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -421,7 +420,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
private static function group_blocks( $parsed_blocks ) {
|
||||
return array_reduce(
|
||||
$parsed_blocks,
|
||||
function( array $carry, array $block ) {
|
||||
function ( array $carry, array $block ) {
|
||||
if ( 'core/template-part' === $block['blockName'] ) {
|
||||
$carry[] = array( $block );
|
||||
return $carry;
|
||||
|
@ -480,7 +479,7 @@ class SingleProductTemplateCompatibility extends AbstractTemplateCompatibility {
|
|||
private static function serialize_blocks( $parsed_blocks ) {
|
||||
return array_reduce(
|
||||
$parsed_blocks,
|
||||
function( $carry, $item ) {
|
||||
function ( $carry, $item ) {
|
||||
if ( is_array( $item ) ) {
|
||||
return $carry . serialize_blocks( $item );
|
||||
}
|
||||
|
|
|
@ -706,16 +706,6 @@ class BlockTemplateUtils {
|
|||
return wc_string_to_bool( $use_blockified_templates );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the passed `$template` has a title, and it's different from the slug.
|
||||
*
|
||||
* @param object $template The template object.
|
||||
* @return boolean
|
||||
*/
|
||||
public static function template_has_title( $template ) {
|
||||
return ! empty( $template->title ) && $template->title !== $template->slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the passed `$template` has the legacy template block.
|
||||
*
|
||||
|
@ -726,6 +716,37 @@ class BlockTemplateUtils {
|
|||
return has_block( 'woocommerce/legacy-template', $template->content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the title, description and area of a template to the correct values and to make them more user-friendly.
|
||||
* For example, instead of:
|
||||
* - Title: `Tag (product_tag)`
|
||||
* - Description: `Displays taxonomy: Tag.`
|
||||
* we display:
|
||||
* - Title: `Products by Tag`
|
||||
* - Description: `Displays products filtered by a tag.`.
|
||||
*
|
||||
* @param WP_Block_Template $template The template object.
|
||||
* @param string $template_type wp_template or wp_template_part.
|
||||
*
|
||||
* @return WP_Block_Template
|
||||
*/
|
||||
public static function update_template_data( $template, $template_type ) {
|
||||
if ( ! $template ) {
|
||||
return $template;
|
||||
}
|
||||
if ( empty( $template->title ) || $template->title === $template->slug ) {
|
||||
$template->title = self::get_block_template_title( $template->slug );
|
||||
}
|
||||
if ( empty( $template->description ) ) {
|
||||
$template->description = self::get_block_template_description( $template->slug );
|
||||
}
|
||||
if ( empty( $template->area ) || 'uncategorized' === $template->area ) {
|
||||
$template->area = self::get_block_template_area( $template->slug, $template_type );
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the templates saved in the database.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue