PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution with eCommerce Templates & Woo Widgets / 2.4.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution with eCommerce Templates & Woo Widgets v2.4.0
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / modules / swatches / attribute-hooks.php
shopengine / modules / swatches Last commit date
assets 4 years ago loop-product-support 4 years ago admin-product.php 4 years ago attribute-hooks.php 4 years ago frontend.php 4 years ago helper.php 4 years ago swatches.php 4 years ago
attribute-hooks.php
222 lines
1 <?php
2
3 namespace ShopEngine\Modules\Swatches;
4
5 use ShopEngine\Traits\Singleton;
6
7 defined('ABSPATH') || exit;
8
9 class Attribute_Hooks {
10
11 use Singleton;
12
13 public function init() {
14
15 $attribute_taxonomies = wc_get_attribute_taxonomies();
16 $types = Swatches::instance()->get_available_types();
17
18 if(empty($attribute_taxonomies)) {
19 return;
20 }
21
22 foreach($attribute_taxonomies as $tax) {
23
24 if(isset($types[$tax->attribute_type])) {
25
26 if(Swatches::PA_LABEL === $tax->attribute_type) {
27 $col = 'label_attribute_column';
28
29 } elseif(Swatches::PA_COLOR === $tax->attribute_type) {
30 $col = 'color_attribute_column';
31
32 } else {
33 $col = 'image_attribute_column';
34 }
35
36 add_filter('manage_edit-pa_' . $tax->attribute_name . '_columns', [$this, $col]);
37 add_filter('manage_pa_' . $tax->attribute_name . '_custom_column', [$this, 'add_attr_column_content'], 10, 3);
38
39 add_action('pa_' . $tax->attribute_name . '_add_form_fields', [$this, 'add_attr_field']);
40 add_action('pa_' . $tax->attribute_name . '_edit_form_fields', [$this, 'edit_attr_field'], 10, 2);
41 }
42 }
43
44 add_action('created_term', [$this, 'persist_term_meta'], 10, 2);
45 add_action('edit_term', [$this, 'persist_term_meta'], 10, 2);
46 add_action('shopengine_attribute_field_chain', [$this, 'attribute_fields'], 10, 3);
47 }
48
49
50 public function add_attr_field($taxonomy) {
51
52 $attr = Helper::get_tax_attribute($taxonomy);
53
54 do_action('shopengine_attribute_field_chain', $attr->attribute_type, '', 'add');
55 }
56
57
58 public function edit_attr_field($term, $taxonomy) {
59
60 $attr = Helper::get_tax_attribute($taxonomy);
61
62 $value = get_term_meta($term->term_id, $attr->attribute_type, true);
63
64 do_action('shopengine_attribute_field_chain', $attr->attribute_type, $value, 'edit');
65 }
66
67 public function label_attribute_column($columns) {
68 $th = esc_html__('Label', 'shopengine');
69 return $this->add_attr_column($columns, $th);
70 }
71
72 public function image_attribute_column($columns) {
73 $th = esc_html__('Thumbnail', 'shopengine');
74 return $this->add_attr_column($columns, $th);
75 }
76
77 public function color_attribute_column($columns) {
78 $th = esc_html__('Color', 'shopengine');
79 return $this->add_attr_column($columns, $th);
80 }
81
82 /**
83 * Add another td in the table
84 *
85 * @param $columns
86 * @return mixed
87 */
88 public function add_attr_column($columns, $th = '') {
89 $new_columns = [];
90
91 if(isset($columns['cb'])) {
92 $new_columns['cb'] = $columns['cb'];
93 }
94
95 $new_columns['pa_preview'] = $th;
96 unset($columns['cb']);
97
98 return $new_columns + $columns;
99 }
100
101
102 public function add_attr_column_content($columns, $column, $term_id) {
103
104 if('pa_preview' !== $column) {
105 return $columns;
106 }
107
108
109 $attr = Helper::get_tax_attribute(sanitize_key($_REQUEST['taxonomy']));
110
111 $value = get_term_meta($term_id, $attr->attribute_type, true);
112
113 switch($attr->attribute_type) {
114 case Swatches::PA_COLOR:
115 printf('<div class="swatches_thumb swatch_color" style="background-color:%s;"></div>', esc_attr($value));
116 break;
117
118 case Swatches::PA_IMAGE:
119 $image = $value ? wp_get_attachment_image_src($value) : '';
120 $image = $image ? $image[0] : Helper::get_dummy();
121 printf('<img class="swatches_thumb swatch_image" src="%s" width="44px" height="44px">', esc_url($image));
122 break;
123
124 case Swatches::PA_LABEL:
125 printf('<div class="swatch_label">%s</div>', esc_html($value));
126 break;
127 }
128
129 return $columns;
130 }
131
132
133 public function persist_term_meta($term_id, $tt_id) {
134
135 $types = Swatches::instance()->get_available_types();
136
137 foreach($types as $type => $label) {
138 if(isset($_POST[$type])) {
139
140 if($type == Swatches::PA_COLOR) {
141
142 update_term_meta($term_id, $type, sanitize_hex_color($_POST[$type]));
143
144 } else {
145
146 update_term_meta($term_id, $type, sanitize_text_field($_POST[$type]));
147 }
148 }
149 }
150 }
151
152
153 public function attribute_fields($type, $value, $form) {
154
155 $types = Swatches::instance()->get_available_types();
156
157 /**
158 * Rejecting all types from other plugin/module
159 *
160 */
161 if(!isset($types[$type])) {
162
163 return;
164 }
165
166 printf(
167 '<%s class="form-field">%s<label for="term-%s">%s</label>%s',
168 'edit' == $form ? 'tr' : 'div',
169 'edit' == $form ? '<th>' : '',
170 esc_attr($type),
171 $types[$type],
172 'edit' == $form ? '</th><td>' : ''
173 );
174
175 switch($type) {
176 case Swatches::PA_IMAGE:
177 $image = $value ? wp_get_attachment_image_src($value) : '';
178 $image = $image ? $image[0] : Helper::get_dummy();
179 ?>
180
181 <div class="shopengine_term_img_thumbnail" style="float:left;margin-right:10px;">
182 <img alt="" src="<?php echo esc_url($image) ?>" width="70px" height="70px"/>
183 </div>
184
185 <div style="line-height:60px;">
186 <input type="hidden" class="shopengine_term_img" name="<?php echo esc_attr($type) ?>"
187 value="<?php echo esc_attr($value) ?>"/>
188
189 <button type="button" class="shopengine_upload_img_button button">
190 <?php esc_html_e('Upload image', 'shopengine'); ?>
191 </button>
192
193 <button type="button"
194 class="shopengine_remove_img_btn button <?php echo esc_attr($value) ? '' : 'hidden' ?>">
195 <?php esc_html_e('Remove image', 'shopengine'); ?>
196 </button>
197 </div> <?php
198
199 break;
200
201 case Swatches::PA_COLOR : ?>
202
203 <input type="color"
204 id="term-<?php echo esc_attr($type) ?>"
205 name="<?php echo esc_attr($type) ?>"
206 value="<?php echo esc_attr($value) ?>"/> <?php
207 break;
208
209 default: ?>
210
211 <input type="text"
212 id="term-<?php echo esc_attr($type) ?>"
213 name="<?php echo esc_attr($type) ?>"
214 value="<?php echo esc_attr($value) ?>"/> <?php
215 break;
216 }
217
218
219 echo 'edit' == $form ? '</td></tr>' : '</div>';
220 }
221 }
222