PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.32
OttoKit: All-in-One Automation Platform v1.1.32
1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / woocommerce / actions / add-attribute-terms.php
suretriggers / src / Integrations / woocommerce / actions Last commit date
add-attribute-terms.php 8 months ago add-customer.php 9 months ago add-emails-to-coupon.php 8 months ago add-order-note.php 10 months ago add-product-attributes.php 8 months ago add-product-to-cart.php 8 months ago add-update-order-custom-fields.php 10 months ago apply-coupon.php 8 months ago check-cart-status.php 8 months ago check-if-order-is-renewal.php 4 months ago create-attribute.php 8 months ago create-bundle-product-order.php 6 months ago create-coupon-code.php 10 months ago create-multi-product-order.php 7 months ago create-new-order.php 10 months ago create-product-variation.php 1 year ago create-product.php 1 year ago create-variable-product.php 8 months ago delete-coupon.php 1 year ago delete-customer.php 8 months ago fetch-coupon-details.php 10 months ago find-orders-by-user-id.php 10 months ago get-all-customers.php 8 months ago get-all-products.php 8 months ago get-customer-by-email.php 8 months ago get-customer-by-id.php 8 months ago get-order-details-by-order-id.php 10 months ago get-order-type-by-order-id.php 4 months ago get-orders-by-email.php 8 months ago get-product-by-id.php 1 year ago list-all-order-notes.php 8 months ago list-all-orders.php 8 months ago remove-product-from-cart.php 8 months ago retrieve-coupons-totals.php 10 months ago retrieve-customers-totals.php 10 months ago retrieve-orders-totals.php 10 months ago retrieve-products-totals.php 10 months ago retrieve-refunds-list.php 10 months ago retrieve-reviews-totals.php 10 months ago retrieve-sales-report.php 10 months ago retrieve-top-sellers-report.php 10 months ago update-product-price.php 8 months ago update-product-stock-quantity.php 8 months ago update-status-of-order.php 10 months ago
add-attribute-terms.php
371 lines
1 <?php
2 /**
3 * AddAttributeTerms.
4 * php version 5.6
5 *
6 * @category AddAttributeTerms
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\Woocommerce\Actions;
15
16 use Exception;
17 use SureTriggers\Integrations\AutomateAction;
18 use SureTriggers\Integrations\WooCommerce\WooCommerce;
19 use SureTriggers\Traits\SingletonLoader;
20
21 /**
22 * AddAttributeTerms
23 *
24 * @category AddAttributeTerms
25 * @package SureTriggers
26 * @author BSF <username@example.com>
27 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
28 * @link https://www.brainstormforce.com/
29 * @since 1.0.0
30 */
31 class AddAttributeTerms extends AutomateAction {
32
33 /**
34 * Integration type.
35 *
36 * @var string
37 */
38 public $integration = 'WooCommerce';
39
40 /**
41 * Action name.
42 *
43 * @var string
44 */
45 public $action = 'wc_add_attribute_terms';
46
47 use SingletonLoader;
48
49 /**
50 * Register a action.
51 *
52 * @param array $actions actions.
53 * @return array
54 */
55 public function register( $actions ) {
56 $actions[ $this->integration ][ $this->action ] = [
57 'label' => __( 'Add Attribute Terms', 'suretriggers' ),
58 'action' => 'wc_add_attribute_terms',
59 'function' => [ $this, 'action_listener' ],
60 ];
61 return $actions;
62 }
63
64 /**
65 * Action listener.
66 *
67 * @param int $user_id user_id.
68 * @param int $automation_id automation_id.
69 * @param array $fields fields.
70 * @param array $selected_options selectedOptions.
71 *
72 * @return void|array|bool
73 */
74 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
75
76 // Check if WooCommerce is active.
77 if ( ! function_exists( 'WC' ) || ! function_exists( 'wc_get_attribute' ) ) {
78 return [
79 'status' => 'error',
80 'message' => __( 'WooCommerce not available', 'suretriggers' ),
81 ];
82 }
83
84 // Validate required fields.
85 foreach ( $fields as $field ) {
86 if ( array_key_exists( 'validationProps', $field ) && empty( $selected_options[ $field['name'] ] ) ) {
87 return [
88 'status' => 'error',
89 'message' => __( 'Required field is missing: ', 'suretriggers' ) . $field['name'],
90 ];
91 }
92 }
93
94 // Get parameters.
95 $attribute_input = ! empty( $selected_options['attribute_slug'] ) ? sanitize_text_field( $selected_options['attribute_slug'] ) : '';
96 $terms_input = ! empty( $selected_options['terms'] ) ? $selected_options['terms'] : '';
97 $term_descriptions = ! empty( $selected_options['term_descriptions'] ) ? $selected_options['term_descriptions'] : '';
98 $term_slugs = ! empty( $selected_options['term_slugs'] ) ? $selected_options['term_slugs'] : '';
99
100 // Backward compatibility - check for legacy fields.
101 if ( empty( $terms_input ) && ! empty( $selected_options['term_name'] ) ) {
102 $terms_input = $selected_options['term_name'];
103 }
104
105 // Determine if input is ID or slug.
106 $attribute_id = 0;
107 $attribute_slug = '';
108
109 if ( is_numeric( $attribute_input ) ) {
110 $attribute_id = intval( $attribute_input );
111 } else {
112 $attribute_slug = $attribute_input;
113 }
114
115 // Validate attribute identification.
116 if ( empty( $attribute_id ) && empty( $attribute_slug ) ) {
117 return [
118 'status' => 'error',
119 'message' => __( 'Attribute ID or Attribute Slug is required', 'suretriggers' ),
120 ];
121 }
122
123 // Validate terms input.
124 if ( empty( $terms_input ) ) {
125 return [
126 'status' => 'error',
127 'message' => __( 'Terms are required', 'suretriggers' ),
128 ];
129 }
130
131 try {
132
133 // Get attribute details.
134 $attribute = null;
135 $taxonomy_name = '';
136
137 if ( ! empty( $attribute_id ) ) {
138 $attribute = wc_get_attribute( $attribute_id );
139 if ( $attribute && isset( $attribute->name ) ) {
140 $taxonomy_name = wc_attribute_taxonomy_name( $attribute->name );
141 } elseif ( $attribute && isset( $attribute->attribute_name ) ) {
142 $taxonomy_name = wc_attribute_taxonomy_name( $attribute->attribute_name );
143 }
144 } elseif ( ! empty( $attribute_slug ) ) {
145 // Check if it's already a taxonomy name (pa_*).
146 if ( strpos( $attribute_slug, 'pa_' ) === 0 ) {
147 $taxonomy_name = $attribute_slug;
148 $attribute_name = str_replace( 'pa_', '', $attribute_slug );
149 // Find attribute by name.
150 $attributes = wc_get_attribute_taxonomies();
151 foreach ( $attributes as $attr ) {
152 if ( $attr->attribute_name === $attribute_name ) {
153 $attribute = $attr;
154 break;
155 }
156 }
157 } else {
158 // Find attribute by slug.
159 $attributes = wc_get_attribute_taxonomies();
160 foreach ( $attributes as $attr ) {
161 if ( $attr->attribute_name === $attribute_slug ) {
162 $attribute = $attr;
163 $taxonomy_name = wc_attribute_taxonomy_name( $attribute_slug );
164 break;
165 }
166 }
167 }
168 }
169
170 if ( ! $attribute && empty( $taxonomy_name ) ) {
171 return [
172 'status' => 'error',
173 'message' => __( 'Attribute not found', 'suretriggers' ),
174 ];
175 }
176
177 // If we don't have taxonomy name but have attribute, generate it.
178 if ( empty( $taxonomy_name ) && $attribute ) {
179 if ( isset( $attribute->name ) ) {
180 $taxonomy_name = wc_attribute_taxonomy_name( $attribute->name );
181 } elseif ( isset( $attribute->attribute_name ) ) {
182 $taxonomy_name = wc_attribute_taxonomy_name( $attribute->attribute_name );
183 }
184 }
185
186 // Ensure taxonomy is registered.
187 if ( ! taxonomy_exists( $taxonomy_name ) ) {
188 $taxonomy_label = '';
189 if ( $attribute && isset( $attribute->label ) ) {
190 $taxonomy_label = $attribute->label;
191 } elseif ( $attribute && isset( $attribute->attribute_label ) ) {
192 $taxonomy_label = $attribute->attribute_label;
193 } else {
194 $taxonomy_label = ucfirst( str_replace( 'pa_', '', $taxonomy_name ) );
195 }
196
197 $is_public = false;
198 if ( $attribute && isset( $attribute->public ) ) {
199 $is_public = (bool) $attribute->public;
200 } elseif ( $attribute && isset( $attribute->attribute_public ) ) {
201 $is_public = (bool) $attribute->attribute_public;
202 }
203
204 register_taxonomy(
205 $taxonomy_name,
206 'product',
207 [
208 'labels' => [
209 'name' => $taxonomy_label,
210 ],
211 'hierarchical' => false,
212 'public' => $is_public,
213 'show_ui' => false,
214 'query_var' => true,
215 'rewrite' => false,
216 ]
217 );
218 }
219
220 // Parse terms input - can be comma-separated string or array.
221 $terms = [];
222 if ( is_string( $terms_input ) ) {
223 $terms = array_map( 'trim', explode( ',', $terms_input ) );
224 } elseif ( is_array( $terms_input ) ) {
225 $terms = array_map( 'trim', $terms_input );
226 }
227
228 // Remove empty terms.
229 $terms = array_filter(
230 $terms,
231 function( $term ) {
232 return ! empty( $term );
233 }
234 );
235
236 // Parse term descriptions - one per line.
237 $descriptions = [];
238 if ( ! empty( $term_descriptions ) ) {
239 $descriptions = array_map( 'trim', explode( "\n", $term_descriptions ) );
240 }
241
242 // Parse term slugs - comma-separated.
243 $custom_slugs = [];
244 if ( ! empty( $term_slugs ) ) {
245 if ( is_string( $term_slugs ) ) {
246 $custom_slugs = array_map( 'trim', explode( ',', $term_slugs ) );
247 } elseif ( is_array( $term_slugs ) ) {
248 $custom_slugs = array_map( 'trim', $term_slugs );
249 }
250 }
251
252 if ( empty( $terms ) ) {
253 return [
254 'status' => 'error',
255 'message' => __( 'No valid terms provided', 'suretriggers' ),
256 ];
257 }
258
259 $created_terms = [];
260 $existing_terms = [];
261 $failed_terms = [];
262
263 foreach ( $terms as $index => $term_name ) {
264 $term_name = sanitize_text_field( $term_name );
265
266 // Check if term already exists.
267 $existing_term = get_term_by( 'name', $term_name, $taxonomy_name );
268 if ( $existing_term ) {
269 $existing_terms[] = [
270 'term_id' => $existing_term->term_id,
271 'name' => $existing_term->name,
272 'slug' => $existing_term->slug,
273 ];
274 continue;
275 }
276
277 // Prepare term arguments.
278 $term_args = [];
279
280 // Add description if provided for this specific term.
281 if ( ! empty( $descriptions[ $index ] ) ) {
282 $term_args['description'] = sanitize_textarea_field( $descriptions[ $index ] );
283 } elseif ( ! empty( $selected_options['term_description'] ) ) {
284 // Backward compatibility.
285 $term_args['description'] = sanitize_textarea_field( $selected_options['term_description'] );
286 }
287
288 // Add custom slug if provided for this specific term.
289 if ( ! empty( $custom_slugs[ $index ] ) ) {
290 $term_args['slug'] = sanitize_title( $custom_slugs[ $index ] );
291 }
292
293 // Create the term.
294 $term_result = wp_insert_term( $term_name, $taxonomy_name, $term_args );
295
296 if ( is_wp_error( $term_result ) ) {
297 $failed_terms[] = [
298 'name' => $term_name,
299 'error' => $term_result->get_error_message(),
300 ];
301 } else {
302 $new_term = get_term( $term_result['term_id'], $taxonomy_name );
303 if ( $new_term && ! is_wp_error( $new_term ) ) {
304 $created_terms[] = [
305 'term_id' => $new_term->term_id,
306 'name' => $new_term->name,
307 'slug' => $new_term->slug,
308 ];
309 }
310 }
311 }
312
313 // Get all terms for this attribute.
314 $all_terms = get_terms(
315 [
316 'taxonomy' => $taxonomy_name,
317 'hide_empty' => false,
318 ]
319 );
320
321 $all_terms_data = [];
322 if ( ! is_wp_error( $all_terms ) && is_array( $all_terms ) ) {
323 foreach ( $all_terms as $term ) {
324 $all_terms_data[] = [
325 'term_id' => $term->term_id,
326 'name' => $term->name,
327 'slug' => $term->slug,
328 'count' => $term->count,
329 ];
330 }
331 }
332
333 // Prepare summary.
334 $summary = [
335 'created_count' => count( $created_terms ),
336 'existing_count' => count( $existing_terms ),
337 'failed_count' => count( $failed_terms ),
338 'total_count' => count( $all_terms_data ),
339 ];
340
341 return [
342 'status' => 'success',
343 'message' => sprintf(
344 __( 'Processed %1$d terms: %2$d created, %3$d existing, %4$d failed', 'suretriggers' ),
345 count( $terms ),
346 $summary['created_count'],
347 $summary['existing_count'],
348 $summary['failed_count']
349 ),
350 'attribute_id' => $attribute ? ( isset( $attribute->id ) ? $attribute->id : ( isset( $attribute->attribute_id ) ? $attribute->attribute_id : null ) ) : null,
351 'attribute_name' => $attribute ? ( isset( $attribute->name ) ? $attribute->name : ( isset( $attribute->attribute_name ) ? $attribute->attribute_name : str_replace( 'pa_', '', $taxonomy_name ) ) ) : str_replace( 'pa_', '', $taxonomy_name ),
352 'attribute_label' => $attribute ? ( isset( $attribute->label ) ? $attribute->label : ( isset( $attribute->attribute_label ) ? $attribute->attribute_label : ucfirst( str_replace( 'pa_', '', $taxonomy_name ) ) ) ) : ucfirst( str_replace( 'pa_', '', $taxonomy_name ) ),
353 'taxonomy_name' => $taxonomy_name,
354 'created_terms' => $created_terms,
355 'existing_terms' => $existing_terms,
356 'failed_terms' => $failed_terms,
357 'all_terms' => $all_terms_data,
358 'summary' => $summary,
359 ];
360
361 } catch ( Exception $e ) {
362 return [
363 'status' => 'error',
364 'message' => __( 'Error adding attribute terms: ', 'suretriggers' ) . $e->getMessage(),
365 ];
366 }
367 }
368 }
369
370 AddAttributeTerms::get_instance();
371