From d4c540c56171f9925400c14515d885b9ffc30baf Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 26 Apr 2022 10:04:58 +0200 Subject: [PATCH 1/4] Add Mini Cart Block Tracking --- .../includes/blocks/class-wc-blocks-utils.php | 21 +++++++++++++ .../woocommerce/includes/class-wc-tracker.php | 30 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php index 4ecb4c212e5..1465dbf5e30 100644 --- a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php +++ b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php @@ -59,6 +59,27 @@ class WC_Blocks_Utils { ); } + /** + * Get all instances of the specified block on a specific template part. + * + * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. + * @param string $template_part_slug The woo page to search, e.g. `header`. + * @return array Array of blocks as returned by parse_blocks(). + */ + public static function get_block_from_template_part( $block_name, $template_part_slug ) { + $template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' ); + $blocks = parse_blocks( $template->content ); + + return array_values( + array_filter( + $blocks, + function ( $block ) use ( $block_name ) { + return ( $block_name === $block['blockName'] ); + } + ) + ); + } + /** * Check if a given page contains a particular block. * diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php index dbae0b03978..66327dcef5e 100644 --- a/plugins/woocommerce/includes/class-wc-tracker.php +++ b/plugins/woocommerce/includes/class-wc-tracker.php @@ -166,6 +166,9 @@ class WC_Tracker { // Cart & checkout tech (blocks or shortcodes). $data['cart_checkout'] = self::get_cart_checkout_info(); + // Mini Cart block. + $data['mini_cart_block'] = self::get_mini_cart_info(); + // WooCommerce Admin info. $data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no'; @@ -776,6 +779,33 @@ class WC_Tracker { ); } + /** + * Get info about the Mini Cart Block. + * + * @return array + */ + public static function get_mini_cart_info() { + $mini_cart_block_name = 'woocommerce/mini-cart'; + $mini_cart_block_data = wc_current_theme_is_fse_theme() ? WC_Blocks_Utils::get_block_from_template_part( $mini_cart_block_name, 'header' ) : + array_reduce( + get_option( 'widget_block' ), + function ( $acc, $block ) use ( $mini_cart_block_name ) { + $parsed_blocks = ! empty( $block ) && is_array( $block ) ? parse_blocks( $block['content'] ) : array(); + if ( ! empty( $parsed_blocks ) && $mini_cart_block_name === $parsed_blocks[0]['blockName'] ) { + array_push( $acc, $parsed_blocks[0] ); + return $acc; + } + return $acc; + }, + array() + ); + + return array( + 'mini_cart_used' => empty( $mini_cart_block_data[0] ) ? 'No' : 'Yes', + 'mini_cart_block_attributes' => empty( $mini_cart_block_data[0] ) ? array() : $mini_cart_block_data[0]['attrs'], + ); + } + /** * Get info about WooCommerce Mobile App usage * From 650fe593a1acf1fe57590d190df8c6643eb36914 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 26 Apr 2022 11:23:44 +0200 Subject: [PATCH 2/4] add changelog --- plugins/woocommerce/changelog/add-track_mini_cart_block | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/add-track_mini_cart_block diff --git a/plugins/woocommerce/changelog/add-track_mini_cart_block b/plugins/woocommerce/changelog/add-track_mini_cart_block new file mode 100644 index 00000000000..ec42fc7ed2f --- /dev/null +++ b/plugins/woocommerce/changelog/add-track_mini_cart_block @@ -0,0 +1,4 @@ +Significance: patch +Type: add + +Adds usage data for the Mini Cart Block to the WC Tracker snapshot. From 98a91cd13238cb856d8239c721a2f064910da623 Mon Sep 17 00:00:00 2001 From: Luigi Date: Wed, 27 Apr 2022 12:01:02 +0200 Subject: [PATCH 3/4] move logic to utils file --- .../includes/blocks/class-wc-blocks-utils.php | 47 ++++++++++++++++++- .../woocommerce/includes/class-wc-tracker.php | 15 +----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php index 1465dbf5e30..b9eb7b08539 100644 --- a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php +++ b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php @@ -70,9 +70,11 @@ class WC_Blocks_Utils { $template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' ); $blocks = parse_blocks( $template->content ); + $flatten_blocks = self::flatten_blocks( $blocks ); + return array_values( array_filter( - $blocks, + $flatten_blocks, function ( $block ) use ( $block_name ) { return ( $block_name === $block['blockName'] ); } @@ -102,4 +104,47 @@ class WC_Blocks_Utils { return false; } + + /** + * Return blocks with their inner blocks flattened. + * + * @param array $blocks Array of blocks as returned by parse_blocks(). + * @return array All blocks. + */ + public static function flatten_blocks( $blocks ) { + return array_reduce( + $blocks, + function( $carry, $block ) { + array_push( $carry, array_diff_key( $block, array_flip( array( 'innerBlocks' ) ) ) ); + if ( isset( $block['innerBlocks'] ) ) { + $inner_blocks = self::flatten_blocks( $block['innerBlocks'] ); + return array_merge( $carry, $inner_blocks ); + } + + return $carry; + }, + array() + ); + } + + /** + * Get all instances of the specified block from the widget area. + * + * @param array $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. + * @return array Array of blocks as returned by parse_blocks(). + */ + public static function get_blocks_from_widget_area( $block_name ) { + return array_reduce( + get_option( 'widget_block' ), + function ( $acc, $block ) use ( $block_name ) { + $parsed_blocks = ! empty( $block ) && is_array( $block ) ? parse_blocks( $block['content'] ) : array(); + if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) { + array_push( $acc, $parsed_blocks[0] ); + return $acc; + } + return $acc; + }, + array() + ); + } } diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php index 66327dcef5e..24f77fef403 100644 --- a/plugins/woocommerce/includes/class-wc-tracker.php +++ b/plugins/woocommerce/includes/class-wc-tracker.php @@ -786,20 +786,7 @@ class WC_Tracker { */ public static function get_mini_cart_info() { $mini_cart_block_name = 'woocommerce/mini-cart'; - $mini_cart_block_data = wc_current_theme_is_fse_theme() ? WC_Blocks_Utils::get_block_from_template_part( $mini_cart_block_name, 'header' ) : - array_reduce( - get_option( 'widget_block' ), - function ( $acc, $block ) use ( $mini_cart_block_name ) { - $parsed_blocks = ! empty( $block ) && is_array( $block ) ? parse_blocks( $block['content'] ) : array(); - if ( ! empty( $parsed_blocks ) && $mini_cart_block_name === $parsed_blocks[0]['blockName'] ) { - array_push( $acc, $parsed_blocks[0] ); - return $acc; - } - return $acc; - }, - array() - ); - + $mini_cart_block_data = wc_current_theme_is_fse_theme() ? WC_Blocks_Utils::get_block_from_template_part( $mini_cart_block_name, 'header' ) : WC_Blocks_Utils::get_blocks_from_widget_area( $mini_cart_block_name ); return array( 'mini_cart_used' => empty( $mini_cart_block_data[0] ) ? 'No' : 'Yes', 'mini_cart_block_attributes' => empty( $mini_cart_block_data[0] ) ? array() : $mini_cart_block_data[0]['attrs'], From 4597c47f63b62027c5acaa931da2ddf0e9a73fda Mon Sep 17 00:00:00 2001 From: Luigi Date: Mon, 2 May 2022 11:21:10 +0200 Subject: [PATCH 4/4] create BlocksUtil file - refactor --- .../includes/blocks/class-wc-blocks-utils.php | 66 ---------------- .../woocommerce/includes/class-wc-tracker.php | 5 +- .../src/Internal/Utilities/BlocksUtil.php | 75 +++++++++++++++++++ 3 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 plugins/woocommerce/src/Internal/Utilities/BlocksUtil.php diff --git a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php index b9eb7b08539..4ecb4c212e5 100644 --- a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php +++ b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php @@ -59,29 +59,6 @@ class WC_Blocks_Utils { ); } - /** - * Get all instances of the specified block on a specific template part. - * - * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. - * @param string $template_part_slug The woo page to search, e.g. `header`. - * @return array Array of blocks as returned by parse_blocks(). - */ - public static function get_block_from_template_part( $block_name, $template_part_slug ) { - $template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' ); - $blocks = parse_blocks( $template->content ); - - $flatten_blocks = self::flatten_blocks( $blocks ); - - return array_values( - array_filter( - $flatten_blocks, - function ( $block ) use ( $block_name ) { - return ( $block_name === $block['blockName'] ); - } - ) - ); - } - /** * Check if a given page contains a particular block. * @@ -104,47 +81,4 @@ class WC_Blocks_Utils { return false; } - - /** - * Return blocks with their inner blocks flattened. - * - * @param array $blocks Array of blocks as returned by parse_blocks(). - * @return array All blocks. - */ - public static function flatten_blocks( $blocks ) { - return array_reduce( - $blocks, - function( $carry, $block ) { - array_push( $carry, array_diff_key( $block, array_flip( array( 'innerBlocks' ) ) ) ); - if ( isset( $block['innerBlocks'] ) ) { - $inner_blocks = self::flatten_blocks( $block['innerBlocks'] ); - return array_merge( $carry, $inner_blocks ); - } - - return $carry; - }, - array() - ); - } - - /** - * Get all instances of the specified block from the widget area. - * - * @param array $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. - * @return array Array of blocks as returned by parse_blocks(). - */ - public static function get_blocks_from_widget_area( $block_name ) { - return array_reduce( - get_option( 'widget_block' ), - function ( $acc, $block ) use ( $block_name ) { - $parsed_blocks = ! empty( $block ) && is_array( $block ) ? parse_blocks( $block['content'] ) : array(); - if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) { - array_push( $acc, $parsed_blocks[0] ); - return $acc; - } - return $acc; - }, - array() - ); - } } diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php index 24f77fef403..20371efd7d4 100644 --- a/plugins/woocommerce/includes/class-wc-tracker.php +++ b/plugins/woocommerce/includes/class-wc-tracker.php @@ -11,6 +11,7 @@ */ use Automattic\Jetpack\Constants; +use Automattic\WooCommerce\Internal\Utilities\BlocksUtil; defined( 'ABSPATH' ) || exit; @@ -784,9 +785,9 @@ class WC_Tracker { * * @return array */ - public static function get_mini_cart_info() { + private static function get_mini_cart_info() { $mini_cart_block_name = 'woocommerce/mini-cart'; - $mini_cart_block_data = wc_current_theme_is_fse_theme() ? WC_Blocks_Utils::get_block_from_template_part( $mini_cart_block_name, 'header' ) : WC_Blocks_Utils::get_blocks_from_widget_area( $mini_cart_block_name ); + $mini_cart_block_data = wc_current_theme_is_fse_theme() ? BlocksUtil::get_block_from_template_part( $mini_cart_block_name, 'header' ) : BlocksUtil::get_blocks_from_widget_area( $mini_cart_block_name ); return array( 'mini_cart_used' => empty( $mini_cart_block_data[0] ) ? 'No' : 'Yes', 'mini_cart_block_attributes' => empty( $mini_cart_block_data[0] ) ? array() : $mini_cart_block_data[0]['attrs'], diff --git a/plugins/woocommerce/src/Internal/Utilities/BlocksUtil.php b/plugins/woocommerce/src/Internal/Utilities/BlocksUtil.php new file mode 100644 index 00000000000..65fbbcefbd8 --- /dev/null +++ b/plugins/woocommerce/src/Internal/Utilities/BlocksUtil.php @@ -0,0 +1,75 @@ +content ); + + $flatten_blocks = self::flatten_blocks( $blocks ); + + return array_values( + array_filter( + $flatten_blocks, + function ( $block ) use ( $block_name ) { + return ( $block_name === $block['blockName'] ); + } + ) + ); + } +}