array( __CLASS__, 'render_handpicked_products' ), 'editor_script' => 'wc-handpicked-products', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => array( 'columns' => array( 'type' => 'number', 'default' => wc_get_theme_support( 'product_blocks::default_columns', 3 ), ), 'editMode' => array( 'type' => 'boolean', 'default' => true, ), 'orderby' => array( 'type' => 'string', 'enum' => array( 'date', 'popularity', 'price_asc', 'price_desc', 'rating', 'title' ), 'default' => 'date', ), 'products' => array( 'type' => 'array', 'items' => array( 'type' => 'number', ), 'default' => array(), ), 'contentVisibility' => self::get_schema_content_visibility(), ), ) ); register_block_type( 'woocommerce/product-best-sellers', array( 'render_callback' => array( __CLASS__, 'render_product_best_sellers' ), 'editor_script' => 'wc-product-best-sellers', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => self::get_shared_attributes(), ) ); register_block_type( 'woocommerce/product-category', array( 'render_callback' => array( __CLASS__, 'render_product_category' ), 'editor_script' => 'wc-product-category', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => array_merge( self::get_shared_attributes(), array( 'orderby' => array( 'type' => 'string', 'enum' => array( 'date', 'popularity', 'price_asc', 'price_desc', 'rating', 'title' ), 'default' => 'date', ), 'editMode' => array( 'type' => 'boolean', 'default' => true, ), ) ), ) ); register_block_type( 'woocommerce/product-new', array( 'render_callback' => array( __CLASS__, 'render_product_new' ), 'editor_script' => 'wc-product-new', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => self::get_shared_attributes(), ) ); register_block_type( 'woocommerce/product-on-sale', array( 'render_callback' => array( __CLASS__, 'render_product_on_sale' ), 'editor_script' => 'wc-product-on-sale', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => array_merge( self::get_shared_attributes(), array( 'orderby' => array( 'type' => 'string', 'enum' => array( 'date', 'popularity', 'price_asc', 'price_desc', 'rating', 'title' ), 'default' => 'date', ), ) ), ) ); register_block_type( 'woocommerce/product-top-rated', array( 'render_callback' => array( __CLASS__, 'render_product_top_rated' ), 'editor_script' => 'wc-product-top-rated', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', 'attributes' => self::get_shared_attributes(), ) ); register_block_type( 'woocommerce/products-by-attribute', array( 'editor_script' => 'wc-products-attribute', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', ) ); register_block_type( 'woocommerce/featured-product', array( 'render_callback' => array( 'WGPB_Block_Featured_Product', 'render' ), 'editor_script' => 'wc-featured-product', 'editor_style' => 'wc-block-editor', 'style' => 'wc-block-style', ) ); } /** * Output useful globals before printing any script tags. * * These are used by @woocommerce/components & the block library to set up defaults * based on user-controlled settings from WordPress. * * @since 2.0.0 */ public static function print_script_settings() { global $wp_locale; $code = get_woocommerce_currency(); $product_counts = wp_count_posts( 'product' ); // NOTE: wcSettings is not used directly, it's only for @woocommerce/components // // Settings and variables can be passed here for access in the app. // Will need `wcAdminAssetUrl` if the ImageAsset component is used. // Will need `dataEndpoints.countries` if Search component is used with 'country' type. // Will need `orderStatuses` if the OrderStatus component is used. // Deliberately excluding: `embedBreadcrumbs`, `trackingEnabled`. $settings = array( 'adminUrl' => admin_url(), 'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ), 'siteLocale' => esc_attr( get_bloginfo( 'language' ) ), 'currency' => array( 'code' => $code, 'precision' => wc_get_price_decimals(), 'symbol' => get_woocommerce_currency_symbol( $code ), 'position' => get_option( 'woocommerce_currency_pos' ), ), 'stockStatuses' => wc_get_product_stock_status_options(), 'siteTitle' => get_bloginfo( 'name' ), 'dataEndpoints' => array(), 'l10n' => array( 'userLocale' => get_user_locale(), 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), ), ); // NOTE: wcSettings is not used directly, it's only for @woocommerce/components. $settings = apply_filters( 'woocommerce_components_settings', $settings ); // Global settings used in each block. $block_settings = array( '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', 1 ), '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 > 200, ); ?> 'object', 'properties' => array( 'title' => array( 'type' => 'boolean', 'default' => true, ), 'price' => array( 'type' => 'boolean', 'default' => true, ), 'rating' => array( 'type' => 'boolean', 'default' => true, ), 'button' => array( 'type' => 'boolean', 'default' => true, ), ), ); } /** * Get a set of attributes shared across most of the grid blocks. * * @return array List of block attributes with type and defaults. */ public static function get_shared_attributes() { return array( 'columns' => array( 'type' => 'number', 'default' => wc_get_theme_support( 'product_blocks::default_columns', 3 ), ), 'rows' => array( 'type' => 'number', 'default' => wc_get_theme_support( 'product_blocks::default_rows', 1 ), ), 'categories' => array( 'type' => 'array', 'items' => array( 'type' => 'number', ), 'default' => array(), ), 'catOperator' => array( 'type' => 'string', 'default' => 'any', ), 'contentVisibility' => self::get_schema_content_visibility(), ); } /** * New products: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_product_new( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-product-new.php'; $block = new WGPB_Block_Product_New( $attributes, $content ); return $block->render(); } /** * Sale products: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_product_on_sale( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-product-on-sale.php'; $block = new WGPB_Block_Product_On_Sale( $attributes, $content ); return $block->render(); } /** * Products by category: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_product_category( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-product-category.php'; $block = new WGPB_Block_Product_Category( $attributes, $content ); return $block->render(); } /** * Top rated products: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_product_top_rated( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-product-top-rated.php'; $block = new WGPB_Block_Product_Top_Rated( $attributes, $content ); return $block->render(); } /** * Best Selling Products: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_product_best_sellers( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-product-best-sellers.php'; $block = new WGPB_Block_Product_Best_Sellers( $attributes, $content ); return $block->render(); } /** * Hand-picked Products: Include and render the dynamic block. * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ public static function render_handpicked_products( $attributes, $content ) { require_once dirname( __FILE__ ) . '/class-wgpb-block-handpicked-products.php'; $block = new WGPB_Block_Handpicked_Products( $attributes, $content ); return $block->render(); } /** * Add body classes. * * @param array $classes Array of CSS classnames. * @return array Modified array of CSS classnames. */ public static function add_theme_body_class( $classes = array() ) { $classes[] = 'theme-' . get_template(); return $classes; } } WGPB_Block_Library::get_instance();