Prevent early translation loading (#52055)

This commit is contained in:
Pascal Birchler 2024-10-17 06:04:36 +02:00 committed by GitHub
parent bd838d824a
commit 9689a80fa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 17 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Removes several side effects in the code base that caused translations to be loaded too early.

View File

@ -82,8 +82,6 @@ class BlockPatterns {
$this->pattern_registry = $pattern_registry;
$this->ptk_patterns_store = $ptk_patterns_store;
$this->dictionary = PatternsHelper::get_patterns_dictionary();
add_action( 'init', array( $this, 'register_block_patterns' ) );
if ( Features::is_enabled( 'pattern-toolkit-full-composability' ) ) {
@ -91,6 +89,19 @@ class BlockPatterns {
}
}
/**
* Returns the Patterns dictionary.
*
* @return array|WP_Error
*/
private function get_patterns_dictionary() {
if ( null === $this->dictionary ) {
$this->dictionary = PatternsHelper::get_patterns_dictionary();
}
return $this->dictionary;
}
/**
* Register block patterns from core.
*
@ -103,7 +114,7 @@ class BlockPatterns {
$patterns = $this->get_block_patterns();
foreach ( $patterns as $pattern ) {
$this->pattern_registry->register_block_pattern( $pattern['source'], $pattern, $this->dictionary );
$this->pattern_registry->register_block_pattern( $pattern['source'], $pattern, $this->get_patterns_dictionary() );
}
}
@ -212,7 +223,7 @@ class BlockPatterns {
$pattern['slug'] = $pattern['name'];
$pattern['content'] = $pattern['html'];
$this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern, $this->dictionary );
$this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern, $this->get_patterns_dictionary() );
}
}

View File

@ -12,20 +12,15 @@ class PatternRegistry {
const SLUG_REGEX = '/^[A-z0-9\/_-]+$/';
const COMMA_SEPARATED_REGEX = '/[\s,]+/';
/**
* Associates pattern slugs with their localized labels for categorization.
* Returns pattern slugs with their localized labels for categorization.
*
* Each key represents a unique pattern slug, while the value is the localized label.
*
* @var array $category_labels
* @return array<string, string>
*/
private $category_labels;
/**
* Constructor.
*/
public function __construct() {
$this->category_labels = [
private function get_category_labels() {
return [
'woo-commerce' => __( 'WooCommerce', 'woocommerce' ),
'intro' => __( 'Intro', 'woocommerce' ),
'featured-selling' => __( 'Featured Selling', 'woocommerce' ),
@ -148,10 +143,10 @@ class PatternRegistry {
}
}
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction
$pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', 'woocommerce' );
if ( ! empty( $pattern_data['description'] ) ) {
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction
$pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woocommerce' );
}
@ -186,13 +181,15 @@ class PatternRegistry {
}
}
$category_labels = $this->get_category_labels();
if ( ! empty( $pattern_data['categories'] ) ) {
foreach ( $pattern_data['categories'] as $key => $category ) {
$category_slug = _wp_to_kebab_case( $category );
$pattern_data['categories'][ $key ] = $category_slug;
$label = isset( $this->category_labels[ $category_slug ] ) ? $this->category_labels[ $category_slug ] : self::kebab_to_capital_case( $category_slug );
$label = $category_labels[ $category_slug ] ?? self::kebab_to_capital_case( $category_slug );
register_block_pattern_category(
$category_slug,