2019-07-01 12:52:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Initializes block assets.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Blocks
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Blocks;
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2020-04-27 15:51:50 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Package;
|
2020-07-07 08:15:46 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
|
2020-04-27 15:51:50 +00:00
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
/**
|
|
|
|
* Assets class.
|
|
|
|
*/
|
|
|
|
class Assets {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize class features on init.
|
2019-09-23 18:07:13 +00:00
|
|
|
*
|
2019-11-19 17:20:22 +00:00
|
|
|
* @since 2.5.0
|
2019-09-23 18:07:13 +00:00
|
|
|
* Moved most initialization to BootStrap and AssetDataRegistry
|
|
|
|
* classes as a part of ongoing refactor
|
2019-07-01 12:52:44 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'init', array( __CLASS__, 'register_assets' ) );
|
|
|
|
add_action( 'body_class', array( __CLASS__, 'add_theme_body_class' ), 1 );
|
2020-05-21 17:09:50 +00:00
|
|
|
add_action( 'admin_body_class', array( __CLASS__, 'add_theme_admin_body_class' ), 1 );
|
2019-09-23 18:07:13 +00:00
|
|
|
add_filter( 'woocommerce_shared_settings', array( __CLASS__, 'get_wc_block_data' ) );
|
2019-07-01 12:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register block scripts & styles.
|
2019-09-23 18:07:13 +00:00
|
|
|
*
|
2019-11-19 17:20:22 +00:00
|
|
|
* @since 2.5.0
|
2019-09-23 18:07:13 +00:00
|
|
|
* Moved data related enqueuing to new AssetDataRegistry class
|
|
|
|
* as part of ongoing refactoring.
|
2019-07-01 12:52:44 +00:00
|
|
|
*/
|
|
|
|
public static function register_assets() {
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api = Package::container()->get( AssetApi::class );
|
|
|
|
self::register_style( 'wc-block-vendors-style', plugins_url( $asset_api->get_block_asset_build_path( 'vendors-style', 'css' ), __DIR__ ), [] );
|
|
|
|
self::register_style( 'wc-block-editor', plugins_url( $asset_api->get_block_asset_build_path( 'editor', 'css' ), __DIR__ ), array( 'wp-edit-blocks' ) );
|
2019-09-03 17:55:20 +00:00
|
|
|
wp_style_add_data( 'wc-block-editor', 'rtl', 'replace' );
|
2020-07-07 08:15:46 +00:00
|
|
|
self::register_style( 'wc-block-style', plugins_url( $asset_api->get_block_asset_build_path( 'style', 'css' ), __DIR__ ), array( 'wc-block-vendors-style' ) );
|
2019-09-03 17:55:20 +00:00
|
|
|
wp_style_add_data( 'wc-block-style', 'rtl', 'replace' );
|
2019-07-01 12:52:44 +00:00
|
|
|
|
|
|
|
// Shared libraries and components across all blocks.
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api->register_script( 'wc-blocks-middleware', 'build/wc-blocks-middleware.js', [], false );
|
|
|
|
$asset_api->register_script( 'wc-blocks-data-store', 'build/wc-blocks-data.js', [ 'wc-blocks-middleware' ], false );
|
|
|
|
$asset_api->register_script( 'wc-blocks', $asset_api->get_block_asset_build_path( 'blocks' ), [], false );
|
|
|
|
$asset_api->register_script( 'wc-vendors', $asset_api->get_block_asset_build_path( 'vendors' ), [], false );
|
|
|
|
$asset_api->register_script( 'wc-blocks-registry', 'build/wc-blocks-registry.js', [], false );
|
|
|
|
$asset_api->register_script( 'wc-shared-context', 'build/wc-shared-context.js', [], false );
|
2020-07-22 12:20:54 +00:00
|
|
|
$asset_api->register_script( 'wc-shared-hocs', 'build/wc-shared-hocs.js', [], false );
|
2019-07-01 12:52:44 +00:00
|
|
|
|
2020-05-08 14:39:03 +00:00
|
|
|
// Inline data.
|
|
|
|
wp_add_inline_script(
|
|
|
|
'wc-blocks-middleware',
|
|
|
|
"var wcStoreApiNonce = '" . esc_js( wp_create_nonce( 'wc_store_api' ) ) . "';",
|
|
|
|
'before'
|
|
|
|
);
|
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
// Individual blocks.
|
2019-10-28 13:53:09 +00:00
|
|
|
$block_dependencies = array( 'wc-vendors', 'wc-blocks' );
|
|
|
|
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api->register_script( 'wc-handpicked-products', $asset_api->get_block_asset_build_path( 'handpicked-products' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-best-sellers', $asset_api->get_block_asset_build_path( 'product-best-sellers' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-category', $asset_api->get_block_asset_build_path( 'product-category' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-new', $asset_api->get_block_asset_build_path( 'product-new' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-on-sale', $asset_api->get_block_asset_build_path( 'product-on-sale' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-top-rated', $asset_api->get_block_asset_build_path( 'product-top-rated' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-products-by-attribute', $asset_api->get_block_asset_build_path( 'products-by-attribute' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-featured-product', $asset_api->get_block_asset_build_path( 'featured-product' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-featured-category', $asset_api->get_block_asset_build_path( 'featured-category' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-categories', $asset_api->get_block_asset_build_path( 'product-categories' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-tag', $asset_api->get_block_asset_build_path( 'product-tag' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-all-reviews', $asset_api->get_block_asset_build_path( 'all-reviews' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-reviews-by-product', $asset_api->get_block_asset_build_path( 'reviews-by-product' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-reviews-by-category', $asset_api->get_block_asset_build_path( 'reviews-by-category' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-product-search', $asset_api->get_block_asset_build_path( 'product-search' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-all-products', $asset_api->get_block_asset_build_path( 'all-products' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-price-filter', $asset_api->get_block_asset_build_path( 'price-filter' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-attribute-filter', $asset_api->get_block_asset_build_path( 'attribute-filter' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-active-filters', $asset_api->get_block_asset_build_path( 'active-filters' ), $block_dependencies );
|
2020-05-27 14:45:18 +00:00
|
|
|
|
2020-06-05 19:13:51 +00:00
|
|
|
if ( Package::is_experimental_build() ) {
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api->register_script( 'wc-single-product-block', $asset_api->get_block_asset_build_path( 'single-product' ), $block_dependencies );
|
2020-05-27 14:45:18 +00:00
|
|
|
}
|
2020-06-05 19:13:51 +00:00
|
|
|
|
|
|
|
if ( Package::is_feature_plugin_build() ) {
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api->register_script( 'wc-checkout-block', $asset_api->get_block_asset_build_path( 'checkout' ), $block_dependencies );
|
|
|
|
$asset_api->register_script( 'wc-cart-block', $asset_api->get_block_asset_build_path( 'cart' ), $block_dependencies );
|
2020-06-05 19:13:51 +00:00
|
|
|
}
|
2019-08-27 15:25:32 +00:00
|
|
|
}
|
2019-08-22 20:56:47 +00:00
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
/**
|
|
|
|
* Add body classes.
|
|
|
|
*
|
|
|
|
* @param array $classes Array of CSS classnames.
|
|
|
|
* @return array Modified array of CSS classnames.
|
|
|
|
*/
|
2019-08-27 15:25:32 +00:00
|
|
|
public static function add_theme_body_class( $classes = [] ) {
|
2019-07-01 12:52:44 +00:00
|
|
|
$classes[] = 'theme-' . get_template();
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:09:50 +00:00
|
|
|
/**
|
|
|
|
* Add theme class to admin body.
|
|
|
|
*
|
|
|
|
* @param array $classes String with the CSS classnames.
|
|
|
|
* @return array Modified string of CSS classnames.
|
|
|
|
*/
|
|
|
|
public static function add_theme_admin_body_class( $classes = '' ) {
|
|
|
|
$classes .= ' theme-' . get_template();
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
/**
|
2019-09-02 10:10:47 +00:00
|
|
|
* Returns block-related data for enqueued wc-block-settings script.
|
2019-07-01 12:52:44 +00:00
|
|
|
*
|
|
|
|
* This is used to map site settings & data into JS-accessible variables.
|
|
|
|
*
|
2019-09-23 18:07:13 +00:00
|
|
|
* @param array $settings The original settings array from the filter.
|
|
|
|
*
|
2019-08-22 20:56:47 +00:00
|
|
|
* @since 2.4.0
|
2019-11-19 17:20:22 +00:00
|
|
|
* @since 2.5.0 returned merged data along with incoming $settings
|
2019-07-01 12:52:44 +00:00
|
|
|
*/
|
2019-09-23 18:07:13 +00:00
|
|
|
public static function get_wc_block_data( $settings ) {
|
2019-10-16 12:02:43 +00:00
|
|
|
$tag_count = wp_count_terms( 'product_tag' );
|
|
|
|
$product_counts = wp_count_posts( 'product' );
|
2020-03-06 12:20:17 +00:00
|
|
|
$page_ids = [
|
|
|
|
'shop' => wc_get_page_id( 'shop' ),
|
2020-03-16 16:38:24 +00:00
|
|
|
'cart' => wc_get_page_id( 'cart' ),
|
2020-03-06 12:20:17 +00:00
|
|
|
'checkout' => wc_get_page_id( 'checkout' ),
|
|
|
|
'privacy' => wc_privacy_policy_page_id(),
|
|
|
|
'terms' => wc_terms_and_conditions_page_id(),
|
|
|
|
];
|
2019-07-01 12:52:44 +00:00
|
|
|
|
|
|
|
// Global settings used in each block.
|
2019-09-23 18:07:13 +00:00
|
|
|
return array_merge(
|
|
|
|
$settings,
|
|
|
|
[
|
2020-03-06 11:43:40 +00:00
|
|
|
'currentUserIsAdmin' => is_user_logged_in() && current_user_can( 'manage_woocommerce' ),
|
2020-03-03 10:12:18 +00:00
|
|
|
'min_columns' => wc_get_theme_support( 'product_blocks::min_columns', 1 ),
|
|
|
|
'max_columns' => wc_get_theme_support( 'product_blocks::max_columns', 6 ),
|
|
|
|
'default_columns' => wc_get_theme_support( 'product_blocks::default_columns', 3 ),
|
|
|
|
'min_rows' => wc_get_theme_support( 'product_blocks::min_rows', 1 ),
|
|
|
|
'max_rows' => wc_get_theme_support( 'product_blocks::max_rows', 6 ),
|
|
|
|
'default_rows' => wc_get_theme_support( 'product_blocks::default_rows', 3 ),
|
|
|
|
'thumbnail_size' => wc_get_theme_support( 'thumbnail_image_width', 300 ),
|
|
|
|
'placeholderImgSrc' => wc_placeholder_img_src(),
|
|
|
|
'min_height' => wc_get_theme_support( 'featured_block::min_height', 500 ),
|
|
|
|
'default_height' => wc_get_theme_support( 'featured_block::default_height', 500 ),
|
|
|
|
'isLargeCatalog' => $product_counts->publish > 100,
|
|
|
|
'limitTags' => $tag_count > 100,
|
|
|
|
'hasTags' => $tag_count > 0,
|
2020-04-22 13:16:17 +00:00
|
|
|
'taxesEnabled' => wc_tax_enabled(),
|
2020-03-03 10:12:18 +00:00
|
|
|
'couponsEnabled' => wc_coupons_enabled(),
|
|
|
|
'shippingEnabled' => wc_shipping_enabled(),
|
2020-04-24 13:44:44 +00:00
|
|
|
'displayItemizedTaxes' => 'itemized' === get_option( 'woocommerce_tax_total_display' ),
|
2020-03-03 10:12:18 +00:00
|
|
|
'displayShopPricesIncludingTax' => 'incl' === get_option( 'woocommerce_tax_display_shop' ),
|
|
|
|
'displayCartPricesIncludingTax' => 'incl' === get_option( 'woocommerce_tax_display_cart' ),
|
2020-04-30 12:54:43 +00:00
|
|
|
'checkoutShowLoginReminder' => 'yes' === get_option( 'woocommerce_enable_checkout_login_reminder' ),
|
2020-03-03 10:12:18 +00:00
|
|
|
'showAvatars' => '1' === get_option( 'show_avatars' ),
|
|
|
|
'reviewRatingsEnabled' => wc_review_ratings_enabled(),
|
|
|
|
'productCount' => array_sum( (array) $product_counts ),
|
|
|
|
'attributes' => array_values( wc_get_attribute_taxonomies() ),
|
|
|
|
'isShippingCalculatorEnabled' => filter_var( get_option( 'woocommerce_enable_shipping_calc' ), FILTER_VALIDATE_BOOLEAN ),
|
|
|
|
'isShippingCostHidden' => filter_var( get_option( 'woocommerce_shipping_cost_requires_address' ), FILTER_VALIDATE_BOOLEAN ),
|
|
|
|
'wcBlocksAssetUrl' => plugins_url( 'assets/', __DIR__ ),
|
2020-07-14 11:35:15 +00:00
|
|
|
'wcBlocksBuildUrl' => plugins_url( 'build/', __DIR__ ),
|
2020-03-03 10:12:18 +00:00
|
|
|
'restApiRoutes' => [
|
2020-04-27 15:51:50 +00:00
|
|
|
'/wc/store' => array_keys( Package::container()->get( RestApi::class )->get_routes_from_namespace( 'wc/store' ) ),
|
2019-11-23 16:29:35 +00:00
|
|
|
],
|
2020-03-06 12:20:17 +00:00
|
|
|
'homeUrl' => esc_url( home_url( '/' ) ),
|
|
|
|
'storePages' => [
|
2020-03-16 16:38:24 +00:00
|
|
|
'shop' => self::format_page_resource( $page_ids['shop'] ),
|
|
|
|
'cart' => self::format_page_resource( $page_ids['cart'] ),
|
|
|
|
'checkout' => self::format_page_resource( $page_ids['checkout'] ),
|
|
|
|
'privacy' => self::format_page_resource( $page_ids['privacy'] ),
|
|
|
|
'terms' => self::format_page_resource( $page_ids['terms'] ),
|
2020-03-06 12:20:17 +00:00
|
|
|
],
|
2020-05-04 19:40:09 +00:00
|
|
|
'checkoutAllowsGuest' => 'yes' === get_option( 'woocommerce_enable_guest_checkout' ),
|
|
|
|
'checkoutAllowsSignup' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout' ),
|
2020-05-01 11:24:28 +00:00
|
|
|
'baseLocation' => wc_get_base_location(),
|
2020-06-05 19:13:51 +00:00
|
|
|
'woocommerceBlocksPhase' => WOOCOMMERCE_BLOCKS_PHASE,
|
2020-08-14 11:08:16 +00:00
|
|
|
'hasDarkEditorStyleSupport' => current_theme_supports( 'dark-editor-style' ),
|
2020-05-06 10:30:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* translators: If your word count is based on single characters (e.g. East Asian characters),
|
|
|
|
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
|
|
|
|
* Do not translate into your own language.
|
|
|
|
*/
|
|
|
|
'wordCountType' => _x( 'words', 'Word count type. Do not translate!', 'woo-gutenberg-products-block' ),
|
2019-09-23 18:07:13 +00:00
|
|
|
]
|
2019-07-01 12:52:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
/**
|
|
|
|
* Format a page object into a standard array of data.
|
|
|
|
*
|
|
|
|
* @param WP_Post|int $page Page object or ID.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function format_page_resource( $page ) {
|
2020-05-01 10:02:20 +00:00
|
|
|
if ( is_numeric( $page ) && $page > 0 ) {
|
2020-03-16 16:38:24 +00:00
|
|
|
$page = get_post( $page );
|
|
|
|
}
|
2020-05-01 10:02:20 +00:00
|
|
|
if ( ! is_a( $page, '\WP_Post' ) ) {
|
2020-03-16 16:38:24 +00:00
|
|
|
return [
|
|
|
|
'id' => 0,
|
|
|
|
'title' => '',
|
|
|
|
'permalink' => false,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'id' => $page->ID,
|
|
|
|
'title' => $page->post_title,
|
|
|
|
'permalink' => get_permalink( $page->ID ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
/**
|
|
|
|
* Get the file modified time as a cache buster if we're in dev mode.
|
|
|
|
*
|
|
|
|
* @param string $file Local path to the file.
|
|
|
|
* @return string The cache buster value to use for the given file.
|
|
|
|
*/
|
|
|
|
protected static function get_file_version( $file ) {
|
2019-10-28 13:53:09 +00:00
|
|
|
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) {
|
|
|
|
return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file );
|
2019-07-01 12:52:44 +00:00
|
|
|
}
|
2019-07-22 15:20:51 +00:00
|
|
|
return \Automattic\WooCommerce\Blocks\Package::get_version();
|
2019-07-01 12:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-07 08:15:46 +00:00
|
|
|
* Queues a block script in the frontend.
|
2019-07-22 09:36:51 +00:00
|
|
|
*
|
|
|
|
* @since 2.3.0
|
2020-05-27 15:20:35 +00:00
|
|
|
* @since 2.6.0 Changed $name to $script_name and added $handle argument.
|
2020-07-07 08:15:46 +00:00
|
|
|
* @since 2.9.0 Made it so scripts are not loaded in admin pages.
|
2019-07-22 09:36:51 +00:00
|
|
|
*
|
2020-01-10 14:37:27 +00:00
|
|
|
* @param string $script_name Name of the script used to identify the file inside build folder.
|
|
|
|
* @param string $handle Optional. Provided if the handle should be different than the script name. `wc-` prefix automatically added.
|
|
|
|
* @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array.
|
2019-07-22 09:36:51 +00:00
|
|
|
*/
|
2020-01-10 14:37:27 +00:00
|
|
|
public static function register_block_script( $script_name, $handle = '', $dependencies = [] ) {
|
2020-07-07 08:15:46 +00:00
|
|
|
$asset_api = Package::container()->get( AssetApi::class );
|
|
|
|
$asset_api->register_block_script( $script_name, $handle, $dependencies );
|
2019-07-22 09:36:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 12:52:44 +00:00
|
|
|
/**
|
|
|
|
* Registers a style according to `wp_register_style`.
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
*
|
|
|
|
* @param string $handle Name of the stylesheet. Should be unique.
|
|
|
|
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
|
|
|
|
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
|
|
|
|
* @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like
|
|
|
|
* 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
|
|
|
|
*/
|
2019-08-27 15:25:32 +00:00
|
|
|
protected static function register_style( $handle, $src, $deps = [], $media = 'all' ) {
|
2019-07-01 12:52:44 +00:00
|
|
|
$filename = str_replace( plugins_url( '/', __DIR__ ), '', $src );
|
|
|
|
$ver = self::get_file_version( $filename );
|
|
|
|
wp_register_style( $handle, $src, $deps, $ver, $media );
|
|
|
|
}
|
|
|
|
}
|