wc_get_page_id( 'shop' ), 'checkout' => wc_get_page_id( 'checkout' ), 'privacy' => wc_privacy_policy_page_id(), 'terms' => wc_terms_and_conditions_page_id(), ]; // Global settings used in each block. return array_merge( $settings, [ 'currentUserIsAdmin' => is_user_logged_in() && current_user_can( 'manage_woocommerce' ), '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, 'couponsEnabled' => wc_coupons_enabled(), 'shippingEnabled' => wc_shipping_enabled(), 'displayShopPricesIncludingTax' => 'incl' === get_option( 'woocommerce_tax_display_shop' ), 'displayCartPricesIncludingTax' => 'incl' === get_option( 'woocommerce_tax_display_cart' ), '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__ ), 'restApiRoutes' => [ '/wc/store' => array_keys( \Automattic\WooCommerce\Blocks\RestApi::get_routes_from_namespace( 'wc/store' ) ), ], 'homeUrl' => esc_url( home_url( '/' ) ), 'storePages' => [ 'shop' => $page_ids['shop'] ? [ 'name' => get_the_title( $page_ids['shop'] ), 'url' => get_permalink( $page_ids['shop'] ), ] : false, 'checkout' => $page_ids['checkout'] ? [ 'name' => get_the_title( $page_ids['checkout'] ), 'url' => get_permalink( $page_ids['checkout'] ), ] : false, 'privacy' => $page_ids['privacy'] ? [ 'name' => get_the_title( $page_ids['privacy'] ), 'url' => get_permalink( $page_ids['privacy'] ), ] : false, 'terms' => $page_ids['terms'] ? [ 'name' => get_the_title( $page_ids['terms'] ), 'url' => get_permalink( $page_ids['terms'] ), ] : false, ], ] ); } /** * 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 ) { if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ) ) { return filemtime( \Automattic\WooCommerce\Blocks\Package::get_path() . $file ); } return \Automattic\WooCommerce\Blocks\Package::get_version(); } /** * Registers a script according to `wp_register_script`, additionally loading the translations for the file. * * @since 2.0.0 * * @param string $handle Name of the script. Should be unique. * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. * @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array. * @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default 'true'. */ protected static function register_script( $handle, $src, $dependencies = [], $has_i18n = true ) { $relative_src = str_replace( plugins_url( '/', __DIR__ ), '', $src ); $asset_path = dirname( __DIR__ ) . '/' . str_replace( '.js', '.asset.php', $relative_src ); if ( file_exists( $asset_path ) ) { $asset = require $asset_path; $dependencies = isset( $asset['dependencies'] ) ? array_merge( $asset['dependencies'], $dependencies ) : $dependencies; $version = ! empty( $asset['version'] ) ? $asset['version'] : self::get_file_version( $relative_src ); } else { $version = self::get_file_version( $relative_src ); } wp_register_script( $handle, $src, $dependencies, $version, true ); if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) { wp_set_script_translations( $handle, 'woo-gutenberg-products-block', dirname( __DIR__ ) . '/languages' ); } } /** * Queues a block script. * * @since 2.3.0 * @since $VID:$ Changed $name to $script_name and added $handle argument. * * @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. */ public static function register_block_script( $script_name, $handle = '', $dependencies = [] ) { $handle = '' !== $handle ? $handle : $script_name; self::register_script( 'wc-' . $handle, plugins_url( self::get_block_asset_build_path( $script_name ), __DIR__ ), $dependencies ); wp_enqueue_script( 'wc-' . $handle ); } /** * 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)'. */ protected static function register_style( $handle, $src, $deps = [], $media = 'all' ) { $filename = str_replace( plugins_url( '/', __DIR__ ), '', $src ); $ver = self::get_file_version( $filename ); wp_register_style( $handle, $src, $deps, $ver, $media ); } /** * Returns the appropriate asset path for loading either legacy builds or * current builds. * * @param string $filename Filename for asset path (without extension). * @param string $type File type (.css or .js). * * @return string The generated path. */ protected static function get_block_asset_build_path( $filename, $type = 'js' ) { global $wp_version; $suffix = version_compare( $wp_version, '5.2', '>' ) ? '' : '-legacy'; return "build/$filename$suffix.$type"; } }