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