upgraded to 4.14

This commit is contained in:
2021-12-20 18:06:11 +00:00
parent 80f1e87db9
commit 3166bdf932
153 changed files with 5204 additions and 1003 deletions

View File

@ -10,6 +10,8 @@
* @since 3.29
*/
defined( 'ABSPATH' ) || exit;
/**
* Class representing WooCommerce Related Products component.
*
@ -26,6 +28,13 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
*/
public static $static_props;
/**
* Number of products to be offset.
*
* @var int Default 0.
*/
public static $offset = 0;
/**
* Initialize.
*
@ -33,12 +42,13 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
* @see {https://github.com/elegantthemes/Divi/issues/17436}
*/
public function init() {
$this->name = esc_html__( 'Woo Related Product', 'et_builder' );
$this->name = esc_html__( 'Woo Related Products', 'et_builder' );
$this->plural = esc_html__( 'Woo Related Products', 'et_builder' );
// Use `et_pb_wc_{module}` for all WooCommerce modules.
$this->slug = 'et_pb_wc_related_products';
$this->vb_support = 'on';
$this->slug = 'et_pb_wc_related_products';
$this->vb_support = 'on';
$this->folder_name = 'et_pb_woo_modules';
$this->main_css_element = '%%order_class%%';
@ -46,14 +56,16 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'general' => array(
'toggles' => array(
'main_content' => et_builder_i18n( 'Content' ),
'elements' => et_builder_i18n( 'Elements' ),
),
),
'advanced' => array(
'toggles' => array(
'overlay' => et_builder_i18n( 'Overlay' ),
'image' => et_builder_i18n( 'Image' ),
'overlay' => et_builder_i18n( 'Overlay' ),
'image' => et_builder_i18n( 'Image' ),
// Avoid Text suffix by manually defining the `star` toggle slug.
'star' => esc_html__( 'Star Rating', 'et_builder' ),
'star' => esc_html__( 'Star Rating', 'et_builder' ),
'sale_badge' => esc_html__( 'Sale Badge Text', 'et_builder' ),
),
),
);
@ -168,7 +180,7 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
),
),
'borders' => array(
'default' => array(
'default' => array(
'css' => array(
'main' => array(
'border_radii' => '%%order_class%%.et_pb_wc_related_products .product',
@ -176,7 +188,7 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
),
),
),
'image' => array(
'image' => array(
'css' => array(
'main' => array(
'border_radii' => '%%order_class%%.et_pb_module .et_shop_image',
@ -188,14 +200,29 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'tab_slug' => 'advanced',
'toggle_slug' => 'image',
),
'sale_badge' => array(
'css' => array(
'main' => array(
'border_radii' => '%%order_class%% span.onsale',
'border_styles' => '%%order_class%% span.onsale',
),
'important' => true,
),
'defaults' => array(
'border_radii' => 'on|3px|3px|3px|3px',
),
'label_prefix' => esc_html__( 'Sale Badge', 'et_builder' ),
'tab_slug' => 'advanced',
'toggle_slug' => 'sale_badge',
),
),
'box_shadow' => array(
'default' => array(
'default' => array(
'css' => array(
'main' => '%%order_class%% .product',
),
),
'image' => array(
'image' => array(
'label' => esc_html__( 'Image Box Shadow', 'et_builder' ),
'option_category' => 'layout',
'tab_slug' => 'advanced',
@ -209,6 +236,17 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'position' => '',
),
),
'sale_badge' => array(
'label' => esc_html__( 'Sale Badge Box Shadow', 'et_builder' ),
'option_category' => 'layout',
'tab_slug' => 'advanced',
'toggle_slug' => 'sale_badge',
'css' => array(
'main' => '%%order_class%% span.onsale',
'overlay' => 'inset',
'important' => true,
),
),
),
'margin_padding' => array(
'css' => array(
@ -250,6 +288,31 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
),
),
'button' => false,
'form_field' => array(
'sale_badge' => array(
'label' => esc_html__( 'Sale Badge', 'et_builder' ),
'background_color' => false,
'text_color' => false,
'focus_background_color' => false,
'focus_text_color' => false,
'font_field' => false,
'margin_padding' => array(
'css' => array(
'main' => '%%order_class%% ul.products li.product span.onsale',
'important' => array( 'custom_margin', 'custom_padding' ),
),
'custom_margin' => array(
'default' => '0px|0px|0px|0px|false|false',
),
'custom_padding' => array(
'default' => '6px|18px|6px|18px|false|false',
),
'toggle_slug' => 'sale_badge',
),
'border_styles' => false,
'box_shadow' => false,
),
),
);
$this->custom_css_fields = array(
@ -352,6 +415,24 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
),
)
),
'include_categories' => array(
'label' => esc_html__( 'Included Categories', 'et_builder' ),
'type' => 'categories',
'meta_categories' => array(
'all' => esc_html__( 'All Categories', 'et_builder' ),
'current' => esc_html__( 'Current Category', 'et_builder' ),
),
'renderer_options' => array(
'use_terms' => true,
'term_name' => 'product_cat',
),
'description' => esc_html__( 'Choose which categories you would like to include.', 'et_builder' ),
'taxonomy_name' => 'product_cat',
'toggle_slug' => 'main_content',
'computed_affects' => array(
'__related_products',
),
),
'sale_badge_color' => array(
'label' => esc_html__( 'Sale Badge Color', 'et_builder' ),
'description' => esc_html__( 'Pick a color to use for the sales bade that appears on products that are on sale.', 'et_builder' ),
@ -394,6 +475,84 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'mobile_options' => true,
'sticky' => true,
),
'show_name' => array(
'label' => esc_html__( 'Show Name', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default_on_front' => 'on',
'toggle_slug' => 'elements',
'description' => esc_html__( 'Turn name on or off.', 'et_builder' ),
'mobile_options' => true,
'hover' => 'tabs',
),
'show_image' => array(
'label' => esc_html__( 'Show Image', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default_on_front' => 'on',
'toggle_slug' => 'elements',
'description' => esc_html__( 'Turn image on or off.', 'et_builder' ),
'mobile_options' => true,
'hover' => 'tabs',
),
'show_price' => array(
'label' => esc_html__( 'Show Price', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default_on_front' => 'on',
'toggle_slug' => 'elements',
'description' => esc_html__( 'Turn price on or off.', 'et_builder' ),
'mobile_options' => true,
'hover' => 'tabs',
),
'show_rating' => array(
'label' => esc_html__( 'Show Rating', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default_on_front' => 'on',
'toggle_slug' => 'elements',
'description' => esc_html__( 'Turn rating on or off.', 'et_builder' ),
'mobile_options' => true,
'hover' => 'tabs',
),
'show_sale_badge' => array(
'label' => esc_html__( 'Show Sale Badge', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'default_on_front' => 'on',
'toggle_slug' => 'elements',
'description' => esc_html__( 'Turn sale badge on or off.', 'et_builder' ),
'mobile_options' => true,
'hover' => 'tabs',
),
'offset_number' => ET_Builder_Module_Helper_Woocommerce_Modules::get_field(
'offset_number',
array(
'computed_affects' => array(
'__related_products',
),
)
),
'__related_products' => array(
'type' => 'computed',
'computed_callback' => array(
@ -406,6 +565,8 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'posts_number',
'columns_number',
'orderby',
'offset_number',
'include_categories',
),
'computed_minimum' => array(
'product',
@ -416,6 +577,50 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
return $fields;
}
/**
* Filters the related product category Ids.
*
* @since 4.14.0
*
* @param array $term_ids Term IDs.
*
* @return array
*/
public static function set_related_products_categories( $term_ids ) {
$include_cats = et_()->array_get( self::$static_props, 'include_categories', '' );
$meta_cats = array( 'all', 'current' );
// WooCommerce by default handles All & Current based on the global $product.
// So return the filtered $term_ids to let WooCommerce take control.
if ( in_array( $include_cats, $meta_cats, true ) || empty( $include_cats ) ) {
return $term_ids;
}
// Return user selected categories if they exist.
$include_cats = explode( ',', $include_cats );
return $include_cats;
}
/**
* Appends offset to the WP_Query that retrieves Products.
*
* @since 4.14.0
*
* @param array $query_args Query args.
*
* @return array
*/
public static function append_offset( $query_args ) {
if ( ! is_array( $query_args ) ) {
return $query_args;
}
$query_args['offset'] = self::$offset;
return $query_args;
}
/**
* {@inheritdoc}
*/
@ -450,6 +655,10 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
* we pass them via a static variable.
*/
self::$static_props = $args;
$offset_number = et_()->array_get( $args, 'offset_number', 0 );
$include_cats = et_()->array_get( $args, 'include_categories', '' );
$include_cats = ! empty( $include_cats ) ? explode( ',', $include_cats ) : array();
$is_include_cats = is_array( $include_cats ) && count( $include_cats ) > 0;
// Force set product's class to ET_Theme_Builder_Woocommerce_Product_Variable_Placeholder
// in TB so related product can outputs visible content based on pre-filled value in TB
@ -457,11 +666,45 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
add_filter( 'woocommerce_product_class', 'et_theme_builder_wc_product_class' );
}
$is_offset_valid = absint( $offset_number ) > 0;
if ( $is_offset_valid ) {
self::$offset = $offset_number;
add_filter(
'woocommerce_shortcode_products_query',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
'append_offset',
)
);
}
if ( $is_include_cats ) {
$product_id = et_builder_wc_get_product_id( $args );
// To include only selected categories the cached transient should be flushed,
// so WooCommerce can compute the categories and cache it again.
delete_transient( 'wc_related_' . $product_id );
add_filter(
'woocommerce_get_related_product_cat_terms',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'set_related_products_categories'
)
);
// Also disable related Products by tag
// so Products from other categories are not included.
add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );
}
add_filter(
'woocommerce_output_related_products_args',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
'set_related_products_args',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'set_related_products_args'
)
);
@ -471,10 +714,37 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
'woocommerce_output_related_products_args',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
'set_related_products_args',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'set_related_products_args'
)
);
if ( $is_include_cats ) {
remove_filter(
'woocommerce_get_related_product_cat_terms',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'set_related_products_categories'
)
);
remove_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );
}
if ( $is_offset_valid ) {
remove_filter(
'woocommerce_shortcode_products_query',
array(
'ET_Builder_Module_Woocommerce_Related_Products',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'append_offset'
)
);
self::$offset = 0;
}
return $output;
}
@ -553,6 +823,54 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
return $selected_args;
}
/**
* Adds Multi view attributes to the Outer wrapper.
*
* Since we do not have control over the WooCommerce Related Products markup, we inject Multi
* view attributes on to the Outer wrapper.
*
* @param array $outer_wrapper_attrs Outer wrapper attributes.
*
* @return array
*/
public function add_multi_view_attrs( $outer_wrapper_attrs ) {
$multi_view = et_pb_multi_view_options( $this );
// phpcs:disable WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned -- Invalid warning.
$multi_view_attrs = $multi_view->render_attrs(
array(
'classes' => array(
'et_pb_wc_related_products_no_name' => array(
'show_name' => 'off',
),
'et_pb_wc_related_products_no_image' => array(
'show_image' => 'off',
),
'et_pb_wc_related_products_no_price' => array(
'show_price' => 'off',
),
'et_pb_wc_related_products_no_rating' => array(
'show_rating' => 'off',
),
'et_pb_wc_related_products_no_sale_badge' => array(
'show_sale_badge' => 'off',
),
),
),
false,
null,
true
);
// phpcs:enable WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned
if ( $multi_view_attrs && is_array( $multi_view_attrs ) ) {
$outer_wrapper_attrs = array_merge( $outer_wrapper_attrs, $multi_view_attrs );
}
return $outer_wrapper_attrs;
}
/**
* Renders the module output.
*
@ -680,19 +998,30 @@ class ET_Builder_Module_Woocommerce_Related_Products extends ET_Builder_Module {
"et_builder_module_{$render_slug}_outer_wrapper_attrs",
array(
'ET_Builder_Module_Helper_Woocommerce_Modules',
'output_data_icon_attrs',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'output_data_icon_attrs'
),
10,
2
);
add_filter(
"et_builder_module_{$render_slug}_outer_wrapper_attrs",
array(
$this,
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'add_multi_view_attrs',
)
);
$output = $this->_render_module_wrapper( $output, $render_slug );
remove_filter(
"et_builder_module_{$render_slug}_outer_wrapper_attrs",
array(
'ET_Builder_Module_Helper_Woocommerce_Modules',
'output_data_icon_attrs',
// phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- Call to a function.
'output_data_icon_attrs'
),
10
);