| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main ProductMeta class. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Elementor\Widgets\Single; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use RadiusTheme\SB\Helpers\Fns; |
| 12 |
use RadiusTheme\SB\Abstracts\ElementorWidgetBase; |
| 13 |
use RadiusTheme\SB\Elementor\Widgets\Controls\MetaSettings; |
| 14 |
use RadiusTheme\SB\Elementor\Widgets\Controls\ProductTaxSettings; |
| 15 |
use WC_Product; |
| 16 |
|
| 17 |
// Do not allow directly accessing this file. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit( 'This script cannot be accessed directly.' ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Product Meta class |
| 24 |
*/ |
| 25 |
class ProductMeta extends ElementorWidgetBase { |
| 26 |
/** |
| 27 |
* Construct function |
| 28 |
* |
| 29 |
* @param array $data default array. |
| 30 |
* @param mixed $args default arg. |
| 31 |
*/ |
| 32 |
public function __construct( $data = [], $args = null ) { |
| 33 |
$this->rtsb_name = esc_html__( 'Product Meta', 'shopbuilder' ); |
| 34 |
$this->rtsb_base = 'rtsb-product-meta'; |
| 35 |
parent::__construct( $data, $args ); |
| 36 |
} |
| 37 |
/** |
| 38 |
* Widget Field |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function widget_fields() { |
| 43 |
$fields = MetaSettings::widget_fields( $this ); |
| 44 |
$tax_settings = ProductTaxSettings::widget_fields( $this ); |
| 45 |
$tax_settings['meta_content']['tab'] = 'style'; |
| 46 |
$fields = $fields + $tax_settings; |
| 47 |
unset( $fields['show_label'] ); |
| 48 |
unset( $fields['meta_label_heading']['condition'] ); |
| 49 |
unset( $fields['label_typo']['condition'] ); |
| 50 |
unset( $fields['meta_label_color']['condition'] ); |
| 51 |
return $fields; |
| 52 |
} |
| 53 |
/** |
| 54 |
* Set Widget Keyword. |
| 55 |
* |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function get_keywords() { |
| 59 |
return [ 'SKU', 'Category', 'Tag' ] + parent::get_keywords(); |
| 60 |
} |
| 61 |
/** |
| 62 |
* Registers the brand row on the product meta for the editor preview. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function apply_hooks() { |
| 67 |
add_action( 'woocommerce_product_meta_end', [ $this, 'show_brand' ], 99 ); |
| 68 |
} |
| 69 |
/** |
| 70 |
* Displays the brand row in the editor preview (WooCommerce core's brand hook bails |
| 71 |
* there because the request is not a singular product). The label is wrapped in a |
| 72 |
* `.rtsb-meta-label` element so it can be styled independently of the value. |
| 73 |
*/ |
| 74 |
public function show_brand() { |
| 75 |
$product = Fns::get_product(); |
| 76 |
if ( ! $product instanceof WC_Product ) { |
| 77 |
$product = wc_get_product(); |
| 78 |
} |
| 79 |
if ( ! $product instanceof WC_Product || ! function_exists( 'wc_get_brands' ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
$terms = get_the_terms( $product->get_id(), 'product_brand' ); |
| 83 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
echo wc_get_brands( $product->get_id(), ', ', $this->brand_meta_before( count( $terms ) ), '</span>' ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Wraps WooCommerce core's brand meta output so its label sits in a `.rtsb-meta-label` |
| 91 |
* element, matching the SKU/Category/Tag rows. Hooked onto `woocommerce_product_brands_output` |
| 92 |
* (WC 9.8+) only while this widget renders, so it never affects brand output elsewhere. |
| 93 |
* |
| 94 |
* @param string $brand_output Default brand HTML from WooCommerce. |
| 95 |
* @param mixed $terms Brand term objects, or empty when none. |
| 96 |
* @param int $post_id Product ID. |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function wrap_brand_label( $brand_output, $terms, $post_id ) { |
| 101 |
if ( empty( $terms ) || is_wp_error( $terms ) || ! function_exists( 'wc_get_brands' ) ) { |
| 102 |
return $brand_output; |
| 103 |
} |
| 104 |
return wc_get_brands( $post_id, ', ', $this->brand_meta_before( count( $terms ) ), '</span>' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Builds the opening markup of the brand row with its label wrapped in `.rtsb-meta-label`. |
| 109 |
* |
| 110 |
* @param int $count Number of brand terms (for the singular/plural label). |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
private function brand_meta_before( $count ) { |
| 115 |
$taxonomy = get_taxonomy( 'product_brand' ); |
| 116 |
if ( ! $taxonomy ) { |
| 117 |
return '<span class="posted_in"><span class="rtsb-meta-label">' . esc_html__( 'Brand:', 'shopbuilder' ) . '</span> '; |
| 118 |
} |
| 119 |
$labels = $taxonomy->labels; |
| 120 |
/* translators: %s - Brand label name */ |
| 121 |
$label = sprintf( _n( '%s:', '%s:', $count, 'shopbuilder' ), $labels->singular_name, $labels->name ); |
| 122 |
return '<span class="posted_in"><span class="rtsb-meta-label">' . esc_html( $label ) . '</span> '; |
| 123 |
} |
| 124 |
/** |
| 125 |
* Render Function |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
protected function render() { |
| 130 |
global $product; |
| 131 |
$_product = $product; |
| 132 |
$product = Fns::get_product(); |
| 133 |
$controllers = $this->get_settings_for_display(); |
| 134 |
if ( $this->is_builder_mode() ) { |
| 135 |
$this->apply_hooks(); |
| 136 |
} |
| 137 |
$this->theme_support(); |
| 138 |
// show_sku. |
| 139 |
$classes = ''; |
| 140 |
if ( ! empty( $controllers['show_sku'] ) ) { |
| 141 |
$classes .= 'rtsb-show-sku '; |
| 142 |
} |
| 143 |
if ( ! empty( $controllers['show_cat'] ) ) { |
| 144 |
$classes .= 'rtsb-show-cat '; |
| 145 |
} |
| 146 |
if ( ! empty( $controllers['show_tag'] ) ) { |
| 147 |
$classes .= 'rtsb-show-tag '; |
| 148 |
} |
| 149 |
if ( ! empty( $controllers['show_brand'] ) ) { |
| 150 |
$classes .= 'rtsb-show-brand '; |
| 151 |
} |
| 152 |
$data = [ |
| 153 |
'template' => 'elementor/single-product/meta', |
| 154 |
'controllers' => $controllers, |
| 155 |
'classes' => $classes, |
| 156 |
]; |
| 157 |
add_filter( 'woocommerce_product_brands_output', [ $this, 'wrap_brand_label' ], 10, 3 ); |
| 158 |
Fns::load_template( $data['template'], $data ); |
| 159 |
remove_filter( 'woocommerce_product_brands_output', [ $this, 'wrap_brand_label' ], 10 ); |
| 160 |
$this->theme_support( 'render_reset' ); |
| 161 |
$product = $_product; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 162 |
} |
| 163 |
} |
| 164 |
|