Merge pull request #26084 from woocommerce/add/cart-checkout-block-settings-data

add customised cart & checkout block settings to wc-tracker snapshot
This commit is contained in:
Christopher Allford 2020-04-06 18:22:24 -07:00 committed by GitHub
commit 172ceb51e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 79 additions and 10 deletions

View File

@ -652,33 +652,102 @@ class WC_Tracker {
return ( '0' !== $result ) ? 'Yes' : 'No';
}
/**
* Get blocks from a woocommerce page.
*
* @param string $woo_page_name A woocommerce page e.g. `checkout` or `cart`.
* @return array Array of blocks as returned by parse_blocks().
*/
private static function get_all_blocks_from_page( $woo_page_name ) {
$page_id = wc_get_page_id( $woo_page_name );
$page = get_post( $page_id );
if ( ! $page ) {
return array();
}
$blocks = parse_blocks( $page->post_content );
if ( ! $blocks ) {
return array();
}
return $blocks;
}
/**
* Get all instances of the specified block on a specific woo page
* (e.g. `cart` or `checkout` page).
*
* @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`.
* @param string $woo_page_name The woo page to search, e.g. `cart`.
* @return array Array of blocks as returned by parse_blocks().
*/
private static function get_blocks_from_page( $block_name, $woo_page_name ) {
$page_blocks = self::get_all_blocks_from_page( $woo_page_name );
// Get any instances of the specified block.
return array_values(
array_filter(
$page_blocks,
function ( $block ) use ( $block_name ) {
return ( $block_name === $block['blockName'] );
}
)
);
}
/**
* Get tracker data for a specific block type on a woocommerce page.
*
* @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`.
* @param string $woo_page_name The woo page to search, e.g. `cart`.
* @return array Associative array of tracker data with keys:
* - page_contains_block
* - block_attributes
*/
public static function get_block_tracker_data( $block_name, $woo_page_name ) {
$blocks = self::get_blocks_from_page( $block_name, $woo_page_name );
$block_present = false;
$attributes = array();
if ( $blocks && count( $blocks ) ) {
// Return any customised attributes from the first block.
$block_present = true;
$attributes = $blocks[0]['attrs'];
}
return array(
'page_contains_block' => $block_present ? 'Yes' : 'No',
'block_attributes' => $attributes,
);
}
/**
* Get info about the cart & checkout pages.
*
* @return array
*/
public static function get_cart_checkout_info() {
global $wpdb;
$cart_page_id = wc_get_page_id( 'cart' );
$checkout_page_id = wc_get_page_id( 'checkout' );
$cart_block_data = self::get_block_tracker_data( 'woocommerce/cart', 'cart' );
$checkout_block_data = self::get_block_tracker_data( 'woocommerce/checkout', 'checkout' );
return array(
'cart_page_contains_cart_block' => self::post_contains_text(
$cart_page_id,
'<!-- wp:woocommerce/cart'
),
'cart_page_contains_cart_shortcode' => self::post_contains_text(
$cart_page_id,
'[woocommerce_cart]'
),
'checkout_page_contains_checkout_block' => self::post_contains_text(
$checkout_page_id,
'<!-- wp:woocommerce/checkout'
),
'checkout_page_contains_checkout_shortcode' => self::post_contains_text(
$checkout_page_id,
'[woocommerce_checkout]'
),
'cart_page_contains_cart_block' => $cart_block_data['page_contains_block'],
'cart_block_attributes' => $cart_block_data['block_attributes'],
'checkout_page_contains_checkout_block' => $checkout_block_data['page_contains_block'],
'checkout_block_attributes' => $checkout_block_data['block_attributes'],
);
}
}