@@ -5,57 +5,57 @@ |
||
| 5 | 5 | * HandpickedProducts class. |
| 6 | 6 | */ |
| 7 | 7 | class HandpickedProducts extends AbstractProductGrid { |
| 8 | - /** |
|
| 9 | - * Block name. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $block_name = 'handpicked-products'; |
|
| 8 | + /** |
|
| 9 | + * Block name. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $block_name = 'handpicked-products'; |
|
| 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 | - $ids = array_map( 'absint', $this->attributes['products'] ); |
|
| 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 | + $ids = array_map( 'absint', $this->attributes['products'] ); |
|
| 22 | 22 | |
| 23 | - $query_args['post__in'] = $ids; |
|
| 24 | - $query_args['posts_per_page'] = count( $ids ); |
|
| 25 | - } |
|
| 23 | + $query_args['post__in'] = $ids; |
|
| 24 | + $query_args['posts_per_page'] = count( $ids ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Set visibility query args. Handpicked products will show hidden products if chosen. |
|
| 29 | - * |
|
| 30 | - * @param array $query_args Query args. |
|
| 31 | - */ |
|
| 32 | - protected function set_visibility_query_args( &$query_args ) { |
|
| 33 | - if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
|
| 34 | - $product_visibility_terms = wc_get_product_visibility_term_ids(); |
|
| 35 | - $query_args['tax_query'][] = array( |
|
| 36 | - 'taxonomy' => 'product_visibility', |
|
| 37 | - 'field' => 'term_taxonomy_id', |
|
| 38 | - 'terms' => array( $product_visibility_terms['outofstock'] ), |
|
| 39 | - 'operator' => 'NOT IN', |
|
| 40 | - ); |
|
| 41 | - } |
|
| 42 | - } |
|
| 27 | + /** |
|
| 28 | + * Set visibility query args. Handpicked products will show hidden products if chosen. |
|
| 29 | + * |
|
| 30 | + * @param array $query_args Query args. |
|
| 31 | + */ |
|
| 32 | + protected function set_visibility_query_args( &$query_args ) { |
|
| 33 | + if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
|
| 34 | + $product_visibility_terms = wc_get_product_visibility_term_ids(); |
|
| 35 | + $query_args['tax_query'][] = array( |
|
| 36 | + 'taxonomy' => 'product_visibility', |
|
| 37 | + 'field' => 'term_taxonomy_id', |
|
| 38 | + 'terms' => array( $product_visibility_terms['outofstock'] ), |
|
| 39 | + 'operator' => 'NOT IN', |
|
| 40 | + ); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get block attributes. |
|
| 46 | - * |
|
| 47 | - * @return array |
|
| 48 | - */ |
|
| 49 | - protected function get_block_type_attributes() { |
|
| 50 | - return array( |
|
| 51 | - 'align' => $this->get_schema_align(), |
|
| 52 | - 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 53 | - 'className' => $this->get_schema_string(), |
|
| 54 | - 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 55 | - 'orderby' => $this->get_schema_orderby(), |
|
| 56 | - 'products' => $this->get_schema_list_ids(), |
|
| 57 | - 'contentVisibility' => $this->get_schema_content_visibility(), |
|
| 58 | - 'isPreview' => $this->get_schema_boolean( false ), |
|
| 59 | - ); |
|
| 60 | - } |
|
| 44 | + /** |
|
| 45 | + * Get block attributes. |
|
| 46 | + * |
|
| 47 | + * @return array |
|
| 48 | + */ |
|
| 49 | + protected function get_block_type_attributes() { |
|
| 50 | + return array( |
|
| 51 | + 'align' => $this->get_schema_align(), |
|
| 52 | + 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 53 | + 'className' => $this->get_schema_string(), |
|
| 54 | + 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 55 | + 'orderby' => $this->get_schema_orderby(), |
|
| 56 | + 'products' => $this->get_schema_list_ids(), |
|
| 57 | + 'contentVisibility' => $this->get_schema_content_visibility(), |
|
| 58 | + 'isPreview' => $this->get_schema_boolean( false ), |
|
| 59 | + ); |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -17,11 +17,11 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @param array $query_args Query args. |
| 19 | 19 | */ |
| 20 | - protected function set_block_query_args( &$query_args ) { |
|
| 21 | - $ids = array_map( 'absint', $this->attributes['products'] ); |
|
| 20 | + protected function set_block_query_args(&$query_args) { |
|
| 21 | + $ids = array_map('absint', $this->attributes['products']); |
|
| 22 | 22 | |
| 23 | 23 | $query_args['post__in'] = $ids; |
| 24 | - $query_args['posts_per_page'] = count( $ids ); |
|
| 24 | + $query_args['posts_per_page'] = count($ids); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -29,13 +29,13 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @param array $query_args Query args. |
| 31 | 31 | */ |
| 32 | - protected function set_visibility_query_args( &$query_args ) { |
|
| 33 | - if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
|
| 32 | + protected function set_visibility_query_args(&$query_args) { |
|
| 33 | + if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { |
|
| 34 | 34 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 35 | 35 | $query_args['tax_query'][] = array( |
| 36 | 36 | 'taxonomy' => 'product_visibility', |
| 37 | 37 | 'field' => 'term_taxonomy_id', |
| 38 | - 'terms' => array( $product_visibility_terms['outofstock'] ), |
|
| 38 | + 'terms' => array($product_visibility_terms['outofstock']), |
|
| 39 | 39 | 'operator' => 'NOT IN', |
| 40 | 40 | ); |
| 41 | 41 | } |
@@ -49,13 +49,13 @@ discard block |
||
| 49 | 49 | protected function get_block_type_attributes() { |
| 50 | 50 | return array( |
| 51 | 51 | 'align' => $this->get_schema_align(), |
| 52 | - 'alignButtons' => $this->get_schema_boolean( false ), |
|
| 52 | + 'alignButtons' => $this->get_schema_boolean(false), |
|
| 53 | 53 | 'className' => $this->get_schema_string(), |
| 54 | - 'columns' => $this->get_schema_number( wc_get_theme_support( 'product_blocks::default_columns', 3 ) ), |
|
| 54 | + 'columns' => $this->get_schema_number(wc_get_theme_support('product_blocks::default_columns', 3)), |
|
| 55 | 55 | 'orderby' => $this->get_schema_orderby(), |
| 56 | 56 | 'products' => $this->get_schema_list_ids(), |
| 57 | 57 | 'contentVisibility' => $this->get_schema_content_visibility(), |
| 58 | - 'isPreview' => $this->get_schema_boolean( false ), |
|
| 58 | + 'isPreview' => $this->get_schema_boolean(false), |
|
| 59 | 59 | ); |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | class ProductAddToCart extends AbstractBlock { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Block name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $block_name = 'product-add-to-cart'; |
|
| 9 | + /** |
|
| 10 | + * Block name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $block_name = 'product-add-to-cart'; |
|
| 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 | } |
@@ -10,305 +10,305 @@ |
||
| 10 | 10 | * @internal |
| 11 | 11 | */ |
| 12 | 12 | class ClassicTemplate extends AbstractDynamicBlock { |
| 13 | - /** |
|
| 14 | - * Block name. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $block_name = 'legacy-template'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * API version. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $api_version = '2'; |
|
| 26 | - |
|
| 27 | - const FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM = 'filter_stock_status'; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Initialize this block. |
|
| 31 | - */ |
|
| 32 | - protected function initialize() { |
|
| 33 | - parent::initialize(); |
|
| 34 | - add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); |
|
| 35 | - add_filter( 'query_vars', array( $this, 'add_query_vars_filter' ) ); |
|
| 36 | - add_filter( 'woocommerce_product_query_meta_query', array( $this, 'filter_products_by_stock' ), 10, 2 ); |
|
| 37 | - |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Render method for the Classic Template block. This method will determine which template to render. |
|
| 42 | - * |
|
| 43 | - * @param array $attributes Block attributes. |
|
| 44 | - * @param string $content Block content. |
|
| 45 | - * |
|
| 46 | - * @return string | void Rendered block type output. |
|
| 47 | - */ |
|
| 48 | - protected function render( $attributes, $content ) { |
|
| 49 | - if ( ! isset( $attributes['template'] ) ) { |
|
| 50 | - return; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * We need to load the scripts here because when using block templates wp_head() gets run after the block |
|
| 55 | - * template. As a result we are trying to enqueue required scripts before we have even registered them. |
|
| 56 | - * |
|
| 57 | - * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447 |
|
| 58 | - */ |
|
| 59 | - if ( class_exists( 'WC_Frontend_Scripts' ) ) { |
|
| 60 | - $frontend_scripts = new \WC_Frontend_Scripts(); |
|
| 61 | - $frontend_scripts::load_scripts(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag', ProductSearchResultsTemplate::SLUG ); |
|
| 65 | - |
|
| 66 | - if ( 'single-product' === $attributes['template'] ) { |
|
| 67 | - return $this->render_single_product(); |
|
| 68 | - } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { |
|
| 69 | - // Set this so that our product filters can detect if it's a PHP template. |
|
| 70 | - $this->asset_data_registry->add( 'is_rendering_php_template', true, true ); |
|
| 71 | - |
|
| 72 | - // Set this so filter blocks being used as widgets know when to render. |
|
| 73 | - $this->asset_data_registry->add( 'has_filterable_products', true, true ); |
|
| 74 | - |
|
| 75 | - $this->asset_data_registry->add( |
|
| 76 | - 'page_url', |
|
| 77 | - html_entity_decode( get_pagenum_link() ), |
|
| 78 | - '' |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - return $this->render_archive_product(); |
|
| 82 | - } else { |
|
| 83 | - ob_start(); |
|
| 84 | - |
|
| 85 | - echo "You're using the ClassicTemplate block"; |
|
| 86 | - |
|
| 87 | - wp_reset_postdata(); |
|
| 88 | - return ob_get_clean(); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Render method for the single product template and parts. |
|
| 94 | - * |
|
| 95 | - * @return string Rendered block type output. |
|
| 96 | - */ |
|
| 97 | - protected function render_single_product() { |
|
| 98 | - ob_start(); |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Hook: woocommerce_before_main_content |
|
| 102 | - * |
|
| 103 | - * Called before rendering the main content for a product. |
|
| 104 | - * |
|
| 105 | - * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
|
| 106 | - * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
|
| 107 | - * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
|
| 108 | - */ |
|
| 109 | - do_action( 'woocommerce_before_main_content' ); |
|
| 110 | - |
|
| 111 | - while ( have_posts() ) : |
|
| 112 | - |
|
| 113 | - the_post(); |
|
| 114 | - wc_get_template_part( 'content', 'single-product' ); |
|
| 115 | - |
|
| 116 | - endwhile; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Hook: woocommerce_after_main_content |
|
| 120 | - * |
|
| 121 | - * Called after rendering the main content for a product. |
|
| 122 | - * |
|
| 123 | - * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
|
| 124 | - */ |
|
| 125 | - do_action( 'woocommerce_after_main_content' ); |
|
| 126 | - |
|
| 127 | - wp_reset_postdata(); |
|
| 128 | - |
|
| 129 | - return ob_get_clean(); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Render method for the archive product template and parts. |
|
| 134 | - * |
|
| 135 | - * @return string Rendered block type output. |
|
| 136 | - */ |
|
| 137 | - protected function render_archive_product() { |
|
| 138 | - ob_start(); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Hook: woocommerce_before_main_content |
|
| 142 | - * |
|
| 143 | - * Called before rendering the main content for a product. |
|
| 144 | - * |
|
| 145 | - * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
|
| 146 | - * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
|
| 147 | - * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
|
| 148 | - */ |
|
| 149 | - do_action( 'woocommerce_before_main_content' ); |
|
| 150 | - |
|
| 151 | - ?> |
|
| 13 | + /** |
|
| 14 | + * Block name. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $block_name = 'legacy-template'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * API version. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $api_version = '2'; |
|
| 26 | + |
|
| 27 | + const FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM = 'filter_stock_status'; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Initialize this block. |
|
| 31 | + */ |
|
| 32 | + protected function initialize() { |
|
| 33 | + parent::initialize(); |
|
| 34 | + add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); |
|
| 35 | + add_filter( 'query_vars', array( $this, 'add_query_vars_filter' ) ); |
|
| 36 | + add_filter( 'woocommerce_product_query_meta_query', array( $this, 'filter_products_by_stock' ), 10, 2 ); |
|
| 37 | + |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Render method for the Classic Template block. This method will determine which template to render. |
|
| 42 | + * |
|
| 43 | + * @param array $attributes Block attributes. |
|
| 44 | + * @param string $content Block content. |
|
| 45 | + * |
|
| 46 | + * @return string | void Rendered block type output. |
|
| 47 | + */ |
|
| 48 | + protected function render( $attributes, $content ) { |
|
| 49 | + if ( ! isset( $attributes['template'] ) ) { |
|
| 50 | + return; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * We need to load the scripts here because when using block templates wp_head() gets run after the block |
|
| 55 | + * template. As a result we are trying to enqueue required scripts before we have even registered them. |
|
| 56 | + * |
|
| 57 | + * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447 |
|
| 58 | + */ |
|
| 59 | + if ( class_exists( 'WC_Frontend_Scripts' ) ) { |
|
| 60 | + $frontend_scripts = new \WC_Frontend_Scripts(); |
|
| 61 | + $frontend_scripts::load_scripts(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag', ProductSearchResultsTemplate::SLUG ); |
|
| 65 | + |
|
| 66 | + if ( 'single-product' === $attributes['template'] ) { |
|
| 67 | + return $this->render_single_product(); |
|
| 68 | + } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { |
|
| 69 | + // Set this so that our product filters can detect if it's a PHP template. |
|
| 70 | + $this->asset_data_registry->add( 'is_rendering_php_template', true, true ); |
|
| 71 | + |
|
| 72 | + // Set this so filter blocks being used as widgets know when to render. |
|
| 73 | + $this->asset_data_registry->add( 'has_filterable_products', true, true ); |
|
| 74 | + |
|
| 75 | + $this->asset_data_registry->add( |
|
| 76 | + 'page_url', |
|
| 77 | + html_entity_decode( get_pagenum_link() ), |
|
| 78 | + '' |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + return $this->render_archive_product(); |
|
| 82 | + } else { |
|
| 83 | + ob_start(); |
|
| 84 | + |
|
| 85 | + echo "You're using the ClassicTemplate block"; |
|
| 86 | + |
|
| 87 | + wp_reset_postdata(); |
|
| 88 | + return ob_get_clean(); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Render method for the single product template and parts. |
|
| 94 | + * |
|
| 95 | + * @return string Rendered block type output. |
|
| 96 | + */ |
|
| 97 | + protected function render_single_product() { |
|
| 98 | + ob_start(); |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Hook: woocommerce_before_main_content |
|
| 102 | + * |
|
| 103 | + * Called before rendering the main content for a product. |
|
| 104 | + * |
|
| 105 | + * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
|
| 106 | + * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
|
| 107 | + * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
|
| 108 | + */ |
|
| 109 | + do_action( 'woocommerce_before_main_content' ); |
|
| 110 | + |
|
| 111 | + while ( have_posts() ) : |
|
| 112 | + |
|
| 113 | + the_post(); |
|
| 114 | + wc_get_template_part( 'content', 'single-product' ); |
|
| 115 | + |
|
| 116 | + endwhile; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Hook: woocommerce_after_main_content |
|
| 120 | + * |
|
| 121 | + * Called after rendering the main content for a product. |
|
| 122 | + * |
|
| 123 | + * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
|
| 124 | + */ |
|
| 125 | + do_action( 'woocommerce_after_main_content' ); |
|
| 126 | + |
|
| 127 | + wp_reset_postdata(); |
|
| 128 | + |
|
| 129 | + return ob_get_clean(); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Render method for the archive product template and parts. |
|
| 134 | + * |
|
| 135 | + * @return string Rendered block type output. |
|
| 136 | + */ |
|
| 137 | + protected function render_archive_product() { |
|
| 138 | + ob_start(); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Hook: woocommerce_before_main_content |
|
| 142 | + * |
|
| 143 | + * Called before rendering the main content for a product. |
|
| 144 | + * |
|
| 145 | + * @see woocommerce_output_content_wrapper() Outputs opening DIV for the content (priority 10) |
|
| 146 | + * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
|
| 147 | + * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
|
| 148 | + */ |
|
| 149 | + do_action( 'woocommerce_before_main_content' ); |
|
| 150 | + |
|
| 151 | + ?> |
|
| 152 | 152 | <header class="woocommerce-products-header"> |
| 153 | 153 | <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?> |
| 154 | 154 | <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1> |
| 155 | 155 | <?php endif; ?> |
| 156 | 156 | |
| 157 | 157 | <?php |
| 158 | - /** |
|
| 159 | - * Hook: woocommerce_archive_description. |
|
| 160 | - * |
|
| 161 | - * @see woocommerce_taxonomy_archive_description() Renders the taxonomy archive description (priority 10) |
|
| 162 | - * @see woocommerce_product_archive_description() Renders the product archive description (priority 10) |
|
| 163 | - */ |
|
| 164 | - do_action( 'woocommerce_archive_description' ); |
|
| 165 | - ?> |
|
| 158 | + /** |
|
| 159 | + * Hook: woocommerce_archive_description. |
|
| 160 | + * |
|
| 161 | + * @see woocommerce_taxonomy_archive_description() Renders the taxonomy archive description (priority 10) |
|
| 162 | + * @see woocommerce_product_archive_description() Renders the product archive description (priority 10) |
|
| 163 | + */ |
|
| 164 | + do_action( 'woocommerce_archive_description' ); |
|
| 165 | + ?> |
|
| 166 | 166 | </header> |
| 167 | 167 | <?php |
| 168 | - if ( woocommerce_product_loop() ) { |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Hook: woocommerce_before_shop_loop. |
|
| 172 | - * |
|
| 173 | - * @see woocommerce_output_all_notices() Render error notices (priority 10) |
|
| 174 | - * @see woocommerce_result_count() Show number of results found (priority 20) |
|
| 175 | - * @see woocommerce_catalog_ordering() Show form to control sort order (priority 30) |
|
| 176 | - */ |
|
| 177 | - do_action( 'woocommerce_before_shop_loop' ); |
|
| 178 | - |
|
| 179 | - woocommerce_product_loop_start(); |
|
| 180 | - |
|
| 181 | - if ( wc_get_loop_prop( 'total' ) ) { |
|
| 182 | - while ( have_posts() ) { |
|
| 183 | - the_post(); |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Hook: woocommerce_shop_loop. |
|
| 187 | - */ |
|
| 188 | - do_action( 'woocommerce_shop_loop' ); |
|
| 189 | - |
|
| 190 | - wc_get_template_part( 'content', 'product' ); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - woocommerce_product_loop_end(); |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Hook: woocommerce_after_shop_loop. |
|
| 198 | - * |
|
| 199 | - * @see woocommerce_pagination() Renders pagination (priority 10) |
|
| 200 | - */ |
|
| 201 | - do_action( 'woocommerce_after_shop_loop' ); |
|
| 202 | - } else { |
|
| 203 | - /** |
|
| 204 | - * Hook: woocommerce_no_products_found. |
|
| 205 | - * |
|
| 206 | - * @see wc_no_products_found() Default no products found content (priority 10) |
|
| 207 | - */ |
|
| 208 | - do_action( 'woocommerce_no_products_found' ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Hook: woocommerce_after_main_content |
|
| 213 | - * |
|
| 214 | - * Called after rendering the main content for a product. |
|
| 215 | - * |
|
| 216 | - * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
|
| 217 | - */ |
|
| 218 | - do_action( 'woocommerce_after_main_content' ); |
|
| 219 | - |
|
| 220 | - wp_reset_postdata(); |
|
| 221 | - return ob_get_clean(); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Get HTML markup with the right classes by attributes. |
|
| 226 | - * This function appends the classname at the first element that have the class attribute. |
|
| 227 | - * Based on the experience, all the wrapper elements have a class attribute. |
|
| 228 | - * |
|
| 229 | - * @param string $content Block content. |
|
| 230 | - * @param array $block Parsed block data. |
|
| 231 | - * @return string Rendered block type output. |
|
| 232 | - */ |
|
| 233 | - public function add_alignment_class_to_wrapper( string $content, array $block ) { |
|
| 234 | - if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { |
|
| 235 | - return $content; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $attributes = (array) $block['attrs']; |
|
| 239 | - |
|
| 240 | - // Set the default alignment to wide. |
|
| 241 | - if ( ! isset( $attributes['align'] ) ) { |
|
| 242 | - $attributes['align'] = 'wide'; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); |
|
| 246 | - |
|
| 247 | - if ( ! isset( $align_class_and_style['class'] ) ) { |
|
| 248 | - return $content; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Find the first tag. |
|
| 252 | - $first_tag = '<[^<>]+>'; |
|
| 253 | - $matches = array(); |
|
| 254 | - preg_match( $first_tag, $content, $matches ); |
|
| 255 | - |
|
| 256 | - // If there is a tag, but it doesn't have a class attribute, add the class attribute. |
|
| 257 | - if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { |
|
| 258 | - $pattern_before_tag_closing = '/.+?(?=>)/'; |
|
| 259 | - return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // If there is a tag, and it has a class already, add the class attribute. |
|
| 263 | - $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; |
|
| 264 | - return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 ); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Filter products by stock status when as query param there is "filter_stock_status" |
|
| 270 | - * |
|
| 271 | - * @param array $meta_query Meta query. |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function filter_products_by_stock( $meta_query ) { |
|
| 275 | - if ( is_admin() ) { |
|
| 276 | - return $meta_query; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - $stock_status = array_keys( wc_get_product_stock_status_options() ); |
|
| 280 | - $values = get_query_var( self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM ); |
|
| 281 | - |
|
| 282 | - $values_to_array = explode( ',', $values ); |
|
| 283 | - |
|
| 284 | - $filtered_values = array_filter( |
|
| 285 | - $values_to_array, |
|
| 286 | - function( $value ) use ( $stock_status ) { |
|
| 287 | - return in_array( $value, $stock_status, true ); |
|
| 288 | - } |
|
| 289 | - ); |
|
| 290 | - |
|
| 291 | - if ( ! empty( $filtered_values ) ) { |
|
| 292 | - |
|
| 293 | - $meta_query[] = array( |
|
| 294 | - 'key' => '_stock_status', |
|
| 295 | - 'value' => $filtered_values, |
|
| 296 | - 'compare' => 'IN', |
|
| 297 | - ); |
|
| 298 | - } |
|
| 299 | - return $meta_query; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Add custom query params |
|
| 305 | - * |
|
| 306 | - * @param array $vars Query vars. |
|
| 307 | - * @return array Query vars. |
|
| 308 | - */ |
|
| 309 | - public function add_query_vars_filter( $vars ) { |
|
| 310 | - $vars[] = self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM; |
|
| 311 | - return $vars; |
|
| 312 | - } |
|
| 168 | + if ( woocommerce_product_loop() ) { |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Hook: woocommerce_before_shop_loop. |
|
| 172 | + * |
|
| 173 | + * @see woocommerce_output_all_notices() Render error notices (priority 10) |
|
| 174 | + * @see woocommerce_result_count() Show number of results found (priority 20) |
|
| 175 | + * @see woocommerce_catalog_ordering() Show form to control sort order (priority 30) |
|
| 176 | + */ |
|
| 177 | + do_action( 'woocommerce_before_shop_loop' ); |
|
| 178 | + |
|
| 179 | + woocommerce_product_loop_start(); |
|
| 180 | + |
|
| 181 | + if ( wc_get_loop_prop( 'total' ) ) { |
|
| 182 | + while ( have_posts() ) { |
|
| 183 | + the_post(); |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Hook: woocommerce_shop_loop. |
|
| 187 | + */ |
|
| 188 | + do_action( 'woocommerce_shop_loop' ); |
|
| 189 | + |
|
| 190 | + wc_get_template_part( 'content', 'product' ); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + woocommerce_product_loop_end(); |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Hook: woocommerce_after_shop_loop. |
|
| 198 | + * |
|
| 199 | + * @see woocommerce_pagination() Renders pagination (priority 10) |
|
| 200 | + */ |
|
| 201 | + do_action( 'woocommerce_after_shop_loop' ); |
|
| 202 | + } else { |
|
| 203 | + /** |
|
| 204 | + * Hook: woocommerce_no_products_found. |
|
| 205 | + * |
|
| 206 | + * @see wc_no_products_found() Default no products found content (priority 10) |
|
| 207 | + */ |
|
| 208 | + do_action( 'woocommerce_no_products_found' ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Hook: woocommerce_after_main_content |
|
| 213 | + * |
|
| 214 | + * Called after rendering the main content for a product. |
|
| 215 | + * |
|
| 216 | + * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
|
| 217 | + */ |
|
| 218 | + do_action( 'woocommerce_after_main_content' ); |
|
| 219 | + |
|
| 220 | + wp_reset_postdata(); |
|
| 221 | + return ob_get_clean(); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Get HTML markup with the right classes by attributes. |
|
| 226 | + * This function appends the classname at the first element that have the class attribute. |
|
| 227 | + * Based on the experience, all the wrapper elements have a class attribute. |
|
| 228 | + * |
|
| 229 | + * @param string $content Block content. |
|
| 230 | + * @param array $block Parsed block data. |
|
| 231 | + * @return string Rendered block type output. |
|
| 232 | + */ |
|
| 233 | + public function add_alignment_class_to_wrapper( string $content, array $block ) { |
|
| 234 | + if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { |
|
| 235 | + return $content; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $attributes = (array) $block['attrs']; |
|
| 239 | + |
|
| 240 | + // Set the default alignment to wide. |
|
| 241 | + if ( ! isset( $attributes['align'] ) ) { |
|
| 242 | + $attributes['align'] = 'wide'; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); |
|
| 246 | + |
|
| 247 | + if ( ! isset( $align_class_and_style['class'] ) ) { |
|
| 248 | + return $content; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Find the first tag. |
|
| 252 | + $first_tag = '<[^<>]+>'; |
|
| 253 | + $matches = array(); |
|
| 254 | + preg_match( $first_tag, $content, $matches ); |
|
| 255 | + |
|
| 256 | + // If there is a tag, but it doesn't have a class attribute, add the class attribute. |
|
| 257 | + if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { |
|
| 258 | + $pattern_before_tag_closing = '/.+?(?=>)/'; |
|
| 259 | + return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // If there is a tag, and it has a class already, add the class attribute. |
|
| 263 | + $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; |
|
| 264 | + return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 ); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Filter products by stock status when as query param there is "filter_stock_status" |
|
| 270 | + * |
|
| 271 | + * @param array $meta_query Meta query. |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function filter_products_by_stock( $meta_query ) { |
|
| 275 | + if ( is_admin() ) { |
|
| 276 | + return $meta_query; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + $stock_status = array_keys( wc_get_product_stock_status_options() ); |
|
| 280 | + $values = get_query_var( self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM ); |
|
| 281 | + |
|
| 282 | + $values_to_array = explode( ',', $values ); |
|
| 283 | + |
|
| 284 | + $filtered_values = array_filter( |
|
| 285 | + $values_to_array, |
|
| 286 | + function( $value ) use ( $stock_status ) { |
|
| 287 | + return in_array( $value, $stock_status, true ); |
|
| 288 | + } |
|
| 289 | + ); |
|
| 290 | + |
|
| 291 | + if ( ! empty( $filtered_values ) ) { |
|
| 292 | + |
|
| 293 | + $meta_query[] = array( |
|
| 294 | + 'key' => '_stock_status', |
|
| 295 | + 'value' => $filtered_values, |
|
| 296 | + 'compare' => 'IN', |
|
| 297 | + ); |
|
| 298 | + } |
|
| 299 | + return $meta_query; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Add custom query params |
|
| 305 | + * |
|
| 306 | + * @param array $vars Query vars. |
|
| 307 | + * @return array Query vars. |
|
| 308 | + */ |
|
| 309 | + public function add_query_vars_filter( $vars ) { |
|
| 310 | + $vars[] = self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM; |
|
| 311 | + return $vars; |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | 314 | } |
@@ -31,9 +31,9 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | protected function initialize() { |
| 33 | 33 | parent::initialize(); |
| 34 | - add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); |
|
| 35 | - add_filter( 'query_vars', array( $this, 'add_query_vars_filter' ) ); |
|
| 36 | - add_filter( 'woocommerce_product_query_meta_query', array( $this, 'filter_products_by_stock' ), 10, 2 ); |
|
| 34 | + add_filter('render_block', array($this, 'add_alignment_class_to_wrapper'), 10, 2); |
|
| 35 | + add_filter('query_vars', array($this, 'add_query_vars_filter')); |
|
| 36 | + add_filter('woocommerce_product_query_meta_query', array($this, 'filter_products_by_stock'), 10, 2); |
|
| 37 | 37 | |
| 38 | 38 | } |
| 39 | 39 | |
@@ -45,8 +45,8 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return string | void Rendered block type output. |
| 47 | 47 | */ |
| 48 | - protected function render( $attributes, $content ) { |
|
| 49 | - if ( ! isset( $attributes['template'] ) ) { |
|
| 48 | + protected function render($attributes, $content) { |
|
| 49 | + if (!isset($attributes['template'])) { |
|
| 50 | 50 | return; |
| 51 | 51 | } |
| 52 | 52 | |
@@ -56,25 +56,25 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447 |
| 58 | 58 | */ |
| 59 | - if ( class_exists( 'WC_Frontend_Scripts' ) ) { |
|
| 59 | + if (class_exists('WC_Frontend_Scripts')) { |
|
| 60 | 60 | $frontend_scripts = new \WC_Frontend_Scripts(); |
| 61 | 61 | $frontend_scripts::load_scripts(); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag', ProductSearchResultsTemplate::SLUG ); |
|
| 64 | + $archive_templates = array('archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag', ProductSearchResultsTemplate::SLUG); |
|
| 65 | 65 | |
| 66 | - if ( 'single-product' === $attributes['template'] ) { |
|
| 66 | + if ('single-product' === $attributes['template']) { |
|
| 67 | 67 | return $this->render_single_product(); |
| 68 | - } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { |
|
| 68 | + } elseif (in_array($attributes['template'], $archive_templates, true)) { |
|
| 69 | 69 | // Set this so that our product filters can detect if it's a PHP template. |
| 70 | - $this->asset_data_registry->add( 'is_rendering_php_template', true, true ); |
|
| 70 | + $this->asset_data_registry->add('is_rendering_php_template', true, true); |
|
| 71 | 71 | |
| 72 | 72 | // Set this so filter blocks being used as widgets know when to render. |
| 73 | - $this->asset_data_registry->add( 'has_filterable_products', true, true ); |
|
| 73 | + $this->asset_data_registry->add('has_filterable_products', true, true); |
|
| 74 | 74 | |
| 75 | 75 | $this->asset_data_registry->add( |
| 76 | 76 | 'page_url', |
| 77 | - html_entity_decode( get_pagenum_link() ), |
|
| 77 | + html_entity_decode(get_pagenum_link()), |
|
| 78 | 78 | '' |
| 79 | 79 | ); |
| 80 | 80 | |
@@ -106,12 +106,12 @@ discard block |
||
| 106 | 106 | * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
| 107 | 107 | * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
| 108 | 108 | */ |
| 109 | - do_action( 'woocommerce_before_main_content' ); |
|
| 109 | + do_action('woocommerce_before_main_content'); |
|
| 110 | 110 | |
| 111 | - while ( have_posts() ) : |
|
| 111 | + while (have_posts()) : |
|
| 112 | 112 | |
| 113 | 113 | the_post(); |
| 114 | - wc_get_template_part( 'content', 'single-product' ); |
|
| 114 | + wc_get_template_part('content', 'single-product'); |
|
| 115 | 115 | |
| 116 | 116 | endwhile; |
| 117 | 117 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
| 124 | 124 | */ |
| 125 | - do_action( 'woocommerce_after_main_content' ); |
|
| 125 | + do_action('woocommerce_after_main_content'); |
|
| 126 | 126 | |
| 127 | 127 | wp_reset_postdata(); |
| 128 | 128 | |
@@ -146,11 +146,11 @@ discard block |
||
| 146 | 146 | * @see woocommerce_breadcrumb() Outputs breadcrumb trail to the current product (priority 20) |
| 147 | 147 | * @see WC_Structured_Data::generate_website_data() Outputs schema markup (priority 30) |
| 148 | 148 | */ |
| 149 | - do_action( 'woocommerce_before_main_content' ); |
|
| 149 | + do_action('woocommerce_before_main_content'); |
|
| 150 | 150 | |
| 151 | 151 | ?> |
| 152 | 152 | <header class="woocommerce-products-header"> |
| 153 | - <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?> |
|
| 153 | + <?php if (apply_filters('woocommerce_show_page_title', true)) : ?> |
|
| 154 | 154 | <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1> |
| 155 | 155 | <?php endif; ?> |
| 156 | 156 | |
@@ -161,11 +161,11 @@ discard block |
||
| 161 | 161 | * @see woocommerce_taxonomy_archive_description() Renders the taxonomy archive description (priority 10) |
| 162 | 162 | * @see woocommerce_product_archive_description() Renders the product archive description (priority 10) |
| 163 | 163 | */ |
| 164 | - do_action( 'woocommerce_archive_description' ); |
|
| 164 | + do_action('woocommerce_archive_description'); |
|
| 165 | 165 | ?> |
| 166 | 166 | </header> |
| 167 | 167 | <?php |
| 168 | - if ( woocommerce_product_loop() ) { |
|
| 168 | + if (woocommerce_product_loop()) { |
|
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | 171 | * Hook: woocommerce_before_shop_loop. |
@@ -174,20 +174,20 @@ discard block |
||
| 174 | 174 | * @see woocommerce_result_count() Show number of results found (priority 20) |
| 175 | 175 | * @see woocommerce_catalog_ordering() Show form to control sort order (priority 30) |
| 176 | 176 | */ |
| 177 | - do_action( 'woocommerce_before_shop_loop' ); |
|
| 177 | + do_action('woocommerce_before_shop_loop'); |
|
| 178 | 178 | |
| 179 | 179 | woocommerce_product_loop_start(); |
| 180 | 180 | |
| 181 | - if ( wc_get_loop_prop( 'total' ) ) { |
|
| 182 | - while ( have_posts() ) { |
|
| 181 | + if (wc_get_loop_prop('total')) { |
|
| 182 | + while (have_posts()) { |
|
| 183 | 183 | the_post(); |
| 184 | 184 | |
| 185 | 185 | /** |
| 186 | 186 | * Hook: woocommerce_shop_loop. |
| 187 | 187 | */ |
| 188 | - do_action( 'woocommerce_shop_loop' ); |
|
| 188 | + do_action('woocommerce_shop_loop'); |
|
| 189 | 189 | |
| 190 | - wc_get_template_part( 'content', 'product' ); |
|
| 190 | + wc_get_template_part('content', 'product'); |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
@@ -198,14 +198,14 @@ discard block |
||
| 198 | 198 | * |
| 199 | 199 | * @see woocommerce_pagination() Renders pagination (priority 10) |
| 200 | 200 | */ |
| 201 | - do_action( 'woocommerce_after_shop_loop' ); |
|
| 201 | + do_action('woocommerce_after_shop_loop'); |
|
| 202 | 202 | } else { |
| 203 | 203 | /** |
| 204 | 204 | * Hook: woocommerce_no_products_found. |
| 205 | 205 | * |
| 206 | 206 | * @see wc_no_products_found() Default no products found content (priority 10) |
| 207 | 207 | */ |
| 208 | - do_action( 'woocommerce_no_products_found' ); |
|
| 208 | + do_action('woocommerce_no_products_found'); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | * |
| 216 | 216 | * @see woocommerce_output_content_wrapper_end() Outputs closing DIV for the content (priority 10) |
| 217 | 217 | */ |
| 218 | - do_action( 'woocommerce_after_main_content' ); |
|
| 218 | + do_action('woocommerce_after_main_content'); |
|
| 219 | 219 | |
| 220 | 220 | wp_reset_postdata(); |
| 221 | 221 | return ob_get_clean(); |
@@ -230,38 +230,38 @@ discard block |
||
| 230 | 230 | * @param array $block Parsed block data. |
| 231 | 231 | * @return string Rendered block type output. |
| 232 | 232 | */ |
| 233 | - public function add_alignment_class_to_wrapper( string $content, array $block ) { |
|
| 234 | - if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { |
|
| 233 | + public function add_alignment_class_to_wrapper(string $content, array $block) { |
|
| 234 | + if (('woocommerce/' . $this->block_name) !== $block['blockName']) { |
|
| 235 | 235 | return $content; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | $attributes = (array) $block['attrs']; |
| 239 | 239 | |
| 240 | 240 | // Set the default alignment to wide. |
| 241 | - if ( ! isset( $attributes['align'] ) ) { |
|
| 241 | + if (!isset($attributes['align'])) { |
|
| 242 | 242 | $attributes['align'] = 'wide'; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - $align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); |
|
| 245 | + $align_class_and_style = StyleAttributesUtils::get_align_class_and_style($attributes); |
|
| 246 | 246 | |
| 247 | - if ( ! isset( $align_class_and_style['class'] ) ) { |
|
| 247 | + if (!isset($align_class_and_style['class'])) { |
|
| 248 | 248 | return $content; |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | // Find the first tag. |
| 252 | 252 | $first_tag = '<[^<>]+>'; |
| 253 | 253 | $matches = array(); |
| 254 | - preg_match( $first_tag, $content, $matches ); |
|
| 254 | + preg_match($first_tag, $content, $matches); |
|
| 255 | 255 | |
| 256 | 256 | // If there is a tag, but it doesn't have a class attribute, add the class attribute. |
| 257 | - if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { |
|
| 257 | + if (isset($matches[0]) && strpos($matches[0], ' class=') === false) { |
|
| 258 | 258 | $pattern_before_tag_closing = '/.+?(?=>)/'; |
| 259 | - return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 ); |
|
| 259 | + return preg_replace($pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | // If there is a tag, and it has a class already, add the class attribute. |
| 263 | 263 | $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; |
| 264 | - return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 ); |
|
| 264 | + return preg_replace($pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | |
@@ -271,24 +271,24 @@ discard block |
||
| 271 | 271 | * @param array $meta_query Meta query. |
| 272 | 272 | * @return array |
| 273 | 273 | */ |
| 274 | - public function filter_products_by_stock( $meta_query ) { |
|
| 275 | - if ( is_admin() ) { |
|
| 274 | + public function filter_products_by_stock($meta_query) { |
|
| 275 | + if (is_admin()) { |
|
| 276 | 276 | return $meta_query; |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | - $stock_status = array_keys( wc_get_product_stock_status_options() ); |
|
| 280 | - $values = get_query_var( self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM ); |
|
| 279 | + $stock_status = array_keys(wc_get_product_stock_status_options()); |
|
| 280 | + $values = get_query_var(self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM); |
|
| 281 | 281 | |
| 282 | - $values_to_array = explode( ',', $values ); |
|
| 282 | + $values_to_array = explode(',', $values); |
|
| 283 | 283 | |
| 284 | 284 | $filtered_values = array_filter( |
| 285 | 285 | $values_to_array, |
| 286 | - function( $value ) use ( $stock_status ) { |
|
| 287 | - return in_array( $value, $stock_status, true ); |
|
| 286 | + function($value) use ($stock_status) { |
|
| 287 | + return in_array($value, $stock_status, true); |
|
| 288 | 288 | } |
| 289 | 289 | ); |
| 290 | 290 | |
| 291 | - if ( ! empty( $filtered_values ) ) { |
|
| 291 | + if (!empty($filtered_values)) { |
|
| 292 | 292 | |
| 293 | 293 | $meta_query[] = array( |
| 294 | 294 | 'key' => '_stock_status', |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | * @param array $vars Query vars. |
| 307 | 307 | * @return array Query vars. |
| 308 | 308 | */ |
| 309 | - public function add_query_vars_filter( $vars ) { |
|
| 309 | + public function add_query_vars_filter($vars) { |
|
| 310 | 310 | $vars[] = self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM; |
| 311 | 311 | return $vars; |
| 312 | 312 | } |
@@ -5,39 +5,39 @@ |
||
| 5 | 5 | * ReviewsByProduct class. |
| 6 | 6 | */ |
| 7 | 7 | class ReviewsByProduct extends AbstractBlock { |
| 8 | - /** |
|
| 9 | - * Block name. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $block_name = 'reviews-by-product'; |
|
| 8 | + /** |
|
| 9 | + * Block name. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $block_name = 'reviews-by-product'; |
|
| 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 | } |
@@ -5,22 +5,22 @@ |
||
| 5 | 5 | * AttributeFilter class. |
| 6 | 6 | */ |
| 7 | 7 | class AttributeFilter extends AbstractBlock { |
| 8 | - /** |
|
| 9 | - * Block name. |
|
| 10 | - * |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $block_name = 'attribute-filter'; |
|
| 8 | + /** |
|
| 9 | + * Block name. |
|
| 10 | + * |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $block_name = 'attribute-filter'; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Extra data passed through from server to client for block. |
|
| 17 | - * |
|
| 18 | - * @param array $attributes Any attributes that currently are available from the block. |
|
| 19 | - * Note, this will be empty in the editor context when the block is |
|
| 20 | - * not in the post content on editor load. |
|
| 21 | - */ |
|
| 22 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 23 | - parent::enqueue_data( $attributes ); |
|
| 24 | - $this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ), true ); |
|
| 25 | - } |
|
| 15 | + /** |
|
| 16 | + * Extra data passed through from server to client for block. |
|
| 17 | + * |
|
| 18 | + * @param array $attributes Any attributes that currently are available from the block. |
|
| 19 | + * Note, this will be empty in the editor context when the block is |
|
| 20 | + * not in the post content on editor load. |
|
| 21 | + */ |
|
| 22 | + protected function enqueue_data( array $attributes = [] ) { |
|
| 23 | + parent::enqueue_data( $attributes ); |
|
| 24 | + $this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ), true ); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -19,8 +19,8 @@ |
||
| 19 | 19 | * Note, this will be empty in the editor context when the block is |
| 20 | 20 | * not in the post content on editor load. |
| 21 | 21 | */ |
| 22 | - protected function enqueue_data( array $attributes = [] ) { |
|
| 23 | - parent::enqueue_data( $attributes ); |
|
| 24 | - $this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ), true ); |
|
| 22 | + protected function enqueue_data(array $attributes = []) { |
|
| 23 | + parent::enqueue_data($attributes); |
|
| 24 | + $this->asset_data_registry->add('attributes', array_values(wc_get_attribute_taxonomies()), true); |
|
| 25 | 25 | } |
| 26 | 26 | } |
@@ -15,30 +15,30 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Library { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Initialize block library features. |
|
| 20 | - * |
|
| 21 | - * @deprecated 5.0.0 |
|
| 22 | - */ |
|
| 23 | - public static function init() { |
|
| 24 | - _deprecated_function( 'Library::init', '5.0.0' ); |
|
| 25 | - } |
|
| 18 | + /** |
|
| 19 | + * Initialize block library features. |
|
| 20 | + * |
|
| 21 | + * @deprecated 5.0.0 |
|
| 22 | + */ |
|
| 23 | + public static function init() { |
|
| 24 | + _deprecated_function( 'Library::init', '5.0.0' ); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Register custom tables within $wpdb object. |
|
| 29 | - * |
|
| 30 | - * @deprecated 5.0.0 |
|
| 31 | - */ |
|
| 32 | - public static function define_tables() { |
|
| 33 | - _deprecated_function( 'Library::define_tables', '5.0.0' ); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * Register custom tables within $wpdb object. |
|
| 29 | + * |
|
| 30 | + * @deprecated 5.0.0 |
|
| 31 | + */ |
|
| 32 | + public static function define_tables() { |
|
| 33 | + _deprecated_function( 'Library::define_tables', '5.0.0' ); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Register blocks, hooking up assets and render functions as needed. |
|
| 38 | - * |
|
| 39 | - * @deprecated 5.0.0 |
|
| 40 | - */ |
|
| 41 | - public static function register_blocks() { |
|
| 42 | - _deprecated_function( 'Library::register_blocks', '5.0.0' ); |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * Register blocks, hooking up assets and render functions as needed. |
|
| 38 | + * |
|
| 39 | + * @deprecated 5.0.0 |
|
| 40 | + */ |
|
| 41 | + public static function register_blocks() { |
|
| 42 | + _deprecated_function( 'Library::register_blocks', '5.0.0' ); |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | * @deprecated 5.0.0 |
| 22 | 22 | */ |
| 23 | 23 | public static function init() { |
| 24 | - _deprecated_function( 'Library::init', '5.0.0' ); |
|
| 24 | + _deprecated_function('Library::init', '5.0.0'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * @deprecated 5.0.0 |
| 31 | 31 | */ |
| 32 | 32 | public static function define_tables() { |
| 33 | - _deprecated_function( 'Library::define_tables', '5.0.0' ); |
|
| 33 | + _deprecated_function('Library::define_tables', '5.0.0'); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -39,6 +39,6 @@ discard block |
||
| 39 | 39 | * @deprecated 5.0.0 |
| 40 | 40 | */ |
| 41 | 41 | public static function register_blocks() { |
| 42 | - _deprecated_function( 'Library::register_blocks', '5.0.0' ); |
|
| 42 | + _deprecated_function('Library::register_blocks', '5.0.0'); |
|
| 43 | 43 | } |
| 44 | 44 | } |
@@ -16,400 +16,400 @@ |
||
| 16 | 16 | * @since 2.5.0 |
| 17 | 17 | */ |
| 18 | 18 | class AssetDataRegistry { |
| 19 | - /** |
|
| 20 | - * Contains registered data. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - private $data = []; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Contains preloaded API data. |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - private $preloaded_api_requests = []; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Lazy data is an array of closures that will be invoked just before |
|
| 35 | - * asset data is generated for the enqueued script. |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - private $lazy_data = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Asset handle for registered data. |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - private $handle = 'wc-settings'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Asset API interface for various asset registration. |
|
| 50 | - * |
|
| 51 | - * @var API |
|
| 52 | - */ |
|
| 53 | - private $api; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Constructor |
|
| 57 | - * |
|
| 58 | - * @param Api $asset_api Asset API interface for various asset registration. |
|
| 59 | - */ |
|
| 60 | - public function __construct( Api $asset_api ) { |
|
| 61 | - $this->api = $asset_api; |
|
| 62 | - $this->init(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Hook into WP asset registration for enqueueing asset data. |
|
| 67 | - */ |
|
| 68 | - protected function init() { |
|
| 69 | - add_action( 'init', array( $this, 'register_data_script' ) ); |
|
| 70 | - add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 71 | - add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Exposes core data via the wcSettings global. This data is shared throughout the client. |
|
| 76 | - * |
|
| 77 | - * Settings that are used by various components or multiple blocks should be added here. Note, that settings here are |
|
| 78 | - * global so be sure not to add anything heavy if possible. |
|
| 79 | - * |
|
| 80 | - * @return array An array containing core data. |
|
| 81 | - */ |
|
| 82 | - protected function get_core_data() { |
|
| 83 | - return [ |
|
| 84 | - 'adminUrl' => admin_url(), |
|
| 85 | - 'countries' => WC()->countries->get_countries(), |
|
| 86 | - 'currency' => $this->get_currency_data(), |
|
| 87 | - 'currentUserIsAdmin' => current_user_can( 'manage_woocommerce' ), |
|
| 88 | - 'homeUrl' => esc_url( home_url( '/' ) ), |
|
| 89 | - 'locale' => $this->get_locale_data(), |
|
| 90 | - 'orderStatuses' => $this->get_order_statuses(), |
|
| 91 | - 'placeholderImgSrc' => wc_placeholder_img_src(), |
|
| 92 | - 'productsSettings' => $this->get_products_settings(), |
|
| 93 | - 'siteTitle' => get_bloginfo( 'name' ), |
|
| 94 | - 'storePages' => $this->get_store_pages(), |
|
| 95 | - 'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ), |
|
| 96 | - 'wcVersion' => defined( 'WC_VERSION' ) ? WC_VERSION : '', |
|
| 97 | - 'wpLoginUrl' => wp_login_url(), |
|
| 98 | - 'wpVersion' => get_bloginfo( 'version' ), |
|
| 99 | - ]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get currency data to include in settings. |
|
| 104 | - * |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - protected function get_currency_data() { |
|
| 108 | - $currency = get_woocommerce_currency(); |
|
| 109 | - |
|
| 110 | - return [ |
|
| 111 | - 'code' => $currency, |
|
| 112 | - 'precision' => wc_get_price_decimals(), |
|
| 113 | - 'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ), |
|
| 114 | - 'symbolPosition' => get_option( 'woocommerce_currency_pos' ), |
|
| 115 | - 'decimalSeparator' => wc_get_price_decimal_separator(), |
|
| 116 | - 'thousandSeparator' => wc_get_price_thousand_separator(), |
|
| 117 | - 'priceFormat' => html_entity_decode( get_woocommerce_price_format() ), |
|
| 118 | - ]; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get locale data to include in settings. |
|
| 123 | - * |
|
| 124 | - * @return array |
|
| 125 | - */ |
|
| 126 | - protected function get_locale_data() { |
|
| 127 | - global $wp_locale; |
|
| 128 | - |
|
| 129 | - return [ |
|
| 130 | - 'siteLocale' => get_locale(), |
|
| 131 | - 'userLocale' => get_user_locale(), |
|
| 132 | - 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), |
|
| 133 | - ]; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get store pages to include in settings. |
|
| 138 | - * |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - protected function get_store_pages() { |
|
| 142 | - return array_map( |
|
| 143 | - [ $this, 'format_page_resource' ], |
|
| 144 | - [ |
|
| 145 | - 'myaccount' => wc_get_page_id( 'myaccount' ), |
|
| 146 | - 'shop' => wc_get_page_id( 'shop' ), |
|
| 147 | - 'cart' => wc_get_page_id( 'cart' ), |
|
| 148 | - 'checkout' => wc_get_page_id( 'checkout' ), |
|
| 149 | - 'privacy' => wc_privacy_policy_page_id(), |
|
| 150 | - 'terms' => wc_terms_and_conditions_page_id(), |
|
| 151 | - ] |
|
| 152 | - ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Get product related settings. |
|
| 157 | - * |
|
| 158 | - * Note: For the time being we are exposing only the settings that are used by blocks. |
|
| 159 | - * |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - protected function get_products_settings() { |
|
| 163 | - return [ |
|
| 164 | - 'cartRedirectAfterAdd' => get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes', |
|
| 165 | - ]; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Format a page object into a standard array of data. |
|
| 170 | - * |
|
| 171 | - * @param WP_Post|int $page Page object or ID. |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - protected function format_page_resource( $page ) { |
|
| 175 | - if ( is_numeric( $page ) && $page > 0 ) { |
|
| 176 | - $page = get_post( $page ); |
|
| 177 | - } |
|
| 178 | - if ( ! is_a( $page, '\WP_Post' ) || 'publish' !== $page->post_status ) { |
|
| 179 | - return [ |
|
| 180 | - 'id' => 0, |
|
| 181 | - 'title' => '', |
|
| 182 | - 'permalink' => false, |
|
| 183 | - ]; |
|
| 184 | - } |
|
| 185 | - return [ |
|
| 186 | - 'id' => $page->ID, |
|
| 187 | - 'title' => $page->post_title, |
|
| 188 | - 'permalink' => get_permalink( $page->ID ), |
|
| 189 | - ]; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Returns block-related data for enqueued wc-settings script. |
|
| 194 | - * Format order statuses by removing a leading 'wc-' if present. |
|
| 195 | - * |
|
| 196 | - * @return array formatted statuses. |
|
| 197 | - */ |
|
| 198 | - protected function get_order_statuses() { |
|
| 199 | - $formatted_statuses = array(); |
|
| 200 | - foreach ( wc_get_order_statuses() as $key => $value ) { |
|
| 201 | - $formatted_key = preg_replace( '/^wc-/', '', $key ); |
|
| 202 | - $formatted_statuses[ $formatted_key ] = $value; |
|
| 203 | - } |
|
| 204 | - return $formatted_statuses; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Used for on demand initialization of asset data and registering it with |
|
| 209 | - * the internal data registry. |
|
| 210 | - * |
|
| 211 | - * Note: core data will overwrite any externally registered data via the api. |
|
| 212 | - */ |
|
| 213 | - protected function initialize_core_data() { |
|
| 214 | - /** |
|
| 215 | - * Filters the array of shared settings. |
|
| 216 | - * |
|
| 217 | - * Low level hook for registration of new data late in the cycle. This is deprecated. |
|
| 218 | - * Instead, use the data api: |
|
| 219 | - * |
|
| 220 | - * ```php |
|
| 221 | - * Automattic\WooCommerce\Blocks\Package::container()->get( Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class )->add( $key, $value ) |
|
| 222 | - * ``` |
|
| 223 | - * |
|
| 224 | - * @deprecated |
|
| 225 | - * @param array $data Settings data. |
|
| 226 | - * @return array |
|
| 227 | - */ |
|
| 228 | - $settings = apply_filters( 'woocommerce_shared_settings', $this->data ); |
|
| 229 | - |
|
| 230 | - // Surface a deprecation warning in the error console. |
|
| 231 | - if ( has_filter( 'woocommerce_shared_settings' ) ) { |
|
| 232 | - $error_handle = 'deprecated-shared-settings-error'; |
|
| 233 | - $error_message = '`woocommerce_shared_settings` filter in Blocks is deprecated. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/docs/contributors/block-assets.md'; |
|
| 234 | - // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
|
| 235 | - wp_register_script( $error_handle, '' ); |
|
| 236 | - wp_enqueue_script( $error_handle ); |
|
| 237 | - wp_add_inline_script( |
|
| 238 | - $error_handle, |
|
| 239 | - sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 240 | - ); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // note this WILL wipe any data already registered to these keys because they are protected. |
|
| 244 | - $this->data = array_replace_recursive( $settings, $this->get_core_data() ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Loops through each registered lazy data callback and adds the returned |
|
| 249 | - * value to the data array. |
|
| 250 | - * |
|
| 251 | - * This method is executed right before preparing the data for printing to |
|
| 252 | - * the rendered screen. |
|
| 253 | - * |
|
| 254 | - * @return void |
|
| 255 | - */ |
|
| 256 | - protected function execute_lazy_data() { |
|
| 257 | - foreach ( $this->lazy_data as $key => $callback ) { |
|
| 258 | - $this->data[ $key ] = $callback(); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Exposes private registered data to child classes. |
|
| 264 | - * |
|
| 265 | - * @return array The registered data on the private data property |
|
| 266 | - */ |
|
| 267 | - protected function get() { |
|
| 268 | - return $this->data; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Allows checking whether a key exists. |
|
| 273 | - * |
|
| 274 | - * @param string $key The key to check if exists. |
|
| 275 | - * @return bool Whether the key exists in the current data registry. |
|
| 276 | - */ |
|
| 277 | - public function exists( $key ) { |
|
| 278 | - return array_key_exists( $key, $this->data ); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Interface for adding data to the registry. |
|
| 283 | - * |
|
| 284 | - * You can only register data that is not already in the registry identified by the given key. If there is a |
|
| 285 | - * duplicate found, unless $ignore_duplicates is true, an exception will be thrown. |
|
| 286 | - * |
|
| 287 | - * @param string $key The key used to reference the data being registered. |
|
| 288 | - * @param mixed $data If not a function, registered to the registry as is. If a function, then the |
|
| 289 | - * callback is invoked right before output to the screen. |
|
| 290 | - * @param boolean $check_key_exists If set to true, duplicate data will be ignored if the key exists. |
|
| 291 | - * If false, duplicate data will cause an exception. |
|
| 292 | - * |
|
| 293 | - * @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error. |
|
| 294 | - */ |
|
| 295 | - public function add( $key, $data, $check_key_exists = false ) { |
|
| 296 | - if ( $check_key_exists && $this->exists( $key ) ) { |
|
| 297 | - return; |
|
| 298 | - } |
|
| 299 | - try { |
|
| 300 | - $this->add_data( $key, $data ); |
|
| 301 | - } catch ( Exception $e ) { |
|
| 302 | - if ( $this->debug() ) { |
|
| 303 | - // bubble up. |
|
| 304 | - throw $e; |
|
| 305 | - } |
|
| 306 | - wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Hydrate from API. |
|
| 312 | - * |
|
| 313 | - * @param string $path REST API path to preload. |
|
| 314 | - */ |
|
| 315 | - public function hydrate_api_request( $path ) { |
|
| 316 | - if ( ! isset( $this->preloaded_api_requests[ $path ] ) ) { |
|
| 317 | - $this->preloaded_api_requests = rest_preload_api_request( $this->preloaded_api_requests, $path ); |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Adds a page permalink to the data registry. |
|
| 323 | - * |
|
| 324 | - * @param integer $page_id Page ID to add to the registry. |
|
| 325 | - */ |
|
| 326 | - public function register_page_id( $page_id ) { |
|
| 327 | - $permalink = $page_id ? get_permalink( $page_id ) : false; |
|
| 328 | - |
|
| 329 | - if ( $permalink ) { |
|
| 330 | - $this->data[ 'page-' . $page_id ] = $permalink; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Callback for registering the data script via WordPress API. |
|
| 336 | - * |
|
| 337 | - * @return void |
|
| 338 | - */ |
|
| 339 | - public function register_data_script() { |
|
| 340 | - $this->api->register_script( |
|
| 341 | - $this->handle, |
|
| 342 | - 'build/wc-settings.js', |
|
| 343 | - [ 'wp-api-fetch' ], |
|
| 344 | - true |
|
| 345 | - ); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Callback for enqueuing asset data via the WP api. |
|
| 350 | - * |
|
| 351 | - * Note: while this is hooked into print/admin_print_scripts, it still only |
|
| 352 | - * happens if the script attached to `wc-settings` handle is enqueued. This |
|
| 353 | - * is done to allow for any potentially expensive data generation to only |
|
| 354 | - * happen for routes that need it. |
|
| 355 | - */ |
|
| 356 | - public function enqueue_asset_data() { |
|
| 357 | - if ( wp_script_is( $this->handle, 'enqueued' ) ) { |
|
| 358 | - $this->initialize_core_data(); |
|
| 359 | - $this->execute_lazy_data(); |
|
| 360 | - |
|
| 361 | - $data = rawurlencode( wp_json_encode( $this->data ) ); |
|
| 362 | - $preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) ); |
|
| 363 | - |
|
| 364 | - wp_add_inline_script( |
|
| 365 | - $this->handle, |
|
| 366 | - " |
|
| 19 | + /** |
|
| 20 | + * Contains registered data. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + private $data = []; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Contains preloaded API data. |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + private $preloaded_api_requests = []; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Lazy data is an array of closures that will be invoked just before |
|
| 35 | + * asset data is generated for the enqueued script. |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + private $lazy_data = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Asset handle for registered data. |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + private $handle = 'wc-settings'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Asset API interface for various asset registration. |
|
| 50 | + * |
|
| 51 | + * @var API |
|
| 52 | + */ |
|
| 53 | + private $api; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Constructor |
|
| 57 | + * |
|
| 58 | + * @param Api $asset_api Asset API interface for various asset registration. |
|
| 59 | + */ |
|
| 60 | + public function __construct( Api $asset_api ) { |
|
| 61 | + $this->api = $asset_api; |
|
| 62 | + $this->init(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Hook into WP asset registration for enqueueing asset data. |
|
| 67 | + */ |
|
| 68 | + protected function init() { |
|
| 69 | + add_action( 'init', array( $this, 'register_data_script' ) ); |
|
| 70 | + add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 71 | + add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Exposes core data via the wcSettings global. This data is shared throughout the client. |
|
| 76 | + * |
|
| 77 | + * Settings that are used by various components or multiple blocks should be added here. Note, that settings here are |
|
| 78 | + * global so be sure not to add anything heavy if possible. |
|
| 79 | + * |
|
| 80 | + * @return array An array containing core data. |
|
| 81 | + */ |
|
| 82 | + protected function get_core_data() { |
|
| 83 | + return [ |
|
| 84 | + 'adminUrl' => admin_url(), |
|
| 85 | + 'countries' => WC()->countries->get_countries(), |
|
| 86 | + 'currency' => $this->get_currency_data(), |
|
| 87 | + 'currentUserIsAdmin' => current_user_can( 'manage_woocommerce' ), |
|
| 88 | + 'homeUrl' => esc_url( home_url( '/' ) ), |
|
| 89 | + 'locale' => $this->get_locale_data(), |
|
| 90 | + 'orderStatuses' => $this->get_order_statuses(), |
|
| 91 | + 'placeholderImgSrc' => wc_placeholder_img_src(), |
|
| 92 | + 'productsSettings' => $this->get_products_settings(), |
|
| 93 | + 'siteTitle' => get_bloginfo( 'name' ), |
|
| 94 | + 'storePages' => $this->get_store_pages(), |
|
| 95 | + 'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ), |
|
| 96 | + 'wcVersion' => defined( 'WC_VERSION' ) ? WC_VERSION : '', |
|
| 97 | + 'wpLoginUrl' => wp_login_url(), |
|
| 98 | + 'wpVersion' => get_bloginfo( 'version' ), |
|
| 99 | + ]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get currency data to include in settings. |
|
| 104 | + * |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + protected function get_currency_data() { |
|
| 108 | + $currency = get_woocommerce_currency(); |
|
| 109 | + |
|
| 110 | + return [ |
|
| 111 | + 'code' => $currency, |
|
| 112 | + 'precision' => wc_get_price_decimals(), |
|
| 113 | + 'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ), |
|
| 114 | + 'symbolPosition' => get_option( 'woocommerce_currency_pos' ), |
|
| 115 | + 'decimalSeparator' => wc_get_price_decimal_separator(), |
|
| 116 | + 'thousandSeparator' => wc_get_price_thousand_separator(), |
|
| 117 | + 'priceFormat' => html_entity_decode( get_woocommerce_price_format() ), |
|
| 118 | + ]; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get locale data to include in settings. |
|
| 123 | + * |
|
| 124 | + * @return array |
|
| 125 | + */ |
|
| 126 | + protected function get_locale_data() { |
|
| 127 | + global $wp_locale; |
|
| 128 | + |
|
| 129 | + return [ |
|
| 130 | + 'siteLocale' => get_locale(), |
|
| 131 | + 'userLocale' => get_user_locale(), |
|
| 132 | + 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), |
|
| 133 | + ]; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get store pages to include in settings. |
|
| 138 | + * |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + protected function get_store_pages() { |
|
| 142 | + return array_map( |
|
| 143 | + [ $this, 'format_page_resource' ], |
|
| 144 | + [ |
|
| 145 | + 'myaccount' => wc_get_page_id( 'myaccount' ), |
|
| 146 | + 'shop' => wc_get_page_id( 'shop' ), |
|
| 147 | + 'cart' => wc_get_page_id( 'cart' ), |
|
| 148 | + 'checkout' => wc_get_page_id( 'checkout' ), |
|
| 149 | + 'privacy' => wc_privacy_policy_page_id(), |
|
| 150 | + 'terms' => wc_terms_and_conditions_page_id(), |
|
| 151 | + ] |
|
| 152 | + ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Get product related settings. |
|
| 157 | + * |
|
| 158 | + * Note: For the time being we are exposing only the settings that are used by blocks. |
|
| 159 | + * |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + protected function get_products_settings() { |
|
| 163 | + return [ |
|
| 164 | + 'cartRedirectAfterAdd' => get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes', |
|
| 165 | + ]; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Format a page object into a standard array of data. |
|
| 170 | + * |
|
| 171 | + * @param WP_Post|int $page Page object or ID. |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + protected function format_page_resource( $page ) { |
|
| 175 | + if ( is_numeric( $page ) && $page > 0 ) { |
|
| 176 | + $page = get_post( $page ); |
|
| 177 | + } |
|
| 178 | + if ( ! is_a( $page, '\WP_Post' ) || 'publish' !== $page->post_status ) { |
|
| 179 | + return [ |
|
| 180 | + 'id' => 0, |
|
| 181 | + 'title' => '', |
|
| 182 | + 'permalink' => false, |
|
| 183 | + ]; |
|
| 184 | + } |
|
| 185 | + return [ |
|
| 186 | + 'id' => $page->ID, |
|
| 187 | + 'title' => $page->post_title, |
|
| 188 | + 'permalink' => get_permalink( $page->ID ), |
|
| 189 | + ]; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Returns block-related data for enqueued wc-settings script. |
|
| 194 | + * Format order statuses by removing a leading 'wc-' if present. |
|
| 195 | + * |
|
| 196 | + * @return array formatted statuses. |
|
| 197 | + */ |
|
| 198 | + protected function get_order_statuses() { |
|
| 199 | + $formatted_statuses = array(); |
|
| 200 | + foreach ( wc_get_order_statuses() as $key => $value ) { |
|
| 201 | + $formatted_key = preg_replace( '/^wc-/', '', $key ); |
|
| 202 | + $formatted_statuses[ $formatted_key ] = $value; |
|
| 203 | + } |
|
| 204 | + return $formatted_statuses; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Used for on demand initialization of asset data and registering it with |
|
| 209 | + * the internal data registry. |
|
| 210 | + * |
|
| 211 | + * Note: core data will overwrite any externally registered data via the api. |
|
| 212 | + */ |
|
| 213 | + protected function initialize_core_data() { |
|
| 214 | + /** |
|
| 215 | + * Filters the array of shared settings. |
|
| 216 | + * |
|
| 217 | + * Low level hook for registration of new data late in the cycle. This is deprecated. |
|
| 218 | + * Instead, use the data api: |
|
| 219 | + * |
|
| 220 | + * ```php |
|
| 221 | + * Automattic\WooCommerce\Blocks\Package::container()->get( Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class )->add( $key, $value ) |
|
| 222 | + * ``` |
|
| 223 | + * |
|
| 224 | + * @deprecated |
|
| 225 | + * @param array $data Settings data. |
|
| 226 | + * @return array |
|
| 227 | + */ |
|
| 228 | + $settings = apply_filters( 'woocommerce_shared_settings', $this->data ); |
|
| 229 | + |
|
| 230 | + // Surface a deprecation warning in the error console. |
|
| 231 | + if ( has_filter( 'woocommerce_shared_settings' ) ) { |
|
| 232 | + $error_handle = 'deprecated-shared-settings-error'; |
|
| 233 | + $error_message = '`woocommerce_shared_settings` filter in Blocks is deprecated. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/docs/contributors/block-assets.md'; |
|
| 234 | + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
|
| 235 | + wp_register_script( $error_handle, '' ); |
|
| 236 | + wp_enqueue_script( $error_handle ); |
|
| 237 | + wp_add_inline_script( |
|
| 238 | + $error_handle, |
|
| 239 | + sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 240 | + ); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // note this WILL wipe any data already registered to these keys because they are protected. |
|
| 244 | + $this->data = array_replace_recursive( $settings, $this->get_core_data() ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Loops through each registered lazy data callback and adds the returned |
|
| 249 | + * value to the data array. |
|
| 250 | + * |
|
| 251 | + * This method is executed right before preparing the data for printing to |
|
| 252 | + * the rendered screen. |
|
| 253 | + * |
|
| 254 | + * @return void |
|
| 255 | + */ |
|
| 256 | + protected function execute_lazy_data() { |
|
| 257 | + foreach ( $this->lazy_data as $key => $callback ) { |
|
| 258 | + $this->data[ $key ] = $callback(); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Exposes private registered data to child classes. |
|
| 264 | + * |
|
| 265 | + * @return array The registered data on the private data property |
|
| 266 | + */ |
|
| 267 | + protected function get() { |
|
| 268 | + return $this->data; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Allows checking whether a key exists. |
|
| 273 | + * |
|
| 274 | + * @param string $key The key to check if exists. |
|
| 275 | + * @return bool Whether the key exists in the current data registry. |
|
| 276 | + */ |
|
| 277 | + public function exists( $key ) { |
|
| 278 | + return array_key_exists( $key, $this->data ); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Interface for adding data to the registry. |
|
| 283 | + * |
|
| 284 | + * You can only register data that is not already in the registry identified by the given key. If there is a |
|
| 285 | + * duplicate found, unless $ignore_duplicates is true, an exception will be thrown. |
|
| 286 | + * |
|
| 287 | + * @param string $key The key used to reference the data being registered. |
|
| 288 | + * @param mixed $data If not a function, registered to the registry as is. If a function, then the |
|
| 289 | + * callback is invoked right before output to the screen. |
|
| 290 | + * @param boolean $check_key_exists If set to true, duplicate data will be ignored if the key exists. |
|
| 291 | + * If false, duplicate data will cause an exception. |
|
| 292 | + * |
|
| 293 | + * @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error. |
|
| 294 | + */ |
|
| 295 | + public function add( $key, $data, $check_key_exists = false ) { |
|
| 296 | + if ( $check_key_exists && $this->exists( $key ) ) { |
|
| 297 | + return; |
|
| 298 | + } |
|
| 299 | + try { |
|
| 300 | + $this->add_data( $key, $data ); |
|
| 301 | + } catch ( Exception $e ) { |
|
| 302 | + if ( $this->debug() ) { |
|
| 303 | + // bubble up. |
|
| 304 | + throw $e; |
|
| 305 | + } |
|
| 306 | + wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Hydrate from API. |
|
| 312 | + * |
|
| 313 | + * @param string $path REST API path to preload. |
|
| 314 | + */ |
|
| 315 | + public function hydrate_api_request( $path ) { |
|
| 316 | + if ( ! isset( $this->preloaded_api_requests[ $path ] ) ) { |
|
| 317 | + $this->preloaded_api_requests = rest_preload_api_request( $this->preloaded_api_requests, $path ); |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Adds a page permalink to the data registry. |
|
| 323 | + * |
|
| 324 | + * @param integer $page_id Page ID to add to the registry. |
|
| 325 | + */ |
|
| 326 | + public function register_page_id( $page_id ) { |
|
| 327 | + $permalink = $page_id ? get_permalink( $page_id ) : false; |
|
| 328 | + |
|
| 329 | + if ( $permalink ) { |
|
| 330 | + $this->data[ 'page-' . $page_id ] = $permalink; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Callback for registering the data script via WordPress API. |
|
| 336 | + * |
|
| 337 | + * @return void |
|
| 338 | + */ |
|
| 339 | + public function register_data_script() { |
|
| 340 | + $this->api->register_script( |
|
| 341 | + $this->handle, |
|
| 342 | + 'build/wc-settings.js', |
|
| 343 | + [ 'wp-api-fetch' ], |
|
| 344 | + true |
|
| 345 | + ); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Callback for enqueuing asset data via the WP api. |
|
| 350 | + * |
|
| 351 | + * Note: while this is hooked into print/admin_print_scripts, it still only |
|
| 352 | + * happens if the script attached to `wc-settings` handle is enqueued. This |
|
| 353 | + * is done to allow for any potentially expensive data generation to only |
|
| 354 | + * happen for routes that need it. |
|
| 355 | + */ |
|
| 356 | + public function enqueue_asset_data() { |
|
| 357 | + if ( wp_script_is( $this->handle, 'enqueued' ) ) { |
|
| 358 | + $this->initialize_core_data(); |
|
| 359 | + $this->execute_lazy_data(); |
|
| 360 | + |
|
| 361 | + $data = rawurlencode( wp_json_encode( $this->data ) ); |
|
| 362 | + $preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) ); |
|
| 363 | + |
|
| 364 | + wp_add_inline_script( |
|
| 365 | + $this->handle, |
|
| 366 | + " |
|
| 367 | 367 | var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) ); |
| 368 | 368 | wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js( $preloaded_api_requests ) . "' ) ) ) ) |
| 369 | 369 | ", |
| 370 | - 'before' |
|
| 371 | - ); |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * See self::add() for docs. |
|
| 377 | - * |
|
| 378 | - * @param string $key Key for the data. |
|
| 379 | - * @param mixed $data Value for the data. |
|
| 380 | - * |
|
| 381 | - * @throws InvalidArgumentException If key is not a string or already |
|
| 382 | - * exists in internal data cache. |
|
| 383 | - */ |
|
| 384 | - protected function add_data( $key, $data ) { |
|
| 385 | - if ( ! is_string( $key ) ) { |
|
| 386 | - if ( $this->debug() ) { |
|
| 387 | - throw new InvalidArgumentException( |
|
| 388 | - 'Key for the data being registered must be a string' |
|
| 389 | - ); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - if ( isset( $this->data[ $key ] ) ) { |
|
| 393 | - if ( $this->debug() ) { |
|
| 394 | - throw new InvalidArgumentException( |
|
| 395 | - 'Overriding existing data with an already registered key is not allowed' |
|
| 396 | - ); |
|
| 397 | - } |
|
| 398 | - return; |
|
| 399 | - } |
|
| 400 | - if ( \is_callable( $data ) ) { |
|
| 401 | - $this->lazy_data[ $key ] = $data; |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - $this->data[ $key ] = $data; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Exposes whether the current site is in debug mode or not. |
|
| 409 | - * |
|
| 410 | - * @return boolean True means the site is in debug mode. |
|
| 411 | - */ |
|
| 412 | - protected function debug() { |
|
| 413 | - return defined( 'WP_DEBUG' ) && WP_DEBUG; |
|
| 414 | - } |
|
| 370 | + 'before' |
|
| 371 | + ); |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * See self::add() for docs. |
|
| 377 | + * |
|
| 378 | + * @param string $key Key for the data. |
|
| 379 | + * @param mixed $data Value for the data. |
|
| 380 | + * |
|
| 381 | + * @throws InvalidArgumentException If key is not a string or already |
|
| 382 | + * exists in internal data cache. |
|
| 383 | + */ |
|
| 384 | + protected function add_data( $key, $data ) { |
|
| 385 | + if ( ! is_string( $key ) ) { |
|
| 386 | + if ( $this->debug() ) { |
|
| 387 | + throw new InvalidArgumentException( |
|
| 388 | + 'Key for the data being registered must be a string' |
|
| 389 | + ); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + if ( isset( $this->data[ $key ] ) ) { |
|
| 393 | + if ( $this->debug() ) { |
|
| 394 | + throw new InvalidArgumentException( |
|
| 395 | + 'Overriding existing data with an already registered key is not allowed' |
|
| 396 | + ); |
|
| 397 | + } |
|
| 398 | + return; |
|
| 399 | + } |
|
| 400 | + if ( \is_callable( $data ) ) { |
|
| 401 | + $this->lazy_data[ $key ] = $data; |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + $this->data[ $key ] = $data; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Exposes whether the current site is in debug mode or not. |
|
| 409 | + * |
|
| 410 | + * @return boolean True means the site is in debug mode. |
|
| 411 | + */ |
|
| 412 | + protected function debug() { |
|
| 413 | + return defined( 'WP_DEBUG' ) && WP_DEBUG; |
|
| 414 | + } |
|
| 415 | 415 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @param Api $asset_api Asset API interface for various asset registration. |
| 59 | 59 | */ |
| 60 | - public function __construct( Api $asset_api ) { |
|
| 60 | + public function __construct(Api $asset_api) { |
|
| 61 | 61 | $this->api = $asset_api; |
| 62 | 62 | $this->init(); |
| 63 | 63 | } |
@@ -66,9 +66,9 @@ discard block |
||
| 66 | 66 | * Hook into WP asset registration for enqueueing asset data. |
| 67 | 67 | */ |
| 68 | 68 | protected function init() { |
| 69 | - add_action( 'init', array( $this, 'register_data_script' ) ); |
|
| 70 | - add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 71 | - add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 ); |
|
| 69 | + add_action('init', array($this, 'register_data_script')); |
|
| 70 | + add_action('wp_print_footer_scripts', array($this, 'enqueue_asset_data'), 1); |
|
| 71 | + add_action('admin_print_footer_scripts', array($this, 'enqueue_asset_data'), 1); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -84,18 +84,18 @@ discard block |
||
| 84 | 84 | 'adminUrl' => admin_url(), |
| 85 | 85 | 'countries' => WC()->countries->get_countries(), |
| 86 | 86 | 'currency' => $this->get_currency_data(), |
| 87 | - 'currentUserIsAdmin' => current_user_can( 'manage_woocommerce' ), |
|
| 88 | - 'homeUrl' => esc_url( home_url( '/' ) ), |
|
| 87 | + 'currentUserIsAdmin' => current_user_can('manage_woocommerce'), |
|
| 88 | + 'homeUrl' => esc_url(home_url('/')), |
|
| 89 | 89 | 'locale' => $this->get_locale_data(), |
| 90 | 90 | 'orderStatuses' => $this->get_order_statuses(), |
| 91 | 91 | 'placeholderImgSrc' => wc_placeholder_img_src(), |
| 92 | 92 | 'productsSettings' => $this->get_products_settings(), |
| 93 | - 'siteTitle' => get_bloginfo( 'name' ), |
|
| 93 | + 'siteTitle' => get_bloginfo('name'), |
|
| 94 | 94 | 'storePages' => $this->get_store_pages(), |
| 95 | - 'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ), |
|
| 96 | - 'wcVersion' => defined( 'WC_VERSION' ) ? WC_VERSION : '', |
|
| 95 | + 'wcAssetUrl' => plugins_url('assets/', WC_PLUGIN_FILE), |
|
| 96 | + 'wcVersion' => defined('WC_VERSION') ? WC_VERSION : '', |
|
| 97 | 97 | 'wpLoginUrl' => wp_login_url(), |
| 98 | - 'wpVersion' => get_bloginfo( 'version' ), |
|
| 98 | + 'wpVersion' => get_bloginfo('version'), |
|
| 99 | 99 | ]; |
| 100 | 100 | } |
| 101 | 101 | |
@@ -110,11 +110,11 @@ discard block |
||
| 110 | 110 | return [ |
| 111 | 111 | 'code' => $currency, |
| 112 | 112 | 'precision' => wc_get_price_decimals(), |
| 113 | - 'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ), |
|
| 114 | - 'symbolPosition' => get_option( 'woocommerce_currency_pos' ), |
|
| 113 | + 'symbol' => html_entity_decode(get_woocommerce_currency_symbol($currency)), |
|
| 114 | + 'symbolPosition' => get_option('woocommerce_currency_pos'), |
|
| 115 | 115 | 'decimalSeparator' => wc_get_price_decimal_separator(), |
| 116 | 116 | 'thousandSeparator' => wc_get_price_thousand_separator(), |
| 117 | - 'priceFormat' => html_entity_decode( get_woocommerce_price_format() ), |
|
| 117 | + 'priceFormat' => html_entity_decode(get_woocommerce_price_format()), |
|
| 118 | 118 | ]; |
| 119 | 119 | } |
| 120 | 120 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | return [ |
| 130 | 130 | 'siteLocale' => get_locale(), |
| 131 | 131 | 'userLocale' => get_user_locale(), |
| 132 | - 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), |
|
| 132 | + 'weekdaysShort' => array_values($wp_locale->weekday_abbrev), |
|
| 133 | 133 | ]; |
| 134 | 134 | } |
| 135 | 135 | |
@@ -140,12 +140,12 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | protected function get_store_pages() { |
| 142 | 142 | return array_map( |
| 143 | - [ $this, 'format_page_resource' ], |
|
| 143 | + [$this, 'format_page_resource'], |
|
| 144 | 144 | [ |
| 145 | - 'myaccount' => wc_get_page_id( 'myaccount' ), |
|
| 146 | - 'shop' => wc_get_page_id( 'shop' ), |
|
| 147 | - 'cart' => wc_get_page_id( 'cart' ), |
|
| 148 | - 'checkout' => wc_get_page_id( 'checkout' ), |
|
| 145 | + 'myaccount' => wc_get_page_id('myaccount'), |
|
| 146 | + 'shop' => wc_get_page_id('shop'), |
|
| 147 | + 'cart' => wc_get_page_id('cart'), |
|
| 148 | + 'checkout' => wc_get_page_id('checkout'), |
|
| 149 | 149 | 'privacy' => wc_privacy_policy_page_id(), |
| 150 | 150 | 'terms' => wc_terms_and_conditions_page_id(), |
| 151 | 151 | ] |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | protected function get_products_settings() { |
| 163 | 163 | return [ |
| 164 | - 'cartRedirectAfterAdd' => get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes', |
|
| 164 | + 'cartRedirectAfterAdd' => get_option('woocommerce_cart_redirect_after_add') === 'yes', |
|
| 165 | 165 | ]; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -171,11 +171,11 @@ discard block |
||
| 171 | 171 | * @param WP_Post|int $page Page object or ID. |
| 172 | 172 | * @return array |
| 173 | 173 | */ |
| 174 | - protected function format_page_resource( $page ) { |
|
| 175 | - if ( is_numeric( $page ) && $page > 0 ) { |
|
| 176 | - $page = get_post( $page ); |
|
| 174 | + protected function format_page_resource($page) { |
|
| 175 | + if (is_numeric($page) && $page > 0) { |
|
| 176 | + $page = get_post($page); |
|
| 177 | 177 | } |
| 178 | - if ( ! is_a( $page, '\WP_Post' ) || 'publish' !== $page->post_status ) { |
|
| 178 | + if (!is_a($page, '\WP_Post') || 'publish' !== $page->post_status) { |
|
| 179 | 179 | return [ |
| 180 | 180 | 'id' => 0, |
| 181 | 181 | 'title' => '', |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | return [ |
| 186 | 186 | 'id' => $page->ID, |
| 187 | 187 | 'title' => $page->post_title, |
| 188 | - 'permalink' => get_permalink( $page->ID ), |
|
| 188 | + 'permalink' => get_permalink($page->ID), |
|
| 189 | 189 | ]; |
| 190 | 190 | } |
| 191 | 191 | |
@@ -197,9 +197,9 @@ discard block |
||
| 197 | 197 | */ |
| 198 | 198 | protected function get_order_statuses() { |
| 199 | 199 | $formatted_statuses = array(); |
| 200 | - foreach ( wc_get_order_statuses() as $key => $value ) { |
|
| 201 | - $formatted_key = preg_replace( '/^wc-/', '', $key ); |
|
| 202 | - $formatted_statuses[ $formatted_key ] = $value; |
|
| 200 | + foreach (wc_get_order_statuses() as $key => $value) { |
|
| 201 | + $formatted_key = preg_replace('/^wc-/', '', $key); |
|
| 202 | + $formatted_statuses[$formatted_key] = $value; |
|
| 203 | 203 | } |
| 204 | 204 | return $formatted_statuses; |
| 205 | 205 | } |
@@ -225,23 +225,23 @@ discard block |
||
| 225 | 225 | * @param array $data Settings data. |
| 226 | 226 | * @return array |
| 227 | 227 | */ |
| 228 | - $settings = apply_filters( 'woocommerce_shared_settings', $this->data ); |
|
| 228 | + $settings = apply_filters('woocommerce_shared_settings', $this->data); |
|
| 229 | 229 | |
| 230 | 230 | // Surface a deprecation warning in the error console. |
| 231 | - if ( has_filter( 'woocommerce_shared_settings' ) ) { |
|
| 231 | + if (has_filter('woocommerce_shared_settings')) { |
|
| 232 | 232 | $error_handle = 'deprecated-shared-settings-error'; |
| 233 | 233 | $error_message = '`woocommerce_shared_settings` filter in Blocks is deprecated. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/docs/contributors/block-assets.md'; |
| 234 | 234 | // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter,WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 235 | - wp_register_script( $error_handle, '' ); |
|
| 236 | - wp_enqueue_script( $error_handle ); |
|
| 235 | + wp_register_script($error_handle, ''); |
|
| 236 | + wp_enqueue_script($error_handle); |
|
| 237 | 237 | wp_add_inline_script( |
| 238 | 238 | $error_handle, |
| 239 | - sprintf( 'console.warn( "%s" );', $error_message ) |
|
| 239 | + sprintf('console.warn( "%s" );', $error_message) |
|
| 240 | 240 | ); |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | // note this WILL wipe any data already registered to these keys because they are protected. |
| 244 | - $this->data = array_replace_recursive( $settings, $this->get_core_data() ); |
|
| 244 | + $this->data = array_replace_recursive($settings, $this->get_core_data()); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -254,8 +254,8 @@ discard block |
||
| 254 | 254 | * @return void |
| 255 | 255 | */ |
| 256 | 256 | protected function execute_lazy_data() { |
| 257 | - foreach ( $this->lazy_data as $key => $callback ) { |
|
| 258 | - $this->data[ $key ] = $callback(); |
|
| 257 | + foreach ($this->lazy_data as $key => $callback) { |
|
| 258 | + $this->data[$key] = $callback(); |
|
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | |
@@ -274,8 +274,8 @@ discard block |
||
| 274 | 274 | * @param string $key The key to check if exists. |
| 275 | 275 | * @return bool Whether the key exists in the current data registry. |
| 276 | 276 | */ |
| 277 | - public function exists( $key ) { |
|
| 278 | - return array_key_exists( $key, $this->data ); |
|
| 277 | + public function exists($key) { |
|
| 278 | + return array_key_exists($key, $this->data); |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | /** |
@@ -292,18 +292,18 @@ discard block |
||
| 292 | 292 | * |
| 293 | 293 | * @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error. |
| 294 | 294 | */ |
| 295 | - public function add( $key, $data, $check_key_exists = false ) { |
|
| 296 | - if ( $check_key_exists && $this->exists( $key ) ) { |
|
| 295 | + public function add($key, $data, $check_key_exists = false) { |
|
| 296 | + if ($check_key_exists && $this->exists($key)) { |
|
| 297 | 297 | return; |
| 298 | 298 | } |
| 299 | 299 | try { |
| 300 | - $this->add_data( $key, $data ); |
|
| 301 | - } catch ( Exception $e ) { |
|
| 302 | - if ( $this->debug() ) { |
|
| 300 | + $this->add_data($key, $data); |
|
| 301 | + } catch (Exception $e) { |
|
| 302 | + if ($this->debug()) { |
|
| 303 | 303 | // bubble up. |
| 304 | 304 | throw $e; |
| 305 | 305 | } |
| 306 | - wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); |
|
| 306 | + wc_caught_exception($e, __METHOD__, [$key, $data]); |
|
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | * |
| 313 | 313 | * @param string $path REST API path to preload. |
| 314 | 314 | */ |
| 315 | - public function hydrate_api_request( $path ) { |
|
| 316 | - if ( ! isset( $this->preloaded_api_requests[ $path ] ) ) { |
|
| 317 | - $this->preloaded_api_requests = rest_preload_api_request( $this->preloaded_api_requests, $path ); |
|
| 315 | + public function hydrate_api_request($path) { |
|
| 316 | + if (!isset($this->preloaded_api_requests[$path])) { |
|
| 317 | + $this->preloaded_api_requests = rest_preload_api_request($this->preloaded_api_requests, $path); |
|
| 318 | 318 | } |
| 319 | 319 | } |
| 320 | 320 | |
@@ -323,11 +323,11 @@ discard block |
||
| 323 | 323 | * |
| 324 | 324 | * @param integer $page_id Page ID to add to the registry. |
| 325 | 325 | */ |
| 326 | - public function register_page_id( $page_id ) { |
|
| 327 | - $permalink = $page_id ? get_permalink( $page_id ) : false; |
|
| 326 | + public function register_page_id($page_id) { |
|
| 327 | + $permalink = $page_id ? get_permalink($page_id) : false; |
|
| 328 | 328 | |
| 329 | - if ( $permalink ) { |
|
| 330 | - $this->data[ 'page-' . $page_id ] = $permalink; |
|
| 329 | + if ($permalink) { |
|
| 330 | + $this->data['page-' . $page_id] = $permalink; |
|
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | $this->api->register_script( |
| 341 | 341 | $this->handle, |
| 342 | 342 | 'build/wc-settings.js', |
| 343 | - [ 'wp-api-fetch' ], |
|
| 343 | + ['wp-api-fetch'], |
|
| 344 | 344 | true |
| 345 | 345 | ); |
| 346 | 346 | } |
@@ -354,18 +354,18 @@ discard block |
||
| 354 | 354 | * happen for routes that need it. |
| 355 | 355 | */ |
| 356 | 356 | public function enqueue_asset_data() { |
| 357 | - if ( wp_script_is( $this->handle, 'enqueued' ) ) { |
|
| 357 | + if (wp_script_is($this->handle, 'enqueued')) { |
|
| 358 | 358 | $this->initialize_core_data(); |
| 359 | 359 | $this->execute_lazy_data(); |
| 360 | 360 | |
| 361 | - $data = rawurlencode( wp_json_encode( $this->data ) ); |
|
| 362 | - $preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) ); |
|
| 361 | + $data = rawurlencode(wp_json_encode($this->data)); |
|
| 362 | + $preloaded_api_requests = rawurlencode(wp_json_encode($this->preloaded_api_requests)); |
|
| 363 | 363 | |
| 364 | 364 | wp_add_inline_script( |
| 365 | 365 | $this->handle, |
| 366 | 366 | " |
| 367 | - var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) ); |
|
| 368 | - wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js( $preloaded_api_requests ) . "' ) ) ) ) |
|
| 367 | + var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js($data) . "' ) ); |
|
| 368 | + wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js($preloaded_api_requests) . "' ) ) ) ) |
|
| 369 | 369 | ", |
| 370 | 370 | 'before' |
| 371 | 371 | ); |
@@ -381,27 +381,27 @@ discard block |
||
| 381 | 381 | * @throws InvalidArgumentException If key is not a string or already |
| 382 | 382 | * exists in internal data cache. |
| 383 | 383 | */ |
| 384 | - protected function add_data( $key, $data ) { |
|
| 385 | - if ( ! is_string( $key ) ) { |
|
| 386 | - if ( $this->debug() ) { |
|
| 384 | + protected function add_data($key, $data) { |
|
| 385 | + if (!is_string($key)) { |
|
| 386 | + if ($this->debug()) { |
|
| 387 | 387 | throw new InvalidArgumentException( |
| 388 | 388 | 'Key for the data being registered must be a string' |
| 389 | 389 | ); |
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | - if ( isset( $this->data[ $key ] ) ) { |
|
| 393 | - if ( $this->debug() ) { |
|
| 392 | + if (isset($this->data[$key])) { |
|
| 393 | + if ($this->debug()) { |
|
| 394 | 394 | throw new InvalidArgumentException( |
| 395 | 395 | 'Overriding existing data with an already registered key is not allowed' |
| 396 | 396 | ); |
| 397 | 397 | } |
| 398 | 398 | return; |
| 399 | 399 | } |
| 400 | - if ( \is_callable( $data ) ) { |
|
| 401 | - $this->lazy_data[ $key ] = $data; |
|
| 400 | + if (\is_callable($data)) { |
|
| 401 | + $this->lazy_data[$key] = $data; |
|
| 402 | 402 | return; |
| 403 | 403 | } |
| 404 | - $this->data[ $key ] = $data; |
|
| 404 | + $this->data[$key] = $data; |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | /** |
@@ -410,6 +410,6 @@ discard block |
||
| 410 | 410 | * @return boolean True means the site is in debug mode. |
| 411 | 411 | */ |
| 412 | 412 | protected function debug() { |
| 413 | - return defined( 'WP_DEBUG' ) && WP_DEBUG; |
|
| 413 | + return defined('WP_DEBUG') && WP_DEBUG; |
|
| 414 | 414 | } |
| 415 | 415 | } |
@@ -11,210 +11,210 @@ |
||
| 11 | 11 | * @since 2.5.0 |
| 12 | 12 | */ |
| 13 | 13 | class Api { |
| 14 | - /** |
|
| 15 | - * Stores inline scripts already enqueued. |
|
| 16 | - * |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - private $inline_scripts = []; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Reference to the Package instance |
|
| 23 | - * |
|
| 24 | - * @var Package |
|
| 25 | - */ |
|
| 26 | - private $package; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Constructor for class |
|
| 30 | - * |
|
| 31 | - * @param Package $package An instance of Package. |
|
| 32 | - */ |
|
| 33 | - public function __construct( Package $package ) { |
|
| 34 | - $this->package = $package; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Get the file modified time as a cache buster if we're in dev mode. |
|
| 39 | - * |
|
| 40 | - * @param string $file Local path to the file (relative to the plugin |
|
| 41 | - * directory). |
|
| 42 | - * @return string The cache buster value to use for the given file. |
|
| 43 | - */ |
|
| 44 | - protected function get_file_version( $file ) { |
|
| 45 | - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) { |
|
| 46 | - return filemtime( $this->package->get_path( trim( $file, '/' ) ) ); |
|
| 47 | - } |
|
| 48 | - return $this->package->get_version(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Retrieve the url to an asset for this plugin. |
|
| 53 | - * |
|
| 54 | - * @param string $relative_path An optional relative path appended to the |
|
| 55 | - * returned url. |
|
| 56 | - * |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - protected function get_asset_url( $relative_path = '' ) { |
|
| 60 | - return $this->package->get_url( $relative_path ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the path to a block's metadata |
|
| 65 | - * |
|
| 66 | - * @param string $block_name The block to get metadata for. |
|
| 67 | - * |
|
| 68 | - * @return string|boolean False if metadata file is not found for the block. |
|
| 69 | - */ |
|
| 70 | - public function get_block_metadata_path( $block_name ) { |
|
| 71 | - $path_to_metadata_from_plugin_root = $this->package->get_path( 'build/' . $block_name . '/block.json' ); |
|
| 72 | - if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - return $path_to_metadata_from_plugin_root; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get src, version and dependencies given a script relative src. |
|
| 80 | - * |
|
| 81 | - * @param string $relative_src Relative src to the script. |
|
| 82 | - * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
|
| 83 | - * |
|
| 84 | - * @return array src, version and dependencies of the script. |
|
| 85 | - */ |
|
| 86 | - public function get_script_data( $relative_src, $dependencies = [] ) { |
|
| 87 | - $src = ''; |
|
| 88 | - $version = '1'; |
|
| 89 | - |
|
| 90 | - if ( $relative_src ) { |
|
| 91 | - $src = $this->get_asset_url( $relative_src ); |
|
| 92 | - $asset_path = $this->package->get_path( |
|
| 93 | - str_replace( '.js', '.asset.php', $relative_src ) |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - if ( file_exists( $asset_path ) ) { |
|
| 97 | - $asset = require $asset_path; |
|
| 98 | - $dependencies = isset( $asset['dependencies'] ) ? array_merge( $asset['dependencies'], $dependencies ) : $dependencies; |
|
| 99 | - $version = ! empty( $asset['version'] ) ? $asset['version'] : $this->get_file_version( $relative_src ); |
|
| 100 | - } else { |
|
| 101 | - $version = $this->get_file_version( $relative_src ); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return array( |
|
| 106 | - 'src' => $src, |
|
| 107 | - 'version' => $version, |
|
| 108 | - 'dependencies' => $dependencies, |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations. |
|
| 114 | - * |
|
| 115 | - * When creating script assets, the following rules should be followed: |
|
| 116 | - * 1. All asset handles should have a `wc-` prefix. |
|
| 117 | - * 2. If the asset handle is for a Block (in editor context) use the `-block` suffix. |
|
| 118 | - * 3. If the asset handle is for a Block (in frontend context) use the `-block-frontend` suffix. |
|
| 119 | - * 4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the `wc-blocks-` prefix. |
|
| 120 | - * |
|
| 121 | - * @since 2.5.0 |
|
| 122 | - * @throws Exception If the registered script has a dependency on itself. |
|
| 123 | - * |
|
| 124 | - * @param string $handle Unique name of the script. |
|
| 125 | - * @param string $relative_src Relative url for the script to the path from plugin root. |
|
| 126 | - * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
|
| 127 | - * @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default: true. |
|
| 128 | - */ |
|
| 129 | - public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) { |
|
| 130 | - $script_data = $this->get_script_data( $relative_src, $dependencies ); |
|
| 131 | - |
|
| 132 | - if ( in_array( $handle, $script_data['dependencies'], true ) ) { |
|
| 133 | - if ( $this->package->feature()->is_development_environment() ) { |
|
| 134 | - $dependencies = array_diff( $script_data['dependencies'], [ $handle ] ); |
|
| 135 | - add_action( |
|
| 136 | - 'admin_notices', |
|
| 137 | - function() use ( $handle ) { |
|
| 138 | - echo '<div class="error"><p>'; |
|
| 139 | - /* translators: %s file handle name. */ |
|
| 140 | - printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) ); |
|
| 141 | - echo '</p></div>'; |
|
| 142 | - } |
|
| 143 | - ); |
|
| 144 | - } else { |
|
| 145 | - throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) ); |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Filters the list of script dependencies. |
|
| 151 | - * |
|
| 152 | - * @param array $dependencies The list of script dependencies. |
|
| 153 | - * @param string $handle The script's handle. |
|
| 154 | - * @return array |
|
| 155 | - */ |
|
| 156 | - $script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle ); |
|
| 157 | - |
|
| 158 | - wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true ); |
|
| 159 | - |
|
| 160 | - if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) { |
|
| 161 | - wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) ); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Registers a style according to `wp_register_style`. |
|
| 167 | - * |
|
| 168 | - * @since 2.5.0 |
|
| 169 | - * @since 2.6.0 Change src to be relative source. |
|
| 170 | - * |
|
| 171 | - * @param string $handle Name of the stylesheet. Should be unique. |
|
| 172 | - * @param string $relative_src Relative source of the stylesheet to the plugin path. |
|
| 173 | - * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|
| 174 | - * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
|
| 175 | - * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
|
| 176 | - */ |
|
| 177 | - public function register_style( $handle, $relative_src, $deps = [], $media = 'all' ) { |
|
| 178 | - $filename = str_replace( plugins_url( '/', __DIR__ ), '', $relative_src ); |
|
| 179 | - $src = $this->get_asset_url( $relative_src ); |
|
| 180 | - $ver = $this->get_file_version( $filename ); |
|
| 181 | - wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Returns the appropriate asset path for loading either legacy builds or |
|
| 186 | - * current builds. |
|
| 187 | - * |
|
| 188 | - * @param string $filename Filename for asset path (without extension). |
|
| 189 | - * @param string $type File type (.css or .js). |
|
| 190 | - * |
|
| 191 | - * @return string The generated path. |
|
| 192 | - */ |
|
| 193 | - public function get_block_asset_build_path( $filename, $type = 'js' ) { |
|
| 194 | - global $wp_version; |
|
| 195 | - $suffix = version_compare( $wp_version, '5.3', '>=' ) |
|
| 196 | - ? '' |
|
| 197 | - : '-legacy'; |
|
| 198 | - return "build/$filename$suffix.$type"; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Adds an inline script, once. |
|
| 203 | - * |
|
| 204 | - * @param string $handle Script handle. |
|
| 205 | - * @param string $script Script contents. |
|
| 206 | - */ |
|
| 207 | - public function add_inline_script( $handle, $script ) { |
|
| 208 | - if ( ! empty( $this->inline_scripts[ $handle ] ) && in_array( $script, $this->inline_scripts[ $handle ], true ) ) { |
|
| 209 | - return; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - wp_add_inline_script( $handle, $script ); |
|
| 213 | - |
|
| 214 | - if ( isset( $this->inline_scripts[ $handle ] ) ) { |
|
| 215 | - $this->inline_scripts[ $handle ][] = $script; |
|
| 216 | - } else { |
|
| 217 | - $this->inline_scripts[ $handle ] = array( $script ); |
|
| 218 | - } |
|
| 219 | - } |
|
| 14 | + /** |
|
| 15 | + * Stores inline scripts already enqueued. |
|
| 16 | + * |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + private $inline_scripts = []; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Reference to the Package instance |
|
| 23 | + * |
|
| 24 | + * @var Package |
|
| 25 | + */ |
|
| 26 | + private $package; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Constructor for class |
|
| 30 | + * |
|
| 31 | + * @param Package $package An instance of Package. |
|
| 32 | + */ |
|
| 33 | + public function __construct( Package $package ) { |
|
| 34 | + $this->package = $package; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Get the file modified time as a cache buster if we're in dev mode. |
|
| 39 | + * |
|
| 40 | + * @param string $file Local path to the file (relative to the plugin |
|
| 41 | + * directory). |
|
| 42 | + * @return string The cache buster value to use for the given file. |
|
| 43 | + */ |
|
| 44 | + protected function get_file_version( $file ) { |
|
| 45 | + if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) { |
|
| 46 | + return filemtime( $this->package->get_path( trim( $file, '/' ) ) ); |
|
| 47 | + } |
|
| 48 | + return $this->package->get_version(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Retrieve the url to an asset for this plugin. |
|
| 53 | + * |
|
| 54 | + * @param string $relative_path An optional relative path appended to the |
|
| 55 | + * returned url. |
|
| 56 | + * |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + protected function get_asset_url( $relative_path = '' ) { |
|
| 60 | + return $this->package->get_url( $relative_path ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the path to a block's metadata |
|
| 65 | + * |
|
| 66 | + * @param string $block_name The block to get metadata for. |
|
| 67 | + * |
|
| 68 | + * @return string|boolean False if metadata file is not found for the block. |
|
| 69 | + */ |
|
| 70 | + public function get_block_metadata_path( $block_name ) { |
|
| 71 | + $path_to_metadata_from_plugin_root = $this->package->get_path( 'build/' . $block_name . '/block.json' ); |
|
| 72 | + if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + return $path_to_metadata_from_plugin_root; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get src, version and dependencies given a script relative src. |
|
| 80 | + * |
|
| 81 | + * @param string $relative_src Relative src to the script. |
|
| 82 | + * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
|
| 83 | + * |
|
| 84 | + * @return array src, version and dependencies of the script. |
|
| 85 | + */ |
|
| 86 | + public function get_script_data( $relative_src, $dependencies = [] ) { |
|
| 87 | + $src = ''; |
|
| 88 | + $version = '1'; |
|
| 89 | + |
|
| 90 | + if ( $relative_src ) { |
|
| 91 | + $src = $this->get_asset_url( $relative_src ); |
|
| 92 | + $asset_path = $this->package->get_path( |
|
| 93 | + str_replace( '.js', '.asset.php', $relative_src ) |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + if ( file_exists( $asset_path ) ) { |
|
| 97 | + $asset = require $asset_path; |
|
| 98 | + $dependencies = isset( $asset['dependencies'] ) ? array_merge( $asset['dependencies'], $dependencies ) : $dependencies; |
|
| 99 | + $version = ! empty( $asset['version'] ) ? $asset['version'] : $this->get_file_version( $relative_src ); |
|
| 100 | + } else { |
|
| 101 | + $version = $this->get_file_version( $relative_src ); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return array( |
|
| 106 | + 'src' => $src, |
|
| 107 | + 'version' => $version, |
|
| 108 | + 'dependencies' => $dependencies, |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations. |
|
| 114 | + * |
|
| 115 | + * When creating script assets, the following rules should be followed: |
|
| 116 | + * 1. All asset handles should have a `wc-` prefix. |
|
| 117 | + * 2. If the asset handle is for a Block (in editor context) use the `-block` suffix. |
|
| 118 | + * 3. If the asset handle is for a Block (in frontend context) use the `-block-frontend` suffix. |
|
| 119 | + * 4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the `wc-blocks-` prefix. |
|
| 120 | + * |
|
| 121 | + * @since 2.5.0 |
|
| 122 | + * @throws Exception If the registered script has a dependency on itself. |
|
| 123 | + * |
|
| 124 | + * @param string $handle Unique name of the script. |
|
| 125 | + * @param string $relative_src Relative url for the script to the path from plugin root. |
|
| 126 | + * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
|
| 127 | + * @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default: true. |
|
| 128 | + */ |
|
| 129 | + public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) { |
|
| 130 | + $script_data = $this->get_script_data( $relative_src, $dependencies ); |
|
| 131 | + |
|
| 132 | + if ( in_array( $handle, $script_data['dependencies'], true ) ) { |
|
| 133 | + if ( $this->package->feature()->is_development_environment() ) { |
|
| 134 | + $dependencies = array_diff( $script_data['dependencies'], [ $handle ] ); |
|
| 135 | + add_action( |
|
| 136 | + 'admin_notices', |
|
| 137 | + function() use ( $handle ) { |
|
| 138 | + echo '<div class="error"><p>'; |
|
| 139 | + /* translators: %s file handle name. */ |
|
| 140 | + printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) ); |
|
| 141 | + echo '</p></div>'; |
|
| 142 | + } |
|
| 143 | + ); |
|
| 144 | + } else { |
|
| 145 | + throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) ); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Filters the list of script dependencies. |
|
| 151 | + * |
|
| 152 | + * @param array $dependencies The list of script dependencies. |
|
| 153 | + * @param string $handle The script's handle. |
|
| 154 | + * @return array |
|
| 155 | + */ |
|
| 156 | + $script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle ); |
|
| 157 | + |
|
| 158 | + wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true ); |
|
| 159 | + |
|
| 160 | + if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) { |
|
| 161 | + wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) ); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Registers a style according to `wp_register_style`. |
|
| 167 | + * |
|
| 168 | + * @since 2.5.0 |
|
| 169 | + * @since 2.6.0 Change src to be relative source. |
|
| 170 | + * |
|
| 171 | + * @param string $handle Name of the stylesheet. Should be unique. |
|
| 172 | + * @param string $relative_src Relative source of the stylesheet to the plugin path. |
|
| 173 | + * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|
| 174 | + * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
|
| 175 | + * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
|
| 176 | + */ |
|
| 177 | + public function register_style( $handle, $relative_src, $deps = [], $media = 'all' ) { |
|
| 178 | + $filename = str_replace( plugins_url( '/', __DIR__ ), '', $relative_src ); |
|
| 179 | + $src = $this->get_asset_url( $relative_src ); |
|
| 180 | + $ver = $this->get_file_version( $filename ); |
|
| 181 | + wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Returns the appropriate asset path for loading either legacy builds or |
|
| 186 | + * current builds. |
|
| 187 | + * |
|
| 188 | + * @param string $filename Filename for asset path (without extension). |
|
| 189 | + * @param string $type File type (.css or .js). |
|
| 190 | + * |
|
| 191 | + * @return string The generated path. |
|
| 192 | + */ |
|
| 193 | + public function get_block_asset_build_path( $filename, $type = 'js' ) { |
|
| 194 | + global $wp_version; |
|
| 195 | + $suffix = version_compare( $wp_version, '5.3', '>=' ) |
|
| 196 | + ? '' |
|
| 197 | + : '-legacy'; |
|
| 198 | + return "build/$filename$suffix.$type"; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Adds an inline script, once. |
|
| 203 | + * |
|
| 204 | + * @param string $handle Script handle. |
|
| 205 | + * @param string $script Script contents. |
|
| 206 | + */ |
|
| 207 | + public function add_inline_script( $handle, $script ) { |
|
| 208 | + if ( ! empty( $this->inline_scripts[ $handle ] ) && in_array( $script, $this->inline_scripts[ $handle ], true ) ) { |
|
| 209 | + return; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + wp_add_inline_script( $handle, $script ); |
|
| 213 | + |
|
| 214 | + if ( isset( $this->inline_scripts[ $handle ] ) ) { |
|
| 215 | + $this->inline_scripts[ $handle ][] = $script; |
|
| 216 | + } else { |
|
| 217 | + $this->inline_scripts[ $handle ] = array( $script ); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | 220 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * |
| 31 | 31 | * @param Package $package An instance of Package. |
| 32 | 32 | */ |
| 33 | - public function __construct( Package $package ) { |
|
| 33 | + public function __construct(Package $package) { |
|
| 34 | 34 | $this->package = $package; |
| 35 | 35 | } |
| 36 | 36 | |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | * directory). |
| 42 | 42 | * @return string The cache buster value to use for the given file. |
| 43 | 43 | */ |
| 44 | - protected function get_file_version( $file ) { |
|
| 45 | - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $this->package->get_path() . $file ) ) { |
|
| 46 | - return filemtime( $this->package->get_path( trim( $file, '/' ) ) ); |
|
| 44 | + protected function get_file_version($file) { |
|
| 45 | + if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && file_exists($this->package->get_path() . $file)) { |
|
| 46 | + return filemtime($this->package->get_path(trim($file, '/'))); |
|
| 47 | 47 | } |
| 48 | 48 | return $this->package->get_version(); |
| 49 | 49 | } |
@@ -56,8 +56,8 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return string |
| 58 | 58 | */ |
| 59 | - protected function get_asset_url( $relative_path = '' ) { |
|
| 60 | - return $this->package->get_url( $relative_path ); |
|
| 59 | + protected function get_asset_url($relative_path = '') { |
|
| 60 | + return $this->package->get_url($relative_path); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return string|boolean False if metadata file is not found for the block. |
| 69 | 69 | */ |
| 70 | - public function get_block_metadata_path( $block_name ) { |
|
| 71 | - $path_to_metadata_from_plugin_root = $this->package->get_path( 'build/' . $block_name . '/block.json' ); |
|
| 72 | - if ( ! file_exists( $path_to_metadata_from_plugin_root ) ) { |
|
| 70 | + public function get_block_metadata_path($block_name) { |
|
| 71 | + $path_to_metadata_from_plugin_root = $this->package->get_path('build/' . $block_name . '/block.json'); |
|
| 72 | + if (!file_exists($path_to_metadata_from_plugin_root)) { |
|
| 73 | 73 | return false; |
| 74 | 74 | } |
| 75 | 75 | return $path_to_metadata_from_plugin_root; |
@@ -83,22 +83,22 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return array src, version and dependencies of the script. |
| 85 | 85 | */ |
| 86 | - public function get_script_data( $relative_src, $dependencies = [] ) { |
|
| 86 | + public function get_script_data($relative_src, $dependencies = []) { |
|
| 87 | 87 | $src = ''; |
| 88 | 88 | $version = '1'; |
| 89 | 89 | |
| 90 | - if ( $relative_src ) { |
|
| 91 | - $src = $this->get_asset_url( $relative_src ); |
|
| 90 | + if ($relative_src) { |
|
| 91 | + $src = $this->get_asset_url($relative_src); |
|
| 92 | 92 | $asset_path = $this->package->get_path( |
| 93 | - str_replace( '.js', '.asset.php', $relative_src ) |
|
| 93 | + str_replace('.js', '.asset.php', $relative_src) |
|
| 94 | 94 | ); |
| 95 | 95 | |
| 96 | - if ( file_exists( $asset_path ) ) { |
|
| 96 | + if (file_exists($asset_path)) { |
|
| 97 | 97 | $asset = require $asset_path; |
| 98 | - $dependencies = isset( $asset['dependencies'] ) ? array_merge( $asset['dependencies'], $dependencies ) : $dependencies; |
|
| 99 | - $version = ! empty( $asset['version'] ) ? $asset['version'] : $this->get_file_version( $relative_src ); |
|
| 98 | + $dependencies = isset($asset['dependencies']) ? array_merge($asset['dependencies'], $dependencies) : $dependencies; |
|
| 99 | + $version = !empty($asset['version']) ? $asset['version'] : $this->get_file_version($relative_src); |
|
| 100 | 100 | } else { |
| 101 | - $version = $this->get_file_version( $relative_src ); |
|
| 101 | + $version = $this->get_file_version($relative_src); |
|
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
@@ -126,23 +126,23 @@ discard block |
||
| 126 | 126 | * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. |
| 127 | 127 | * @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default: true. |
| 128 | 128 | */ |
| 129 | - public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) { |
|
| 130 | - $script_data = $this->get_script_data( $relative_src, $dependencies ); |
|
| 129 | + public function register_script($handle, $relative_src, $dependencies = [], $has_i18n = true) { |
|
| 130 | + $script_data = $this->get_script_data($relative_src, $dependencies); |
|
| 131 | 131 | |
| 132 | - if ( in_array( $handle, $script_data['dependencies'], true ) ) { |
|
| 133 | - if ( $this->package->feature()->is_development_environment() ) { |
|
| 134 | - $dependencies = array_diff( $script_data['dependencies'], [ $handle ] ); |
|
| 132 | + if (in_array($handle, $script_data['dependencies'], true)) { |
|
| 133 | + if ($this->package->feature()->is_development_environment()) { |
|
| 134 | + $dependencies = array_diff($script_data['dependencies'], [$handle]); |
|
| 135 | 135 | add_action( |
| 136 | 136 | 'admin_notices', |
| 137 | - function() use ( $handle ) { |
|
| 137 | + function() use ($handle) { |
|
| 138 | 138 | echo '<div class="error"><p>'; |
| 139 | 139 | /* translators: %s file handle name. */ |
| 140 | - printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) ); |
|
| 140 | + printf(esc_html__('Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce'), esc_html($handle)); |
|
| 141 | 141 | echo '</p></div>'; |
| 142 | 142 | } |
| 143 | 143 | ); |
| 144 | 144 | } else { |
| 145 | - throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) ); |
|
| 145 | + throw new Exception(sprintf('Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle)); |
|
| 146 | 146 | } |
| 147 | 147 | } |
| 148 | 148 | |
@@ -153,12 +153,12 @@ discard block |
||
| 153 | 153 | * @param string $handle The script's handle. |
| 154 | 154 | * @return array |
| 155 | 155 | */ |
| 156 | - $script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle ); |
|
| 156 | + $script_dependencies = apply_filters('woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle); |
|
| 157 | 157 | |
| 158 | - wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true ); |
|
| 158 | + wp_register_script($handle, $script_data['src'], $script_dependencies, $script_data['version'], true); |
|
| 159 | 159 | |
| 160 | - if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) { |
|
| 161 | - wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) ); |
|
| 160 | + if ($has_i18n && function_exists('wp_set_script_translations')) { |
|
| 161 | + wp_set_script_translations($handle, 'woocommerce', $this->package->get_path('languages')); |
|
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
@@ -174,11 +174,11 @@ discard block |
||
| 174 | 174 | * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like |
| 175 | 175 | * 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 176 | 176 | */ |
| 177 | - public function register_style( $handle, $relative_src, $deps = [], $media = 'all' ) { |
|
| 178 | - $filename = str_replace( plugins_url( '/', __DIR__ ), '', $relative_src ); |
|
| 179 | - $src = $this->get_asset_url( $relative_src ); |
|
| 180 | - $ver = $this->get_file_version( $filename ); |
|
| 181 | - wp_register_style( $handle, $src, $deps, $ver, $media ); |
|
| 177 | + public function register_style($handle, $relative_src, $deps = [], $media = 'all') { |
|
| 178 | + $filename = str_replace(plugins_url('/', __DIR__), '', $relative_src); |
|
| 179 | + $src = $this->get_asset_url($relative_src); |
|
| 180 | + $ver = $this->get_file_version($filename); |
|
| 181 | + wp_register_style($handle, $src, $deps, $ver, $media); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @return string The generated path. |
| 192 | 192 | */ |
| 193 | - public function get_block_asset_build_path( $filename, $type = 'js' ) { |
|
| 193 | + public function get_block_asset_build_path($filename, $type = 'js') { |
|
| 194 | 194 | global $wp_version; |
| 195 | - $suffix = version_compare( $wp_version, '5.3', '>=' ) |
|
| 195 | + $suffix = version_compare($wp_version, '5.3', '>=') |
|
| 196 | 196 | ? '' |
| 197 | 197 | : '-legacy'; |
| 198 | 198 | return "build/$filename$suffix.$type"; |
@@ -204,17 +204,17 @@ discard block |
||
| 204 | 204 | * @param string $handle Script handle. |
| 205 | 205 | * @param string $script Script contents. |
| 206 | 206 | */ |
| 207 | - public function add_inline_script( $handle, $script ) { |
|
| 208 | - if ( ! empty( $this->inline_scripts[ $handle ] ) && in_array( $script, $this->inline_scripts[ $handle ], true ) ) { |
|
| 207 | + public function add_inline_script($handle, $script) { |
|
| 208 | + if (!empty($this->inline_scripts[$handle]) && in_array($script, $this->inline_scripts[$handle], true)) { |
|
| 209 | 209 | return; |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - wp_add_inline_script( $handle, $script ); |
|
| 212 | + wp_add_inline_script($handle, $script); |
|
| 213 | 213 | |
| 214 | - if ( isset( $this->inline_scripts[ $handle ] ) ) { |
|
| 215 | - $this->inline_scripts[ $handle ][] = $script; |
|
| 214 | + if (isset($this->inline_scripts[$handle])) { |
|
| 215 | + $this->inline_scripts[$handle][] = $script; |
|
| 216 | 216 | } else { |
| 217 | - $this->inline_scripts[ $handle ] = array( $script ); |
|
| 217 | + $this->inline_scripts[$handle] = array($script); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | } |
@@ -15,239 +15,239 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | final class BlockTypesController { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Instance of the asset API. |
|
| 20 | - * |
|
| 21 | - * @var AssetApi |
|
| 22 | - */ |
|
| 23 | - protected $asset_api; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Instance of the asset data registry. |
|
| 27 | - * |
|
| 28 | - * @var AssetDataRegistry |
|
| 29 | - */ |
|
| 30 | - protected $asset_data_registry; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Constructor. |
|
| 34 | - * |
|
| 35 | - * @param AssetApi $asset_api Instance of the asset API. |
|
| 36 | - * @param AssetDataRegistry $asset_data_registry Instance of the asset data registry. |
|
| 37 | - */ |
|
| 38 | - public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) { |
|
| 39 | - $this->asset_api = $asset_api; |
|
| 40 | - $this->asset_data_registry = $asset_data_registry; |
|
| 41 | - $this->init(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Initialize class features. |
|
| 46 | - */ |
|
| 47 | - protected function init() { |
|
| 48 | - add_action( 'init', array( $this, 'register_blocks' ) ); |
|
| 49 | - add_filter( 'render_block', array( $this, 'add_data_attributes' ), 10, 2 ); |
|
| 50 | - add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) ); |
|
| 51 | - add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Register blocks, hooking up assets and render functions as needed. |
|
| 56 | - */ |
|
| 57 | - public function register_blocks() { |
|
| 58 | - $block_types = $this->get_block_types(); |
|
| 59 | - |
|
| 60 | - foreach ( $block_types as $block_type ) { |
|
| 61 | - $block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type; |
|
| 62 | - $block_type_instance = new $block_type_class( $this->asset_api, $this->asset_data_registry, new IntegrationRegistry() ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Add data- attributes to blocks when rendered if the block is under the woocommerce/ namespace. |
|
| 69 | - * |
|
| 70 | - * @param string $content Block content. |
|
| 71 | - * @param array $block Parsed block data. |
|
| 72 | - * @return string |
|
| 73 | - */ |
|
| 74 | - public function add_data_attributes( $content, $block ) { |
|
| 75 | - $block_name = $block['blockName']; |
|
| 76 | - $block_namespace = strtok( $block_name ?? '', '/' ); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Filters the list of allowed block namespaces. |
|
| 80 | - * |
|
| 81 | - * This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render. |
|
| 82 | - * |
|
| 83 | - * @param array $allowed_namespaces List of namespaces. |
|
| 84 | - */ |
|
| 85 | - $allowed_namespaces = array_merge( [ 'woocommerce', 'woocommerce-checkout' ], (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', [] ) ); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Filters the list of allowed Block Names |
|
| 89 | - * |
|
| 90 | - * This hook defines which block names should have block name and attribute data- attributes appended on render. |
|
| 91 | - * |
|
| 92 | - * @param array $allowed_namespaces List of namespaces. |
|
| 93 | - */ |
|
| 94 | - $allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', [] ); |
|
| 95 | - |
|
| 96 | - if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) { |
|
| 97 | - return $content; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $attributes = (array) $block['attrs']; |
|
| 101 | - $exclude_attributes = [ 'className', 'align' ]; |
|
| 102 | - $escaped_data_attributes = [ |
|
| 103 | - 'data-block-name="' . esc_attr( $block['blockName'] ) . '"', |
|
| 104 | - ]; |
|
| 105 | - |
|
| 106 | - foreach ( $attributes as $key => $value ) { |
|
| 107 | - if ( in_array( $key, $exclude_attributes, true ) ) { |
|
| 108 | - continue; |
|
| 109 | - } |
|
| 110 | - if ( is_bool( $value ) ) { |
|
| 111 | - $value = $value ? 'true' : 'false'; |
|
| 112 | - } |
|
| 113 | - if ( ! is_scalar( $value ) ) { |
|
| 114 | - $value = wp_json_encode( $value ); |
|
| 115 | - } |
|
| 116 | - $escaped_data_attributes[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"'; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return preg_replace( '/^<div /', '<div ' . implode( ' ', $escaped_data_attributes ) . ' ', trim( $content ) ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Adds a redirect field to the login form so blocks can redirect users after login. |
|
| 124 | - */ |
|
| 125 | - public function redirect_to_field() { |
|
| 126 | - // phpcs:ignore WordPress.Security.NonceVerification |
|
| 127 | - if ( empty( $_GET['redirect_to'] ) ) { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Hide legacy widgets with a feature complete block equivalent in the inserter |
|
| 135 | - * and prevent them from showing as an option in the Legacy Widget block. |
|
| 136 | - * |
|
| 137 | - * @param array $widget_types An array of widgets hidden in core. |
|
| 138 | - * @return array $widget_types An array inluding the WooCommerce widgets to hide. |
|
| 139 | - */ |
|
| 140 | - public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { |
|
| 141 | - array_push( |
|
| 142 | - $widget_types, |
|
| 143 | - 'woocommerce_product_search', |
|
| 144 | - 'woocommerce_product_categories', |
|
| 145 | - 'woocommerce_recent_reviews', |
|
| 146 | - 'woocommerce_product_tag_cloud', |
|
| 147 | - 'woocommerce_price_filter', |
|
| 148 | - 'woocommerce_layered_nav', |
|
| 149 | - 'woocommerce_layered_nav_filters' |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - return $widget_types; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Get list of block types. |
|
| 157 | - * |
|
| 158 | - * @return array |
|
| 159 | - */ |
|
| 160 | - protected function get_block_types() { |
|
| 161 | - global $pagenow; |
|
| 162 | - |
|
| 163 | - $block_types = [ |
|
| 164 | - 'AllReviews', |
|
| 165 | - 'FeaturedCategory', |
|
| 166 | - 'FeaturedProduct', |
|
| 167 | - 'HandpickedProducts', |
|
| 168 | - 'ProductBestSellers', |
|
| 169 | - 'ProductCategories', |
|
| 170 | - 'ProductCategory', |
|
| 171 | - 'ProductNew', |
|
| 172 | - 'ProductOnSale', |
|
| 173 | - 'ProductsByAttribute', |
|
| 174 | - 'ProductTopRated', |
|
| 175 | - 'ReviewsByProduct', |
|
| 176 | - 'ReviewsByCategory', |
|
| 177 | - 'ProductSearch', |
|
| 178 | - 'ProductTag', |
|
| 179 | - 'AllProducts', |
|
| 180 | - 'PriceFilter', |
|
| 181 | - 'AttributeFilter', |
|
| 182 | - 'StockFilter', |
|
| 183 | - 'ActiveFilters', |
|
| 184 | - 'ClassicTemplate', |
|
| 185 | - 'ProductAddToCart', |
|
| 186 | - 'ProductButton', |
|
| 187 | - 'ProductCategoryList', |
|
| 188 | - 'ProductImage', |
|
| 189 | - 'ProductPrice', |
|
| 190 | - 'ProductRating', |
|
| 191 | - 'ProductSaleBadge', |
|
| 192 | - 'ProductSKU', |
|
| 193 | - 'ProductStockIndicator', |
|
| 194 | - 'ProductSummary', |
|
| 195 | - 'ProductTagList', |
|
| 196 | - 'ProductTitle', |
|
| 197 | - 'MiniCart', |
|
| 198 | - 'MiniCartContents', |
|
| 199 | - ]; |
|
| 200 | - |
|
| 201 | - if ( Package::feature()->is_feature_plugin_build() ) { |
|
| 202 | - $block_types[] = 'Checkout'; |
|
| 203 | - $block_types[] = 'Cart'; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ( Package::feature()->is_experimental_build() ) { |
|
| 207 | - $block_types[] = 'SingleProduct'; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * This disables specific blocks in Widget Areas by not registering them. |
|
| 212 | - */ |
|
| 213 | - if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 214 | - $block_types = array_diff( |
|
| 215 | - $block_types, |
|
| 216 | - [ |
|
| 217 | - 'AllProducts', |
|
| 218 | - 'Cart', |
|
| 219 | - 'Checkout', |
|
| 220 | - ] |
|
| 221 | - ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * This disables specific blocks in Widget Areas by not registering them. |
|
| 226 | - */ |
|
| 227 | - if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 228 | - $block_types = array_diff( |
|
| 229 | - $block_types, |
|
| 230 | - [ |
|
| 231 | - 'AllProducts', |
|
| 232 | - 'Cart', |
|
| 233 | - 'Checkout', |
|
| 234 | - ] |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * This disables specific blocks in Post and Page editor by not registering them. |
|
| 240 | - */ |
|
| 241 | - if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) { |
|
| 242 | - $block_types = array_diff( |
|
| 243 | - $block_types, |
|
| 244 | - [ |
|
| 245 | - 'ClassicTemplate', |
|
| 246 | - ] |
|
| 247 | - ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - return $block_types; |
|
| 251 | - } |
|
| 18 | + /** |
|
| 19 | + * Instance of the asset API. |
|
| 20 | + * |
|
| 21 | + * @var AssetApi |
|
| 22 | + */ |
|
| 23 | + protected $asset_api; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Instance of the asset data registry. |
|
| 27 | + * |
|
| 28 | + * @var AssetDataRegistry |
|
| 29 | + */ |
|
| 30 | + protected $asset_data_registry; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Constructor. |
|
| 34 | + * |
|
| 35 | + * @param AssetApi $asset_api Instance of the asset API. |
|
| 36 | + * @param AssetDataRegistry $asset_data_registry Instance of the asset data registry. |
|
| 37 | + */ |
|
| 38 | + public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) { |
|
| 39 | + $this->asset_api = $asset_api; |
|
| 40 | + $this->asset_data_registry = $asset_data_registry; |
|
| 41 | + $this->init(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Initialize class features. |
|
| 46 | + */ |
|
| 47 | + protected function init() { |
|
| 48 | + add_action( 'init', array( $this, 'register_blocks' ) ); |
|
| 49 | + add_filter( 'render_block', array( $this, 'add_data_attributes' ), 10, 2 ); |
|
| 50 | + add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) ); |
|
| 51 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Register blocks, hooking up assets and render functions as needed. |
|
| 56 | + */ |
|
| 57 | + public function register_blocks() { |
|
| 58 | + $block_types = $this->get_block_types(); |
|
| 59 | + |
|
| 60 | + foreach ( $block_types as $block_type ) { |
|
| 61 | + $block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type; |
|
| 62 | + $block_type_instance = new $block_type_class( $this->asset_api, $this->asset_data_registry, new IntegrationRegistry() ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Add data- attributes to blocks when rendered if the block is under the woocommerce/ namespace. |
|
| 69 | + * |
|
| 70 | + * @param string $content Block content. |
|
| 71 | + * @param array $block Parsed block data. |
|
| 72 | + * @return string |
|
| 73 | + */ |
|
| 74 | + public function add_data_attributes( $content, $block ) { |
|
| 75 | + $block_name = $block['blockName']; |
|
| 76 | + $block_namespace = strtok( $block_name ?? '', '/' ); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Filters the list of allowed block namespaces. |
|
| 80 | + * |
|
| 81 | + * This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render. |
|
| 82 | + * |
|
| 83 | + * @param array $allowed_namespaces List of namespaces. |
|
| 84 | + */ |
|
| 85 | + $allowed_namespaces = array_merge( [ 'woocommerce', 'woocommerce-checkout' ], (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', [] ) ); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Filters the list of allowed Block Names |
|
| 89 | + * |
|
| 90 | + * This hook defines which block names should have block name and attribute data- attributes appended on render. |
|
| 91 | + * |
|
| 92 | + * @param array $allowed_namespaces List of namespaces. |
|
| 93 | + */ |
|
| 94 | + $allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', [] ); |
|
| 95 | + |
|
| 96 | + if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) { |
|
| 97 | + return $content; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $attributes = (array) $block['attrs']; |
|
| 101 | + $exclude_attributes = [ 'className', 'align' ]; |
|
| 102 | + $escaped_data_attributes = [ |
|
| 103 | + 'data-block-name="' . esc_attr( $block['blockName'] ) . '"', |
|
| 104 | + ]; |
|
| 105 | + |
|
| 106 | + foreach ( $attributes as $key => $value ) { |
|
| 107 | + if ( in_array( $key, $exclude_attributes, true ) ) { |
|
| 108 | + continue; |
|
| 109 | + } |
|
| 110 | + if ( is_bool( $value ) ) { |
|
| 111 | + $value = $value ? 'true' : 'false'; |
|
| 112 | + } |
|
| 113 | + if ( ! is_scalar( $value ) ) { |
|
| 114 | + $value = wp_json_encode( $value ); |
|
| 115 | + } |
|
| 116 | + $escaped_data_attributes[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"'; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return preg_replace( '/^<div /', '<div ' . implode( ' ', $escaped_data_attributes ) . ' ', trim( $content ) ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Adds a redirect field to the login form so blocks can redirect users after login. |
|
| 124 | + */ |
|
| 125 | + public function redirect_to_field() { |
|
| 126 | + // phpcs:ignore WordPress.Security.NonceVerification |
|
| 127 | + if ( empty( $_GET['redirect_to'] ) ) { |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | + echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Hide legacy widgets with a feature complete block equivalent in the inserter |
|
| 135 | + * and prevent them from showing as an option in the Legacy Widget block. |
|
| 136 | + * |
|
| 137 | + * @param array $widget_types An array of widgets hidden in core. |
|
| 138 | + * @return array $widget_types An array inluding the WooCommerce widgets to hide. |
|
| 139 | + */ |
|
| 140 | + public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { |
|
| 141 | + array_push( |
|
| 142 | + $widget_types, |
|
| 143 | + 'woocommerce_product_search', |
|
| 144 | + 'woocommerce_product_categories', |
|
| 145 | + 'woocommerce_recent_reviews', |
|
| 146 | + 'woocommerce_product_tag_cloud', |
|
| 147 | + 'woocommerce_price_filter', |
|
| 148 | + 'woocommerce_layered_nav', |
|
| 149 | + 'woocommerce_layered_nav_filters' |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + return $widget_types; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Get list of block types. |
|
| 157 | + * |
|
| 158 | + * @return array |
|
| 159 | + */ |
|
| 160 | + protected function get_block_types() { |
|
| 161 | + global $pagenow; |
|
| 162 | + |
|
| 163 | + $block_types = [ |
|
| 164 | + 'AllReviews', |
|
| 165 | + 'FeaturedCategory', |
|
| 166 | + 'FeaturedProduct', |
|
| 167 | + 'HandpickedProducts', |
|
| 168 | + 'ProductBestSellers', |
|
| 169 | + 'ProductCategories', |
|
| 170 | + 'ProductCategory', |
|
| 171 | + 'ProductNew', |
|
| 172 | + 'ProductOnSale', |
|
| 173 | + 'ProductsByAttribute', |
|
| 174 | + 'ProductTopRated', |
|
| 175 | + 'ReviewsByProduct', |
|
| 176 | + 'ReviewsByCategory', |
|
| 177 | + 'ProductSearch', |
|
| 178 | + 'ProductTag', |
|
| 179 | + 'AllProducts', |
|
| 180 | + 'PriceFilter', |
|
| 181 | + 'AttributeFilter', |
|
| 182 | + 'StockFilter', |
|
| 183 | + 'ActiveFilters', |
|
| 184 | + 'ClassicTemplate', |
|
| 185 | + 'ProductAddToCart', |
|
| 186 | + 'ProductButton', |
|
| 187 | + 'ProductCategoryList', |
|
| 188 | + 'ProductImage', |
|
| 189 | + 'ProductPrice', |
|
| 190 | + 'ProductRating', |
|
| 191 | + 'ProductSaleBadge', |
|
| 192 | + 'ProductSKU', |
|
| 193 | + 'ProductStockIndicator', |
|
| 194 | + 'ProductSummary', |
|
| 195 | + 'ProductTagList', |
|
| 196 | + 'ProductTitle', |
|
| 197 | + 'MiniCart', |
|
| 198 | + 'MiniCartContents', |
|
| 199 | + ]; |
|
| 200 | + |
|
| 201 | + if ( Package::feature()->is_feature_plugin_build() ) { |
|
| 202 | + $block_types[] = 'Checkout'; |
|
| 203 | + $block_types[] = 'Cart'; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ( Package::feature()->is_experimental_build() ) { |
|
| 207 | + $block_types[] = 'SingleProduct'; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * This disables specific blocks in Widget Areas by not registering them. |
|
| 212 | + */ |
|
| 213 | + if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 214 | + $block_types = array_diff( |
|
| 215 | + $block_types, |
|
| 216 | + [ |
|
| 217 | + 'AllProducts', |
|
| 218 | + 'Cart', |
|
| 219 | + 'Checkout', |
|
| 220 | + ] |
|
| 221 | + ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * This disables specific blocks in Widget Areas by not registering them. |
|
| 226 | + */ |
|
| 227 | + if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 228 | + $block_types = array_diff( |
|
| 229 | + $block_types, |
|
| 230 | + [ |
|
| 231 | + 'AllProducts', |
|
| 232 | + 'Cart', |
|
| 233 | + 'Checkout', |
|
| 234 | + ] |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * This disables specific blocks in Post and Page editor by not registering them. |
|
| 240 | + */ |
|
| 241 | + if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) { |
|
| 242 | + $block_types = array_diff( |
|
| 243 | + $block_types, |
|
| 244 | + [ |
|
| 245 | + 'ClassicTemplate', |
|
| 246 | + ] |
|
| 247 | + ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + return $block_types; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | 253 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * @param AssetApi $asset_api Instance of the asset API. |
| 36 | 36 | * @param AssetDataRegistry $asset_data_registry Instance of the asset data registry. |
| 37 | 37 | */ |
| 38 | - public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_registry ) { |
|
| 38 | + public function __construct(AssetApi $asset_api, AssetDataRegistry $asset_data_registry) { |
|
| 39 | 39 | $this->asset_api = $asset_api; |
| 40 | 40 | $this->asset_data_registry = $asset_data_registry; |
| 41 | 41 | $this->init(); |
@@ -45,10 +45,10 @@ discard block |
||
| 45 | 45 | * Initialize class features. |
| 46 | 46 | */ |
| 47 | 47 | protected function init() { |
| 48 | - add_action( 'init', array( $this, 'register_blocks' ) ); |
|
| 49 | - add_filter( 'render_block', array( $this, 'add_data_attributes' ), 10, 2 ); |
|
| 50 | - add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) ); |
|
| 51 | - add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_legacy_widgets_with_block_equivalent' ) ); |
|
| 48 | + add_action('init', array($this, 'register_blocks')); |
|
| 49 | + add_filter('render_block', array($this, 'add_data_attributes'), 10, 2); |
|
| 50 | + add_action('woocommerce_login_form_end', array($this, 'redirect_to_field')); |
|
| 51 | + add_filter('widget_types_to_hide_from_legacy_widget_block', array($this, 'hide_legacy_widgets_with_block_equivalent')); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | public function register_blocks() { |
| 58 | 58 | $block_types = $this->get_block_types(); |
| 59 | 59 | |
| 60 | - foreach ( $block_types as $block_type ) { |
|
| 60 | + foreach ($block_types as $block_type) { |
|
| 61 | 61 | $block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type; |
| 62 | - $block_type_instance = new $block_type_class( $this->asset_api, $this->asset_data_registry, new IntegrationRegistry() ); |
|
| 62 | + $block_type_instance = new $block_type_class($this->asset_api, $this->asset_data_registry, new IntegrationRegistry()); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | } |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | * @param array $block Parsed block data. |
| 72 | 72 | * @return string |
| 73 | 73 | */ |
| 74 | - public function add_data_attributes( $content, $block ) { |
|
| 74 | + public function add_data_attributes($content, $block) { |
|
| 75 | 75 | $block_name = $block['blockName']; |
| 76 | - $block_namespace = strtok( $block_name ?? '', '/' ); |
|
| 76 | + $block_namespace = strtok($block_name ?? '', '/'); |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * Filters the list of allowed block namespaces. |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @param array $allowed_namespaces List of namespaces. |
| 84 | 84 | */ |
| 85 | - $allowed_namespaces = array_merge( [ 'woocommerce', 'woocommerce-checkout' ], (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', [] ) ); |
|
| 85 | + $allowed_namespaces = array_merge(['woocommerce', 'woocommerce-checkout'], (array) apply_filters('__experimental_woocommerce_blocks_add_data_attributes_to_namespace', [])); |
|
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * Filters the list of allowed Block Names |
@@ -91,32 +91,32 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @param array $allowed_namespaces List of namespaces. |
| 93 | 93 | */ |
| 94 | - $allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', [] ); |
|
| 94 | + $allowed_blocks = (array) apply_filters('__experimental_woocommerce_blocks_add_data_attributes_to_block', []); |
|
| 95 | 95 | |
| 96 | - if ( ! in_array( $block_namespace, $allowed_namespaces, true ) && ! in_array( $block_name, $allowed_blocks, true ) ) { |
|
| 96 | + if (!in_array($block_namespace, $allowed_namespaces, true) && !in_array($block_name, $allowed_blocks, true)) { |
|
| 97 | 97 | return $content; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | $attributes = (array) $block['attrs']; |
| 101 | - $exclude_attributes = [ 'className', 'align' ]; |
|
| 101 | + $exclude_attributes = ['className', 'align']; |
|
| 102 | 102 | $escaped_data_attributes = [ |
| 103 | - 'data-block-name="' . esc_attr( $block['blockName'] ) . '"', |
|
| 103 | + 'data-block-name="' . esc_attr($block['blockName']) . '"', |
|
| 104 | 104 | ]; |
| 105 | 105 | |
| 106 | - foreach ( $attributes as $key => $value ) { |
|
| 107 | - if ( in_array( $key, $exclude_attributes, true ) ) { |
|
| 106 | + foreach ($attributes as $key => $value) { |
|
| 107 | + if (in_array($key, $exclude_attributes, true)) { |
|
| 108 | 108 | continue; |
| 109 | 109 | } |
| 110 | - if ( is_bool( $value ) ) { |
|
| 110 | + if (is_bool($value)) { |
|
| 111 | 111 | $value = $value ? 'true' : 'false'; |
| 112 | 112 | } |
| 113 | - if ( ! is_scalar( $value ) ) { |
|
| 114 | - $value = wp_json_encode( $value ); |
|
| 113 | + if (!is_scalar($value)) { |
|
| 114 | + $value = wp_json_encode($value); |
|
| 115 | 115 | } |
| 116 | - $escaped_data_attributes[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"'; |
|
| 116 | + $escaped_data_attributes[] = 'data-' . esc_attr(strtolower(preg_replace('/(?<!\ )[A-Z]/', '-$0', $key))) . '="' . esc_attr($value) . '"'; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - return preg_replace( '/^<div /', '<div ' . implode( ' ', $escaped_data_attributes ) . ' ', trim( $content ) ); |
|
| 119 | + return preg_replace('/^<div /', '<div ' . implode(' ', $escaped_data_attributes) . ' ', trim($content)); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -124,10 +124,10 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | public function redirect_to_field() { |
| 126 | 126 | // phpcs:ignore WordPress.Security.NonceVerification |
| 127 | - if ( empty( $_GET['redirect_to'] ) ) { |
|
| 127 | + if (empty($_GET['redirect_to'])) { |
|
| 128 | 128 | return; |
| 129 | 129 | } |
| 130 | - echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 130 | + echo '<input type="hidden" name="redirect" value="' . esc_attr(esc_url_raw(wp_unslash($_GET['redirect_to']))) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | * @param array $widget_types An array of widgets hidden in core. |
| 138 | 138 | * @return array $widget_types An array inluding the WooCommerce widgets to hide. |
| 139 | 139 | */ |
| 140 | - public function hide_legacy_widgets_with_block_equivalent( $widget_types ) { |
|
| 140 | + public function hide_legacy_widgets_with_block_equivalent($widget_types) { |
|
| 141 | 141 | array_push( |
| 142 | 142 | $widget_types, |
| 143 | 143 | 'woocommerce_product_search', |
@@ -198,19 +198,19 @@ discard block |
||
| 198 | 198 | 'MiniCartContents', |
| 199 | 199 | ]; |
| 200 | 200 | |
| 201 | - if ( Package::feature()->is_feature_plugin_build() ) { |
|
| 201 | + if (Package::feature()->is_feature_plugin_build()) { |
|
| 202 | 202 | $block_types[] = 'Checkout'; |
| 203 | 203 | $block_types[] = 'Cart'; |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - if ( Package::feature()->is_experimental_build() ) { |
|
| 206 | + if (Package::feature()->is_experimental_build()) { |
|
| 207 | 207 | $block_types[] = 'SingleProduct'; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
| 211 | 211 | * This disables specific blocks in Widget Areas by not registering them. |
| 212 | 212 | */ |
| 213 | - if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 213 | + if (in_array($pagenow, ['widgets.php', 'themes.php', 'customize.php'], true) && (empty($_GET['page']) || 'gutenberg-edit-site' !== $_GET['page'])) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 214 | 214 | $block_types = array_diff( |
| 215 | 215 | $block_types, |
| 216 | 216 | [ |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | /** |
| 225 | 225 | * This disables specific blocks in Widget Areas by not registering them. |
| 226 | 226 | */ |
| 227 | - if ( in_array( $pagenow, [ 'widgets.php', 'themes.php', 'customize.php' ], true ) && ( empty( $_GET['page'] ) || 'gutenberg-edit-site' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 227 | + if (in_array($pagenow, ['widgets.php', 'themes.php', 'customize.php'], true) && (empty($_GET['page']) || 'gutenberg-edit-site' !== $_GET['page'])) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 228 | 228 | $block_types = array_diff( |
| 229 | 229 | $block_types, |
| 230 | 230 | [ |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | /** |
| 239 | 239 | * This disables specific blocks in Post and Page editor by not registering them. |
| 240 | 240 | */ |
| 241 | - if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) { |
|
| 241 | + if (in_array($pagenow, ['post.php', 'post-new.php'], true)) { |
|
| 242 | 242 | $block_types = array_diff( |
| 243 | 243 | $block_types, |
| 244 | 244 | [ |