@@ -6,20 +6,20 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class ProductNew extends AbstractProductGrid { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Block name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $block_name = 'product-new'; |
|
| 9 | + /** |
|
| 10 | + * Block name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $block_name = 'product-new'; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set args specific to this block |
|
| 18 | - * |
|
| 19 | - * @param array $query_args Query args. |
|
| 20 | - */ |
|
| 21 | - protected function set_block_query_args( &$query_args ) { |
|
| 22 | - $query_args['orderby'] = 'date'; |
|
| 23 | - $query_args['order'] = 'DESC'; |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * Set args specific to this block |
|
| 18 | + * |
|
| 19 | + * @param array $query_args Query args. |
|
| 20 | + */ |
|
| 21 | + protected function set_block_query_args( &$query_args ) { |
|
| 22 | + $query_args['orderby'] = 'date'; |
|
| 23 | + $query_args['order'] = 'DESC'; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -18,7 +18,7 @@ |
||
| 18 | 18 | * |
| 19 | 19 | * @param array $query_args Query args. |
| 20 | 20 | */ |
| 21 | - protected function set_block_query_args( &$query_args ) { |
|
| 21 | + protected function set_block_query_args(&$query_args) { |
|
| 22 | 22 | $query_args['orderby'] = 'date'; |
| 23 | 23 | $query_args['order'] = 'DESC'; |
| 24 | 24 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class ProductSKU extends AbstractBlock { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Block name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $block_name = 'product-sku'; |
|
| 9 | + /** |
|
| 10 | + * Block name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $block_name = 'product-sku'; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * API version name. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $api_version = '2'; |
|
| 16 | + /** |
|
| 17 | + * API version name. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $api_version = '2'; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Register script and style assets for the block type before it is registered. |
|
| 25 | - * |
|
| 26 | - * This registers the scripts; it does not enqueue them. |
|
| 27 | - */ |
|
| 28 | - protected function register_block_type_assets() { |
|
| 29 | - parent::register_block_type_assets(); |
|
| 30 | - $this->register_chunk_translations( [ $this->block_name ] ); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Register script and style assets for the block type before it is registered. |
|
| 25 | + * |
|
| 26 | + * This registers the scripts; it does not enqueue them. |
|
| 27 | + */ |
|
| 28 | + protected function register_block_type_assets() { |
|
| 29 | + parent::register_block_type_assets(); |
|
| 30 | + $this->register_chunk_translations( [ $this->block_name ] ); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -51,6 +51,6 @@ |
||
| 51 | 51 | */ |
| 52 | 52 | protected function register_block_type_assets() { |
| 53 | 53 | parent::register_block_type_assets(); |
| 54 | - $this->register_chunk_translations( [ $this->block_name ] ); |
|
| 54 | + $this->register_chunk_translations([$this->block_name]); |
|
| 55 | 55 | } |
| 56 | 56 | } |
@@ -5,66 +5,66 @@ |
||
| 5 | 5 | * ProductTag class. |
| 6 | 6 | */ |
| 7 | 7 | class ProductTag extends AbstractProductGrid { |
| 8 | - /** |
|
| 9 | - * Block name. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $block_name = 'product-tag'; |
|
| 8 | + /** |
|
| 9 | + * Block name. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $block_name = 'product-tag'; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Set args specific to this block. |
|
| 17 | - * |
|
| 18 | - * @param array $query_args Query args. |
|
| 19 | - */ |
|
| 20 | - protected function set_block_query_args( &$query_args ) { |
|
| 21 | - if ( ! empty( $this->attributes['tags'] ) ) { |
|
| 22 | - $query_args['tax_query'][] = array( |
|
| 23 | - 'taxonomy' => 'product_tag', |
|
| 24 | - 'terms' => array_map( 'absint', $this->attributes['tags'] ), |
|
| 25 | - 'field' => 'term_id', |
|
| 26 | - 'operator' => isset( $this->attributes['tagOperator'] ) && 'any' === $this->attributes['tagOperator'] ? 'IN' : 'AND', |
|
| 27 | - ); |
|
| 28 | - } |
|
| 29 | - } |
|
| 15 | + /** |
|
| 16 | + * Set args specific to this block. |
|
| 17 | + * |
|
| 18 | + * @param array $query_args Query args. |
|
| 19 | + */ |
|
| 20 | + protected function set_block_query_args( &$query_args ) { |
|
| 21 | + if ( ! empty( $this->attributes['tags'] ) ) { |
|
| 22 | + $query_args['tax_query'][] = array( |
|
| 23 | + 'taxonomy' => 'product_tag', |
|
| 24 | + 'terms' => array_map( 'absint', $this->attributes['tags'] ), |
|
| 25 | + 'field' => 'term_id', |
|
| 26 | + 'operator' => isset( $this->attributes['tagOperator'] ) && 'any' === $this->attributes['tagOperator'] ? 'IN' : 'AND', |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Get block attributes. |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - protected function get_block_type_attributes() { |
|
| 37 | - return array( |
|
| 38 | - 'className' => $this->get_schema_string(), |
|
| 39 | - 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 40 | - 'rows' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_rows', 3 ) ), |
|
| 41 | - 'contentVisibility' => $this->get_schema_content_visibility(), |
|
| 42 | - 'align' => $this->get_schema_align(), |
|
| 43 | - 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 44 | - 'orderby' => $this->get_schema_orderby(), |
|
| 45 | - 'tags' => $this->get_schema_list_ids(), |
|
| 46 | - 'tagOperator' => array( |
|
| 47 | - 'type' => 'string', |
|
| 48 | - 'default' => 'any', |
|
| 49 | - ), |
|
| 50 | - 'isPreview' => $this->get_schema_boolean( false ), |
|
| 51 | - 'stockStatus' => array_keys( wc_get_product_stock_status_options() ), |
|
| 52 | - ); |
|
| 53 | - } |
|
| 31 | + /** |
|
| 32 | + * Get block attributes. |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + protected function get_block_type_attributes() { |
|
| 37 | + return array( |
|
| 38 | + 'className' => $this->get_schema_string(), |
|
| 39 | + 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 40 | + 'rows' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_rows', 3 ) ), |
|
| 41 | + 'contentVisibility' => $this->get_schema_content_visibility(), |
|
| 42 | + 'align' => $this->get_schema_align(), |
|
| 43 | + 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 44 | + 'orderby' => $this->get_schema_orderby(), |
|
| 45 | + 'tags' => $this->get_schema_list_ids(), |
|
| 46 | + 'tagOperator' => array( |
|
| 47 | + 'type' => 'string', |
|
| 48 | + 'default' => 'any', |
|
| 49 | + ), |
|
| 50 | + 'isPreview' => $this->get_schema_boolean( false ), |
|
| 51 | + 'stockStatus' => array_keys( wc_get_product_stock_status_options() ), |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Extra data passed through from server to client for block. |
|
| 57 | - * |
|
| 58 | - * @param array $attributes Any attributes that currently are available from the block. |
|
| 59 | - * Note, this will be empty in the editor context when the block is |
|
| 60 | - * not in the post content on editor load. |
|
| 61 | - */ |
|
| 62 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 63 | - parent::enqueue_data( $attributes ); |
|
| 55 | + /** |
|
| 56 | + * Extra data passed through from server to client for block. |
|
| 57 | + * |
|
| 58 | + * @param array $attributes Any attributes that currently are available from the block. |
|
| 59 | + * Note, this will be empty in the editor context when the block is |
|
| 60 | + * not in the post content on editor load. |
|
| 61 | + */ |
|
| 62 | + protected function enqueue_data( array $attributes = [] ) { |
|
| 63 | + parent::enqueue_data( $attributes ); |
|
| 64 | 64 | |
| 65 | - $tag_count = wp_count_terms( 'product_tag' ); |
|
| 65 | + $tag_count = wp_count_terms( 'product_tag' ); |
|
| 66 | 66 | |
| 67 | - $this->asset_data_registry->add( 'hasTags', $tag_count > 0, true ); |
|
| 68 | - $this->asset_data_registry->add( 'limitTags', $tag_count > 100, true ); |
|
| 69 | - } |
|
| 67 | + $this->asset_data_registry->add( 'hasTags', $tag_count > 0, true ); |
|
| 68 | + $this->asset_data_registry->add( 'limitTags', $tag_count > 100, true ); |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -17,13 +17,13 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @param array $query_args Query args. |
| 19 | 19 | */ |
| 20 | - protected function set_block_query_args( &$query_args ) { |
|
| 21 | - if ( ! empty( $this->attributes['tags'] ) ) { |
|
| 20 | + protected function set_block_query_args(&$query_args) { |
|
| 21 | + if (!empty($this->attributes['tags'])) { |
|
| 22 | 22 | $query_args['tax_query'][] = array( |
| 23 | 23 | 'taxonomy' => 'product_tag', |
| 24 | - 'terms' => array_map( 'absint', $this->attributes['tags'] ), |
|
| 24 | + 'terms' => array_map('absint', $this->attributes['tags']), |
|
| 25 | 25 | 'field' => 'term_id', |
| 26 | - 'operator' => isset( $this->attributes['tagOperator'] ) && 'any' === $this->attributes['tagOperator'] ? 'IN' : 'AND', |
|
| 26 | + 'operator' => isset($this->attributes['tagOperator']) && 'any' === $this->attributes['tagOperator'] ? 'IN' : 'AND', |
|
| 27 | 27 | ); |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -36,19 +36,19 @@ discard block |
||
| 36 | 36 | protected function get_block_type_attributes() { |
| 37 | 37 | return array( |
| 38 | 38 | 'className' => $this->get_schema_string(), |
| 39 | - 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 40 | - 'rows' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_rows', 3 ) ), |
|
| 39 | + 'columns' => $this->get_schema_number(wc_get_theme_support('product_blocks::default_columns', 3)), |
|
| 40 | + 'rows' => $this->get_schema_number(wc_get_theme_support('product_blocks::default_rows', 3)), |
|
| 41 | 41 | 'contentVisibility' => $this->get_schema_content_visibility(), |
| 42 | 42 | 'align' => $this->get_schema_align(), |
| 43 | - 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 43 | + 'alignButtons' => $this->get_schema_boolean(false), |
|
| 44 | 44 | 'orderby' => $this->get_schema_orderby(), |
| 45 | 45 | 'tags' => $this->get_schema_list_ids(), |
| 46 | 46 | 'tagOperator' => array( |
| 47 | 47 | 'type' => 'string', |
| 48 | 48 | 'default' => 'any', |
| 49 | 49 | ), |
| 50 | - 'isPreview' => $this->get_schema_boolean( false ), |
|
| 51 | - 'stockStatus' => array_keys( wc_get_product_stock_status_options() ), |
|
| 50 | + 'isPreview' => $this->get_schema_boolean(false), |
|
| 51 | + 'stockStatus' => array_keys(wc_get_product_stock_status_options()), |
|
| 52 | 52 | ); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -59,12 +59,12 @@ discard block |
||
| 59 | 59 | * Note, this will be empty in the editor context when the block is |
| 60 | 60 | * not in the post content on editor load. |
| 61 | 61 | */ |
| 62 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 63 | - parent::enqueue_data( $attributes ); |
|
| 62 | + protected function enqueue_data(array $attributes = []) { |
|
| 63 | + parent::enqueue_data($attributes); |
|
| 64 | 64 | |
| 65 | - $tag_count = wp_count_terms( 'product_tag' ); |
|
| 65 | + $tag_count = wp_count_terms('product_tag'); |
|
| 66 | 66 | |
| 67 | - $this->asset_data_registry->add( 'hasTags', $tag_count > 0, true ); |
|
| 68 | - $this->asset_data_registry->add( 'limitTags', $tag_count > 100, true ); |
|
| 67 | + $this->asset_data_registry->add('hasTags', $tag_count > 0, true); |
|
| 68 | + $this->asset_data_registry->add('limitTags', $tag_count > 100, true); |
|
| 69 | 69 | } |
| 70 | 70 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class PriceFilter extends AbstractBlock { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Block name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $block_name = 'price-filter'; |
|
| 9 | + /** |
|
| 10 | + * Block name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $block_name = 'price-filter'; |
|
| 15 | 15 | } |
@@ -7,100 +7,100 @@ discard block |
||
| 7 | 7 | * @internal |
| 8 | 8 | */ |
| 9 | 9 | class Checkout extends AbstractBlock { |
| 10 | - /** |
|
| 11 | - * Block name. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $block_name = 'checkout'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Chunks build folder. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $chunks_folder = 'checkout-blocks'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Get the editor script handle for this block type. |
|
| 26 | - * |
|
| 27 | - * @param string $key Data to get, or default to everything. |
|
| 28 | - * @return array|string; |
|
| 29 | - */ |
|
| 30 | - protected function get_block_type_editor_script( $key = null ) { |
|
| 31 | - $script = [ |
|
| 32 | - 'handle' => 'wc-' . $this->block_name . '-block', |
|
| 33 | - 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name ), |
|
| 34 | - 'dependencies' => [ 'wc-blocks' ], |
|
| 35 | - ]; |
|
| 36 | - return $key ? $script[ $key ] : $script; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Get the frontend script handle for this block type. |
|
| 41 | - * |
|
| 42 | - * @see $this->register_block_type() |
|
| 43 | - * @param string $key Data to get, or default to everything. |
|
| 44 | - * @return array|string |
|
| 45 | - */ |
|
| 46 | - protected function get_block_type_script( $key = null ) { |
|
| 47 | - $script = [ |
|
| 48 | - 'handle' => 'wc-' . $this->block_name . '-block-frontend', |
|
| 49 | - 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name . '-frontend' ), |
|
| 50 | - 'dependencies' => [], |
|
| 51 | - ]; |
|
| 52 | - return $key ? $script[ $key ] : $script; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Enqueue frontend assets for this block, just in time for rendering. |
|
| 57 | - * |
|
| 58 | - * @param array $attributes Any attributes that currently are available from the block. |
|
| 59 | - */ |
|
| 60 | - protected function enqueue_assets( array $attributes ) { |
|
| 61 | - /** |
|
| 62 | - * Fires before checkout block scripts are enqueued. |
|
| 63 | - */ |
|
| 64 | - do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' ); |
|
| 65 | - parent::enqueue_assets( $attributes ); |
|
| 66 | - /** |
|
| 67 | - * Fires after checkout block scripts are enqueued. |
|
| 68 | - */ |
|
| 69 | - do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Append frontend scripts when rendering the block. |
|
| 74 | - * |
|
| 75 | - * @param array $attributes Block attributes. |
|
| 76 | - * @param string $content Block content. |
|
| 77 | - * @return string Rendered block type output. |
|
| 78 | - */ |
|
| 79 | - protected function render( $attributes, $content ) { |
|
| 80 | - if ( $this->is_checkout_endpoint() ) { |
|
| 81 | - // Note: Currently the block only takes care of the main checkout form -- if an endpoint is set, refer to the |
|
| 82 | - // legacy shortcode instead and do not render block. |
|
| 83 | - return '[woocommerce_checkout]'; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Deregister core checkout scripts and styles. |
|
| 87 | - wp_dequeue_script( 'wc-checkout' ); |
|
| 88 | - wp_dequeue_script( 'wc-password-strength-meter' ); |
|
| 89 | - wp_dequeue_script( 'selectWoo' ); |
|
| 90 | - wp_dequeue_style( 'select2' ); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * We need to check if $content has any templates from prior iterations of the block, in order to update to the latest iteration. |
|
| 94 | - * We test the iteration version by searching for new blocks brought in by it. |
|
| 95 | - * The blocks used for testing should be always available in the block (not removable by the user). |
|
| 96 | - * Checkout i1's content was returning an empty div, with no data-block-name attribute |
|
| 97 | - */ |
|
| 98 | - $regex_for_empty_block = '/<div class="[a-zA-Z0-9_\- ]*wp-block-woocommerce-checkout[a-zA-Z0-9_\- ]*"><\/div>/mi'; |
|
| 99 | - $has_i1_template = preg_match( $regex_for_empty_block, $content ); |
|
| 100 | - |
|
| 101 | - if ( $has_i1_template ) { |
|
| 102 | - // This fallback needs to match the default templates defined in our Blocks. |
|
| 103 | - $inner_blocks_html = ' |
|
| 10 | + /** |
|
| 11 | + * Block name. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $block_name = 'checkout'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Chunks build folder. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $chunks_folder = 'checkout-blocks'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Get the editor script handle for this block type. |
|
| 26 | + * |
|
| 27 | + * @param string $key Data to get, or default to everything. |
|
| 28 | + * @return array|string; |
|
| 29 | + */ |
|
| 30 | + protected function get_block_type_editor_script( $key = null ) { |
|
| 31 | + $script = [ |
|
| 32 | + 'handle' => 'wc-' . $this->block_name . '-block', |
|
| 33 | + 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name ), |
|
| 34 | + 'dependencies' => [ 'wc-blocks' ], |
|
| 35 | + ]; |
|
| 36 | + return $key ? $script[ $key ] : $script; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Get the frontend script handle for this block type. |
|
| 41 | + * |
|
| 42 | + * @see $this->register_block_type() |
|
| 43 | + * @param string $key Data to get, or default to everything. |
|
| 44 | + * @return array|string |
|
| 45 | + */ |
|
| 46 | + protected function get_block_type_script( $key = null ) { |
|
| 47 | + $script = [ |
|
| 48 | + 'handle' => 'wc-' . $this->block_name . '-block-frontend', |
|
| 49 | + 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name . '-frontend' ), |
|
| 50 | + 'dependencies' => [], |
|
| 51 | + ]; |
|
| 52 | + return $key ? $script[ $key ] : $script; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Enqueue frontend assets for this block, just in time for rendering. |
|
| 57 | + * |
|
| 58 | + * @param array $attributes Any attributes that currently are available from the block. |
|
| 59 | + */ |
|
| 60 | + protected function enqueue_assets( array $attributes ) { |
|
| 61 | + /** |
|
| 62 | + * Fires before checkout block scripts are enqueued. |
|
| 63 | + */ |
|
| 64 | + do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' ); |
|
| 65 | + parent::enqueue_assets( $attributes ); |
|
| 66 | + /** |
|
| 67 | + * Fires after checkout block scripts are enqueued. |
|
| 68 | + */ |
|
| 69 | + do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Append frontend scripts when rendering the block. |
|
| 74 | + * |
|
| 75 | + * @param array $attributes Block attributes. |
|
| 76 | + * @param string $content Block content. |
|
| 77 | + * @return string Rendered block type output. |
|
| 78 | + */ |
|
| 79 | + protected function render( $attributes, $content ) { |
|
| 80 | + if ( $this->is_checkout_endpoint() ) { |
|
| 81 | + // Note: Currently the block only takes care of the main checkout form -- if an endpoint is set, refer to the |
|
| 82 | + // legacy shortcode instead and do not render block. |
|
| 83 | + return '[woocommerce_checkout]'; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Deregister core checkout scripts and styles. |
|
| 87 | + wp_dequeue_script( 'wc-checkout' ); |
|
| 88 | + wp_dequeue_script( 'wc-password-strength-meter' ); |
|
| 89 | + wp_dequeue_script( 'selectWoo' ); |
|
| 90 | + wp_dequeue_style( 'select2' ); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * We need to check if $content has any templates from prior iterations of the block, in order to update to the latest iteration. |
|
| 94 | + * We test the iteration version by searching for new blocks brought in by it. |
|
| 95 | + * The blocks used for testing should be always available in the block (not removable by the user). |
|
| 96 | + * Checkout i1's content was returning an empty div, with no data-block-name attribute |
|
| 97 | + */ |
|
| 98 | + $regex_for_empty_block = '/<div class="[a-zA-Z0-9_\- ]*wp-block-woocommerce-checkout[a-zA-Z0-9_\- ]*"><\/div>/mi'; |
|
| 99 | + $has_i1_template = preg_match( $regex_for_empty_block, $content ); |
|
| 100 | + |
|
| 101 | + if ( $has_i1_template ) { |
|
| 102 | + // This fallback needs to match the default templates defined in our Blocks. |
|
| 103 | + $inner_blocks_html = ' |
|
| 104 | 104 | <div data-block-name="woocommerce/checkout-fields-block" class="wp-block-woocommerce-checkout-fields-block"> |
| 105 | 105 | <div data-block-name="woocommerce/checkout-express-payment-block" class="wp-block-woocommerce-checkout-express-payment-block"></div> |
| 106 | 106 | <div data-block-name="woocommerce/checkout-contact-information-block" class="wp-block-woocommerce-checkout-contact-information-block"></div> |
@@ -108,24 +108,24 @@ discard block |
||
| 108 | 108 | <div data-block-name="woocommerce/checkout-billing-address-block" class="wp-block-woocommerce-checkout-billing-address-block"></div> |
| 109 | 109 | <div data-block-name="woocommerce/checkout-shipping-methods-block" class="wp-block-woocommerce-checkout-shipping-methods-block"></div> |
| 110 | 110 | <div data-block-name="woocommerce/checkout-payment-block" class="wp-block-woocommerce-checkout-payment-block"></div>' . |
| 111 | - ( isset( $attributes['showOrderNotes'] ) && false === $attributes['showOrderNotes'] ? '' : '<div data-block-name="woocommerce/checkout-order-note-block" class="wp-block-woocommerce-checkout-order-note-block"></div>' ) . |
|
| 112 | - ( isset( $attributes['showPolicyLinks'] ) && false === $attributes['showPolicyLinks'] ? '' : '<div data-block-name="woocommerce/checkout-terms-block" class="wp-block-woocommerce-checkout-terms-block"></div>' ) . |
|
| 113 | - '<div data-block-name="woocommerce/checkout-actions-block" class="wp-block-woocommerce-checkout-actions-block"></div> |
|
| 111 | + ( isset( $attributes['showOrderNotes'] ) && false === $attributes['showOrderNotes'] ? '' : '<div data-block-name="woocommerce/checkout-order-note-block" class="wp-block-woocommerce-checkout-order-note-block"></div>' ) . |
|
| 112 | + ( isset( $attributes['showPolicyLinks'] ) && false === $attributes['showPolicyLinks'] ? '' : '<div data-block-name="woocommerce/checkout-terms-block" class="wp-block-woocommerce-checkout-terms-block"></div>' ) . |
|
| 113 | + '<div data-block-name="woocommerce/checkout-actions-block" class="wp-block-woocommerce-checkout-actions-block"></div> |
|
| 114 | 114 | </div> |
| 115 | 115 | <div data-block-name="woocommerce/checkout-totals-block" class="wp-block-woocommerce-checkout-totals-block"> |
| 116 | 116 | <div data-block-name="woocommerce/checkout-order-summary-block" class="wp-block-woocommerce-checkout-order-summary-block"></div> |
| 117 | 117 | </div> |
| 118 | 118 | '; |
| 119 | 119 | |
| 120 | - $content = str_replace( '</div>', $inner_blocks_html . '</div>', $content ); |
|
| 121 | - } |
|
| 120 | + $content = str_replace( '</div>', $inner_blocks_html . '</div>', $content ); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Checkout i3 added inner blocks for Order summary. |
|
| 125 | - * We need to add them to Checkout i2 templates. |
|
| 126 | - * The order needs to match the order in which these blocks were registered. |
|
| 127 | - */ |
|
| 128 | - $order_summary_with_inner_blocks = '$0 |
|
| 123 | + /** |
|
| 124 | + * Checkout i3 added inner blocks for Order summary. |
|
| 125 | + * We need to add them to Checkout i2 templates. |
|
| 126 | + * The order needs to match the order in which these blocks were registered. |
|
| 127 | + */ |
|
| 128 | + $order_summary_with_inner_blocks = '$0 |
|
| 129 | 129 | <div data-block-name="woocommerce/checkout-order-summary-cart-items-block" class="wp-block-woocommerce-checkout-order-summary-cart-items-block"></div> |
| 130 | 130 | <div data-block-name="woocommerce/checkout-order-summary-subtotal-block" class="wp-block-woocommerce-checkout-order-summary-subtotal-block"></div> |
| 131 | 131 | <div data-block-name="woocommerce/checkout-order-summary-fee-block" class="wp-block-woocommerce-checkout-order-summary-fee-block"></div> |
@@ -134,281 +134,281 @@ discard block |
||
| 134 | 134 | <div data-block-name="woocommerce/checkout-order-summary-shipping-block" class="wp-block-woocommerce-checkout-order-summary-shipping-block"></div> |
| 135 | 135 | <div data-block-name="woocommerce/checkout-order-summary-taxes-block" class="wp-block-woocommerce-checkout-order-summary-taxes-block"></div> |
| 136 | 136 | '; |
| 137 | - // Order summary subtotal block was added in i3, so we search for it to see if we have a Checkout i2 template. |
|
| 138 | - $regex_for_order_summary_subtotal = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-subtotal-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
|
| 139 | - $regex_for_order_summary = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
|
| 140 | - $has_i2_template = ! preg_match( $regex_for_order_summary_subtotal, $content ); |
|
| 141 | - |
|
| 142 | - if ( $has_i2_template ) { |
|
| 143 | - $content = preg_replace( $regex_for_order_summary, $order_summary_with_inner_blocks, $content ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $content; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Check if we're viewing a checkout page endpoint, rather than the main checkout page itself. |
|
| 151 | - * |
|
| 152 | - * @return boolean |
|
| 153 | - */ |
|
| 154 | - protected function is_checkout_endpoint() { |
|
| 155 | - return is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Extra data passed through from server to client for block. |
|
| 160 | - * |
|
| 161 | - * @param array $attributes Any attributes that currently are available from the block. |
|
| 162 | - * Note, this will be empty in the editor context when the block is |
|
| 163 | - * not in the post content on editor load. |
|
| 164 | - */ |
|
| 165 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 166 | - parent::enqueue_data( $attributes ); |
|
| 167 | - |
|
| 168 | - $this->asset_data_registry->add( |
|
| 169 | - 'allowedCountries', |
|
| 170 | - function() { |
|
| 171 | - return $this->deep_sort_with_accents( WC()->countries->get_allowed_countries() ); |
|
| 172 | - }, |
|
| 173 | - true |
|
| 174 | - ); |
|
| 175 | - $this->asset_data_registry->add( |
|
| 176 | - 'allowedStates', |
|
| 177 | - function() { |
|
| 178 | - return $this->deep_sort_with_accents( WC()->countries->get_allowed_country_states() ); |
|
| 179 | - }, |
|
| 180 | - true |
|
| 181 | - ); |
|
| 182 | - $this->asset_data_registry->add( |
|
| 183 | - 'shippingCountries', |
|
| 184 | - function() { |
|
| 185 | - return $this->deep_sort_with_accents( WC()->countries->get_shipping_countries() ); |
|
| 186 | - }, |
|
| 187 | - true |
|
| 188 | - ); |
|
| 189 | - $this->asset_data_registry->add( |
|
| 190 | - 'shippingStates', |
|
| 191 | - function() { |
|
| 192 | - return $this->deep_sort_with_accents( WC()->countries->get_shipping_country_states() ); |
|
| 193 | - }, |
|
| 194 | - true |
|
| 195 | - ); |
|
| 196 | - $this->asset_data_registry->add( |
|
| 197 | - 'countryLocale', |
|
| 198 | - function() { |
|
| 199 | - // Merge country and state data to work around https://github.com/woocommerce/woocommerce/issues/28944. |
|
| 200 | - $country_locale = wc()->countries->get_country_locale(); |
|
| 201 | - $states = wc()->countries->get_states(); |
|
| 202 | - |
|
| 203 | - foreach ( $states as $country => $states ) { |
|
| 204 | - if ( empty( $states ) ) { |
|
| 205 | - $country_locale[ $country ]['state']['required'] = false; |
|
| 206 | - $country_locale[ $country ]['state']['hidden'] = true; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - return $country_locale; |
|
| 210 | - }, |
|
| 211 | - true |
|
| 212 | - ); |
|
| 213 | - $this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true ); |
|
| 214 | - $this->asset_data_registry->add( |
|
| 215 | - 'checkoutAllowsGuest', |
|
| 216 | - false === filter_var( |
|
| 217 | - wc()->checkout()->is_registration_required(), |
|
| 218 | - FILTER_VALIDATE_BOOLEAN |
|
| 219 | - ), |
|
| 220 | - true |
|
| 221 | - ); |
|
| 222 | - $this->asset_data_registry->add( |
|
| 223 | - 'checkoutAllowsSignup', |
|
| 224 | - filter_var( |
|
| 225 | - wc()->checkout()->is_registration_enabled(), |
|
| 226 | - FILTER_VALIDATE_BOOLEAN |
|
| 227 | - ), |
|
| 228 | - true |
|
| 229 | - ); |
|
| 230 | - $this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true ); |
|
| 231 | - $this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true ); |
|
| 232 | - $this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true ); |
|
| 233 | - $this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true ); |
|
| 234 | - $this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true ); |
|
| 235 | - $this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true ); |
|
| 236 | - $this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true ); |
|
| 237 | - $this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 ); |
|
| 238 | - |
|
| 239 | - $is_block_editor = $this->is_block_editor(); |
|
| 240 | - |
|
| 241 | - // Hydrate the following data depending on admin or frontend context. |
|
| 242 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'shippingMethodsExist' ) ) { |
|
| 243 | - $methods_exist = wc_get_shipping_method_count( false, true ) > 0; |
|
| 244 | - $this->asset_data_registry->add( 'shippingMethodsExist', $methods_exist ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalShippingMethods' ) ) { |
|
| 248 | - $shipping_methods = WC()->shipping()->get_shipping_methods(); |
|
| 249 | - $formatted_shipping_methods = array_reduce( |
|
| 250 | - $shipping_methods, |
|
| 251 | - function( $acc, $method ) { |
|
| 252 | - if ( $method->supports( 'settings' ) ) { |
|
| 253 | - $acc[] = [ |
|
| 254 | - 'id' => $method->id, |
|
| 255 | - 'title' => $method->method_title, |
|
| 256 | - 'description' => $method->method_description, |
|
| 257 | - ]; |
|
| 258 | - } |
|
| 259 | - return $acc; |
|
| 260 | - }, |
|
| 261 | - [] |
|
| 262 | - ); |
|
| 263 | - $this->asset_data_registry->add( 'globalShippingMethods', $formatted_shipping_methods ); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'activeShippingZones' ) && class_exists( '\WC_Shipping_Zones' ) ) { |
|
| 267 | - $shipping_zones = \WC_Shipping_Zones::get_zones(); |
|
| 268 | - $formatted_shipping_zones = array_reduce( |
|
| 269 | - $shipping_zones, |
|
| 270 | - function( $acc, $zone ) { |
|
| 271 | - $acc[] = [ |
|
| 272 | - 'id' => $zone['id'], |
|
| 273 | - 'title' => $zone['zone_name'], |
|
| 274 | - 'description' => $zone['formatted_zone_location'], |
|
| 275 | - ]; |
|
| 276 | - return $acc; |
|
| 277 | - }, |
|
| 278 | - [] |
|
| 279 | - ); |
|
| 280 | - $formatted_shipping_zones[] = [ |
|
| 281 | - 'id' => 0, |
|
| 282 | - 'title' => __( 'International', 'woocommerce' ), |
|
| 283 | - 'description' => __( 'Locations outside all other zones', 'woocommerce' ), |
|
| 284 | - ]; |
|
| 285 | - $this->asset_data_registry->add( 'activeShippingZones', $formatted_shipping_zones ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalPaymentMethods' ) ) { |
|
| 289 | - $payment_methods = WC()->payment_gateways->payment_gateways(); |
|
| 290 | - $formatted_payment_methods = array_reduce( |
|
| 291 | - $payment_methods, |
|
| 292 | - function( $acc, $method ) { |
|
| 293 | - if ( 'yes' === $method->enabled ) { |
|
| 294 | - $acc[] = [ |
|
| 295 | - 'id' => $method->id, |
|
| 296 | - 'title' => $method->method_title, |
|
| 297 | - 'description' => $method->method_description, |
|
| 298 | - ]; |
|
| 299 | - } |
|
| 300 | - return $acc; |
|
| 301 | - }, |
|
| 302 | - [] |
|
| 303 | - ); |
|
| 304 | - $this->asset_data_registry->add( 'globalPaymentMethods', $formatted_payment_methods ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - if ( ! is_admin() && ! WC()->is_rest_api_request() ) { |
|
| 308 | - $this->hydrate_from_api(); |
|
| 309 | - $this->hydrate_customer_payment_methods(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Fires after checkout block data is registered. |
|
| 314 | - */ |
|
| 315 | - do_action( 'woocommerce_blocks_checkout_enqueue_data' ); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Are we currently on the admin block editor screen? |
|
| 320 | - */ |
|
| 321 | - protected function is_block_editor() { |
|
| 322 | - if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - $screen = get_current_screen(); |
|
| 326 | - |
|
| 327 | - return $screen && $screen->is_block_editor(); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Removes accents from an array of values, sorts by the values, then returns the original array values sorted. |
|
| 332 | - * |
|
| 333 | - * @param array $array Array of values to sort. |
|
| 334 | - * @return array Sorted array. |
|
| 335 | - */ |
|
| 336 | - protected function deep_sort_with_accents( $array ) { |
|
| 337 | - if ( ! is_array( $array ) || empty( $array ) ) { |
|
| 338 | - return $array; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - if ( is_array( reset( $array ) ) ) { |
|
| 342 | - return array_map( [ $this, 'deep_sort_with_accents' ], $array ); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - $array_without_accents = array_map( 'remove_accents', array_map( 'wc_strtolower', array_map( 'html_entity_decode', $array ) ) ); |
|
| 346 | - asort( $array_without_accents ); |
|
| 347 | - return array_replace( $array_without_accents, $array ); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Get customer payment methods for use in checkout. |
|
| 352 | - */ |
|
| 353 | - protected function hydrate_customer_payment_methods() { |
|
| 354 | - if ( ! is_user_logged_in() || $this->asset_data_registry->exists( 'customerPaymentMethods' ) ) { |
|
| 355 | - return; |
|
| 356 | - } |
|
| 357 | - add_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 358 | - $this->asset_data_registry->add( |
|
| 359 | - 'customerPaymentMethods', |
|
| 360 | - wc_get_customer_saved_methods_list( get_current_user_id() ) |
|
| 361 | - ); |
|
| 362 | - remove_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Hydrate the checkout block with data from the API. |
|
| 367 | - */ |
|
| 368 | - protected function hydrate_from_api() { |
|
| 369 | - $this->asset_data_registry->hydrate_api_request( '/wc/store/v1/cart' ); |
|
| 370 | - |
|
| 371 | - // Print existing notices now, otherwise they are caught by the Cart |
|
| 372 | - // Controller and converted to exceptions. |
|
| 373 | - wc_print_notices(); |
|
| 374 | - add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 375 | - |
|
| 376 | - $rest_preload_api_requests = rest_preload_api_request( [], '/wc/store/v1/checkout' ); |
|
| 377 | - $this->asset_data_registry->add( 'checkoutData', $rest_preload_api_requests['/wc/store/v1/checkout']['body'] ?? [] ); |
|
| 378 | - |
|
| 379 | - remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Callback for woocommerce_payment_methods_list_item filter to add token id |
|
| 384 | - * to the generated list. |
|
| 385 | - * |
|
| 386 | - * @param array $list_item The current list item for the saved payment method. |
|
| 387 | - * @param \WC_Token $token The token for the current list item. |
|
| 388 | - * |
|
| 389 | - * @return array The list item with the token id added. |
|
| 390 | - */ |
|
| 391 | - public static function include_token_id_with_payment_methods( $list_item, $token ) { |
|
| 392 | - $list_item['tokenId'] = $token->get_id(); |
|
| 393 | - $brand = ! empty( $list_item['method']['brand'] ) ? |
|
| 394 | - strtolower( $list_item['method']['brand'] ) : |
|
| 395 | - ''; |
|
| 396 | - // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- need to match on translated value from core. |
|
| 397 | - if ( ! empty( $brand ) && esc_html__( 'Credit card', 'woocommerce' ) !== $brand ) { |
|
| 398 | - $list_item['method']['brand'] = wc_get_credit_card_type_label( $brand ); |
|
| 399 | - } |
|
| 400 | - return $list_item; |
|
| 401 | - } |
|
| 402 | - /** |
|
| 403 | - * Register script and style assets for the block type before it is registered. |
|
| 404 | - * |
|
| 405 | - * This registers the scripts; it does not enqueue them. |
|
| 406 | - */ |
|
| 407 | - protected function register_block_type_assets() { |
|
| 408 | - parent::register_block_type_assets(); |
|
| 409 | - $chunks = $this->get_chunks_paths( $this->chunks_folder ); |
|
| 410 | - $vendor_chunks = $this->get_chunks_paths( 'vendors--cart-blocks' ); |
|
| 411 | - $shared_chunks = [ 'cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend' ]; |
|
| 412 | - $this->register_chunk_translations( array_merge( $chunks, $vendor_chunks, $shared_chunks ) ); |
|
| 413 | - } |
|
| 137 | + // Order summary subtotal block was added in i3, so we search for it to see if we have a Checkout i2 template. |
|
| 138 | + $regex_for_order_summary_subtotal = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-subtotal-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
|
| 139 | + $regex_for_order_summary = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
|
| 140 | + $has_i2_template = ! preg_match( $regex_for_order_summary_subtotal, $content ); |
|
| 141 | + |
|
| 142 | + if ( $has_i2_template ) { |
|
| 143 | + $content = preg_replace( $regex_for_order_summary, $order_summary_with_inner_blocks, $content ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $content; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Check if we're viewing a checkout page endpoint, rather than the main checkout page itself. |
|
| 151 | + * |
|
| 152 | + * @return boolean |
|
| 153 | + */ |
|
| 154 | + protected function is_checkout_endpoint() { |
|
| 155 | + return is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Extra data passed through from server to client for block. |
|
| 160 | + * |
|
| 161 | + * @param array $attributes Any attributes that currently are available from the block. |
|
| 162 | + * Note, this will be empty in the editor context when the block is |
|
| 163 | + * not in the post content on editor load. |
|
| 164 | + */ |
|
| 165 | + protected function enqueue_data( array $attributes = [] ) { |
|
| 166 | + parent::enqueue_data( $attributes ); |
|
| 167 | + |
|
| 168 | + $this->asset_data_registry->add( |
|
| 169 | + 'allowedCountries', |
|
| 170 | + function() { |
|
| 171 | + return $this->deep_sort_with_accents( WC()->countries->get_allowed_countries() ); |
|
| 172 | + }, |
|
| 173 | + true |
|
| 174 | + ); |
|
| 175 | + $this->asset_data_registry->add( |
|
| 176 | + 'allowedStates', |
|
| 177 | + function() { |
|
| 178 | + return $this->deep_sort_with_accents( WC()->countries->get_allowed_country_states() ); |
|
| 179 | + }, |
|
| 180 | + true |
|
| 181 | + ); |
|
| 182 | + $this->asset_data_registry->add( |
|
| 183 | + 'shippingCountries', |
|
| 184 | + function() { |
|
| 185 | + return $this->deep_sort_with_accents( WC()->countries->get_shipping_countries() ); |
|
| 186 | + }, |
|
| 187 | + true |
|
| 188 | + ); |
|
| 189 | + $this->asset_data_registry->add( |
|
| 190 | + 'shippingStates', |
|
| 191 | + function() { |
|
| 192 | + return $this->deep_sort_with_accents( WC()->countries->get_shipping_country_states() ); |
|
| 193 | + }, |
|
| 194 | + true |
|
| 195 | + ); |
|
| 196 | + $this->asset_data_registry->add( |
|
| 197 | + 'countryLocale', |
|
| 198 | + function() { |
|
| 199 | + // Merge country and state data to work around https://github.com/woocommerce/woocommerce/issues/28944. |
|
| 200 | + $country_locale = wc()->countries->get_country_locale(); |
|
| 201 | + $states = wc()->countries->get_states(); |
|
| 202 | + |
|
| 203 | + foreach ( $states as $country => $states ) { |
|
| 204 | + if ( empty( $states ) ) { |
|
| 205 | + $country_locale[ $country ]['state']['required'] = false; |
|
| 206 | + $country_locale[ $country ]['state']['hidden'] = true; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + return $country_locale; |
|
| 210 | + }, |
|
| 211 | + true |
|
| 212 | + ); |
|
| 213 | + $this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true ); |
|
| 214 | + $this->asset_data_registry->add( |
|
| 215 | + 'checkoutAllowsGuest', |
|
| 216 | + false === filter_var( |
|
| 217 | + wc()->checkout()->is_registration_required(), |
|
| 218 | + FILTER_VALIDATE_BOOLEAN |
|
| 219 | + ), |
|
| 220 | + true |
|
| 221 | + ); |
|
| 222 | + $this->asset_data_registry->add( |
|
| 223 | + 'checkoutAllowsSignup', |
|
| 224 | + filter_var( |
|
| 225 | + wc()->checkout()->is_registration_enabled(), |
|
| 226 | + FILTER_VALIDATE_BOOLEAN |
|
| 227 | + ), |
|
| 228 | + true |
|
| 229 | + ); |
|
| 230 | + $this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true ); |
|
| 231 | + $this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true ); |
|
| 232 | + $this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true ); |
|
| 233 | + $this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true ); |
|
| 234 | + $this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true ); |
|
| 235 | + $this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true ); |
|
| 236 | + $this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true ); |
|
| 237 | + $this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 ); |
|
| 238 | + |
|
| 239 | + $is_block_editor = $this->is_block_editor(); |
|
| 240 | + |
|
| 241 | + // Hydrate the following data depending on admin or frontend context. |
|
| 242 | + if ( $is_block_editor && ! $this->asset_data_registry->exists( 'shippingMethodsExist' ) ) { |
|
| 243 | + $methods_exist = wc_get_shipping_method_count( false, true ) > 0; |
|
| 244 | + $this->asset_data_registry->add( 'shippingMethodsExist', $methods_exist ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalShippingMethods' ) ) { |
|
| 248 | + $shipping_methods = WC()->shipping()->get_shipping_methods(); |
|
| 249 | + $formatted_shipping_methods = array_reduce( |
|
| 250 | + $shipping_methods, |
|
| 251 | + function( $acc, $method ) { |
|
| 252 | + if ( $method->supports( 'settings' ) ) { |
|
| 253 | + $acc[] = [ |
|
| 254 | + 'id' => $method->id, |
|
| 255 | + 'title' => $method->method_title, |
|
| 256 | + 'description' => $method->method_description, |
|
| 257 | + ]; |
|
| 258 | + } |
|
| 259 | + return $acc; |
|
| 260 | + }, |
|
| 261 | + [] |
|
| 262 | + ); |
|
| 263 | + $this->asset_data_registry->add( 'globalShippingMethods', $formatted_shipping_methods ); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + if ( $is_block_editor && ! $this->asset_data_registry->exists( 'activeShippingZones' ) && class_exists( '\WC_Shipping_Zones' ) ) { |
|
| 267 | + $shipping_zones = \WC_Shipping_Zones::get_zones(); |
|
| 268 | + $formatted_shipping_zones = array_reduce( |
|
| 269 | + $shipping_zones, |
|
| 270 | + function( $acc, $zone ) { |
|
| 271 | + $acc[] = [ |
|
| 272 | + 'id' => $zone['id'], |
|
| 273 | + 'title' => $zone['zone_name'], |
|
| 274 | + 'description' => $zone['formatted_zone_location'], |
|
| 275 | + ]; |
|
| 276 | + return $acc; |
|
| 277 | + }, |
|
| 278 | + [] |
|
| 279 | + ); |
|
| 280 | + $formatted_shipping_zones[] = [ |
|
| 281 | + 'id' => 0, |
|
| 282 | + 'title' => __( 'International', 'woocommerce' ), |
|
| 283 | + 'description' => __( 'Locations outside all other zones', 'woocommerce' ), |
|
| 284 | + ]; |
|
| 285 | + $this->asset_data_registry->add( 'activeShippingZones', $formatted_shipping_zones ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalPaymentMethods' ) ) { |
|
| 289 | + $payment_methods = WC()->payment_gateways->payment_gateways(); |
|
| 290 | + $formatted_payment_methods = array_reduce( |
|
| 291 | + $payment_methods, |
|
| 292 | + function( $acc, $method ) { |
|
| 293 | + if ( 'yes' === $method->enabled ) { |
|
| 294 | + $acc[] = [ |
|
| 295 | + 'id' => $method->id, |
|
| 296 | + 'title' => $method->method_title, |
|
| 297 | + 'description' => $method->method_description, |
|
| 298 | + ]; |
|
| 299 | + } |
|
| 300 | + return $acc; |
|
| 301 | + }, |
|
| 302 | + [] |
|
| 303 | + ); |
|
| 304 | + $this->asset_data_registry->add( 'globalPaymentMethods', $formatted_payment_methods ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + if ( ! is_admin() && ! WC()->is_rest_api_request() ) { |
|
| 308 | + $this->hydrate_from_api(); |
|
| 309 | + $this->hydrate_customer_payment_methods(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Fires after checkout block data is registered. |
|
| 314 | + */ |
|
| 315 | + do_action( 'woocommerce_blocks_checkout_enqueue_data' ); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Are we currently on the admin block editor screen? |
|
| 320 | + */ |
|
| 321 | + protected function is_block_editor() { |
|
| 322 | + if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + $screen = get_current_screen(); |
|
| 326 | + |
|
| 327 | + return $screen && $screen->is_block_editor(); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Removes accents from an array of values, sorts by the values, then returns the original array values sorted. |
|
| 332 | + * |
|
| 333 | + * @param array $array Array of values to sort. |
|
| 334 | + * @return array Sorted array. |
|
| 335 | + */ |
|
| 336 | + protected function deep_sort_with_accents( $array ) { |
|
| 337 | + if ( ! is_array( $array ) || empty( $array ) ) { |
|
| 338 | + return $array; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if ( is_array( reset( $array ) ) ) { |
|
| 342 | + return array_map( [ $this, 'deep_sort_with_accents' ], $array ); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + $array_without_accents = array_map( 'remove_accents', array_map( 'wc_strtolower', array_map( 'html_entity_decode', $array ) ) ); |
|
| 346 | + asort( $array_without_accents ); |
|
| 347 | + return array_replace( $array_without_accents, $array ); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Get customer payment methods for use in checkout. |
|
| 352 | + */ |
|
| 353 | + protected function hydrate_customer_payment_methods() { |
|
| 354 | + if ( ! is_user_logged_in() || $this->asset_data_registry->exists( 'customerPaymentMethods' ) ) { |
|
| 355 | + return; |
|
| 356 | + } |
|
| 357 | + add_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 358 | + $this->asset_data_registry->add( |
|
| 359 | + 'customerPaymentMethods', |
|
| 360 | + wc_get_customer_saved_methods_list( get_current_user_id() ) |
|
| 361 | + ); |
|
| 362 | + remove_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Hydrate the checkout block with data from the API. |
|
| 367 | + */ |
|
| 368 | + protected function hydrate_from_api() { |
|
| 369 | + $this->asset_data_registry->hydrate_api_request( '/wc/store/v1/cart' ); |
|
| 370 | + |
|
| 371 | + // Print existing notices now, otherwise they are caught by the Cart |
|
| 372 | + // Controller and converted to exceptions. |
|
| 373 | + wc_print_notices(); |
|
| 374 | + add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 375 | + |
|
| 376 | + $rest_preload_api_requests = rest_preload_api_request( [], '/wc/store/v1/checkout' ); |
|
| 377 | + $this->asset_data_registry->add( 'checkoutData', $rest_preload_api_requests['/wc/store/v1/checkout']['body'] ?? [] ); |
|
| 378 | + |
|
| 379 | + remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Callback for woocommerce_payment_methods_list_item filter to add token id |
|
| 384 | + * to the generated list. |
|
| 385 | + * |
|
| 386 | + * @param array $list_item The current list item for the saved payment method. |
|
| 387 | + * @param \WC_Token $token The token for the current list item. |
|
| 388 | + * |
|
| 389 | + * @return array The list item with the token id added. |
|
| 390 | + */ |
|
| 391 | + public static function include_token_id_with_payment_methods( $list_item, $token ) { |
|
| 392 | + $list_item['tokenId'] = $token->get_id(); |
|
| 393 | + $brand = ! empty( $list_item['method']['brand'] ) ? |
|
| 394 | + strtolower( $list_item['method']['brand'] ) : |
|
| 395 | + ''; |
|
| 396 | + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- need to match on translated value from core. |
|
| 397 | + if ( ! empty( $brand ) && esc_html__( 'Credit card', 'woocommerce' ) !== $brand ) { |
|
| 398 | + $list_item['method']['brand'] = wc_get_credit_card_type_label( $brand ); |
|
| 399 | + } |
|
| 400 | + return $list_item; |
|
| 401 | + } |
|
| 402 | + /** |
|
| 403 | + * Register script and style assets for the block type before it is registered. |
|
| 404 | + * |
|
| 405 | + * This registers the scripts; it does not enqueue them. |
|
| 406 | + */ |
|
| 407 | + protected function register_block_type_assets() { |
|
| 408 | + parent::register_block_type_assets(); |
|
| 409 | + $chunks = $this->get_chunks_paths( $this->chunks_folder ); |
|
| 410 | + $vendor_chunks = $this->get_chunks_paths( 'vendors--cart-blocks' ); |
|
| 411 | + $shared_chunks = [ 'cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend' ]; |
|
| 412 | + $this->register_chunk_translations( array_merge( $chunks, $vendor_chunks, $shared_chunks ) ); |
|
| 413 | + } |
|
| 414 | 414 | } |
@@ -27,13 +27,13 @@ discard block |
||
| 27 | 27 | * @param string $key Data to get, or default to everything. |
| 28 | 28 | * @return array|string; |
| 29 | 29 | */ |
| 30 | - protected function get_block_type_editor_script( $key = null ) { |
|
| 30 | + protected function get_block_type_editor_script($key = null) { |
|
| 31 | 31 | $script = [ |
| 32 | 32 | 'handle' => 'wc-' . $this->block_name . '-block', |
| 33 | - 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name ), |
|
| 34 | - 'dependencies' => [ 'wc-blocks' ], |
|
| 33 | + 'path' => $this->asset_api->get_block_asset_build_path($this->block_name), |
|
| 34 | + 'dependencies' => ['wc-blocks'], |
|
| 35 | 35 | ]; |
| 36 | - return $key ? $script[ $key ] : $script; |
|
| 36 | + return $key ? $script[$key] : $script; |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
@@ -43,13 +43,13 @@ discard block |
||
| 43 | 43 | * @param string $key Data to get, or default to everything. |
| 44 | 44 | * @return array|string |
| 45 | 45 | */ |
| 46 | - protected function get_block_type_script( $key = null ) { |
|
| 46 | + protected function get_block_type_script($key = null) { |
|
| 47 | 47 | $script = [ |
| 48 | 48 | 'handle' => 'wc-' . $this->block_name . '-block-frontend', |
| 49 | - 'path' => $this->asset_api->get_block_asset_build_path( $this->block_name . '-frontend' ), |
|
| 49 | + 'path' => $this->asset_api->get_block_asset_build_path($this->block_name . '-frontend'), |
|
| 50 | 50 | 'dependencies' => [], |
| 51 | 51 | ]; |
| 52 | - return $key ? $script[ $key ] : $script; |
|
| 52 | + return $key ? $script[$key] : $script; |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -57,16 +57,16 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @param array $attributes Any attributes that currently are available from the block. |
| 59 | 59 | */ |
| 60 | - protected function enqueue_assets( array $attributes ) { |
|
| 60 | + protected function enqueue_assets(array $attributes) { |
|
| 61 | 61 | /** |
| 62 | 62 | * Fires before checkout block scripts are enqueued. |
| 63 | 63 | */ |
| 64 | - do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' ); |
|
| 65 | - parent::enqueue_assets( $attributes ); |
|
| 64 | + do_action('woocommerce_blocks_enqueue_checkout_block_scripts_before'); |
|
| 65 | + parent::enqueue_assets($attributes); |
|
| 66 | 66 | /** |
| 67 | 67 | * Fires after checkout block scripts are enqueued. |
| 68 | 68 | */ |
| 69 | - do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' ); |
|
| 69 | + do_action('woocommerce_blocks_enqueue_checkout_block_scripts_after'); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -76,18 +76,18 @@ discard block |
||
| 76 | 76 | * @param string $content Block content. |
| 77 | 77 | * @return string Rendered block type output. |
| 78 | 78 | */ |
| 79 | - protected function render( $attributes, $content ) { |
|
| 80 | - if ( $this->is_checkout_endpoint() ) { |
|
| 79 | + protected function render($attributes, $content) { |
|
| 80 | + if ($this->is_checkout_endpoint()) { |
|
| 81 | 81 | // Note: Currently the block only takes care of the main checkout form -- if an endpoint is set, refer to the |
| 82 | 82 | // legacy shortcode instead and do not render block. |
| 83 | 83 | return '[woocommerce_checkout]'; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | // Deregister core checkout scripts and styles. |
| 87 | - wp_dequeue_script( 'wc-checkout' ); |
|
| 88 | - wp_dequeue_script( 'wc-password-strength-meter' ); |
|
| 89 | - wp_dequeue_script( 'selectWoo' ); |
|
| 90 | - wp_dequeue_style( 'select2' ); |
|
| 87 | + wp_dequeue_script('wc-checkout'); |
|
| 88 | + wp_dequeue_script('wc-password-strength-meter'); |
|
| 89 | + wp_dequeue_script('selectWoo'); |
|
| 90 | + wp_dequeue_style('select2'); |
|
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * We need to check if $content has any templates from prior iterations of the block, in order to update to the latest iteration. |
@@ -96,9 +96,9 @@ discard block |
||
| 96 | 96 | * Checkout i1's content was returning an empty div, with no data-block-name attribute |
| 97 | 97 | */ |
| 98 | 98 | $regex_for_empty_block = '/<div class="[a-zA-Z0-9_\- ]*wp-block-woocommerce-checkout[a-zA-Z0-9_\- ]*"><\/div>/mi'; |
| 99 | - $has_i1_template = preg_match( $regex_for_empty_block, $content ); |
|
| 99 | + $has_i1_template = preg_match($regex_for_empty_block, $content); |
|
| 100 | 100 | |
| 101 | - if ( $has_i1_template ) { |
|
| 101 | + if ($has_i1_template) { |
|
| 102 | 102 | // This fallback needs to match the default templates defined in our Blocks. |
| 103 | 103 | $inner_blocks_html = ' |
| 104 | 104 | <div data-block-name="woocommerce/checkout-fields-block" class="wp-block-woocommerce-checkout-fields-block"> |
@@ -108,8 +108,8 @@ discard block |
||
| 108 | 108 | <div data-block-name="woocommerce/checkout-billing-address-block" class="wp-block-woocommerce-checkout-billing-address-block"></div> |
| 109 | 109 | <div data-block-name="woocommerce/checkout-shipping-methods-block" class="wp-block-woocommerce-checkout-shipping-methods-block"></div> |
| 110 | 110 | <div data-block-name="woocommerce/checkout-payment-block" class="wp-block-woocommerce-checkout-payment-block"></div>' . |
| 111 | - ( isset( $attributes['showOrderNotes'] ) && false === $attributes['showOrderNotes'] ? '' : '<div data-block-name="woocommerce/checkout-order-note-block" class="wp-block-woocommerce-checkout-order-note-block"></div>' ) . |
|
| 112 | - ( isset( $attributes['showPolicyLinks'] ) && false === $attributes['showPolicyLinks'] ? '' : '<div data-block-name="woocommerce/checkout-terms-block" class="wp-block-woocommerce-checkout-terms-block"></div>' ) . |
|
| 111 | + (isset($attributes['showOrderNotes']) && false === $attributes['showOrderNotes'] ? '' : '<div data-block-name="woocommerce/checkout-order-note-block" class="wp-block-woocommerce-checkout-order-note-block"></div>') . |
|
| 112 | + (isset($attributes['showPolicyLinks']) && false === $attributes['showPolicyLinks'] ? '' : '<div data-block-name="woocommerce/checkout-terms-block" class="wp-block-woocommerce-checkout-terms-block"></div>') . |
|
| 113 | 113 | '<div data-block-name="woocommerce/checkout-actions-block" class="wp-block-woocommerce-checkout-actions-block"></div> |
| 114 | 114 | </div> |
| 115 | 115 | <div data-block-name="woocommerce/checkout-totals-block" class="wp-block-woocommerce-checkout-totals-block"> |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | </div> |
| 118 | 118 | '; |
| 119 | 119 | |
| 120 | - $content = str_replace( '</div>', $inner_blocks_html . '</div>', $content ); |
|
| 120 | + $content = str_replace('</div>', $inner_blocks_html . '</div>', $content); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -137,10 +137,10 @@ discard block |
||
| 137 | 137 | // Order summary subtotal block was added in i3, so we search for it to see if we have a Checkout i2 template. |
| 138 | 138 | $regex_for_order_summary_subtotal = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-subtotal-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
| 139 | 139 | $regex_for_order_summary = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-order-summary-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi'; |
| 140 | - $has_i2_template = ! preg_match( $regex_for_order_summary_subtotal, $content ); |
|
| 140 | + $has_i2_template = !preg_match($regex_for_order_summary_subtotal, $content); |
|
| 141 | 141 | |
| 142 | - if ( $has_i2_template ) { |
|
| 143 | - $content = preg_replace( $regex_for_order_summary, $order_summary_with_inner_blocks, $content ); |
|
| 142 | + if ($has_i2_template) { |
|
| 143 | + $content = preg_replace($regex_for_order_summary, $order_summary_with_inner_blocks, $content); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | return $content; |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | * @return boolean |
| 153 | 153 | */ |
| 154 | 154 | protected function is_checkout_endpoint() { |
| 155 | - return is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' ); |
|
| 155 | + return is_wc_endpoint_url('order-pay') || is_wc_endpoint_url('order-received'); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | /** |
@@ -162,34 +162,34 @@ discard block |
||
| 162 | 162 | * Note, this will be empty in the editor context when the block is |
| 163 | 163 | * not in the post content on editor load. |
| 164 | 164 | */ |
| 165 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 166 | - parent::enqueue_data( $attributes ); |
|
| 165 | + protected function enqueue_data(array $attributes = []) { |
|
| 166 | + parent::enqueue_data($attributes); |
|
| 167 | 167 | |
| 168 | 168 | $this->asset_data_registry->add( |
| 169 | 169 | 'allowedCountries', |
| 170 | 170 | function() { |
| 171 | - return $this->deep_sort_with_accents( WC()->countries->get_allowed_countries() ); |
|
| 171 | + return $this->deep_sort_with_accents(WC()->countries->get_allowed_countries()); |
|
| 172 | 172 | }, |
| 173 | 173 | true |
| 174 | 174 | ); |
| 175 | 175 | $this->asset_data_registry->add( |
| 176 | 176 | 'allowedStates', |
| 177 | 177 | function() { |
| 178 | - return $this->deep_sort_with_accents( WC()->countries->get_allowed_country_states() ); |
|
| 178 | + return $this->deep_sort_with_accents(WC()->countries->get_allowed_country_states()); |
|
| 179 | 179 | }, |
| 180 | 180 | true |
| 181 | 181 | ); |
| 182 | 182 | $this->asset_data_registry->add( |
| 183 | 183 | 'shippingCountries', |
| 184 | 184 | function() { |
| 185 | - return $this->deep_sort_with_accents( WC()->countries->get_shipping_countries() ); |
|
| 185 | + return $this->deep_sort_with_accents(WC()->countries->get_shipping_countries()); |
|
| 186 | 186 | }, |
| 187 | 187 | true |
| 188 | 188 | ); |
| 189 | 189 | $this->asset_data_registry->add( |
| 190 | 190 | 'shippingStates', |
| 191 | 191 | function() { |
| 192 | - return $this->deep_sort_with_accents( WC()->countries->get_shipping_country_states() ); |
|
| 192 | + return $this->deep_sort_with_accents(WC()->countries->get_shipping_country_states()); |
|
| 193 | 193 | }, |
| 194 | 194 | true |
| 195 | 195 | ); |
@@ -200,17 +200,17 @@ discard block |
||
| 200 | 200 | $country_locale = wc()->countries->get_country_locale(); |
| 201 | 201 | $states = wc()->countries->get_states(); |
| 202 | 202 | |
| 203 | - foreach ( $states as $country => $states ) { |
|
| 204 | - if ( empty( $states ) ) { |
|
| 205 | - $country_locale[ $country ]['state']['required'] = false; |
|
| 206 | - $country_locale[ $country ]['state']['hidden'] = true; |
|
| 203 | + foreach ($states as $country => $states) { |
|
| 204 | + if (empty($states)) { |
|
| 205 | + $country_locale[$country]['state']['required'] = false; |
|
| 206 | + $country_locale[$country]['state']['hidden'] = true; |
|
| 207 | 207 | } |
| 208 | 208 | } |
| 209 | 209 | return $country_locale; |
| 210 | 210 | }, |
| 211 | 211 | true |
| 212 | 212 | ); |
| 213 | - $this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true ); |
|
| 213 | + $this->asset_data_registry->add('baseLocation', wc_get_base_location(), true); |
|
| 214 | 214 | $this->asset_data_registry->add( |
| 215 | 215 | 'checkoutAllowsGuest', |
| 216 | 216 | false === filter_var( |
@@ -227,29 +227,29 @@ discard block |
||
| 227 | 227 | ), |
| 228 | 228 | true |
| 229 | 229 | ); |
| 230 | - $this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true ); |
|
| 231 | - $this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true ); |
|
| 232 | - $this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true ); |
|
| 233 | - $this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true ); |
|
| 234 | - $this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true ); |
|
| 235 | - $this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true ); |
|
| 236 | - $this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true ); |
|
| 237 | - $this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 ); |
|
| 230 | + $this->asset_data_registry->add('checkoutShowLoginReminder', filter_var(get_option('woocommerce_enable_checkout_login_reminder'), FILTER_VALIDATE_BOOLEAN), true); |
|
| 231 | + $this->asset_data_registry->add('displayCartPricesIncludingTax', 'incl' === get_option('woocommerce_tax_display_cart'), true); |
|
| 232 | + $this->asset_data_registry->add('displayItemizedTaxes', 'itemized' === get_option('woocommerce_tax_total_display'), true); |
|
| 233 | + $this->asset_data_registry->add('taxesEnabled', wc_tax_enabled(), true); |
|
| 234 | + $this->asset_data_registry->add('couponsEnabled', wc_coupons_enabled(), true); |
|
| 235 | + $this->asset_data_registry->add('shippingEnabled', wc_shipping_enabled(), true); |
|
| 236 | + $this->asset_data_registry->add('hasDarkEditorStyleSupport', current_theme_supports('dark-editor-style'), true); |
|
| 237 | + $this->asset_data_registry->register_page_id(isset($attributes['cartPageId']) ? $attributes['cartPageId'] : 0); |
|
| 238 | 238 | |
| 239 | 239 | $is_block_editor = $this->is_block_editor(); |
| 240 | 240 | |
| 241 | 241 | // Hydrate the following data depending on admin or frontend context. |
| 242 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'shippingMethodsExist' ) ) { |
|
| 243 | - $methods_exist = wc_get_shipping_method_count( false, true ) > 0; |
|
| 244 | - $this->asset_data_registry->add( 'shippingMethodsExist', $methods_exist ); |
|
| 242 | + if ($is_block_editor && !$this->asset_data_registry->exists('shippingMethodsExist')) { |
|
| 243 | + $methods_exist = wc_get_shipping_method_count(false, true) > 0; |
|
| 244 | + $this->asset_data_registry->add('shippingMethodsExist', $methods_exist); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalShippingMethods' ) ) { |
|
| 247 | + if ($is_block_editor && !$this->asset_data_registry->exists('globalShippingMethods')) { |
|
| 248 | 248 | $shipping_methods = WC()->shipping()->get_shipping_methods(); |
| 249 | 249 | $formatted_shipping_methods = array_reduce( |
| 250 | 250 | $shipping_methods, |
| 251 | - function( $acc, $method ) { |
|
| 252 | - if ( $method->supports( 'settings' ) ) { |
|
| 251 | + function($acc, $method) { |
|
| 252 | + if ($method->supports('settings')) { |
|
| 253 | 253 | $acc[] = [ |
| 254 | 254 | 'id' => $method->id, |
| 255 | 255 | 'title' => $method->method_title, |
@@ -260,14 +260,14 @@ discard block |
||
| 260 | 260 | }, |
| 261 | 261 | [] |
| 262 | 262 | ); |
| 263 | - $this->asset_data_registry->add( 'globalShippingMethods', $formatted_shipping_methods ); |
|
| 263 | + $this->asset_data_registry->add('globalShippingMethods', $formatted_shipping_methods); |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'activeShippingZones' ) && class_exists( '\WC_Shipping_Zones' ) ) { |
|
| 266 | + if ($is_block_editor && !$this->asset_data_registry->exists('activeShippingZones') && class_exists('\WC_Shipping_Zones')) { |
|
| 267 | 267 | $shipping_zones = \WC_Shipping_Zones::get_zones(); |
| 268 | 268 | $formatted_shipping_zones = array_reduce( |
| 269 | 269 | $shipping_zones, |
| 270 | - function( $acc, $zone ) { |
|
| 270 | + function($acc, $zone) { |
|
| 271 | 271 | $acc[] = [ |
| 272 | 272 | 'id' => $zone['id'], |
| 273 | 273 | 'title' => $zone['zone_name'], |
@@ -279,18 +279,18 @@ discard block |
||
| 279 | 279 | ); |
| 280 | 280 | $formatted_shipping_zones[] = [ |
| 281 | 281 | 'id' => 0, |
| 282 | - 'title' => __( 'International', 'woocommerce' ), |
|
| 283 | - 'description' => __( 'Locations outside all other zones', 'woocommerce' ), |
|
| 282 | + 'title' => __('International', 'woocommerce'), |
|
| 283 | + 'description' => __('Locations outside all other zones', 'woocommerce'), |
|
| 284 | 284 | ]; |
| 285 | - $this->asset_data_registry->add( 'activeShippingZones', $formatted_shipping_zones ); |
|
| 285 | + $this->asset_data_registry->add('activeShippingZones', $formatted_shipping_zones); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalPaymentMethods' ) ) { |
|
| 288 | + if ($is_block_editor && !$this->asset_data_registry->exists('globalPaymentMethods')) { |
|
| 289 | 289 | $payment_methods = WC()->payment_gateways->payment_gateways(); |
| 290 | 290 | $formatted_payment_methods = array_reduce( |
| 291 | 291 | $payment_methods, |
| 292 | - function( $acc, $method ) { |
|
| 293 | - if ( 'yes' === $method->enabled ) { |
|
| 292 | + function($acc, $method) { |
|
| 293 | + if ('yes' === $method->enabled) { |
|
| 294 | 294 | $acc[] = [ |
| 295 | 295 | 'id' => $method->id, |
| 296 | 296 | 'title' => $method->method_title, |
@@ -301,10 +301,10 @@ discard block |
||
| 301 | 301 | }, |
| 302 | 302 | [] |
| 303 | 303 | ); |
| 304 | - $this->asset_data_registry->add( 'globalPaymentMethods', $formatted_payment_methods ); |
|
| 304 | + $this->asset_data_registry->add('globalPaymentMethods', $formatted_payment_methods); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - if ( ! is_admin() && ! WC()->is_rest_api_request() ) { |
|
| 307 | + if (!is_admin() && !WC()->is_rest_api_request()) { |
|
| 308 | 308 | $this->hydrate_from_api(); |
| 309 | 309 | $this->hydrate_customer_payment_methods(); |
| 310 | 310 | } |
@@ -312,14 +312,14 @@ discard block |
||
| 312 | 312 | /** |
| 313 | 313 | * Fires after checkout block data is registered. |
| 314 | 314 | */ |
| 315 | - do_action( 'woocommerce_blocks_checkout_enqueue_data' ); |
|
| 315 | + do_action('woocommerce_blocks_checkout_enqueue_data'); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | /** |
| 319 | 319 | * Are we currently on the admin block editor screen? |
| 320 | 320 | */ |
| 321 | 321 | protected function is_block_editor() { |
| 322 | - if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { |
|
| 322 | + if (!is_admin() || !function_exists('get_current_screen')) { |
|
| 323 | 323 | return false; |
| 324 | 324 | } |
| 325 | 325 | $screen = get_current_screen(); |
@@ -333,50 +333,50 @@ discard block |
||
| 333 | 333 | * @param array $array Array of values to sort. |
| 334 | 334 | * @return array Sorted array. |
| 335 | 335 | */ |
| 336 | - protected function deep_sort_with_accents( $array ) { |
|
| 337 | - if ( ! is_array( $array ) || empty( $array ) ) { |
|
| 336 | + protected function deep_sort_with_accents($array) { |
|
| 337 | + if (!is_array($array) || empty($array)) { |
|
| 338 | 338 | return $array; |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - if ( is_array( reset( $array ) ) ) { |
|
| 342 | - return array_map( [ $this, 'deep_sort_with_accents' ], $array ); |
|
| 341 | + if (is_array(reset($array))) { |
|
| 342 | + return array_map([$this, 'deep_sort_with_accents'], $array); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | - $array_without_accents = array_map( 'remove_accents', array_map( 'wc_strtolower', array_map( 'html_entity_decode', $array ) ) ); |
|
| 346 | - asort( $array_without_accents ); |
|
| 347 | - return array_replace( $array_without_accents, $array ); |
|
| 345 | + $array_without_accents = array_map('remove_accents', array_map('wc_strtolower', array_map('html_entity_decode', $array))); |
|
| 346 | + asort($array_without_accents); |
|
| 347 | + return array_replace($array_without_accents, $array); |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | /** |
| 351 | 351 | * Get customer payment methods for use in checkout. |
| 352 | 352 | */ |
| 353 | 353 | protected function hydrate_customer_payment_methods() { |
| 354 | - if ( ! is_user_logged_in() || $this->asset_data_registry->exists( 'customerPaymentMethods' ) ) { |
|
| 354 | + if (!is_user_logged_in() || $this->asset_data_registry->exists('customerPaymentMethods')) { |
|
| 355 | 355 | return; |
| 356 | 356 | } |
| 357 | - add_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 357 | + add_filter('woocommerce_payment_methods_list_item', [$this, 'include_token_id_with_payment_methods'], 10, 2); |
|
| 358 | 358 | $this->asset_data_registry->add( |
| 359 | 359 | 'customerPaymentMethods', |
| 360 | - wc_get_customer_saved_methods_list( get_current_user_id() ) |
|
| 360 | + wc_get_customer_saved_methods_list(get_current_user_id()) |
|
| 361 | 361 | ); |
| 362 | - remove_filter( 'woocommerce_payment_methods_list_item', [ $this, 'include_token_id_with_payment_methods' ], 10, 2 ); |
|
| 362 | + remove_filter('woocommerce_payment_methods_list_item', [$this, 'include_token_id_with_payment_methods'], 10, 2); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
| 366 | 366 | * Hydrate the checkout block with data from the API. |
| 367 | 367 | */ |
| 368 | 368 | protected function hydrate_from_api() { |
| 369 | - $this->asset_data_registry->hydrate_api_request( '/wc/store/v1/cart' ); |
|
| 369 | + $this->asset_data_registry->hydrate_api_request('/wc/store/v1/cart'); |
|
| 370 | 370 | |
| 371 | 371 | // Print existing notices now, otherwise they are caught by the Cart |
| 372 | 372 | // Controller and converted to exceptions. |
| 373 | 373 | wc_print_notices(); |
| 374 | - add_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 374 | + add_filter('woocommerce_store_api_disable_nonce_check', '__return_true'); |
|
| 375 | 375 | |
| 376 | - $rest_preload_api_requests = rest_preload_api_request( [], '/wc/store/v1/checkout' ); |
|
| 377 | - $this->asset_data_registry->add( 'checkoutData', $rest_preload_api_requests['/wc/store/v1/checkout']['body'] ?? [] ); |
|
| 376 | + $rest_preload_api_requests = rest_preload_api_request([], '/wc/store/v1/checkout'); |
|
| 377 | + $this->asset_data_registry->add('checkoutData', $rest_preload_api_requests['/wc/store/v1/checkout']['body'] ?? []); |
|
| 378 | 378 | |
| 379 | - remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); |
|
| 379 | + remove_filter('woocommerce_store_api_disable_nonce_check', '__return_true'); |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | /** |
@@ -388,14 +388,13 @@ discard block |
||
| 388 | 388 | * |
| 389 | 389 | * @return array The list item with the token id added. |
| 390 | 390 | */ |
| 391 | - public static function include_token_id_with_payment_methods( $list_item, $token ) { |
|
| 391 | + public static function include_token_id_with_payment_methods($list_item, $token) { |
|
| 392 | 392 | $list_item['tokenId'] = $token->get_id(); |
| 393 | - $brand = ! empty( $list_item['method']['brand'] ) ? |
|
| 394 | - strtolower( $list_item['method']['brand'] ) : |
|
| 395 | - ''; |
|
| 393 | + $brand = !empty($list_item['method']['brand']) ? |
|
| 394 | + strtolower($list_item['method']['brand']) : ''; |
|
| 396 | 395 | // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- need to match on translated value from core. |
| 397 | - if ( ! empty( $brand ) && esc_html__( 'Credit card', 'woocommerce' ) !== $brand ) { |
|
| 398 | - $list_item['method']['brand'] = wc_get_credit_card_type_label( $brand ); |
|
| 396 | + if (!empty($brand) && esc_html__('Credit card', 'woocommerce') !== $brand) { |
|
| 397 | + $list_item['method']['brand'] = wc_get_credit_card_type_label($brand); |
|
| 399 | 398 | } |
| 400 | 399 | return $list_item; |
| 401 | 400 | } |
@@ -406,9 +405,9 @@ discard block |
||
| 406 | 405 | */ |
| 407 | 406 | protected function register_block_type_assets() { |
| 408 | 407 | parent::register_block_type_assets(); |
| 409 | - $chunks = $this->get_chunks_paths( $this->chunks_folder ); |
|
| 410 | - $vendor_chunks = $this->get_chunks_paths( 'vendors--cart-blocks' ); |
|
| 411 | - $shared_chunks = [ 'cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend' ]; |
|
| 412 | - $this->register_chunk_translations( array_merge( $chunks, $vendor_chunks, $shared_chunks ) ); |
|
| 408 | + $chunks = $this->get_chunks_paths($this->chunks_folder); |
|
| 409 | + $vendor_chunks = $this->get_chunks_paths('vendors--cart-blocks'); |
|
| 410 | + $shared_chunks = ['cart-blocks/order-summary-shipping--checkout-blocks/order-summary-shipping-frontend']; |
|
| 411 | + $this->register_chunk_translations(array_merge($chunks, $vendor_chunks, $shared_chunks)); |
|
| 413 | 412 | } |
| 414 | 413 | } |
@@ -7,41 +7,41 @@ |
||
| 7 | 7 | * @internal |
| 8 | 8 | */ |
| 9 | 9 | class AtomicBlock extends AbstractBlock { |
| 10 | - /** |
|
| 11 | - * Get the editor script data for this block type. |
|
| 12 | - * |
|
| 13 | - * @param string $key Data to get, or default to everything. |
|
| 14 | - * @return null |
|
| 15 | - */ |
|
| 16 | - protected function get_block_type_editor_script( $key = null ) { |
|
| 17 | - return null; |
|
| 18 | - } |
|
| 10 | + /** |
|
| 11 | + * Get the editor script data for this block type. |
|
| 12 | + * |
|
| 13 | + * @param string $key Data to get, or default to everything. |
|
| 14 | + * @return null |
|
| 15 | + */ |
|
| 16 | + protected function get_block_type_editor_script( $key = null ) { |
|
| 17 | + return null; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Get the editor style handle for this block type. |
|
| 22 | - * |
|
| 23 | - * @return null |
|
| 24 | - */ |
|
| 25 | - protected function get_block_type_editor_style() { |
|
| 26 | - return null; |
|
| 27 | - } |
|
| 20 | + /** |
|
| 21 | + * Get the editor style handle for this block type. |
|
| 22 | + * |
|
| 23 | + * @return null |
|
| 24 | + */ |
|
| 25 | + protected function get_block_type_editor_style() { |
|
| 26 | + return null; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Get the frontend script handle for this block type. |
|
| 31 | - * |
|
| 32 | - * @param string $key Data to get, or default to everything. |
|
| 33 | - * @return null |
|
| 34 | - */ |
|
| 35 | - protected function get_block_type_script( $key = null ) { |
|
| 36 | - return null; |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Get the frontend script handle for this block type. |
|
| 31 | + * |
|
| 32 | + * @param string $key Data to get, or default to everything. |
|
| 33 | + * @return null |
|
| 34 | + */ |
|
| 35 | + protected function get_block_type_script( $key = null ) { |
|
| 36 | + return null; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get the frontend style handle for this block type. |
|
| 41 | - * |
|
| 42 | - * @return null |
|
| 43 | - */ |
|
| 44 | - protected function get_block_type_style() { |
|
| 45 | - return null; |
|
| 46 | - } |
|
| 39 | + /** |
|
| 40 | + * Get the frontend style handle for this block type. |
|
| 41 | + * |
|
| 42 | + * @return null |
|
| 43 | + */ |
|
| 44 | + protected function get_block_type_style() { |
|
| 45 | + return null; |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @param string $key Data to get, or default to everything. |
| 14 | 14 | * @return null |
| 15 | 15 | */ |
| 16 | - protected function get_block_type_editor_script( $key = null ) { |
|
| 16 | + protected function get_block_type_editor_script($key = null) { |
|
| 17 | 17 | return null; |
| 18 | 18 | } |
| 19 | 19 | |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * @param string $key Data to get, or default to everything. |
| 33 | 33 | * @return null |
| 34 | 34 | */ |
| 35 | - protected function get_block_type_script( $key = null ) { |
|
| 35 | + protected function get_block_type_script($key = null) { |
|
| 36 | 36 | return null; |
| 37 | 37 | } |
| 38 | 38 | |
@@ -9,215 +9,215 @@ discard block |
||
| 9 | 9 | class ProductCategories extends AbstractDynamicBlock { |
| 10 | 10 | |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Block name. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - protected $block_name = 'product-categories'; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Default attribute values, should match what's set in JS `registerBlockType`. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $defaults = array( |
|
| 25 | - 'hasCount' => true, |
|
| 26 | - 'hasImage' => false, |
|
| 27 | - 'hasEmpty' => false, |
|
| 28 | - 'isDropdown' => false, |
|
| 29 | - 'isHierarchical' => true, |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Get block attributes. |
|
| 34 | - * |
|
| 35 | - * @return array |
|
| 36 | - */ |
|
| 37 | - protected function get_block_type_attributes() { |
|
| 38 | - return array_merge( |
|
| 39 | - parent::get_block_type_attributes(), |
|
| 40 | - array( |
|
| 41 | - 'align' => $this->get_schema_align(), |
|
| 42 | - 'className' => $this->get_schema_string(), |
|
| 43 | - 'hasCount' => $this->get_schema_boolean( true ), |
|
| 44 | - 'hasImage' => $this->get_schema_boolean( false ), |
|
| 45 | - 'hasEmpty' => $this->get_schema_boolean( false ), |
|
| 46 | - 'isDropdown' => $this->get_schema_boolean( false ), |
|
| 47 | - 'isHierarchical' => $this->get_schema_boolean( true ), |
|
| 48 | - 'textColor' => $this->get_schema_string(), |
|
| 49 | - 'fontSize' => $this->get_schema_string(), |
|
| 50 | - 'lineHeight' => $this->get_schema_string(), |
|
| 51 | - 'style' => array( 'type' => 'object' ), |
|
| 52 | - ) |
|
| 53 | - ); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Render the Product Categories List block. |
|
| 58 | - * |
|
| 59 | - * @param array $attributes Block attributes. |
|
| 60 | - * @param string $content Block content. |
|
| 61 | - * @return string Rendered block type output. |
|
| 62 | - */ |
|
| 63 | - protected function render( $attributes, $content ) { |
|
| 64 | - $uid = uniqid( 'product-categories-' ); |
|
| 65 | - $categories = $this->get_categories( $attributes ); |
|
| 66 | - |
|
| 67 | - if ( empty( $categories ) ) { |
|
| 68 | - return ''; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ( ! empty( $content ) ) { |
|
| 72 | - // Deal with legacy attributes (before this was an SSR block) that differ from defaults. |
|
| 73 | - if ( strstr( $content, 'data-has-count="false"' ) ) { |
|
| 74 | - $attributes['hasCount'] = false; |
|
| 75 | - } |
|
| 76 | - if ( strstr( $content, 'data-is-dropdown="true"' ) ) { |
|
| 77 | - $attributes['isDropdown'] = true; |
|
| 78 | - } |
|
| 79 | - if ( strstr( $content, 'data-is-hierarchical="false"' ) ) { |
|
| 80 | - $attributes['isHierarchical'] = false; |
|
| 81 | - } |
|
| 82 | - if ( strstr( $content, 'data-has-empty="true"' ) ) { |
|
| 83 | - $attributes['hasEmpty'] = true; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( |
|
| 88 | - $attributes, |
|
| 89 | - array( 'line_height', 'text_color', 'font_size' ) |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - $classes = $this->get_container_classes( $attributes ) . ' ' . $classes_and_styles['classes']; |
|
| 93 | - $styles = $classes_and_styles['styles']; |
|
| 94 | - |
|
| 95 | - $output = '<div class="wp-block-woocommerce-product-categories ' . esc_attr( $classes ) . '" style="' . esc_attr( $styles ) . '">'; |
|
| 96 | - $output .= ! empty( $attributes['isDropdown'] ) ? $this->renderDropdown( $categories, $attributes, $uid ) : $this->renderList( $categories, $attributes, $uid ); |
|
| 97 | - $output .= '</div>'; |
|
| 98 | - |
|
| 99 | - return $output; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the list of classes to apply to this block. |
|
| 104 | - * |
|
| 105 | - * @param array $attributes Block attributes. Default empty array. |
|
| 106 | - * @return string space-separated list of classes. |
|
| 107 | - */ |
|
| 108 | - protected function get_container_classes( $attributes = array() ) { |
|
| 109 | - |
|
| 110 | - $classes = array( 'wc-block-product-categories' ); |
|
| 111 | - |
|
| 112 | - if ( isset( $attributes['align'] ) ) { |
|
| 113 | - $classes[] = "align{$attributes['align']}"; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - if ( ! empty( $attributes['className'] ) ) { |
|
| 117 | - $classes[] = $attributes['className']; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if ( $attributes['isDropdown'] ) { |
|
| 121 | - $classes[] = 'is-dropdown'; |
|
| 122 | - } else { |
|
| 123 | - $classes[] = 'is-list'; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return implode( ' ', $classes ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Get categories (terms) from the db. |
|
| 131 | - * |
|
| 132 | - * @param array $attributes Block attributes. Default empty array. |
|
| 133 | - * @return array |
|
| 134 | - */ |
|
| 135 | - protected function get_categories( $attributes ) { |
|
| 136 | - $hierarchical = wc_string_to_bool( $attributes['isHierarchical'] ); |
|
| 137 | - $categories = get_terms( |
|
| 138 | - 'product_cat', |
|
| 139 | - [ |
|
| 140 | - 'hide_empty' => ! $attributes['hasEmpty'], |
|
| 141 | - 'pad_counts' => true, |
|
| 142 | - 'hierarchical' => true, |
|
| 143 | - ] |
|
| 144 | - ); |
|
| 145 | - |
|
| 146 | - if ( ! is_array( $categories ) || empty( $categories ) ) { |
|
| 147 | - return []; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // This ensures that no categories with a product count of 0 is rendered. |
|
| 151 | - if ( ! $attributes['hasEmpty'] ) { |
|
| 152 | - $categories = array_filter( |
|
| 153 | - $categories, |
|
| 154 | - function( $category ) { |
|
| 155 | - return 0 !== $category->count; |
|
| 156 | - } |
|
| 157 | - ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $hierarchical ? $this->build_category_tree( $categories ) : $categories; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Build hierarchical tree of categories. |
|
| 165 | - * |
|
| 166 | - * @param array $categories List of terms. |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - protected function build_category_tree( $categories ) { |
|
| 170 | - $categories_by_parent = []; |
|
| 171 | - |
|
| 172 | - foreach ( $categories as $category ) { |
|
| 173 | - if ( ! isset( $categories_by_parent[ 'cat-' . $category->parent ] ) ) { |
|
| 174 | - $categories_by_parent[ 'cat-' . $category->parent ] = []; |
|
| 175 | - } |
|
| 176 | - $categories_by_parent[ 'cat-' . $category->parent ][] = $category; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $tree = $categories_by_parent['cat-0']; |
|
| 180 | - unset( $categories_by_parent['cat-0'] ); |
|
| 181 | - |
|
| 182 | - foreach ( $tree as $category ) { |
|
| 183 | - if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 184 | - $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $tree; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Build hierarchical tree of categories by appending children in the tree. |
|
| 193 | - * |
|
| 194 | - * @param array $categories List of terms. |
|
| 195 | - * @param array $categories_by_parent List of terms grouped by parent. |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - protected function fill_category_children( $categories, $categories_by_parent ) { |
|
| 199 | - foreach ( $categories as $category ) { |
|
| 200 | - if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 201 | - $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - return $categories; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Render the category list as a dropdown. |
|
| 209 | - * |
|
| 210 | - * @param array $categories List of terms. |
|
| 211 | - * @param array $attributes Block attributes. Default empty array. |
|
| 212 | - * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 213 | - * @return string Rendered output. |
|
| 214 | - */ |
|
| 215 | - protected function renderDropdown( $categories, $attributes, $uid ) { |
|
| 216 | - $aria_label = empty( $attributes['hasCount'] ) ? |
|
| 217 | - __( 'List of categories', 'woocommerce' ) : |
|
| 218 | - __( 'List of categories with their product counts', 'woocommerce' ); |
|
| 219 | - |
|
| 220 | - $output = ' |
|
| 12 | + /** |
|
| 13 | + * Block name. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + protected $block_name = 'product-categories'; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Default attribute values, should match what's set in JS `registerBlockType`. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $defaults = array( |
|
| 25 | + 'hasCount' => true, |
|
| 26 | + 'hasImage' => false, |
|
| 27 | + 'hasEmpty' => false, |
|
| 28 | + 'isDropdown' => false, |
|
| 29 | + 'isHierarchical' => true, |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Get block attributes. |
|
| 34 | + * |
|
| 35 | + * @return array |
|
| 36 | + */ |
|
| 37 | + protected function get_block_type_attributes() { |
|
| 38 | + return array_merge( |
|
| 39 | + parent::get_block_type_attributes(), |
|
| 40 | + array( |
|
| 41 | + 'align' => $this->get_schema_align(), |
|
| 42 | + 'className' => $this->get_schema_string(), |
|
| 43 | + 'hasCount' => $this->get_schema_boolean( true ), |
|
| 44 | + 'hasImage' => $this->get_schema_boolean( false ), |
|
| 45 | + 'hasEmpty' => $this->get_schema_boolean( false ), |
|
| 46 | + 'isDropdown' => $this->get_schema_boolean( false ), |
|
| 47 | + 'isHierarchical' => $this->get_schema_boolean( true ), |
|
| 48 | + 'textColor' => $this->get_schema_string(), |
|
| 49 | + 'fontSize' => $this->get_schema_string(), |
|
| 50 | + 'lineHeight' => $this->get_schema_string(), |
|
| 51 | + 'style' => array( 'type' => 'object' ), |
|
| 52 | + ) |
|
| 53 | + ); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Render the Product Categories List block. |
|
| 58 | + * |
|
| 59 | + * @param array $attributes Block attributes. |
|
| 60 | + * @param string $content Block content. |
|
| 61 | + * @return string Rendered block type output. |
|
| 62 | + */ |
|
| 63 | + protected function render( $attributes, $content ) { |
|
| 64 | + $uid = uniqid( 'product-categories-' ); |
|
| 65 | + $categories = $this->get_categories( $attributes ); |
|
| 66 | + |
|
| 67 | + if ( empty( $categories ) ) { |
|
| 68 | + return ''; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ( ! empty( $content ) ) { |
|
| 72 | + // Deal with legacy attributes (before this was an SSR block) that differ from defaults. |
|
| 73 | + if ( strstr( $content, 'data-has-count="false"' ) ) { |
|
| 74 | + $attributes['hasCount'] = false; |
|
| 75 | + } |
|
| 76 | + if ( strstr( $content, 'data-is-dropdown="true"' ) ) { |
|
| 77 | + $attributes['isDropdown'] = true; |
|
| 78 | + } |
|
| 79 | + if ( strstr( $content, 'data-is-hierarchical="false"' ) ) { |
|
| 80 | + $attributes['isHierarchical'] = false; |
|
| 81 | + } |
|
| 82 | + if ( strstr( $content, 'data-has-empty="true"' ) ) { |
|
| 83 | + $attributes['hasEmpty'] = true; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( |
|
| 88 | + $attributes, |
|
| 89 | + array( 'line_height', 'text_color', 'font_size' ) |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + $classes = $this->get_container_classes( $attributes ) . ' ' . $classes_and_styles['classes']; |
|
| 93 | + $styles = $classes_and_styles['styles']; |
|
| 94 | + |
|
| 95 | + $output = '<div class="wp-block-woocommerce-product-categories ' . esc_attr( $classes ) . '" style="' . esc_attr( $styles ) . '">'; |
|
| 96 | + $output .= ! empty( $attributes['isDropdown'] ) ? $this->renderDropdown( $categories, $attributes, $uid ) : $this->renderList( $categories, $attributes, $uid ); |
|
| 97 | + $output .= '</div>'; |
|
| 98 | + |
|
| 99 | + return $output; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the list of classes to apply to this block. |
|
| 104 | + * |
|
| 105 | + * @param array $attributes Block attributes. Default empty array. |
|
| 106 | + * @return string space-separated list of classes. |
|
| 107 | + */ |
|
| 108 | + protected function get_container_classes( $attributes = array() ) { |
|
| 109 | + |
|
| 110 | + $classes = array( 'wc-block-product-categories' ); |
|
| 111 | + |
|
| 112 | + if ( isset( $attributes['align'] ) ) { |
|
| 113 | + $classes[] = "align{$attributes['align']}"; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + if ( ! empty( $attributes['className'] ) ) { |
|
| 117 | + $classes[] = $attributes['className']; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if ( $attributes['isDropdown'] ) { |
|
| 121 | + $classes[] = 'is-dropdown'; |
|
| 122 | + } else { |
|
| 123 | + $classes[] = 'is-list'; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return implode( ' ', $classes ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Get categories (terms) from the db. |
|
| 131 | + * |
|
| 132 | + * @param array $attributes Block attributes. Default empty array. |
|
| 133 | + * @return array |
|
| 134 | + */ |
|
| 135 | + protected function get_categories( $attributes ) { |
|
| 136 | + $hierarchical = wc_string_to_bool( $attributes['isHierarchical'] ); |
|
| 137 | + $categories = get_terms( |
|
| 138 | + 'product_cat', |
|
| 139 | + [ |
|
| 140 | + 'hide_empty' => ! $attributes['hasEmpty'], |
|
| 141 | + 'pad_counts' => true, |
|
| 142 | + 'hierarchical' => true, |
|
| 143 | + ] |
|
| 144 | + ); |
|
| 145 | + |
|
| 146 | + if ( ! is_array( $categories ) || empty( $categories ) ) { |
|
| 147 | + return []; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // This ensures that no categories with a product count of 0 is rendered. |
|
| 151 | + if ( ! $attributes['hasEmpty'] ) { |
|
| 152 | + $categories = array_filter( |
|
| 153 | + $categories, |
|
| 154 | + function( $category ) { |
|
| 155 | + return 0 !== $category->count; |
|
| 156 | + } |
|
| 157 | + ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $hierarchical ? $this->build_category_tree( $categories ) : $categories; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Build hierarchical tree of categories. |
|
| 165 | + * |
|
| 166 | + * @param array $categories List of terms. |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + protected function build_category_tree( $categories ) { |
|
| 170 | + $categories_by_parent = []; |
|
| 171 | + |
|
| 172 | + foreach ( $categories as $category ) { |
|
| 173 | + if ( ! isset( $categories_by_parent[ 'cat-' . $category->parent ] ) ) { |
|
| 174 | + $categories_by_parent[ 'cat-' . $category->parent ] = []; |
|
| 175 | + } |
|
| 176 | + $categories_by_parent[ 'cat-' . $category->parent ][] = $category; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $tree = $categories_by_parent['cat-0']; |
|
| 180 | + unset( $categories_by_parent['cat-0'] ); |
|
| 181 | + |
|
| 182 | + foreach ( $tree as $category ) { |
|
| 183 | + if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 184 | + $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $tree; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Build hierarchical tree of categories by appending children in the tree. |
|
| 193 | + * |
|
| 194 | + * @param array $categories List of terms. |
|
| 195 | + * @param array $categories_by_parent List of terms grouped by parent. |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + protected function fill_category_children( $categories, $categories_by_parent ) { |
|
| 199 | + foreach ( $categories as $category ) { |
|
| 200 | + if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 201 | + $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + return $categories; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Render the category list as a dropdown. |
|
| 209 | + * |
|
| 210 | + * @param array $categories List of terms. |
|
| 211 | + * @param array $attributes Block attributes. Default empty array. |
|
| 212 | + * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 213 | + * @return string Rendered output. |
|
| 214 | + */ |
|
| 215 | + protected function renderDropdown( $categories, $attributes, $uid ) { |
|
| 216 | + $aria_label = empty( $attributes['hasCount'] ) ? |
|
| 217 | + __( 'List of categories', 'woocommerce' ) : |
|
| 218 | + __( 'List of categories with their product counts', 'woocommerce' ); |
|
| 219 | + |
|
| 220 | + $output = ' |
|
| 221 | 221 | <div class="wc-block-product-categories__dropdown"> |
| 222 | 222 | <label |
| 223 | 223 | class="screen-reader-text" |
@@ -252,23 +252,23 @@ discard block |
||
| 252 | 252 | </svg> |
| 253 | 253 | </button> |
| 254 | 254 | '; |
| 255 | - return $output; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Render dropdown options list. |
|
| 260 | - * |
|
| 261 | - * @param array $categories List of terms. |
|
| 262 | - * @param array $attributes Block attributes. Default empty array. |
|
| 263 | - * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 264 | - * @param int $depth Current depth. |
|
| 265 | - * @return string Rendered output. |
|
| 266 | - */ |
|
| 267 | - protected function renderDropdownOptions( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 268 | - $output = ''; |
|
| 269 | - |
|
| 270 | - foreach ( $categories as $category ) { |
|
| 271 | - $output .= ' |
|
| 255 | + return $output; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Render dropdown options list. |
|
| 260 | + * |
|
| 261 | + * @param array $categories List of terms. |
|
| 262 | + * @param array $attributes Block attributes. Default empty array. |
|
| 263 | + * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 264 | + * @param int $depth Current depth. |
|
| 265 | + * @return string Rendered output. |
|
| 266 | + */ |
|
| 267 | + protected function renderDropdownOptions( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 268 | + $output = ''; |
|
| 269 | + |
|
| 270 | + foreach ( $categories as $category ) { |
|
| 271 | + $output .= ' |
|
| 272 | 272 | <option value="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '"> |
| 273 | 273 | ' . str_repeat( '−', $depth ) . ' |
| 274 | 274 | ' . esc_html( $category->name ) . ' |
@@ -276,112 +276,112 @@ discard block |
||
| 276 | 276 | </option> |
| 277 | 277 | ' . ( ! empty( $category->children ) ? $this->renderDropdownOptions( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . ' |
| 278 | 278 | '; |
| 279 | - } |
|
| 280 | - |
|
| 281 | - return $output; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Render the category list as a list. |
|
| 286 | - * |
|
| 287 | - * @param array $categories List of terms. |
|
| 288 | - * @param array $attributes Block attributes. Default empty array. |
|
| 289 | - * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 290 | - * @param int $depth Current depth. |
|
| 291 | - * @return string Rendered output. |
|
| 292 | - */ |
|
| 293 | - protected function renderList( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 294 | - $classes = [ |
|
| 295 | - 'wc-block-product-categories-list', |
|
| 296 | - 'wc-block-product-categories-list--depth-' . absint( $depth ), |
|
| 297 | - ]; |
|
| 298 | - if ( ! empty( $attributes['hasImage'] ) ) { |
|
| 299 | - $classes[] = 'wc-block-product-categories-list--has-images'; |
|
| 300 | - } |
|
| 301 | - $output = '<ul class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $this->renderListItems( $categories, $attributes, $uid, $depth ) . '</ul>'; |
|
| 302 | - |
|
| 303 | - return $output; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Render a list of terms. |
|
| 308 | - * |
|
| 309 | - * @param array $categories List of terms. |
|
| 310 | - * @param array $attributes Block attributes. Default empty array. |
|
| 311 | - * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 312 | - * @param int $depth Current depth. |
|
| 313 | - * @return string Rendered output. |
|
| 314 | - */ |
|
| 315 | - protected function renderListItems( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 316 | - $output = ''; |
|
| 317 | - |
|
| 318 | - $link_color_class_and_style = StyleAttributesUtils::get_link_color_class_and_style( $attributes ); |
|
| 319 | - |
|
| 320 | - $link_color_style = isset( $link_color_class_and_style['style'] ) ? $link_color_class_and_style['style'] : ''; |
|
| 321 | - |
|
| 322 | - foreach ( $categories as $category ) { |
|
| 323 | - $output .= ' |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return $output; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Render the category list as a list. |
|
| 286 | + * |
|
| 287 | + * @param array $categories List of terms. |
|
| 288 | + * @param array $attributes Block attributes. Default empty array. |
|
| 289 | + * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 290 | + * @param int $depth Current depth. |
|
| 291 | + * @return string Rendered output. |
|
| 292 | + */ |
|
| 293 | + protected function renderList( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 294 | + $classes = [ |
|
| 295 | + 'wc-block-product-categories-list', |
|
| 296 | + 'wc-block-product-categories-list--depth-' . absint( $depth ), |
|
| 297 | + ]; |
|
| 298 | + if ( ! empty( $attributes['hasImage'] ) ) { |
|
| 299 | + $classes[] = 'wc-block-product-categories-list--has-images'; |
|
| 300 | + } |
|
| 301 | + $output = '<ul class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $this->renderListItems( $categories, $attributes, $uid, $depth ) . '</ul>'; |
|
| 302 | + |
|
| 303 | + return $output; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Render a list of terms. |
|
| 308 | + * |
|
| 309 | + * @param array $categories List of terms. |
|
| 310 | + * @param array $attributes Block attributes. Default empty array. |
|
| 311 | + * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
|
| 312 | + * @param int $depth Current depth. |
|
| 313 | + * @return string Rendered output. |
|
| 314 | + */ |
|
| 315 | + protected function renderListItems( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 316 | + $output = ''; |
|
| 317 | + |
|
| 318 | + $link_color_class_and_style = StyleAttributesUtils::get_link_color_class_and_style( $attributes ); |
|
| 319 | + |
|
| 320 | + $link_color_style = isset( $link_color_class_and_style['style'] ) ? $link_color_class_and_style['style'] : ''; |
|
| 321 | + |
|
| 322 | + foreach ( $categories as $category ) { |
|
| 323 | + $output .= ' |
|
| 324 | 324 | <li class="wc-block-product-categories-list-item"> |
| 325 | 325 | <a style="' . esc_attr( $link_color_style ) . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' |
| 326 | - . $this->get_image_html( $category, $attributes ) |
|
| 327 | - . '<span class="wc-block-product-categories-list-item__name">' . esc_html( $category->name ) . '</span>' |
|
| 328 | - . '</a>' |
|
| 329 | - . $this->getCount( $category, $attributes ) |
|
| 330 | - . ( ! empty( $category->children ) ? $this->renderList( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . ' |
|
| 326 | + . $this->get_image_html( $category, $attributes ) |
|
| 327 | + . '<span class="wc-block-product-categories-list-item__name">' . esc_html( $category->name ) . '</span>' |
|
| 328 | + . '</a>' |
|
| 329 | + . $this->getCount( $category, $attributes ) |
|
| 330 | + . ( ! empty( $category->children ) ? $this->renderList( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . ' |
|
| 331 | 331 | </li> |
| 332 | 332 | '; |
| 333 | - } |
|
| 334 | - |
|
| 335 | - return preg_replace( '/\r|\n/', '', $output ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Returns the category image html |
|
| 340 | - * |
|
| 341 | - * @param \WP_Term $category Term object. |
|
| 342 | - * @param array $attributes Block attributes. Default empty array. |
|
| 343 | - * @param string $size Image size, defaults to 'woocommerce_thumbnail'. |
|
| 344 | - * @return string |
|
| 345 | - */ |
|
| 346 | - public function get_image_html( $category, $attributes, $size = 'woocommerce_thumbnail' ) { |
|
| 347 | - if ( empty( $attributes['hasImage'] ) ) { |
|
| 348 | - return ''; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $image_id = get_term_meta( $category->term_id, 'thumbnail_id', true ); |
|
| 352 | - |
|
| 353 | - if ( ! $image_id ) { |
|
| 354 | - return '<span class="wc-block-product-categories-list-item__image wc-block-product-categories-list-item__image--placeholder">' . wc_placeholder_img( 'woocommerce_thumbnail' ) . '</span>'; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - return '<span class="wc-block-product-categories-list-item__image">' . wp_get_attachment_image( $image_id, 'woocommerce_thumbnail' ) . '</span>'; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Get the count, if displaying. |
|
| 362 | - * |
|
| 363 | - * @param object $category Term object. |
|
| 364 | - * @param array $attributes Block attributes. Default empty array. |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - protected function getCount( $category, $attributes ) { |
|
| 368 | - if ( empty( $attributes['hasCount'] ) ) { |
|
| 369 | - return ''; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - if ( $attributes['isDropdown'] ) { |
|
| 373 | - return '(' . absint( $category->count ) . ')'; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - $screen_reader_text = sprintf( |
|
| 377 | - /* translators: %s number of products in cart. */ |
|
| 378 | - _n( '%d product', '%d products', absint( $category->count ), 'woocommerce' ), |
|
| 379 | - absint( $category->count ) |
|
| 380 | - ); |
|
| 381 | - |
|
| 382 | - return '<span class="wc-block-product-categories-list-item-count">' |
|
| 383 | - . '<span aria-hidden="true">' . absint( $category->count ) . '</span>' |
|
| 384 | - . '<span class="screen-reader-text">' . esc_html( $screen_reader_text ) . '</span>' |
|
| 385 | - . '</span>'; |
|
| 386 | - } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return preg_replace( '/\r|\n/', '', $output ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Returns the category image html |
|
| 340 | + * |
|
| 341 | + * @param \WP_Term $category Term object. |
|
| 342 | + * @param array $attributes Block attributes. Default empty array. |
|
| 343 | + * @param string $size Image size, defaults to 'woocommerce_thumbnail'. |
|
| 344 | + * @return string |
|
| 345 | + */ |
|
| 346 | + public function get_image_html( $category, $attributes, $size = 'woocommerce_thumbnail' ) { |
|
| 347 | + if ( empty( $attributes['hasImage'] ) ) { |
|
| 348 | + return ''; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $image_id = get_term_meta( $category->term_id, 'thumbnail_id', true ); |
|
| 352 | + |
|
| 353 | + if ( ! $image_id ) { |
|
| 354 | + return '<span class="wc-block-product-categories-list-item__image wc-block-product-categories-list-item__image--placeholder">' . wc_placeholder_img( 'woocommerce_thumbnail' ) . '</span>'; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + return '<span class="wc-block-product-categories-list-item__image">' . wp_get_attachment_image( $image_id, 'woocommerce_thumbnail' ) . '</span>'; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Get the count, if displaying. |
|
| 362 | + * |
|
| 363 | + * @param object $category Term object. |
|
| 364 | + * @param array $attributes Block attributes. Default empty array. |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + protected function getCount( $category, $attributes ) { |
|
| 368 | + if ( empty( $attributes['hasCount'] ) ) { |
|
| 369 | + return ''; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + if ( $attributes['isDropdown'] ) { |
|
| 373 | + return '(' . absint( $category->count ) . ')'; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + $screen_reader_text = sprintf( |
|
| 377 | + /* translators: %s number of products in cart. */ |
|
| 378 | + _n( '%d product', '%d products', absint( $category->count ), 'woocommerce' ), |
|
| 379 | + absint( $category->count ) |
|
| 380 | + ); |
|
| 381 | + |
|
| 382 | + return '<span class="wc-block-product-categories-list-item-count">' |
|
| 383 | + . '<span aria-hidden="true">' . absint( $category->count ) . '</span>' |
|
| 384 | + . '<span class="screen-reader-text">' . esc_html( $screen_reader_text ) . '</span>' |
|
| 385 | + . '</span>'; |
|
| 386 | + } |
|
| 387 | 387 | } |
@@ -40,15 +40,15 @@ discard block |
||
| 40 | 40 | array( |
| 41 | 41 | 'align' => $this->get_schema_align(), |
| 42 | 42 | 'className' => $this->get_schema_string(), |
| 43 | - 'hasCount' => $this->get_schema_boolean( true ), |
|
| 44 | - 'hasImage' => $this->get_schema_boolean( false ), |
|
| 45 | - 'hasEmpty' => $this->get_schema_boolean( false ), |
|
| 46 | - 'isDropdown' => $this->get_schema_boolean( false ), |
|
| 47 | - 'isHierarchical' => $this->get_schema_boolean( true ), |
|
| 43 | + 'hasCount' => $this->get_schema_boolean(true), |
|
| 44 | + 'hasImage' => $this->get_schema_boolean(false), |
|
| 45 | + 'hasEmpty' => $this->get_schema_boolean(false), |
|
| 46 | + 'isDropdown' => $this->get_schema_boolean(false), |
|
| 47 | + 'isHierarchical' => $this->get_schema_boolean(true), |
|
| 48 | 48 | 'textColor' => $this->get_schema_string(), |
| 49 | 49 | 'fontSize' => $this->get_schema_string(), |
| 50 | 50 | 'lineHeight' => $this->get_schema_string(), |
| 51 | - 'style' => array( 'type' => 'object' ), |
|
| 51 | + 'style' => array('type' => 'object'), |
|
| 52 | 52 | ) |
| 53 | 53 | ); |
| 54 | 54 | } |
@@ -60,40 +60,40 @@ discard block |
||
| 60 | 60 | * @param string $content Block content. |
| 61 | 61 | * @return string Rendered block type output. |
| 62 | 62 | */ |
| 63 | - protected function render( $attributes, $content ) { |
|
| 64 | - $uid = uniqid( 'product-categories-' ); |
|
| 65 | - $categories = $this->get_categories( $attributes ); |
|
| 63 | + protected function render($attributes, $content) { |
|
| 64 | + $uid = uniqid('product-categories-'); |
|
| 65 | + $categories = $this->get_categories($attributes); |
|
| 66 | 66 | |
| 67 | - if ( empty( $categories ) ) { |
|
| 67 | + if (empty($categories)) { |
|
| 68 | 68 | return ''; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if ( ! empty( $content ) ) { |
|
| 71 | + if (!empty($content)) { |
|
| 72 | 72 | // Deal with legacy attributes (before this was an SSR block) that differ from defaults. |
| 73 | - if ( strstr( $content, 'data-has-count="false"' ) ) { |
|
| 73 | + if (strstr($content, 'data-has-count="false"')) { |
|
| 74 | 74 | $attributes['hasCount'] = false; |
| 75 | 75 | } |
| 76 | - if ( strstr( $content, 'data-is-dropdown="true"' ) ) { |
|
| 76 | + if (strstr($content, 'data-is-dropdown="true"')) { |
|
| 77 | 77 | $attributes['isDropdown'] = true; |
| 78 | 78 | } |
| 79 | - if ( strstr( $content, 'data-is-hierarchical="false"' ) ) { |
|
| 79 | + if (strstr($content, 'data-is-hierarchical="false"')) { |
|
| 80 | 80 | $attributes['isHierarchical'] = false; |
| 81 | 81 | } |
| 82 | - if ( strstr( $content, 'data-has-empty="true"' ) ) { |
|
| 82 | + if (strstr($content, 'data-has-empty="true"')) { |
|
| 83 | 83 | $attributes['hasEmpty'] = true; |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( |
| 88 | 88 | $attributes, |
| 89 | - array( 'line_height', 'text_color', 'font_size' ) |
|
| 89 | + array('line_height', 'text_color', 'font_size') |
|
| 90 | 90 | ); |
| 91 | 91 | |
| 92 | - $classes = $this->get_container_classes( $attributes ) . ' ' . $classes_and_styles['classes']; |
|
| 92 | + $classes = $this->get_container_classes($attributes) . ' ' . $classes_and_styles['classes']; |
|
| 93 | 93 | $styles = $classes_and_styles['styles']; |
| 94 | 94 | |
| 95 | - $output = '<div class="wp-block-woocommerce-product-categories ' . esc_attr( $classes ) . '" style="' . esc_attr( $styles ) . '">'; |
|
| 96 | - $output .= ! empty( $attributes['isDropdown'] ) ? $this->renderDropdown( $categories, $attributes, $uid ) : $this->renderList( $categories, $attributes, $uid ); |
|
| 95 | + $output = '<div class="wp-block-woocommerce-product-categories ' . esc_attr($classes) . '" style="' . esc_attr($styles) . '">'; |
|
| 96 | + $output .= !empty($attributes['isDropdown']) ? $this->renderDropdown($categories, $attributes, $uid) : $this->renderList($categories, $attributes, $uid); |
|
| 97 | 97 | $output .= '</div>'; |
| 98 | 98 | |
| 99 | 99 | return $output; |
@@ -105,25 +105,25 @@ discard block |
||
| 105 | 105 | * @param array $attributes Block attributes. Default empty array. |
| 106 | 106 | * @return string space-separated list of classes. |
| 107 | 107 | */ |
| 108 | - protected function get_container_classes( $attributes = array() ) { |
|
| 108 | + protected function get_container_classes($attributes = array()) { |
|
| 109 | 109 | |
| 110 | - $classes = array( 'wc-block-product-categories' ); |
|
| 110 | + $classes = array('wc-block-product-categories'); |
|
| 111 | 111 | |
| 112 | - if ( isset( $attributes['align'] ) ) { |
|
| 112 | + if (isset($attributes['align'])) { |
|
| 113 | 113 | $classes[] = "align{$attributes['align']}"; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - if ( ! empty( $attributes['className'] ) ) { |
|
| 116 | + if (!empty($attributes['className'])) { |
|
| 117 | 117 | $classes[] = $attributes['className']; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if ( $attributes['isDropdown'] ) { |
|
| 120 | + if ($attributes['isDropdown']) { |
|
| 121 | 121 | $classes[] = 'is-dropdown'; |
| 122 | 122 | } else { |
| 123 | 123 | $classes[] = 'is-list'; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - return implode( ' ', $classes ); |
|
| 126 | + return implode(' ', $classes); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -132,32 +132,32 @@ discard block |
||
| 132 | 132 | * @param array $attributes Block attributes. Default empty array. |
| 133 | 133 | * @return array |
| 134 | 134 | */ |
| 135 | - protected function get_categories( $attributes ) { |
|
| 136 | - $hierarchical = wc_string_to_bool( $attributes['isHierarchical'] ); |
|
| 135 | + protected function get_categories($attributes) { |
|
| 136 | + $hierarchical = wc_string_to_bool($attributes['isHierarchical']); |
|
| 137 | 137 | $categories = get_terms( |
| 138 | 138 | 'product_cat', |
| 139 | 139 | [ |
| 140 | - 'hide_empty' => ! $attributes['hasEmpty'], |
|
| 140 | + 'hide_empty' => !$attributes['hasEmpty'], |
|
| 141 | 141 | 'pad_counts' => true, |
| 142 | 142 | 'hierarchical' => true, |
| 143 | 143 | ] |
| 144 | 144 | ); |
| 145 | 145 | |
| 146 | - if ( ! is_array( $categories ) || empty( $categories ) ) { |
|
| 146 | + if (!is_array($categories) || empty($categories)) { |
|
| 147 | 147 | return []; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | // This ensures that no categories with a product count of 0 is rendered. |
| 151 | - if ( ! $attributes['hasEmpty'] ) { |
|
| 151 | + if (!$attributes['hasEmpty']) { |
|
| 152 | 152 | $categories = array_filter( |
| 153 | 153 | $categories, |
| 154 | - function( $category ) { |
|
| 154 | + function($category) { |
|
| 155 | 155 | return 0 !== $category->count; |
| 156 | 156 | } |
| 157 | 157 | ); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - return $hierarchical ? $this->build_category_tree( $categories ) : $categories; |
|
| 160 | + return $hierarchical ? $this->build_category_tree($categories) : $categories; |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -166,22 +166,22 @@ discard block |
||
| 166 | 166 | * @param array $categories List of terms. |
| 167 | 167 | * @return array |
| 168 | 168 | */ |
| 169 | - protected function build_category_tree( $categories ) { |
|
| 169 | + protected function build_category_tree($categories) { |
|
| 170 | 170 | $categories_by_parent = []; |
| 171 | 171 | |
| 172 | - foreach ( $categories as $category ) { |
|
| 173 | - if ( ! isset( $categories_by_parent[ 'cat-' . $category->parent ] ) ) { |
|
| 174 | - $categories_by_parent[ 'cat-' . $category->parent ] = []; |
|
| 172 | + foreach ($categories as $category) { |
|
| 173 | + if (!isset($categories_by_parent['cat-' . $category->parent])) { |
|
| 174 | + $categories_by_parent['cat-' . $category->parent] = []; |
|
| 175 | 175 | } |
| 176 | - $categories_by_parent[ 'cat-' . $category->parent ][] = $category; |
|
| 176 | + $categories_by_parent['cat-' . $category->parent][] = $category; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $tree = $categories_by_parent['cat-0']; |
| 180 | - unset( $categories_by_parent['cat-0'] ); |
|
| 180 | + unset($categories_by_parent['cat-0']); |
|
| 181 | 181 | |
| 182 | - foreach ( $tree as $category ) { |
|
| 183 | - if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 184 | - $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 182 | + foreach ($tree as $category) { |
|
| 183 | + if (!empty($categories_by_parent['cat-' . $category->term_id])) { |
|
| 184 | + $category->children = $this->fill_category_children($categories_by_parent['cat-' . $category->term_id], $categories_by_parent); |
|
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | |
@@ -195,10 +195,10 @@ discard block |
||
| 195 | 195 | * @param array $categories_by_parent List of terms grouped by parent. |
| 196 | 196 | * @return array |
| 197 | 197 | */ |
| 198 | - protected function fill_category_children( $categories, $categories_by_parent ) { |
|
| 199 | - foreach ( $categories as $category ) { |
|
| 200 | - if ( ! empty( $categories_by_parent[ 'cat-' . $category->term_id ] ) ) { |
|
| 201 | - $category->children = $this->fill_category_children( $categories_by_parent[ 'cat-' . $category->term_id ], $categories_by_parent ); |
|
| 198 | + protected function fill_category_children($categories, $categories_by_parent) { |
|
| 199 | + foreach ($categories as $category) { |
|
| 200 | + if (!empty($categories_by_parent['cat-' . $category->term_id])) { |
|
| 201 | + $category->children = $this->fill_category_children($categories_by_parent['cat-' . $category->term_id], $categories_by_parent); |
|
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | return $categories; |
@@ -212,31 +212,30 @@ discard block |
||
| 212 | 212 | * @param int $uid Unique ID for the rendered block, used for HTML IDs. |
| 213 | 213 | * @return string Rendered output. |
| 214 | 214 | */ |
| 215 | - protected function renderDropdown( $categories, $attributes, $uid ) { |
|
| 216 | - $aria_label = empty( $attributes['hasCount'] ) ? |
|
| 217 | - __( 'List of categories', 'woocommerce' ) : |
|
| 218 | - __( 'List of categories with their product counts', 'woocommerce' ); |
|
| 215 | + protected function renderDropdown($categories, $attributes, $uid) { |
|
| 216 | + $aria_label = empty($attributes['hasCount']) ? |
|
| 217 | + __('List of categories', 'woocommerce') : __('List of categories with their product counts', 'woocommerce'); |
|
| 219 | 218 | |
| 220 | 219 | $output = ' |
| 221 | 220 | <div class="wc-block-product-categories__dropdown"> |
| 222 | 221 | <label |
| 223 | 222 | class="screen-reader-text" |
| 224 | - for="' . esc_attr( $uid ) . '-select" |
|
| 223 | + for="' . esc_attr($uid) . '-select" |
|
| 225 | 224 | > |
| 226 | - ' . esc_html__( 'Select a category', 'woocommerce' ) . ' |
|
| 225 | + ' . esc_html__('Select a category', 'woocommerce') . ' |
|
| 227 | 226 | </label> |
| 228 | - <select aria-label="' . esc_attr( $aria_label ) . '" id="' . esc_attr( $uid ) . '-select"> |
|
| 227 | + <select aria-label="' . esc_attr($aria_label) . '" id="' . esc_attr($uid) . '-select"> |
|
| 229 | 228 | <option value="false" hidden> |
| 230 | - ' . esc_html__( 'Select a category', 'woocommerce' ) . ' |
|
| 229 | + ' . esc_html__('Select a category', 'woocommerce') . ' |
|
| 231 | 230 | </option> |
| 232 | - ' . $this->renderDropdownOptions( $categories, $attributes, $uid ) . ' |
|
| 231 | + ' . $this->renderDropdownOptions($categories, $attributes, $uid) . ' |
|
| 233 | 232 | </select> |
| 234 | 233 | </div> |
| 235 | 234 | <button |
| 236 | 235 | type="button" |
| 237 | 236 | class="wc-block-product-categories__button" |
| 238 | - aria-label="' . esc_html__( 'Go to category', 'woocommerce' ) . '" |
|
| 239 | - onclick="const url = document.getElementById( \'' . esc_attr( $uid ) . '-select\' ).value; if ( \'false\' !== url ) document.location.href = url;" |
|
| 237 | + aria-label="' . esc_html__('Go to category', 'woocommerce') . '" |
|
| 238 | + onclick="const url = document.getElementById( \'' . esc_attr($uid) . '-select\' ).value; if ( \'false\' !== url ) document.location.href = url;" |
|
| 240 | 239 | > |
| 241 | 240 | <svg |
| 242 | 241 | aria-hidden="true" |
@@ -264,17 +263,17 @@ discard block |
||
| 264 | 263 | * @param int $depth Current depth. |
| 265 | 264 | * @return string Rendered output. |
| 266 | 265 | */ |
| 267 | - protected function renderDropdownOptions( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 266 | + protected function renderDropdownOptions($categories, $attributes, $uid, $depth = 0) { |
|
| 268 | 267 | $output = ''; |
| 269 | 268 | |
| 270 | - foreach ( $categories as $category ) { |
|
| 269 | + foreach ($categories as $category) { |
|
| 271 | 270 | $output .= ' |
| 272 | - <option value="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '"> |
|
| 273 | - ' . str_repeat( '−', $depth ) . ' |
|
| 274 | - ' . esc_html( $category->name ) . ' |
|
| 275 | - ' . $this->getCount( $category, $attributes ) . ' |
|
| 271 | + <option value="' . esc_attr(get_term_link($category->term_id, 'product_cat')) . '"> |
|
| 272 | + ' . str_repeat('−', $depth) . ' |
|
| 273 | + ' . esc_html($category->name) . ' |
|
| 274 | + ' . $this->getCount($category, $attributes) . ' |
|
| 276 | 275 | </option> |
| 277 | - ' . ( ! empty( $category->children ) ? $this->renderDropdownOptions( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . ' |
|
| 276 | + ' . (!empty($category->children) ? $this->renderDropdownOptions($category->children, $attributes, $uid, $depth + 1) : '') . ' |
|
| 278 | 277 | '; |
| 279 | 278 | } |
| 280 | 279 | |
@@ -290,15 +289,15 @@ discard block |
||
| 290 | 289 | * @param int $depth Current depth. |
| 291 | 290 | * @return string Rendered output. |
| 292 | 291 | */ |
| 293 | - protected function renderList( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 292 | + protected function renderList($categories, $attributes, $uid, $depth = 0) { |
|
| 294 | 293 | $classes = [ |
| 295 | 294 | 'wc-block-product-categories-list', |
| 296 | - 'wc-block-product-categories-list--depth-' . absint( $depth ), |
|
| 295 | + 'wc-block-product-categories-list--depth-' . absint($depth), |
|
| 297 | 296 | ]; |
| 298 | - if ( ! empty( $attributes['hasImage'] ) ) { |
|
| 297 | + if (!empty($attributes['hasImage'])) { |
|
| 299 | 298 | $classes[] = 'wc-block-product-categories-list--has-images'; |
| 300 | 299 | } |
| 301 | - $output = '<ul class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $this->renderListItems( $categories, $attributes, $uid, $depth ) . '</ul>'; |
|
| 300 | + $output = '<ul class="' . esc_attr(implode(' ', $classes)) . '">' . $this->renderListItems($categories, $attributes, $uid, $depth) . '</ul>'; |
|
| 302 | 301 | |
| 303 | 302 | return $output; |
| 304 | 303 | } |
@@ -312,27 +311,27 @@ discard block |
||
| 312 | 311 | * @param int $depth Current depth. |
| 313 | 312 | * @return string Rendered output. |
| 314 | 313 | */ |
| 315 | - protected function renderListItems( $categories, $attributes, $uid, $depth = 0 ) { |
|
| 314 | + protected function renderListItems($categories, $attributes, $uid, $depth = 0) { |
|
| 316 | 315 | $output = ''; |
| 317 | 316 | |
| 318 | - $link_color_class_and_style = StyleAttributesUtils::get_link_color_class_and_style( $attributes ); |
|
| 317 | + $link_color_class_and_style = StyleAttributesUtils::get_link_color_class_and_style($attributes); |
|
| 319 | 318 | |
| 320 | - $link_color_style = isset( $link_color_class_and_style['style'] ) ? $link_color_class_and_style['style'] : ''; |
|
| 319 | + $link_color_style = isset($link_color_class_and_style['style']) ? $link_color_class_and_style['style'] : ''; |
|
| 321 | 320 | |
| 322 | - foreach ( $categories as $category ) { |
|
| 321 | + foreach ($categories as $category) { |
|
| 323 | 322 | $output .= ' |
| 324 | 323 | <li class="wc-block-product-categories-list-item"> |
| 325 | - <a style="' . esc_attr( $link_color_style ) . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' |
|
| 326 | - . $this->get_image_html( $category, $attributes ) |
|
| 327 | - . '<span class="wc-block-product-categories-list-item__name">' . esc_html( $category->name ) . '</span>' |
|
| 324 | + <a style="' . esc_attr($link_color_style) . '" href="' . esc_attr(get_term_link($category->term_id, 'product_cat')) . '">' |
|
| 325 | + . $this->get_image_html($category, $attributes) |
|
| 326 | + . '<span class="wc-block-product-categories-list-item__name">' . esc_html($category->name) . '</span>' |
|
| 328 | 327 | . '</a>' |
| 329 | - . $this->getCount( $category, $attributes ) |
|
| 330 | - . ( ! empty( $category->children ) ? $this->renderList( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . ' |
|
| 328 | + . $this->getCount($category, $attributes) |
|
| 329 | + . (!empty($category->children) ? $this->renderList($category->children, $attributes, $uid, $depth + 1) : '') . ' |
|
| 331 | 330 | </li> |
| 332 | 331 | '; |
| 333 | 332 | } |
| 334 | 333 | |
| 335 | - return preg_replace( '/\r|\n/', '', $output ); |
|
| 334 | + return preg_replace('/\r|\n/', '', $output); |
|
| 336 | 335 | } |
| 337 | 336 | |
| 338 | 337 | /** |
@@ -343,18 +342,18 @@ discard block |
||
| 343 | 342 | * @param string $size Image size, defaults to 'woocommerce_thumbnail'. |
| 344 | 343 | * @return string |
| 345 | 344 | */ |
| 346 | - public function get_image_html( $category, $attributes, $size = 'woocommerce_thumbnail' ) { |
|
| 347 | - if ( empty( $attributes['hasImage'] ) ) { |
|
| 345 | + public function get_image_html($category, $attributes, $size = 'woocommerce_thumbnail') { |
|
| 346 | + if (empty($attributes['hasImage'])) { |
|
| 348 | 347 | return ''; |
| 349 | 348 | } |
| 350 | 349 | |
| 351 | - $image_id = get_term_meta( $category->term_id, 'thumbnail_id', true ); |
|
| 350 | + $image_id = get_term_meta($category->term_id, 'thumbnail_id', true); |
|
| 352 | 351 | |
| 353 | - if ( ! $image_id ) { |
|
| 354 | - return '<span class="wc-block-product-categories-list-item__image wc-block-product-categories-list-item__image--placeholder">' . wc_placeholder_img( 'woocommerce_thumbnail' ) . '</span>'; |
|
| 352 | + if (!$image_id) { |
|
| 353 | + return '<span class="wc-block-product-categories-list-item__image wc-block-product-categories-list-item__image--placeholder">' . wc_placeholder_img('woocommerce_thumbnail') . '</span>'; |
|
| 355 | 354 | } |
| 356 | 355 | |
| 357 | - return '<span class="wc-block-product-categories-list-item__image">' . wp_get_attachment_image( $image_id, 'woocommerce_thumbnail' ) . '</span>'; |
|
| 356 | + return '<span class="wc-block-product-categories-list-item__image">' . wp_get_attachment_image($image_id, 'woocommerce_thumbnail') . '</span>'; |
|
| 358 | 357 | } |
| 359 | 358 | |
| 360 | 359 | /** |
@@ -364,24 +363,24 @@ discard block |
||
| 364 | 363 | * @param array $attributes Block attributes. Default empty array. |
| 365 | 364 | * @return string |
| 366 | 365 | */ |
| 367 | - protected function getCount( $category, $attributes ) { |
|
| 368 | - if ( empty( $attributes['hasCount'] ) ) { |
|
| 366 | + protected function getCount($category, $attributes) { |
|
| 367 | + if (empty($attributes['hasCount'])) { |
|
| 369 | 368 | return ''; |
| 370 | 369 | } |
| 371 | 370 | |
| 372 | - if ( $attributes['isDropdown'] ) { |
|
| 373 | - return '(' . absint( $category->count ) . ')'; |
|
| 371 | + if ($attributes['isDropdown']) { |
|
| 372 | + return '(' . absint($category->count) . ')'; |
|
| 374 | 373 | } |
| 375 | 374 | |
| 376 | 375 | $screen_reader_text = sprintf( |
| 377 | 376 | /* translators: %s number of products in cart. */ |
| 378 | - _n( '%d product', '%d products', absint( $category->count ), 'woocommerce' ), |
|
| 379 | - absint( $category->count ) |
|
| 377 | + _n('%d product', '%d products', absint($category->count), 'woocommerce'), |
|
| 378 | + absint($category->count) |
|
| 380 | 379 | ); |
| 381 | 380 | |
| 382 | 381 | return '<span class="wc-block-product-categories-list-item-count">' |
| 383 | - . '<span aria-hidden="true">' . absint( $category->count ) . '</span>' |
|
| 384 | - . '<span class="screen-reader-text">' . esc_html( $screen_reader_text ) . '</span>' |
|
| 382 | + . '<span aria-hidden="true">' . absint($category->count) . '</span>' |
|
| 383 | + . '<span class="screen-reader-text">' . esc_html($screen_reader_text) . '</span>' |
|
| 385 | 384 | . '</span>'; |
| 386 | 385 | } |
| 387 | 386 | } |
@@ -5,39 +5,39 @@ |
||
| 5 | 5 | * AllReviews class. |
| 6 | 6 | */ |
| 7 | 7 | class AllReviews extends AbstractBlock { |
| 8 | - /** |
|
| 9 | - * Block name. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $block_name = 'all-reviews'; |
|
| 8 | + /** |
|
| 9 | + * Block name. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $block_name = 'all-reviews'; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Get the frontend script handle for this block type. |
|
| 17 | - * |
|
| 18 | - * @see $this->register_block_type() |
|
| 19 | - * @param string $key Data to get, or default to everything. |
|
| 20 | - * @return array|string |
|
| 21 | - */ |
|
| 22 | - protected function get_block_type_script( $key = null ) { |
|
| 23 | - $script = [ |
|
| 24 | - 'handle' => 'wc-reviews-block-frontend', |
|
| 25 | - 'path' => $this->asset_api->get_block_asset_build_path( 'reviews-frontend' ), |
|
| 26 | - 'dependencies' => [], |
|
| 27 | - ]; |
|
| 28 | - return $key ? $script[ $key ] : $script; |
|
| 29 | - } |
|
| 15 | + /** |
|
| 16 | + * Get the frontend script handle for this block type. |
|
| 17 | + * |
|
| 18 | + * @see $this->register_block_type() |
|
| 19 | + * @param string $key Data to get, or default to everything. |
|
| 20 | + * @return array|string |
|
| 21 | + */ |
|
| 22 | + protected function get_block_type_script( $key = null ) { |
|
| 23 | + $script = [ |
|
| 24 | + 'handle' => 'wc-reviews-block-frontend', |
|
| 25 | + 'path' => $this->asset_api->get_block_asset_build_path( 'reviews-frontend' ), |
|
| 26 | + 'dependencies' => [], |
|
| 27 | + ]; |
|
| 28 | + return $key ? $script[ $key ] : $script; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Extra data passed through from server to client for block. |
|
| 33 | - * |
|
| 34 | - * @param array $attributes Any attributes that currently are available from the block. |
|
| 35 | - * Note, this will be empty in the editor context when the block is |
|
| 36 | - * not in the post content on editor load. |
|
| 37 | - */ |
|
| 38 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 39 | - parent::enqueue_data( $attributes ); |
|
| 40 | - $this->asset_data_registry->add( 'reviewRatingsEnabled', wc_review_ratings_enabled(), true ); |
|
| 41 | - $this->asset_data_registry->add( 'showAvatars', '1' === get_option( 'show_avatars' ), true ); |
|
| 42 | - } |
|
| 31 | + /** |
|
| 32 | + * Extra data passed through from server to client for block. |
|
| 33 | + * |
|
| 34 | + * @param array $attributes Any attributes that currently are available from the block. |
|
| 35 | + * Note, this will be empty in the editor context when the block is |
|
| 36 | + * not in the post content on editor load. |
|
| 37 | + */ |
|
| 38 | + protected function enqueue_data( array $attributes = [] ) { |
|
| 39 | + parent::enqueue_data( $attributes ); |
|
| 40 | + $this->asset_data_registry->add( 'reviewRatingsEnabled', wc_review_ratings_enabled(), true ); |
|
| 41 | + $this->asset_data_registry->add( 'showAvatars', '1' === get_option( 'show_avatars' ), true ); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -19,13 +19,13 @@ discard block |
||
| 19 | 19 | * @param string $key Data to get, or default to everything. |
| 20 | 20 | * @return array|string |
| 21 | 21 | */ |
| 22 | - protected function get_block_type_script( $key = null ) { |
|
| 22 | + protected function get_block_type_script($key = null) { |
|
| 23 | 23 | $script = [ |
| 24 | 24 | 'handle' => 'wc-reviews-block-frontend', |
| 25 | - 'path' => $this->asset_api->get_block_asset_build_path( 'reviews-frontend' ), |
|
| 25 | + 'path' => $this->asset_api->get_block_asset_build_path('reviews-frontend'), |
|
| 26 | 26 | 'dependencies' => [], |
| 27 | 27 | ]; |
| 28 | - return $key ? $script[ $key ] : $script; |
|
| 28 | + return $key ? $script[$key] : $script; |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
@@ -35,9 +35,9 @@ discard block |
||
| 35 | 35 | * Note, this will be empty in the editor context when the block is |
| 36 | 36 | * not in the post content on editor load. |
| 37 | 37 | */ |
| 38 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 39 | - parent::enqueue_data( $attributes ); |
|
| 40 | - $this->asset_data_registry->add( 'reviewRatingsEnabled', wc_review_ratings_enabled(), true ); |
|
| 41 | - $this->asset_data_registry->add( 'showAvatars', '1' === get_option( 'show_avatars' ), true ); |
|
| 38 | + protected function enqueue_data(array $attributes = []) { |
|
| 39 | + parent::enqueue_data($attributes); |
|
| 40 | + $this->asset_data_registry->add('reviewRatingsEnabled', wc_review_ratings_enabled(), true); |
|
| 41 | + $this->asset_data_registry->add('showAvatars', '1' === get_option('show_avatars'), true); |
|
| 42 | 42 | } |
| 43 | 43 | } |
@@ -6,33 +6,33 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class ProductOnSale extends AbstractProductGrid { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Block name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $block_name = 'product-on-sale'; |
|
| 9 | + /** |
|
| 10 | + * Block name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $block_name = 'product-on-sale'; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set args specific to this block |
|
| 18 | - * |
|
| 19 | - * @param array $query_args Query args. |
|
| 20 | - */ |
|
| 21 | - protected function set_block_query_args( &$query_args ) { |
|
| 22 | - $query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); |
|
| 23 | - } |
|
| 24 | - /** |
|
| 25 | - * Get block attributes. |
|
| 26 | - * |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - protected function get_block_type_attributes() { |
|
| 30 | - return array_merge( |
|
| 31 | - parent::get_block_type_attributes(), |
|
| 32 | - array( |
|
| 33 | - 'className' => $this->get_schema_string(), |
|
| 34 | - 'orderby' => $this->get_schema_orderby(), |
|
| 35 | - ) |
|
| 36 | - ); |
|
| 37 | - } |
|
| 16 | + /** |
|
| 17 | + * Set args specific to this block |
|
| 18 | + * |
|
| 19 | + * @param array $query_args Query args. |
|
| 20 | + */ |
|
| 21 | + protected function set_block_query_args( &$query_args ) { |
|
| 22 | + $query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); |
|
| 23 | + } |
|
| 24 | + /** |
|
| 25 | + * Get block attributes. |
|
| 26 | + * |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + protected function get_block_type_attributes() { |
|
| 30 | + return array_merge( |
|
| 31 | + parent::get_block_type_attributes(), |
|
| 32 | + array( |
|
| 33 | + 'className' => $this->get_schema_string(), |
|
| 34 | + 'orderby' => $this->get_schema_orderby(), |
|
| 35 | + ) |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -18,8 +18,8 @@ |
||
| 18 | 18 | * |
| 19 | 19 | * @param array $query_args Query args. |
| 20 | 20 | */ |
| 21 | - protected function set_block_query_args( &$query_args ) { |
|
| 22 | - $query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); |
|
| 21 | + protected function set_block_query_args(&$query_args) { |
|
| 22 | + $query_args['post__in'] = array_merge(array(0), wc_get_product_ids_on_sale()); |
|
| 23 | 23 | } |
| 24 | 24 | /** |
| 25 | 25 | * Get block attributes. |