Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Utils
File: CartCheckoutUtils.php
<?php // phpcs:ignore Generic.PHP.RequireStrictTypes.MissingDeclaration
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Utils;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\Block_Scanner;
[3] Fix | Delete
[4] Fix | Delete
/**
[5] Fix | Delete
* Class containing utility methods for dealing with the Cart and Checkout blocks.
[6] Fix | Delete
*/
[7] Fix | Delete
class CartCheckoutUtils {
[8] Fix | Delete
/**
[9] Fix | Delete
* Caches if we're on the cart page.
[10] Fix | Delete
*
[11] Fix | Delete
* @var bool
[12] Fix | Delete
*/
[13] Fix | Delete
private static $is_cart_page = null;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Caches if we're on the checkout page.
[17] Fix | Delete
*
[18] Fix | Delete
* @var bool
[19] Fix | Delete
*/
[20] Fix | Delete
private static $is_checkout_page = null;
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Returns true if the current page is a specific page type (cart or checkout).
[24] Fix | Delete
*
[25] Fix | Delete
* This is determined by looking at the global $post object and comparing it to the post ID defined in settings,
[26] Fix | Delete
* or checking the page contents for a block or shortcode.
[27] Fix | Delete
*
[28] Fix | Delete
* This function cannot be used accurately before the `pre_get_posts` action has been run.
[29] Fix | Delete
*
[30] Fix | Delete
* @param string $page_type The page type to check for.
[31] Fix | Delete
* @return bool|null
[32] Fix | Delete
*/
[33] Fix | Delete
private static function is_page_type( string $page_type ): ?bool {
[34] Fix | Delete
if ( ! did_action( 'pre_get_posts' ) ) {
[35] Fix | Delete
return null;
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
$page_id = wc_get_page_id( $page_type );
[39] Fix | Delete
[40] Fix | Delete
if ( $page_id && is_page( $page_id ) ) {
[41] Fix | Delete
return true;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
// If the is_page check returned false, check the page contents for a cart block or shortcode.
[45] Fix | Delete
global $post;
[46] Fix | Delete
[47] Fix | Delete
if ( null === $post ) {
[48] Fix | Delete
return null;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
if ( $post instanceof \WP_Post ) {
[52] Fix | Delete
return wc_post_content_has_shortcode( 'cart' === $page_type ? 'woocommerce_cart' : 'woocommerce_checkout' ) || self::has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', $page_type, $post->post_content );
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
return false;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Returns true on the cart page.
[60] Fix | Delete
*
[61] Fix | Delete
* @return bool
[62] Fix | Delete
*/
[63] Fix | Delete
public static function is_cart_page(): bool {
[64] Fix | Delete
if ( null === self::$is_cart_page ) {
[65] Fix | Delete
self::$is_cart_page = self::is_page_type( 'cart' );
[66] Fix | Delete
}
[67] Fix | Delete
return true === self::$is_cart_page;
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Returns true on the checkout page.
[72] Fix | Delete
*
[73] Fix | Delete
* @return bool
[74] Fix | Delete
*/
[75] Fix | Delete
public static function is_checkout_page(): bool {
[76] Fix | Delete
if ( null === self::$is_checkout_page ) {
[77] Fix | Delete
self::$is_checkout_page = self::is_page_type( 'checkout' );
[78] Fix | Delete
}
[79] Fix | Delete
return true === self::$is_checkout_page;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Returns true if shipping methods exist in the store. Excludes local pickup and only counts enabled shipping methods.
[84] Fix | Delete
*
[85] Fix | Delete
* @return bool true if shipping methods exist.
[86] Fix | Delete
*/
[87] Fix | Delete
public static function shipping_methods_exist() {
[88] Fix | Delete
// Local pickup is included with legacy shipping methods since they do not support shipping zones.
[89] Fix | Delete
$local_pickup_count = count(
[90] Fix | Delete
array_filter(
[91] Fix | Delete
WC()->shipping()->get_shipping_methods(),
[92] Fix | Delete
function ( $method ) {
[93] Fix | Delete
return isset( $method->enabled ) && 'yes' === $method->enabled && ! $method->supports( 'shipping-zones' ) && $method->supports( 'local-pickup' );
[94] Fix | Delete
}
[95] Fix | Delete
)
[96] Fix | Delete
);
[97] Fix | Delete
[98] Fix | Delete
$shipping_methods_count = wc_get_shipping_method_count( true, true ) - $local_pickup_count;
[99] Fix | Delete
return $shipping_methods_count > 0;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Check if the post content contains a block with a specific attribute value.
[104] Fix | Delete
*
[105] Fix | Delete
* @param string $block_id The block ID to check for.
[106] Fix | Delete
* @param string $attribute The attribute to check.
[107] Fix | Delete
* @param string $value The value to check for.
[108] Fix | Delete
* @param string $post_content The post content to check.
[109] Fix | Delete
* @return boolean
[110] Fix | Delete
*/
[111] Fix | Delete
public static function has_block_variation( $block_id, $attribute, $value, $post_content ) {
[112] Fix | Delete
if ( ! $post_content ) {
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
$scanner = Block_Scanner::create( $post_content );
[117] Fix | Delete
if ( ! $scanner ) {
[118] Fix | Delete
return false;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
while ( $scanner->next_delimiter() ) {
[122] Fix | Delete
if ( ! $scanner->opens_block( $block_id ) ) {
[123] Fix | Delete
continue;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
$attrs = $scanner->allocate_and_return_parsed_attributes();
[127] Fix | Delete
[128] Fix | Delete
if ( isset( $attrs[ $attribute ] ) && $value === $attrs[ $attribute ] ) {
[129] Fix | Delete
return true;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
// `Cart` is default for `woocommerce/classic-shortcode` so it will be empty in the block attributes.
[133] Fix | Delete
if ( 'woocommerce/classic-shortcode' === $block_id &&
[134] Fix | Delete
'shortcode' === $attribute &&
[135] Fix | Delete
'cart' === $value &&
[136] Fix | Delete
! isset( $attrs['shortcode'] ) ) {
[137] Fix | Delete
return true;
[138] Fix | Delete
}
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
return false;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Checks if the default cart page is using the Cart block.
[146] Fix | Delete
*
[147] Fix | Delete
* @return bool true if the WC cart page is using the Cart block.
[148] Fix | Delete
*/
[149] Fix | Delete
public static function is_cart_block_default() {
[150] Fix | Delete
if ( wp_is_block_theme() ) {
[151] Fix | Delete
// Ignore the pages and check the templates.
[152] Fix | Delete
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'cart' ), 'wp_template' );
[153] Fix | Delete
foreach ( $templates_from_db as $template ) {
[154] Fix | Delete
if ( has_block( 'woocommerce/cart', $template->content ) ) {
[155] Fix | Delete
return true;
[156] Fix | Delete
}
[157] Fix | Delete
}
[158] Fix | Delete
}
[159] Fix | Delete
$cart_page_id = wc_get_page_id( 'cart' );
[160] Fix | Delete
return $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Checks if the default checkout page is using the Checkout block.
[165] Fix | Delete
*
[166] Fix | Delete
* @return bool true if the WC checkout page is using the Checkout block.
[167] Fix | Delete
*/
[168] Fix | Delete
public static function is_checkout_block_default() {
[169] Fix | Delete
if ( wp_is_block_theme() ) {
[170] Fix | Delete
// Ignore the pages and check the templates.
[171] Fix | Delete
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'checkout' ), 'wp_template' );
[172] Fix | Delete
foreach ( $templates_from_db as $template ) {
[173] Fix | Delete
if ( has_block( 'woocommerce/checkout', $template->content ) ) {
[174] Fix | Delete
return true;
[175] Fix | Delete
}
[176] Fix | Delete
}
[177] Fix | Delete
}
[178] Fix | Delete
$checkout_page_id = wc_get_page_id( 'checkout' );
[179] Fix | Delete
return $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/**
[183] Fix | Delete
* Migrate checkout block field visibility attributes to settings when using the checkout block.
[184] Fix | Delete
*
[185] Fix | Delete
* This migration routine is called if the options (woocommerce_checkout_phone_field, woocommerce_checkout_company_field,
[186] Fix | Delete
* woocommerce_checkout_address_2_field) are not set. They are not set by default; they were orignally set by the
[187] Fix | Delete
* customizer interface of the legacy shortcode based checkout.
[188] Fix | Delete
*
[189] Fix | Delete
* Once migration is initiated, the settings will be updated and will not trigger this routine again.
[190] Fix | Delete
*
[191] Fix | Delete
* Note: The block only stores non-default attributes. Not all attributes will be present.
[192] Fix | Delete
*
[193] Fix | Delete
* e.g. `{"showCompanyField":true,"requireCompanyField":true,"showApartmentField":false,"className":"wc-block-checkout"}`
[194] Fix | Delete
*
[195] Fix | Delete
* If the attributes are missing, we assume default values are needed.
[196] Fix | Delete
*/
[197] Fix | Delete
protected static function migrate_checkout_block_field_visibility_attributes() {
[198] Fix | Delete
// Before migrating attributes, migrate the "default" options checkout block uses into the settings.
[199] Fix | Delete
update_option( 'woocommerce_checkout_phone_field', 'optional' );
[200] Fix | Delete
update_option( 'woocommerce_checkout_company_field', 'hidden' );
[201] Fix | Delete
update_option( 'woocommerce_checkout_address_2_field', 'optional' );
[202] Fix | Delete
[203] Fix | Delete
// Parse the block from the checkout page.
[204] Fix | Delete
$checkout_blocks = \WC_Blocks_Utils::get_blocks_from_page( 'woocommerce/checkout', 'checkout' );
[205] Fix | Delete
[206] Fix | Delete
if ( empty( $checkout_blocks ) || ! isset( $checkout_blocks[0]['attrs'] ) ) {
[207] Fix | Delete
return;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
// Combine actual attributes with default values.
[211] Fix | Delete
$block_attributes = wp_parse_args(
[212] Fix | Delete
$checkout_blocks[0]['attrs'],
[213] Fix | Delete
array(
[214] Fix | Delete
'showPhoneField' => true,
[215] Fix | Delete
'requirePhoneField' => false,
[216] Fix | Delete
'showCompanyField' => false,
[217] Fix | Delete
'requireCompanyField' => false,
[218] Fix | Delete
'showApartmentField' => true,
[219] Fix | Delete
'requireApartmentField' => false,
[220] Fix | Delete
)
[221] Fix | Delete
);
[222] Fix | Delete
[223] Fix | Delete
if ( $block_attributes['showPhoneField'] ) {
[224] Fix | Delete
update_option( 'woocommerce_checkout_phone_field', $block_attributes['requirePhoneField'] ? 'required' : 'optional' );
[225] Fix | Delete
} else {
[226] Fix | Delete
update_option( 'woocommerce_checkout_phone_field', 'hidden' );
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
if ( $block_attributes['showCompanyField'] ) {
[230] Fix | Delete
update_option( 'woocommerce_checkout_company_field', $block_attributes['requireCompanyField'] ? 'required' : 'optional' );
[231] Fix | Delete
} else {
[232] Fix | Delete
update_option( 'woocommerce_checkout_company_field', 'hidden' );
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
if ( $block_attributes['showApartmentField'] ) {
[236] Fix | Delete
update_option( 'woocommerce_checkout_address_2_field', $block_attributes['requireApartmentField'] ? 'required' : 'optional' );
[237] Fix | Delete
} else {
[238] Fix | Delete
update_option( 'woocommerce_checkout_address_2_field', 'hidden' );
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* Get the default visibility for the address_2 field.
[244] Fix | Delete
*
[245] Fix | Delete
* @return string
[246] Fix | Delete
*/
[247] Fix | Delete
public static function get_company_field_visibility() {
[248] Fix | Delete
$option_value = get_option( 'woocommerce_checkout_company_field' );
[249] Fix | Delete
[250] Fix | Delete
if ( $option_value ) {
[251] Fix | Delete
return $option_value;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
if ( self::is_checkout_block_default() ) {
[255] Fix | Delete
self::migrate_checkout_block_field_visibility_attributes();
[256] Fix | Delete
return get_option( 'woocommerce_checkout_company_field', 'hidden' );
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
return 'optional';
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Get the default visibility for the address_2 field.
[264] Fix | Delete
*
[265] Fix | Delete
* @return string
[266] Fix | Delete
*/
[267] Fix | Delete
public static function get_address_2_field_visibility() {
[268] Fix | Delete
$option_value = get_option( 'woocommerce_checkout_address_2_field' );
[269] Fix | Delete
[270] Fix | Delete
if ( $option_value ) {
[271] Fix | Delete
return $option_value;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
if ( self::is_checkout_block_default() ) {
[275] Fix | Delete
self::migrate_checkout_block_field_visibility_attributes();
[276] Fix | Delete
return get_option( 'woocommerce_checkout_address_2_field', 'optional' );
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
return 'optional';
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* Get the default visibility for the address_2 field.
[284] Fix | Delete
*
[285] Fix | Delete
* @return string
[286] Fix | Delete
*/
[287] Fix | Delete
public static function get_phone_field_visibility() {
[288] Fix | Delete
$option_value = get_option( 'woocommerce_checkout_phone_field' );
[289] Fix | Delete
[290] Fix | Delete
if ( $option_value ) {
[291] Fix | Delete
return $option_value;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
if ( self::is_checkout_block_default() ) {
[295] Fix | Delete
self::migrate_checkout_block_field_visibility_attributes();
[296] Fix | Delete
return get_option( 'woocommerce_checkout_phone_field', 'optional' );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return 'required';
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Checks if the template overriding the page loads the page content or not.
[304] Fix | Delete
* Templates by default load the page content, but if that block is deleted the content can get out of sync with the one presented in the page editor.
[305] Fix | Delete
*
[306] Fix | Delete
* @param string $block The block to check.
[307] Fix | Delete
*
[308] Fix | Delete
* @return bool true if the template has out of sync content.
[309] Fix | Delete
*/
[310] Fix | Delete
public static function is_overriden_by_custom_template_content( string $block ): bool {
[311] Fix | Delete
[312] Fix | Delete
$block = str_replace( 'woocommerce/', '', $block );
[313] Fix | Delete
[314] Fix | Delete
if ( wp_is_block_theme() ) {
[315] Fix | Delete
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'page-' . $block ) );
[316] Fix | Delete
foreach ( $templates_from_db as $template ) {
[317] Fix | Delete
if ( ! has_block( 'woocommerce/page-content-wrapper', $template->content ) ) {
[318] Fix | Delete
// Return true if the template does not load the page content via the woocommerce/page-content-wrapper block.
[319] Fix | Delete
return true;
[320] Fix | Delete
}
[321] Fix | Delete
}
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
return false;
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
/**
[328] Fix | Delete
* Gets country codes, names, states, and locale information.
[329] Fix | Delete
*
[330] Fix | Delete
* @return array
[331] Fix | Delete
*/
[332] Fix | Delete
public static function get_country_data() {
[333] Fix | Delete
$billing_countries = WC()->countries->get_allowed_countries();
[334] Fix | Delete
$shipping_countries = WC()->countries->get_shipping_countries();
[335] Fix | Delete
$country_states = wc()->countries->get_states();
[336] Fix | Delete
$all_countries = self::deep_sort_with_accents( array_unique( array_merge( $billing_countries, $shipping_countries ) ) );
[337] Fix | Delete
$country_locales = array_map(
[338] Fix | Delete
function ( $locale ) {
[339] Fix | Delete
foreach ( $locale as $field => $field_data ) {
[340] Fix | Delete
if ( isset( $field_data['priority'] ) ) {
[341] Fix | Delete
$locale[ $field ]['index'] = $field_data['priority'];
[342] Fix | Delete
unset( $locale[ $field ]['priority'] );
[343] Fix | Delete
}
[344] Fix | Delete
if ( isset( $field_data['class'] ) ) {
[345] Fix | Delete
unset( $locale[ $field ]['class'] );
[346] Fix | Delete
}
[347] Fix | Delete
}
[348] Fix | Delete
return $locale;
[349] Fix | Delete
},
[350] Fix | Delete
WC()->countries->get_country_locale()
[351] Fix | Delete
);
[352] Fix | Delete
[353] Fix | Delete
$country_data = array();
[354] Fix | Delete
[355] Fix | Delete
foreach ( array_keys( $all_countries ) as $country_code ) {
[356] Fix | Delete
$country_data[ $country_code ] = array(
[357] Fix | Delete
'allowBilling' => isset( $billing_countries[ $country_code ] ),
[358] Fix | Delete
'allowShipping' => isset( $shipping_countries[ $country_code ] ),
[359] Fix | Delete
'states' => $country_states[ $country_code ] ?? array(),
[360] Fix | Delete
'locale' => $country_locales[ $country_code ] ?? array(),
[361] Fix | Delete
);
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
return $country_data;
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* Removes accents from an array of values, sorts by the values, then returns the original array values sorted.
[369] Fix | Delete
*
[370] Fix | Delete
* @param array $sort_array Array of values to sort.
[371] Fix | Delete
* @return array Sorted array.
[372] Fix | Delete
*/
[373] Fix | Delete
protected static function deep_sort_with_accents( $sort_array ) {
[374] Fix | Delete
if ( ! is_array( $sort_array ) || empty( $sort_array ) ) {
[375] Fix | Delete
return $sort_array;
[376] Fix | Delete
}
[377] Fix | Delete
[378] Fix | Delete
$array_without_accents = array_map(
[379] Fix | Delete
function ( $value ) {
[380] Fix | Delete
return is_array( $value )
[381] Fix | Delete
? self::deep_sort_with_accents( $value )
[382] Fix | Delete
: remove_accents( wc_strtolower( html_entity_decode( $value ) ) );
[383] Fix | Delete
},
[384] Fix | Delete
$sort_array
[385] Fix | Delete
);
[386] Fix | Delete
[387] Fix | Delete
asort( $array_without_accents );
[388] Fix | Delete
return array_replace( $array_without_accents, $sort_array );
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
/**
[392] Fix | Delete
* Retrieves formatted shipping zones from WooCommerce.
[393] Fix | Delete
*
[394] Fix | Delete
* @return array An array of formatted shipping zones.
[395] Fix | Delete
*/
[396] Fix | Delete
public static function get_shipping_zones() {
[397] Fix | Delete
$shipping_zones = \WC_Shipping_Zones::get_zones();
[398] Fix | Delete
$formatted_shipping_zones = array_reduce(
[399] Fix | Delete
$shipping_zones,
[400] Fix | Delete
function ( $acc, $zone ) {
[401] Fix | Delete
$acc[] = array(
[402] Fix | Delete
'id' => $zone['id'],
[403] Fix | Delete
'title' => $zone['zone_name'],
[404] Fix | Delete
'description' => $zone['formatted_zone_location'],
[405] Fix | Delete
);
[406] Fix | Delete
return $acc;
[407] Fix | Delete
},
[408] Fix | Delete
array()
[409] Fix | Delete
);
[410] Fix | Delete
$formatted_shipping_zones[] = array(
[411] Fix | Delete
'id' => 0,
[412] Fix | Delete
'title' => __( 'International', 'woocommerce' ),
[413] Fix | Delete
'description' => __( 'Locations outside all other zones', 'woocommerce' ),
[414] Fix | Delete
);
[415] Fix | Delete
return $formatted_shipping_zones;
[416] Fix | Delete
}
[417] Fix | Delete
[418] Fix | Delete
/**
[419] Fix | Delete
* Recursively search the checkout block to find the express checkout block and
[420] Fix | Delete
* get the button style attributes using the parse_blocks function.
[421] Fix | Delete
*
[422] Fix | Delete
* @param array $blocks Blocks to search.
[423] Fix | Delete
* @param string $cart_or_checkout The block type to check.
[424] Fix | Delete
*
[425] Fix | Delete
* @return array Block attributes.
[426] Fix | Delete
*/
[427] Fix | Delete
public static function find_express_checkout_attributes_in_parsed_blocks( $blocks, $cart_or_checkout ) {
[428] Fix | Delete
$express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block';
[429] Fix | Delete
foreach ( $blocks as $block ) {
[430] Fix | Delete
if ( ! empty( $block['blockName'] ) && $express_block_name === $block['blockName'] && ! empty( $block['attrs'] ) ) {
[431] Fix | Delete
return $block['attrs'];
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[435] Fix | Delete
$answer = self::find_express_checkout_attributes_in_parsed_blocks( $block['innerBlocks'], $cart_or_checkout );
[436] Fix | Delete
if ( $answer ) {
[437] Fix | Delete
return $answer;
[438] Fix | Delete
}
[439] Fix | Delete
}
[440] Fix | Delete
}
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
/**
[444] Fix | Delete
* Recursively search the checkout block to find the express checkout block and
[445] Fix | Delete
* get the button style attributes
[446] Fix | Delete
*
[447] Fix | Delete
* @param string|array $post_content The post content.
[448] Fix | Delete
* @param string $cart_or_checkout The block type to check.
[449] Fix | Delete
*
[450] Fix | Delete
* @return array|null Block attributes, if present and valid, otherwise `null`.
[451] Fix | Delete
*/
[452] Fix | Delete
public static function find_express_checkout_attributes( $post_content, $cart_or_checkout ) {
[453] Fix | Delete
if ( is_array( $post_content ) ) {
[454] Fix | Delete
// If an array is passed, assume it's already been parsed with parse_blocks,
[455] Fix | Delete
// use the old method, and show a deprecation warning.
[456] Fix | Delete
wc_deprecated_argument(
[457] Fix | Delete
'post_content',
[458] Fix | Delete
'10.3.0',
[459] Fix | Delete
'Passing parsed blocks as an array in $post_content is deprecated. Please pass the post content as a string.'
[460] Fix | Delete
);
[461] Fix | Delete
return self::find_express_checkout_attributes_in_parsed_blocks( $post_content, $cart_or_checkout );
[462] Fix | Delete
}
[463] Fix | Delete
[464] Fix | Delete
$express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block';
[465] Fix | Delete
[466] Fix | Delete
$scanner = Block_Scanner::create( $post_content );
[467] Fix | Delete
[468] Fix | Delete
while ( $scanner->next_delimiter() ) {
[469] Fix | Delete
if ( $scanner->opens_block( $express_block_name ) ) {
[470] Fix | Delete
return $scanner->allocate_and_return_parsed_attributes();
[471] Fix | Delete
}
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
return null;
[475] Fix | Delete
}
[476] Fix | Delete
[477] Fix | Delete
/**
[478] Fix | Delete
* Given an array of blocks, find the express payment block and update its attributes.
[479] Fix | Delete
*
[480] Fix | Delete
* @param array $blocks Blocks to search.
[481] Fix | Delete
* @param string $cart_or_checkout The block type to check.
[482] Fix | Delete
* @param array $updated_attrs The new attributes to set.
[483] Fix | Delete
*/
[484] Fix | Delete
public static function update_blocks_with_new_attrs( &$blocks, $cart_or_checkout, $updated_attrs ) {
[485] Fix | Delete
$express_block_name = 'woocommerce/' . $cart_or_checkout . '-express-payment-block';
[486] Fix | Delete
foreach ( $blocks as $key => &$block ) {
[487] Fix | Delete
if ( ! empty( $block['blockName'] ) && $express_block_name === $block['blockName'] ) {
[488] Fix | Delete
$blocks[ $key ]['attrs'] = $updated_attrs;
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[492] Fix | Delete
self::update_blocks_with_new_attrs( $block['innerBlocks'], $cart_or_checkout, $updated_attrs );
[493] Fix | Delete
}
[494] Fix | Delete
}
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
/**
[498] Fix | Delete
* Check if the cart page is defined.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function