Plugin Directory

source: woo-bulk-editor/trunk/ext/bulk/bulk.php

Last change on this file was 3031584, checked in by RealMag777, 15 months ago

https://bulk-editor.com/update-v-2-1-4-2-v-1-1-4-2/

File size: 48.7 KB
Line 
1<?php
2if (!defined('ABSPATH')) {
3    exit; // Exit if accessed directly
4}
5
6final class WOOBE_BULK extends WOOBE_EXT {
7
8    protected $slug = 'bulk'; //unique
9    public $text_keys = array();
10    public $num_keys = array();
11    public $other_keys = array();
12
13    public function __construct() {
14
15        $this->init_bulk_keys();
16
17        add_action('woobe_ext_scripts', array($this, 'woobe_ext_scripts'), 1);
18        add_action('woobe_tools_panel_buttons_end', array($this, 'woobe_tools_panel_buttons_end'), 1);
19
20        //ajax
21        add_action('wp_ajax_woobe_bulk_products_count', array($this, 'woobe_bulk_products_count'), 1);
22        add_action('wp_ajax_woobe_bulk_products', array($this, 'woobe_bulk_products'), 1);
23        add_action('wp_ajax_woobe_bulk_finish', array($this, 'woobe_bulk_finish'), 1);
24        add_action('wp_ajax_woobe_bulk_draw_gallery_btn', array($this, 'woobe_bulk_draw_gallery_btn'), 1);
25        add_action('wp_ajax_woobe_bulk_draw_download_files_btn', array($this, 'woobe_bulk_draw_download_files_btn'), 1);
26        add_action('wp_ajax_woobe_bulk_draw_cross_sells_btn', array($this, 'woobe_bulk_draw_cross_sells_btn'), 1);
27        add_action('wp_ajax_woobe_bulk_draw_upsell_ids_btn', array($this, 'woobe_bulk_draw_upsell_ids_btn'), 1);
28        add_action('wp_ajax_woobe_bulk_draw_grouped_ids_btn', array($this, 'woobe_bulk_draw_grouped_ids_btn'), 1);
29        add_action('wp_ajax_woobe_bulk_get_att_terms', array($this, 'woobe_bulk_get_att_terms'), 1);
30
31        add_action('wp_ajax_woobe_bulk_delete_products_count', array($this, 'woobe_bulk_delete_products_count'), 1);
32        add_action('wp_ajax_woobe_bulk_delete_products', array($this, 'woobe_bulk_delete_products'), 1);
33
34        add_action('woobe_bulk_going', array($this, 'woobe_bulk_going'), 10, 2);
35
36        //tabs
37        $this->add_tab($this->slug, 'top_panel', esc_html__('Bulk Edit', 'woo-bulk-editor'), 'pencil');
38        add_action('woobe_ext_top_panel_' . $this->slug, array($this, 'woobe_ext_panel'), 1);
39    }
40
41    public function woobe_ext_scripts() {
42        wp_enqueue_script('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/js/' . $this->slug . '.js', array(), WOOBE_VERSION);
43        wp_enqueue_style('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/css/' . $this->slug . '.css', array(), WOOBE_VERSION);
44        ?>
45        <script>
46            lang.<?php echo $this->slug ?> = {};
47            lang.<?php echo $this->slug ?>.want_to_bulk = "<?php echo esc_html__('Will be edited next:', 'woo-bulk-editor') ?>";
48            lang.<?php echo $this->slug ?>.want_to_delete = "<?php echo esc_html__('Sure? Delete products?', 'woo-bulk-editor') ?>";
49            lang.<?php echo $this->slug ?>.deleting = "<?php echo esc_html__('Bulk deleting', 'woo-bulk-editor') ?>";
50            lang.<?php echo $this->slug ?>.deleted = "<?php echo esc_html__('Product(s) deleted!', 'woo-bulk-editor') ?>";
51            lang.<?php echo $this->slug ?>.bulking = "<?php echo esc_html__('Bulk editing', 'woo-bulk-editor') ?> ...";
52            lang.<?php echo $this->slug ?>.bulked = "<?php echo esc_html__('Product(s) edited! Table redrawing ...', 'woo-bulk-editor') ?>";
53            lang.<?php echo $this->slug ?>.bulked2 = "<?php echo esc_html__('Product(s) edited!', 'woo-bulk-editor') ?>";
54            lang.<?php echo $this->slug ?>.bulk_is_going = "<?php echo esc_html__('ATTENTION: Bulk operation is going!', 'woo-bulk-editor') ?>";
55                        lang.<?php echo $this->slug ?>.attention_all_products = "<?php echo esc_html__('ATTENTION: You have not applied a filter and have not selected any products. This operation will be applied to all products in your database.', 'woo-bulk-editor') ?>";
56        </script>
57        <?php
58    }
59
60    public function woobe_tools_panel_buttons_end() {
61        global $WOOBE;
62        ?>
63        &nbsp;|&nbsp;<span>
64            <?php echo WOOBE_HELPER::draw_advanced_switcher(0, 'woobe_bind_editing', '', array('true' => esc_html__('binded editing', 'woo-bulk-editor'), 'false' => esc_html__('binded editing', 'woo-bulk-editor')), array('true' => 1, 'false' => 0), 'js_check_woobe_bind_editing', 'woobe_bind_editing'); ?>
65
66            <?php
67            $bind_tooltip = '';
68            if ($WOOBE->show_notes) {
69                $fields = $WOOBE->settings->get_fields();
70                if (!empty($fields)) {
71                    $bind_tooltip = [];
72                    foreach ($fields as $field_key => $f) {
73                        if ($f['direct']) {
74                            $t = strip_tags($f['title']);
75                            if (!empty($t) AND $field_key != 'ID') {
76                                $bind_tooltip[] = $t;
77                            }
78                        }
79                    }
80
81                    $bind_tooltip = sprintf(esc_html__('In FREE version of the plugin you can change only next fields: %s', 'woo-bulk-editor'), implode(', ', $bind_tooltip));
82                }
83            }
84            ?>
85
86            <?php echo WOOBE_HELPER::draw_tooltip(esc_html__('In this mode to the all selected products will be set the value of a product field which been edited', 'woo-bulk-editor') . '. ' . $bind_tooltip) ?>
87
88        </span>
89        <?php
90    }
91
92    public function woobe_ext_panel() {
93        $data = array();
94        $data['shop_manager_visibility'] = $this->settings->get_shop_manager_visibility();
95        $data['text_keys'] = $this->text_keys;
96        $data['num_keys'] = $this->num_keys;
97        $data['other_keys'] = $this->other_keys;
98        $data['settings_fields'] = $this->settings->get_fields();
99        echo WOOBE_HELPER::render_html($this->get_ext_path() . 'views/panel.php', $data);
100    }
101
102    //ajax
103    public function woobe_bulk_products_count() {
104        if (!current_user_can('manage_woocommerce')) {
105            die('0');
106        }
107
108                if (!isset($_REQUEST['bulk_form_nonce']) || !wp_verify_nonce($_REQUEST['bulk_form_nonce'], 'woobe_bulk_form_nonce')) {
109            die('0');
110        }       
111        //***
112
113        $bulk_data = array();
114
115        if (!isset($_REQUEST['woobe_bind_editing'])) {
116            parse_str($_REQUEST['bulk_data'], $bulk_data);
117
118            $bulk_data = WOOBE_HELPER::sanitize_array($bulk_data);
119        } else {
120            //binded editing operation works
121            if (is_array($_REQUEST['val'])) {
122                $value = WOOBE_HELPER::sanitize_array($_REQUEST['val']);
123            } else {
124                $value = wp_kses($_REQUEST['val'], wp_kses_allowed_html('post'));
125                $value = str_replace("&amp;", "&", $value);
126            }
127
128
129            $field_key = sanitize_text_field($_REQUEST['field']);
130
131            //***
132
133            $bulk_data['woobe_bulk'] = array(
134                'is' => array(
135                    $field_key => 1
136                ),
137                $field_key => array(
138                    'value' => $value,
139                    'behavior' => sanitize_text_field($_REQUEST['behavior'])
140                )
141            );
142        }
143
144
145        $this->storage->set_val('woobe_bulk_' . WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']), $bulk_data['woobe_bulk']);
146
147        if (!isset($_REQUEST['no_filter'])) {
148            //get count of filtered - doesn work if bulk for checked products
149            $products = $this->products->gets(array(
150                'fields' => 'ids',
151                'no_found_rows' => true
152            ));
153            echo json_encode($products->posts);
154        }
155
156        //***
157
158        do_action('woobe_bulk_started', WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']));
159
160        exit;
161    }
162
163    public function woobe_bulk_delete_products_count() {
164        if (!current_user_can('manage_woocommerce')) {
165            die('0');
166        }
167                if (!isset($_REQUEST['bulk_form_nonce']) || !wp_verify_nonce($_REQUEST['bulk_form_nonce'], 'woobe_bulk_form_nonce')) {
168            die('0');
169        }       
170        $bulk_data = array();
171
172        if (!isset($_REQUEST['woobe_bind_editing'])) {
173            parse_str($_REQUEST['bulk_data'], $bulk_data);
174            $bulk_data = WOOBE_HELPER::sanitize_array($bulk_data);
175        } else {
176            //binded editing operation works
177            if (is_array($_REQUEST['val'])) {
178                $value = WOOBE_HELPER::sanitize_array($_REQUEST['val']);
179            } else {
180                $value = wp_kses($_REQUEST['val'], wp_kses_allowed_html('post'));
181            }
182
183            $field_key = sanitize_text_field($_REQUEST['field']);
184
185            //***
186
187            $bulk_data['woobe_bulk'] = array(
188                'is' => array(
189                    $field_key => 1
190                ),
191                $field_key => array(
192                    'value' => $value,
193                    'behavior' => sanitize_text_field($_REQUEST['behavior'])
194                )
195            );
196        }
197
198
199        $this->storage->set_val('woobe_bulk_' . WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']), $bulk_data['woobe_bulk']);
200
201        if (!isset($_REQUEST['no_filter'])) {
202            //get count of filtered - doesn work if bulk for checked products
203            $products = $this->products->gets(array(
204                'fields' => 'ids',
205                'no_found_rows' => true
206            ));
207            echo json_encode($products->posts);
208        }
209
210        exit;
211    }
212
213    public function woobe_bulk_delete_products() {
214        if (!current_user_can('manage_woocommerce')) {
215            die('0');
216        }
217        if (!isset($_REQUEST['products_ids'])) {
218            die('0');
219        }
220               
221                if (!isset($_REQUEST['bulk_form_nonce']) || !wp_verify_nonce($_REQUEST['bulk_form_nonce'], 'woobe_bulk_form_nonce')) {
222            die('0');
223        }               
224
225        if (is_array($_REQUEST['products_ids'])) {
226            $is_variations_solo = intval($_REQUEST['woobe_show_variations']);
227
228            $products_ids = array_map(function ($item) {
229                return intval($item); //sanitize intval
230            }, $_REQUEST['products_ids']);
231
232            //as we want to change variations only but have ids of parents - lets get variations ids
233            if ($is_variations_solo AND!empty($products_ids)) {
234                $vars_ids = array();
235                foreach ($products_ids as $product_id) {
236                    $product = $this->products->get_product($product_id);
237                    if ($product->is_type('variable')) {
238                        $children = $product->get_children();
239                        if (!empty($children)) {
240                            $vars_ids = array_merge($vars_ids, $children);
241                        }
242                    }
243                }
244                $products_ids = array_unique(array_merge($products_ids, $vars_ids));
245            }
246            $woobe_bulk = $this->storage->get_val('woobe_bulk_' . WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']));
247
248            foreach ($products_ids as $id) {
249                if ($is_variations_solo) {
250
251                    //lets check that currenct variation has the same attributes combination
252                    if (isset($woobe_bulk['combination_attributes']) AND!empty($woobe_bulk['combination_attributes'])) {
253
254                        $variation = $this->products->get_product($id);
255                        $attributes = $variation->get_attributes();
256
257                        //***
258
259                        $go = FALSE;
260
261                        //***
262
263                        if (!empty($attributes)) {
264                            foreach ($woobe_bulk['combination_attributes'] as $comb) {
265                                //lets look is $attributes the same set of attributes as in $comb
266                                $ak_att = array_keys($attributes);
267                                $ak_cv = array_keys($comb);
268
269                                //fix for non-latin symbols
270                                if (!empty($ak_att)) {
271                                    $ak_att = array_map('urldecode', $ak_att);
272                                }
273
274                                //fix for non-latin symbols
275                                if (!empty($ak_cv)) {
276                                    $ak_cv = array_map('urldecode', $ak_cv);
277                                }
278
279                                sort($ak_att);
280                                sort($ak_cv);
281
282                                if ($ak_att === $ak_cv) {
283                                                               
284                                                                        $new_attributes = $attributes;
285                                                                        $new_comb = $comb;                                                                                     
286                                                                        if (in_array('-1',$comb)){
287                                                                                $delete_keys = array_keys($comb, '-1',);
288
289                                                                                foreach ($delete_keys as $d_key) {
290                                                                                        if (isset($new_attributes[$d_key])) {
291                                                                                                unset($new_attributes[$d_key]);
292                                                                                                unset($new_comb[$d_key]);
293                                                                                        }
294                                                                                }
295
296                                                                        }
297                                                                       
298                                                                        $av_att = array_values($new_attributes);
299                                                                        $av_cv = array_values($new_comb);
300
301                                    //fix for non-latin symbols
302                                    if (!empty($ak_att)) {
303                                        $av_att = array_map('urldecode', $av_att);
304                                    }
305
306
307                                    if (!empty($av_cv)) {
308                                        $av_cv = array_map('urldecode', $av_cv);
309                                    }
310
311                                    sort($av_att);
312                                    sort($av_cv);
313                                                                       
314                                    if ($av_att === $av_cv) {
315                                        $go = TRUE;
316                                        break;
317                                    }
318                                }
319                            }
320                        }
321
322                        //***
323
324                        if (!$go) {
325                            continue;
326                        }
327                    }
328                }
329                //wp_delete_post($id,false);
330                wp_trash_post(intval($id));
331            }
332        } else {
333            die('0');
334        }
335
336        die(json_encode($_REQUEST['products_ids']));
337        exit;
338    }
339
340    //ajax
341    public function woobe_bulk_products() {
342        if (!current_user_can('manage_woocommerce')) {
343            die('0');
344        }
345
346        if (!isset($_REQUEST['products_ids'])) {
347            die('0');
348        }
349                if (!isset($_REQUEST['bulk_form_nonce']) || !wp_verify_nonce($_REQUEST['bulk_form_nonce'], 'woobe_bulk_form_nonce')) {
350            die('0');
351        }       
352        //***
353
354        $fields = $this->settings->get_fields();
355        $woobe_bulk = $this->storage->get_val('woobe_bulk_' . WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']));
356        //key for history bulk opearation, not related to products keys
357        $_REQUEST['woobe_bulk_key'] = WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']);
358
359        $is_variations_solo = intval($_REQUEST['woobe_show_variations']);
360        $products_ids = $_REQUEST['products_ids']; //sanitize in cycle below
361        //***
362        //as we want to change variations only but have ids of parents - lets get variations ids
363        if ($is_variations_solo AND!empty($products_ids)) {
364            $vars_ids = array();
365            foreach ($products_ids as $product_id) {
366                $product_id = intval($product_id); //sanitize
367
368                $product = $this->products->get_product($product_id);
369                if ($product->is_type('variable')) {
370                    $children = $product->get_children();
371                    if (!empty($children)) {
372                        $vars_ids = array_merge($vars_ids, $children);
373                    }
374                }
375            }
376
377            $products_ids = array_unique(array_merge($products_ids, $vars_ids));
378        }
379
380        //***
381
382        if (isset($woobe_bulk['is']) AND!empty($woobe_bulk['is']) AND!empty($products_ids)) {
383
384            //***
385
386            foreach ($woobe_bulk['is'] as $field_key => $is) {
387
388                if ($fields[$field_key]['edit_view'] === 'calendar') {
389
390
391                    if (!is_int($woobe_bulk[$field_key]['value']) AND (isset($fields[$field_key]["field_type"]) AND $fields[$field_key]["field_type"] == "meta")) {
392                        $woobe_bulk[$field_key]['value'] = strtotime($woobe_bulk[$field_key]['value']); // - wrong way
393                    } else {
394                        $woobe_bulk[$field_key]['value'] = $this->products->normalize_calendar_date($woobe_bulk[$field_key]['value'], $field_key);
395                    }
396                }
397
398                //***
399                //speedfix
400                wp_defer_term_counting(false);
401                wp_defer_comment_counting(true);
402
403                //***
404
405                if (intval($is) === 1) {
406
407                    foreach ($products_ids as $product_id) {
408
409                        if ($is_variations_solo) {
410                            //if enabled editing of variations only parent-products are ignored
411                            $product = $this->products->get_product($product_id);
412                            if (!$product->is_type('variation')) {
413                                continue;
414                            }
415
416                            //lets check that currenct variation has the same attributes combination
417                            if (isset($woobe_bulk['combination_attributes']) AND!empty($woobe_bulk['combination_attributes'])) {
418
419                                $variation = $this->products->get_product($product_id);
420                                $attributes = $variation->get_attributes();
421
422                                //***
423
424                                $go = FALSE;
425
426                                //***
427
428                                if (!empty($attributes)) {
429                                    foreach ($woobe_bulk['combination_attributes'] as $comb) {
430                                        //lets look is $attributes the same set of attributes as in $comb
431                                        $ak_att = array_keys($attributes);
432                                        $ak_cv = array_keys($comb);
433                                                                               
434                                        //fix for non-latin symbols
435                                        if (!empty($ak_att)) {
436                                            $ak_att = array_map('urldecode', $ak_att);
437                                        }
438
439                                        //fix for non-latin symbols
440                                        if (!empty($ak_cv)) {
441                                            $ak_cv = array_map('urldecode', $ak_cv);
442                                        }
443
444                                        sort($ak_att);
445                                        sort($ak_cv);
446
447                                        if ($ak_att === $ak_cv) {
448                                            $new_attributes = $attributes;
449                                            $new_comb = $comb;                                                                                 
450                                                                                        if (in_array('-1',$comb)){
451                                                                                                $delete_keys = array_keys($comb, '-1',);
452                                                                                               
453                                                                                                foreach ($delete_keys as $d_key) {
454                                                                                                        if (isset($new_attributes[$d_key])) {
455                                                                                                                unset($new_attributes[$d_key]);
456                                                                                                                unset($new_comb[$d_key]);
457                                                                                                        }
458                                                                                                }
459                                                                                               
460                                                                                        }
461                                                                                       
462                                            $av_att = array_values($new_attributes);
463                                            $av_cv = array_values($new_comb);
464
465                                            //fix for non-latin symbols
466                                            if (!empty($ak_att)) {
467                                                $av_att = array_map('urldecode', $av_att);
468                                            }
469
470                                            if (!empty($av_cv)) {
471                                                $av_cv = array_map('urldecode', $av_cv);
472                                            }
473
474                                            sort($av_att);
475                                            sort($av_cv);
476                                            if ($av_att === $av_cv) {
477                                                $go = TRUE;
478                                                break;
479                                            }
480                                        }
481                                    }
482                                }
483
484                                //***
485
486                                if (!$go) {
487                                    continue;
488                                }
489                            }
490                        }
491
492                        //***
493
494
495                        switch ($field_key) {
496                            case 'post_title':
497                            case 'post_content':
498                            case 'post_excerpt':
499                            case 'post_name':
500                            case 'purchase_note':
501                            case 'sku':
502                            case 'tax_class':
503                            case 'backorders':
504                            case 'sold_individually':
505                            case 'reviews_allowed':
506                            case 'product_url':
507                            case 'button_text':
508                                $this->_process_text_data($woobe_bulk, $field_key, $product_id);
509                                break;
510
511                            case 'post_status':
512                            case 'stock_status':
513                            case 'manage_stock':
514                            case 'tax_status':
515                            case 'catalog_visibility':
516                            case 'product_type':
517                            case 'featured':
518                            case 'virtual':
519                            case 'downloadable':
520                            case 'download_files':
521                            case 'gallery':
522                            case 'upsell_ids':
523                            case 'cross_sell_ids':
524                            case 'grouped_ids':
525                            case 'post_date':
526                                if (intval($woobe_bulk[$field_key]['value']) !== -1) {
527                                    $this->products->update_page_field($product_id, $field_key, $woobe_bulk[$field_key]['value']);
528                                }
529                                break;
530                            case 'attribute_visibility':
531
532                                if (!isset($woobe_bulk[$field_key]['value']) OR!is_array($woobe_bulk[$field_key]['value'])) {
533
534                                    $woobe_bulk[$field_key]['value'] = array();
535                                }
536                                if (!isset($woobe_bulk[$field_key]['visible'])) {
537                                    $woobe_bulk[$field_key]['visible'] = 1;
538                                }
539                                $this->products->set_product_attributes_visible($product_id, $woobe_bulk[$field_key]['value'], $woobe_bulk[$field_key]['visible']);
540
541                                break;
542                            case 'regular_price':
543                            case 'sale_price':
544                            case 'stock_quantity':
545                            case 'download_expiry':
546                            case 'download_limit':
547                            case 'date_on_sale_from':
548                            case 'date_on_sale_to':
549                            case 'weight':
550                            case 'length':
551                            case 'width':
552                            case 'height':
553                            case 'product_shipping_class':
554                            case 'menu_order':
555                            case 'post_author':
556                            case 'total_sales':
557                            case 'review_count':
558                            case 'average_rating':
559                                $this->_process_number_data($woobe_bulk, $field_key, $product_id);
560                                break;
561
562                            default:
563                                break;
564                        }
565
566                        //***
567
568                        if (isset($fields[$field_key]) AND $fields[$field_key]['field_type'] === 'taxonomy') {
569                            //if (!empty($woobe_bulk[$field_key]['value'])) {
570                            do_action('woobe_before_update_page_field', $field_key, $product_id, 0); //for the History
571                            if (!isset($woobe_bulk[$field_key]['behavior'])) {
572                                $woobe_bulk[$field_key]['behavior'] = "";
573                            }
574                            switch ($woobe_bulk[$field_key]['behavior']) {
575                                case 'append':
576                                    if (is_taxonomy_hierarchical($field_key)) {
577                                        wp_set_post_terms($product_id, $woobe_bulk[$field_key]['value'], $field_key, true);
578                                    } else {
579                                        //product_tag for example
580                                        foreach ($woobe_bulk[$field_key]['value'] as $term_id) {
581                                            $t = get_term_by('id', $term_id, $field_key);
582                                            wp_set_post_terms($product_id, $t->slug, $field_key, true);
583                                        }
584                                    }
585                                    break;
586                                case 'replace':
587                                case 'new':
588                                    if (is_taxonomy_hierarchical($field_key)) {
589                                        wp_set_post_terms($product_id, $woobe_bulk[$field_key]['value'], $field_key, false);
590                                    } else {
591                                        //product_tag for example
592                                        $append = false; //clean previous by first one then append
593                                        if (!empty($woobe_bulk[$field_key]['value']) AND is_array($woobe_bulk[$field_key]['value'])) {
594                                            foreach ($woobe_bulk[$field_key]['value'] as $term_id) {
595                                                $t = get_term_by('id', $term_id, $field_key);
596                                                wp_set_post_terms($product_id, $t->slug, $field_key, $append);
597                                                $append = true;
598                                            }
599                                        }
600                                    }
601                                    break;
602                                case 'remove':
603                                    foreach ($woobe_bulk[$field_key]['value'] as $term_id) {
604                                        $t = get_term_by('id', $term_id, $field_key);
605                                        wp_remove_object_terms($product_id, $t->slug, $field_key);
606                                    }
607                                    break;
608                            }
609                            //}
610                        }
611
612                        //***
613
614                        if (isset($fields[$field_key]) AND $fields[$field_key]['field_type'] === 'attribute') {
615                            if (!isset($woobe_bulk[$field_key]['value'])) {
616                                $woobe_bulk[$field_key]['value'] = '';
617                            }
618                            do_action('woobe_before_update_page_field', $field_key, $product_id, 0); //for the History
619                            $this->products->set_product_attributes($product_id, $field_key, $woobe_bulk[$field_key]['value'], $woobe_bulk[$field_key]['behavior']);
620                        }
621
622                        //***
623
624                        if (isset($fields[$field_key]) AND $fields[$field_key]['field_type'] === 'meta') {
625                            switch ($fields[$field_key]['type']) {
626                                case 'string':
627
628                                    //if data is serialized in ine string
629                                    if ($fields[$field_key]['edit_view'] == 'meta_popup_editor') {
630                                        if (!is_array($woobe_bulk[$field_key]['value'])) {
631
632                                            //if not else parsed
633                                            parse_str($woobe_bulk[$field_key]['value'], $meta_val);
634
635                                            $woobe_bulk[$field_key]['value'] = $this->products->process_jsoned_meta_data($meta_val);
636                                        }
637                                    }
638                                                                        //***
639                                    if ($fields[$field_key]['edit_view'] == 'gallery_popup_editor') {
640                                       
641                                        if (!is_array($woobe_bulk[$field_key]['value'])) {
642                                            //if not else parsed
643                                            parse_str($woobe_bulk[$field_key]['value'], $meta_val);
644                                            if (!empty($meta_val['woobe_gallery_images'])) {
645                                                $woobe_bulk[$field_key]['value'] = $meta_val;
646                                                                                        } 
647                                        }
648                                       
649                                    }
650                                    //***
651
652                                    if ($fields[$field_key]['edit_view'] !== 'switcher') {
653                                        $this->_process_text_data($woobe_bulk, $field_key, $product_id);
654                                    } else {
655                                        if (intval($woobe_bulk[$field_key]['value']) !== -1) {
656                                            $this->products->update_page_field($product_id, $field_key, intval($woobe_bulk[$field_key]['value']));
657                                        }
658                                    }
659                                    break;
660
661                                case 'number':
662                                    $this->_process_number_data($woobe_bulk, $field_key, $product_id);
663                                    break;
664
665                                default:
666                                    break;
667                            }
668                        }
669                    }
670                }
671            }
672
673            do_action('woobe_bulk_going', sanitize_text_field($_REQUEST['woobe_bulk_key']), count($products_ids));
674        }
675
676
677
678        die('done');
679    }
680
681    public function woobe_bulk_going($bulk_key, $products_count) {
682        $count_key = 'woobe_bulk_' . strtolower($bulk_key) . '_count';
683        $count_now = intval($this->storage->get_val($count_key));
684        $this->storage->set_val($count_key, $products_count + $count_now);
685    }
686
687    private function _process_text_data($woobe_bulk, $field_key, $product_id) {
688        //if (!empty($woobe_bulk[$field_key]['value'])) {
689        $val = $this->products->get_post_field($product_id, $field_key);
690                $woobe_bulk[$field_key]['value'] = $this->products->string_macros($woobe_bulk[$field_key]['value'], $field_key, $product_id);
691        switch ($woobe_bulk[$field_key]['behavior']) {
692            case 'append':
693                $val = $this->products->string_replacer($val . $woobe_bulk[$field_key]['value'], $product_id);
694                break;
695            case 'prepend':
696                $val = $this->products->string_replacer($woobe_bulk[$field_key]['value'] . $val, $product_id);
697                break;
698            case 'new':
699                $val = $this->products->string_replacer($woobe_bulk[$field_key]['value'], $product_id);
700                break;
701            case 'replace':
702                $replace_to = $this->products->string_replacer($woobe_bulk[$field_key]['replace_to'], $product_id);
703                $replace_from = $this->products->string_replacer($woobe_bulk[$field_key]['value'], $product_id);
704
705                //fix  for  apostrophe
706                $replace_from = str_replace("\'", "'", $replace_from);
707
708                if ($woobe_bulk[$field_key]['case'] == 'ignore') {
709                    $val = str_ireplace($replace_from, $replace_to, $val);
710                } else {
711                    $val = str_replace($replace_from, $replace_to, $val);
712                    /*
713                     * https://stackoverflow.com/questions/19317493/php-preg-replace-case-insensitive-match-with-case-sensitive-replacement
714                      $val = preg_replace_callback('/\b' . $replace_from . '\b/i', function($matches) use ($replace_to) {
715                      $i = 0;
716                      return join('', array_map(function($char) use ($matches, &$i) {
717                      return ctype_lower($matches[0][$i++]) ? strtolower($char) : strtoupper($char);
718                      }, str_split($replace_to)));
719                      }, $val);
720                     *
721                     */
722                }
723
724                break;
725        }
726
727        //***
728        $empty_exceptions = array('tax_class'); //setting empty values is possible with this fields
729
730        $can = true; //!empty($val);
731
732        if (in_array($field_key, $empty_exceptions)) {
733            $can = true;
734        }
735
736        if ($can) {
737                       
738            $val = $this->products->update_page_field($product_id, $field_key, $val);
739        }
740        //}
741    }
742
743    private function _process_number_data($woobe_bulk, $field_key, $product_id) {
744        if ($woobe_bulk[$field_key]['behavior'] != 'new') {
745            $val = floatval($this->products->get_post_field($product_id, $field_key));
746        }
747
748        //***
749
750        switch ($woobe_bulk[$field_key]['behavior']) {
751            case 'new':
752                $val = floatval($woobe_bulk[$field_key]['value']);
753                break;
754
755            case 'invalue':
756                $val += floatval($woobe_bulk[$field_key]['value']);
757                break;
758
759            case 'devalue':
760                $val -= floatval($woobe_bulk[$field_key]['value']);
761                break;
762
763            case 'inpercent':
764                $val = $val + $val * floatval($woobe_bulk[$field_key]['value']) / 100;
765                break;
766
767            case 'depercent':
768                $val = $val - $val * floatval($woobe_bulk[$field_key]['value']) / 100;
769                break;
770
771            case 'devalue_regular_price':
772                //for sale_price only
773                $val = floatval($this->products->get_post_field($product_id, 'regular_price')) - floatval($woobe_bulk[$field_key]['value']);
774                break;
775
776            case 'depercent_regular_price':
777                //for sale_price only
778                $val = floatval($this->products->get_post_field($product_id, 'regular_price'));
779                $val = $val - $val * floatval($woobe_bulk[$field_key]['value']) / 100;
780                break;
781
782            case 'invalue_sale_price':
783                //for regular_price only
784                $val = floatval($this->products->get_post_field($product_id, 'sale_price') + floatval($woobe_bulk[$field_key]['value']));
785                break;
786
787            case 'inpercent_sale_price':
788                //for regular_price only
789                $val =  floatval($this->products->get_post_field($product_id, 'sale_price'));
790                $val = $val + $val * floatval($woobe_bulk[$field_key]['value']) / 100;
791                break;
792        }
793
794        if (isset($_REQUEST['num_formula_action']) AND isset($_REQUEST['num_formula_value']) AND $_REQUEST['num_formula_value'] != '-1') {
795            $v_key = esc_textarea($_REQUEST['num_formula_value']);
796            $action = esc_textarea($_REQUEST['num_formula_action']);
797            $v_data = floatval(get_post_meta($product_id, $v_key, true));
798
799            switch ($action) {
800                case '-':
801                    $val = $val - $v_data;
802                    break;
803                case '*':
804                    $val = $val * $v_data;
805                    break;
806                case '/':
807                    if ($v_data == 0) {
808                        $v_data = 1;
809                    }
810                    $val = $val / $v_data;
811                    break;
812
813                default:
814                    $val = $val + $v_data;
815                    break;
816            }
817        }
818               
819                if (isset($_REQUEST['num_rand_data']) && is_array($_REQUEST['num_rand_data'])) {
820                        $rand_data = wc_clean($_REQUEST['num_rand_data']);
821                        if (isset($rand_data['from']) && isset($rand_data['to']) && ($rand_data['from'] != $rand_data['to']) && ($rand_data['from'] < $rand_data['to'])) {
822                                $from = (float)$rand_data['from'];
823                                $to = (float)$rand_data['to'];
824                                $decimal = 1;
825                                if (isset($rand_data['decimal'])) {
826                                        $decimal = (int)$rand_data['decimal'];
827                                }
828                                $action = '+';
829                                if (isset($rand_data['action'])) {
830                                        $action = $rand_data['action'];
831                                }
832                               
833                                $rand_val = rand($from * $decimal, $to * $decimal)/$decimal;
834                                switch ($action) {
835                                        case '-':
836                                                $val = $val - $rand_val;
837                                                break;
838                                        case '*':
839                                                $val = $val * $rand_val;
840                                                break;
841                                        case '/':
842                                                if ($rand_val == 0) {
843                                                        $rand_val = 1;
844                                                }
845                                                $val = $val / $rand_val;
846                                                break;
847
848                                        default:
849                                                $val = $val + $rand_val;
850                                                break;
851                                }                               
852                               
853                        }
854                }
855
856        //***
857
858        $convert = TRUE;
859        if ($field_key == 'sale_price') {
860            //sale price CAN NOT be more OR even equal to the regular price
861            if ($val >= $this->products->get_post_field($product_id, 'regular_price')) {
862                $convert = FALSE;
863            }
864            //to delete sale price
865            if ($val <= 0) {
866                $val = -1;
867                $val = $this->products->update_page_field($product_id, $field_key, $val);
868                $convert = FALSE;
869            }
870        }
871
872        //***
873
874        if ($convert) {
875            $val = $this->products->update_page_field($product_id, $field_key, floatval($val));
876        }
877    }
878
879    public function woobe_bulk_finish() {
880        do_action('woobe_bulk_finished', WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']));
881        $count_key = 'woobe_bulk_' . WOOBE_HELPER::sanitize_bulk_key($_REQUEST['bulk_key']) . '_count';
882        die($this->storage->get_val($count_key) . '');
883    }
884
885    private function init_bulk_keys() {
886
887        $fields = woobe_get_fields();
888
889        $this->text_keys = array(
890            'post_title' => array(
891                'title' => esc_html__('title', 'woo-bulk-editor'),
892                'css_classes' => isset($fields['post_title']['css_classes']) ? $fields['post_title']['css_classes'] : ''
893            ),
894            'post_content' => array(
895                'title' => esc_html__('description', 'woo-bulk-editor'),
896                'css_classes' => isset($fields['post_content']['css_classes']) ? $fields['post_content']['css_classes'] : ''
897            ),
898            'post_excerpt' => array(
899                'title' => esc_html__('short description', 'woo-bulk-editor'),
900                'css_classes' => isset($fields['post_excerpt']['css_classes']) ? $fields['post_excerpt']['css_classes'] : ''
901            ),
902            'post_name' => array(
903                'title' => esc_html__('product slug', 'woo-bulk-editor'),
904                'css_classes' => isset($fields['post_name']['css_classes']) ? $fields['post_name']['css_classes'] : ''
905            ),
906            'sku' => array(
907                'title' => esc_html__('SKU', 'woo-bulk-editor'),
908                'css_classes' => isset($fields['sku']['css_classes']) ? $fields['sku']['css_classes'] : ''
909            )
910        );
911
912        $this->other_keys = array(
913            'post_status' => array(
914                'title' => esc_html__('post status', 'woo-bulk-editor'),
915                'options' => $fields['post_status']['select_options'],
916                'direct' => $fields['post_status']['direct'],
917                'css_classes' => isset($fields['post_status']['css_classes']) ? $fields['post_status']['css_classes'] : ''
918            ),
919            'stock_status' => array(
920                'title' => esc_html__('stock status', 'woo-bulk-editor'),
921                'options' => $fields['stock_status']['select_options'],
922                'direct' => $fields['stock_status']['direct'],
923                'css_classes' => isset($fields['stock_status']['css_classes']) ? $fields['stock_status']['css_classes'] : ''
924            ),
925            'tax_status' => array(
926                'title' => esc_html__('tax status', 'woo-bulk-editor'),
927                'options' => $fields['tax_status']['select_options'],
928                'direct' => $fields['tax_status']['direct'],
929                'css_classes' => isset($fields['tax_status']['css_classes']) ? $fields['tax_status']['css_classes'] : ''
930            ),
931            'catalog_visibility' => array(
932                'title' => esc_html__('catalog visibility', 'woo-bulk-editor'),
933                'options' => $fields['catalog_visibility']['select_options'],
934                'direct' => $fields['catalog_visibility']['direct'],
935                'css_classes' => isset($fields['catalog_visibility']['css_classes']) ? $fields['catalog_visibility']['css_classes'] : ''
936            ),
937            'product_type' => array(
938                'title' => esc_html__('product type', 'woo-bulk-editor'),
939                'options' => $fields['product_type']['select_options'],
940                'direct' => $fields['product_type']['direct'],
941                'css_classes' => isset($fields['product_type']['css_classes']) ? $fields['product_type']['css_classes'] : ''
942            ),
943            'featured' => array(
944                'title' => esc_html__('featured', 'woo-bulk-editor'),
945                'options' => $fields['featured']['select_options'],
946                'direct' => $fields['featured']['direct'],
947                'css_classes' => isset($fields['featured']['css_classes']) ? $fields['featured']['css_classes'] : ''
948            ),
949        );
950        //***
951
952        $options1 = array(
953            'invalue' => esc_html__('increase by value', 'woo-bulk-editor'),
954            'devalue' => esc_html__('decrease by value', 'woo-bulk-editor'),
955            'inpercent' => esc_html__('increase by %', 'woo-bulk-editor'),
956            'depercent' => esc_html__('decrease by %', 'woo-bulk-editor'),
957            'new' => esc_html__('set new', 'woo-bulk-editor')
958        );
959
960        $options2 = array(
961            'invalue' => esc_html__('increase by value', 'woo-bulk-editor'),
962            'devalue' => esc_html__('decrease by value', 'woo-bulk-editor'),
963            'delete' => esc_html__('delete', 'woo-bulk-editor'),
964            'new' => esc_html__('set new', 'woo-bulk-editor')
965        );
966
967        //***
968
969        $this->num_keys = array(
970            'regular_price' => array(
971                'title' => esc_html__('regular price', 'woo-bulk-editor'),
972                'direct' => $fields['regular_price']['direct'],
973                'options' => array(
974                    'invalue' => esc_html__('increase by value', 'woo-bulk-editor'),
975                    'devalue' => esc_html__('decrease by value', 'woo-bulk-editor'),
976                    'inpercent' => esc_html__('increase by %', 'woo-bulk-editor'),
977                    'depercent' => esc_html__('decrease by %', 'woo-bulk-editor'),
978                    'inpercent_sale_price' => esc_html__('sale price plus %', 'woo-bulk-editor'),
979                    'invalue_sale_price' => esc_html__('sale price plus value', 'woo-bulk-editor'),
980                    'new' => esc_html__('set new', 'woo-bulk-editor')
981                ),
982                'css_classes' => isset($fields['regular_price']['css_classes']) ? $fields['regular_price']['css_classes'] : ''
983            ),
984            'sale_price' => array(
985                'title' => esc_html__('sale price', 'woo-bulk-editor'),
986                'direct' => $fields['sale_price']['direct'],
987                'options' => array(
988                    'invalue' => esc_html__('increase by value', 'woo-bulk-editor'),
989                    'devalue' => esc_html__('decrease by value', 'woo-bulk-editor'),
990                    'inpercent' => esc_html__('increase by %', 'woo-bulk-editor'),
991                    'depercent' => esc_html__('decrease by %', 'woo-bulk-editor'),
992                    'depercent_regular_price' => esc_html__('regular price minus %', 'woo-bulk-editor'),
993                    'devalue_regular_price' => esc_html__('regular price minus value', 'woo-bulk-editor'),
994                    'new' => esc_html__('set new', 'woo-bulk-editor')
995                ),
996                'css_classes' => isset($fields['sale_price']['css_classes']) ? $fields['sale_price']['css_classes'] : ''
997            ),
998            'stock_quantity' => array(
999                'title' => esc_html__('in stock quantity', 'woo-bulk-editor'),
1000                'direct' => $fields['stock_quantity']['direct'],
1001                'options' => $options2,
1002                'css_classes' => isset($fields['stock_quantity']['css_classes']) ? $fields['stock_quantity']['css_classes'] : ''
1003            ),
1004            'download_expiry' => array(
1005                'title' => esc_html__('download expiry', 'woo-bulk-editor'),
1006                'direct' => $fields['download_expiry']['direct'],
1007                'options' => $options2,
1008                'css_classes' => isset($fields['download_expiry']['css_classes']) ? $fields['download_expiry']['css_classes'] : ''
1009            ),
1010            'download_limit' => array(
1011                'title' => esc_html__('download limit', 'woo-bulk-editor'),
1012                'direct' => $fields['download_limit']['direct'],
1013                'options' => $options2,
1014                'css_classes' => isset($fields['download_limit']['css_classes']) ? $fields['download_limit']['css_classes'] : ''
1015            )
1016        );
1017    }
1018
1019    //ajax
1020    public function woobe_bulk_draw_gallery_btn() {
1021        $images = [];
1022        parse_str($_REQUEST['images'], $images); //sanitize below in array_map
1023        $data = array();
1024        $woobe_gallery_images = isset($images['woobe_gallery_images']) ? $images['woobe_gallery_images'] : array();
1025        //sanitizing to intval
1026        $woobe_gallery_images = array_map(function ($item) {
1027            return intval($item); //sanitize intval
1028        }, $woobe_gallery_images);
1029
1030        $data['html'] = WOOBE_HELPER::draw_gallery_popup_editor_btn(sanitize_text_field($_REQUEST['field']), 0, $woobe_gallery_images);
1031        $data['images_ids'] = implode(',', $woobe_gallery_images); //for any case, but now we not need it because updating of products applies by serialized data
1032
1033
1034        die(json_encode($data));
1035    }
1036
1037    //ajax
1038    public function woobe_bulk_draw_download_files_btn() {
1039        $files = [];
1040        parse_str($_REQUEST['files'], $files);
1041        $files = WOOBE_HELPER::sanitize_array($files);
1042        $count = 0;
1043
1044        if (isset($files['_wc_file_names'])) {
1045            $count = count($files['_wc_file_names']);
1046        }
1047
1048        echo WOOBE_HELPER::draw_downloads_popup_editor_btn(sanitize_text_field($_REQUEST['field']), 0, $count);
1049
1050        exit;
1051    }
1052
1053    //ajax
1054    public function woobe_bulk_draw_cross_sells_btn() {
1055        $products = [];
1056        parse_str($_REQUEST['products'], $products);
1057
1058        $ids = array();
1059        if (isset($products['woobe_prod_ids'])) {
1060            $ids = $products['woobe_prod_ids'];
1061        }
1062
1063        $ids = array_map(function ($item) {
1064            return intval($item); //sanitize intval
1065        }, $ids);
1066
1067        echo WOOBE_HELPER::draw_cross_sells_popup_editor_btn(sanitize_text_field($_REQUEST['field']), 0, $ids);
1068
1069        exit;
1070    }
1071
1072    //ajax
1073    public function woobe_bulk_draw_upsell_ids_btn() {
1074        $products = [];
1075        parse_str($_REQUEST['products'], $products);
1076
1077        $ids = array();
1078        if (isset($products['woobe_prod_ids'])) {
1079            $ids = array_map(function ($item) {
1080                return intval($item); //sanitize intval
1081            }, $products['woobe_prod_ids']);
1082        }
1083
1084        echo WOOBE_HELPER::draw_upsells_popup_editor_btn($_REQUEST['field'], 0, $ids);
1085
1086        exit;
1087    }
1088
1089    //ajax
1090    public function woobe_bulk_draw_grouped_ids_btn() {
1091        $products = [];
1092        parse_str($_REQUEST['products'], $products); //sanitize below
1093
1094        $ids = array();
1095        if (isset($products['woobe_prod_ids'])) {
1096            $ids = array_map(function ($item) {
1097                return intval($item); //sanitize intval
1098            }, $products['woobe_prod_ids']);
1099        }
1100
1101        echo WOOBE_HELPER::draw_grouped_popup_editor_btn(sanitize_text_field($_REQUEST['field']), 0, $ids);
1102
1103        exit;
1104    }
1105
1106    //ajax
1107    public function woobe_bulk_get_att_terms() {
1108
1109        $drop_downs = '';
1110        if (!empty($_REQUEST['attributes'])) {
1111            foreach ($_REQUEST['attributes'] as $pa) {
1112                $pa = sanitize_text_field($pa); //sanitize attribute name
1113
1114                $terms = WOOBE_HELPER::get_taxonomies_terms_hierarchy($pa);
1115                if (!empty($terms)) {
1116                    $options = array();
1117                    $options[''] = esc_html__('not selected', 'woo-bulk-editor');
1118                                        $options['-1'] = esc_html__('Any', 'woo-bulk-editor');
1119                    foreach ($terms as $t) {
1120                        $options[$t['slug']] = $t['name'];
1121                    }
1122
1123                    $drop_downs .= WOOBE_HELPER::draw_select(array(
1124                                'field' => 0,
1125                                'product_id' => 0,
1126                                'class' => '',
1127                                'options' => $options,
1128                                'name' => 'woobe_bulk[combination_attributes][' . sanitize_text_field($_REQUEST['hash_key']) . '][' . $pa . ']'
1129                    ));
1130                }
1131            }
1132        }
1133
1134        die($drop_downs);
1135    }
1136
1137}
Note: See TracBrowser for help on using the repository browser.