AIPatterns.php
1 year ago
PTKClient.php
1 year ago
PTKPatternsStore.php
2 weeks ago
PatternRegistry.php
2 weeks ago
PatternRegistry.php
196 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Patterns; |
| 3 | |
| 4 | /** |
| 5 | * PatternRegistry class. |
| 6 | * |
| 7 | * @internal |
| 8 | */ |
| 9 | class PatternRegistry { |
| 10 | const SLUG_REGEX = '/^[A-z0-9\/_-]+$/'; |
| 11 | const COMMA_SEPARATED_REGEX = '/[\s,]+/'; |
| 12 | |
| 13 | /** |
| 14 | * Returns pattern slugs with their localized labels for categorization. |
| 15 | * |
| 16 | * Each key represents a unique pattern slug, while the value is the localized label. |
| 17 | * |
| 18 | * @return array<string, string> |
| 19 | */ |
| 20 | private function get_category_labels() { |
| 21 | return [ |
| 22 | 'woo-commerce' => __( 'WooCommerce', 'woocommerce' ), |
| 23 | 'intro' => __( 'Intro', 'woocommerce' ), |
| 24 | 'featured-selling' => __( 'Featured Selling', 'woocommerce' ), |
| 25 | 'about' => __( 'About', 'woocommerce' ), |
| 26 | 'social-media' => __( 'Social Media', 'woocommerce' ), |
| 27 | 'services' => __( 'Services', 'woocommerce' ), |
| 28 | 'reviews' => __( 'Reviews', 'woocommerce' ), |
| 29 | ]; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Register a block pattern. |
| 34 | * |
| 35 | * @param string $source The pattern source. |
| 36 | * @param array $pattern_data The pattern data. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function register_block_pattern( $source, $pattern_data ) { |
| 41 | if ( empty( $pattern_data['slug'] ) ) { |
| 42 | _doing_it_wrong( |
| 43 | 'register_block_patterns', |
| 44 | esc_html( |
| 45 | sprintf( |
| 46 | /* translators: %s: file name. */ |
| 47 | __( 'Could not register pattern "%s" as a block pattern ("Slug" field missing)', 'woocommerce' ), |
| 48 | $source |
| 49 | ) |
| 50 | ), |
| 51 | '6.0.0' |
| 52 | ); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if ( ! preg_match( self::SLUG_REGEX, $pattern_data['slug'] ) ) { |
| 57 | _doing_it_wrong( |
| 58 | 'register_block_patterns', |
| 59 | esc_html( |
| 60 | sprintf( |
| 61 | /* translators: %1s: file name; %2s: slug value found. */ |
| 62 | __( 'Could not register pattern "%1$s" as a block pattern (invalid slug "%2$s")', 'woocommerce' ), |
| 63 | $source, |
| 64 | $pattern_data['slug'] |
| 65 | ) |
| 66 | ), |
| 67 | '6.0.0' |
| 68 | ); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if ( \WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_data['slug'] ) ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Title is a required property. |
| 77 | if ( ! isset( $pattern_data['title'] ) || ! $pattern_data['title'] ) { |
| 78 | _doing_it_wrong( |
| 79 | 'register_block_patterns', |
| 80 | esc_html( |
| 81 | sprintf( |
| 82 | /* translators: %1s: file name; %2s: slug value found. */ |
| 83 | __( 'Could not register pattern "%s" as a block pattern ("Title" field missing)', 'woocommerce' ), |
| 84 | $source |
| 85 | ) |
| 86 | ), |
| 87 | '6.0.0' |
| 88 | ); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // For properties of type array, parse data as comma-separated. |
| 93 | foreach ( array( 'categories', 'keywords', 'blockTypes' ) as $property ) { |
| 94 | if ( ! empty( $pattern_data[ $property ] ) ) { |
| 95 | if ( is_array( $pattern_data[ $property ] ) ) { |
| 96 | $pattern_data[ $property ] = array_values( |
| 97 | array_map( |
| 98 | function ( $property ) { |
| 99 | return $property['title']; |
| 100 | }, |
| 101 | $pattern_data[ $property ] |
| 102 | ) |
| 103 | ); |
| 104 | } else { |
| 105 | $pattern_data[ $property ] = array_filter( |
| 106 | preg_split( |
| 107 | self::COMMA_SEPARATED_REGEX, |
| 108 | (string) $pattern_data[ $property ] |
| 109 | ) |
| 110 | ); |
| 111 | } |
| 112 | } else { |
| 113 | unset( $pattern_data[ $property ] ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Parse properties of type int. |
| 118 | foreach ( array( 'viewportWidth' ) as $property ) { |
| 119 | if ( ! empty( $pattern_data[ $property ] ) ) { |
| 120 | $pattern_data[ $property ] = (int) $pattern_data[ $property ]; |
| 121 | } else { |
| 122 | unset( $pattern_data[ $property ] ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Parse properties of type bool. |
| 127 | foreach ( array( 'inserter' ) as $property ) { |
| 128 | if ( ! empty( $pattern_data[ $property ] ) ) { |
| 129 | $pattern_data[ $property ] = in_array( |
| 130 | strtolower( $pattern_data[ $property ] ), |
| 131 | array( 'yes', 'true' ), |
| 132 | true |
| 133 | ); |
| 134 | } else { |
| 135 | unset( $pattern_data[ $property ] ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction |
| 140 | $pattern_data['title'] = translate_with_gettext_context( wp_strip_all_tags( html_entity_decode( (string) $pattern_data['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ), 'Pattern title', 'woocommerce' ); |
| 141 | if ( ! empty( $pattern_data['description'] ) ) { |
| 142 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.LowLevelTranslationFunction |
| 143 | $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', 'woocommerce' ); |
| 144 | } |
| 145 | |
| 146 | // A pattern is registrable as long as it provides either inline content |
| 147 | // or a `filePath` that core can load lazily (WP 6.5+). Bail only when |
| 148 | // neither is available. |
| 149 | if ( empty( $pattern_data['content'] ) && empty( $pattern_data['filePath'] ) ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // When a `filePath` is provided, let core load the content lazily on |
| 154 | // demand. Drop any empty `content` so core falls back to `filePath` |
| 155 | // instead of registering an empty pattern. |
| 156 | if ( ! empty( $pattern_data['filePath'] ) && empty( $pattern_data['content'] ) ) { |
| 157 | unset( $pattern_data['content'] ); |
| 158 | } |
| 159 | |
| 160 | $category_labels = $this->get_category_labels(); |
| 161 | |
| 162 | if ( ! empty( $pattern_data['categories'] ) ) { |
| 163 | foreach ( $pattern_data['categories'] as $key => $category ) { |
| 164 | $category_slug = _wp_to_kebab_case( $category ); |
| 165 | |
| 166 | $pattern_data['categories'][ $key ] = $category_slug; |
| 167 | |
| 168 | $label = $category_labels[ $category_slug ] ?? self::kebab_to_capital_case( $category_slug ); |
| 169 | |
| 170 | register_block_pattern_category( |
| 171 | $category_slug, |
| 172 | array( |
| 173 | 'label' => $label, |
| 174 | ), |
| 175 | ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | register_block_pattern( $pattern_data['slug'], $pattern_data ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Convert a kebab-case string to capital case. |
| 184 | * |
| 185 | * @param string $value The kebab-case string. |
| 186 | * |
| 187 | * @return string |
| 188 | */ |
| 189 | private static function kebab_to_capital_case( $value ) { |
| 190 | $string = str_replace( '-', ' ', $value ); |
| 191 | $string = ucwords( $string ); |
| 192 | |
| 193 | return $string; |
| 194 | } |
| 195 | } |
| 196 |