Plugin Directory


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • woo-bulk-editor/trunk/ext/bulkoperations/bulkoperations.php

    r2844667 r2970262  
    11<?php
    22if (!defined('ABSPATH')) {
    33    exit; // Exit if accessed directly
    44}
    55
    66//any operations with variations only
    77final class WOOBE_BULKOPERATIONS extends WOOBE_EXT {
    88
    99    protected $slug = 'bulkoperations'; //unique
    1010
    1111    //protected $is = 'external';
    1212
    1313    public function __construct() {
    1414        add_action('woobe_ext_scripts', array($this, 'woobe_ext_scripts'), 1);
    1515        add_action('woobe_tools_panel_buttons', array($this, 'woobe_tools_panel_buttons'), 1);
    1616        add_action('woobe_page_end', array($this, 'woobe_page_end'), 1);
    1717
    1818        //ajax
    1919        add_action('wp_ajax_woobe_bulkoperations_get_att_terms', array($this, 'woobe_bulkoperations_get_att_terms'), 1);
    2020        add_action('wp_ajax_woobe_bulkoperations_get_possible_combos', array($this, 'woobe_bulkoperations_get_possible_combos'), 1);
    2121        add_action('wp_ajax_woobe_bulkoperations_get_prod_count', array($this, 'woobe_bulkoperations_get_prod_count'), 1);
    2222        add_action('wp_ajax_woobe_bulkoperations_apply_combinations', array($this, 'woobe_bulkoperations_apply_combinations'), 1);
    2323        add_action('wp_ajax_woobe_bulkoperations_apply_default_combination', array($this, 'woobe_bulkoperations_apply_default_combination'), 1);
    2424        add_action('wp_ajax_woobe_bulkoperations_get_product_variations', array($this, 'woobe_bulkoperations_get_product_variations'), 1);
    2525        add_action('wp_ajax_woobe_bulkoperations_delete', array($this, 'woobe_bulkoperations_delete'), 1);
    2626        add_action('wp_ajax_woobe_bulkoperations_ordering', array($this, 'woobe_bulkoperations_ordering'), 1);
    2727        add_action('wp_ajax_woobe_bulkoperations_swap', array($this, 'woobe_bulkoperations_swap'), 1);
    2828        add_action('wp_ajax_woobe_bulkoperations_attaching', array($this, 'woobe_bulkoperations_attaching'), 1);
    2929        add_action('wp_ajax_woobe_bulkoperations_visibility', array($this, 'woobe_bulkoperations_visibility'), 1);
    3030    }
    3131
    3232    public function woobe_ext_scripts() {
    3333        wp_enqueue_script('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/js/' . $this->slug . '.js', array(), WOOBE_VERSION);
    3434        wp_enqueue_script('woobe_ext_' . $this->slug . '-2', $this->get_ext_link() . 'assets/js/' . $this->slug . '-2.js', array(), WOOBE_VERSION);
    3535        wp_enqueue_script('woobe_ext_' . $this->slug . '-3', $this->get_ext_link() . 'assets/js/' . $this->slug . '-3.js', array(), WOOBE_VERSION);
    3636        wp_enqueue_script('woobe_ext_' . $this->slug . '-4', $this->get_ext_link() . 'assets/js/' . $this->slug . '-4.js', array(), WOOBE_VERSION);
    3737        wp_enqueue_script('woobe_ext_' . $this->slug . '-5', $this->get_ext_link() . 'assets/js/' . $this->slug . '-5.js', array(), WOOBE_VERSION);
    3838        wp_enqueue_script('woobe_ext_' . $this->slug . '-6', $this->get_ext_link() . 'assets/js/' . $this->slug . '-6.js', array(), WOOBE_VERSION);
    3939        wp_enqueue_script('woobe_ext_' . $this->slug . '-7', $this->get_ext_link() . 'assets/js/' . $this->slug . '-7.js', array(), WOOBE_VERSION);
    4040        wp_enqueue_style('woobe_ext_' . $this->slug, $this->get_ext_link() . 'assets/css/' . $this->slug . '.css', array(), WOOBE_VERSION);
    4141        ?>
    4242        <script>
    4343            lang.<?php echo $this->slug ?> = {};
    4444            lang.<?php echo $this->slug ?>.going = '<?php esc_html_e('ATTENTION: Variations Advanced Bulk Operation is going', 'woo-bulk-editor') ?>';
    4545            lang.<?php echo $this->slug ?>.finished = '<?php esc_html_e('Variations Advanced Bulk Operation is finished', 'woo-bulk-editor') ?>';
    4646            lang.<?php echo $this->slug ?>.finished2 = '<?php esc_html_e('Attaching of the default combination for the products variations is finished!', 'woo-bulk-editor') ?>';
    4747            lang.<?php echo $this->slug ?>.finished3 = '<?php esc_html_e('Deleting of the products variations is finished', 'woo-bulk-editor') ?>';
    4848            lang.<?php echo $this->slug ?>.finished4 = '<?php esc_html_e('Ordering of the products variations is finished', 'woo-bulk-editor') ?>';
    4949            lang.<?php echo $this->slug ?>.finished5 = '<?php esc_html_e('Swap of variations is finished', 'woo-bulk-editor') ?>';
    5050            lang.<?php echo $this->slug ?>.finished6 = '<?php esc_html_e('Attaching of the products variations is finished', 'woo-bulk-editor') ?>';
    5151            lang.<?php echo $this->slug ?>.generating = '<?php esc_html_e('Generating possible combinations', 'woo-bulk-editor') ?>';
    5252            lang.<?php echo $this->slug ?>.generated = '<?php esc_html_e('Possible combinations been generated.', 'woo-bulk-editor') ?>';
    5353            lang.<?php echo $this->slug ?>.no_combinations = '<?php esc_html_e('Combination(s) not selected!', 'woo-bulk-editor') ?>';
    5454            lang.<?php echo $this->slug ?>.not_selected_var = '<?php esc_html_e('variation is not selected', 'woo-bulk-editor') ?>';
    5555            lang.<?php echo $this->slug ?>.no_vars = '<?php esc_html_e('the product has no variations', 'woo-bulk-editor') ?>';
    5656        </script>
    5757        <?php
    5858    }
    5959
    6060    public function woobe_tools_panel_buttons() {
    6161        ?>
    6262        <a href="#" class="button button-secondary woobe_tools_panel_newvars_btn" title="<?php esc_html_e('Variations Advanced Bulk Operations', 'woo-bulk-editor') ?>"></a>
    6363        <?php
    6464    }
    6565
    6666    public function woobe_page_end() {
    6767        $data = array();
    6868        $data['attributes'] = wc_get_attribute_taxonomies();
    6969        echo WOOBE_HELPER::render_html($this->get_ext_path() . 'views/panel.php', $data);
    7070    }
    7171
    7272    //ajax
    7373    public function woobe_bulkoperations_get_att_terms() {
     74        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     75            die('0');
     76        }
    7477        die(json_encode(WOOBE_HELPER::get_taxonomies_terms_hierarchy(sanitize_text_field($_REQUEST['attribute']))));
    7578    }
    7679
    7780    //ajax
    7881    public function woobe_bulkoperations_get_possible_combos() {
     82        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     83            die('0');
     84        }       
    7985        try {
    8086            $res = "";
    8187            if (isset($_REQUEST['arrays'])) {
    8288                //no db writing, ajax to DOM
    8389                $res = json_encode($this->generate_combinations($_REQUEST['arrays']));
    8490            }
    8591        } catch (Exception $e) {
    8692            print_r($e);
    8793        }
    8894        die($res);
    8995    }
    9096
    9197    //https://gist.github.com/fabiocicerchia/4556892
    9298    private function generate_combinations($data, &$all = array(), array $group = array(), $value = null, $i = 0) {
     99        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     100            die('0');
     101        }       
    93102        if (!is_array($data)) {
    94103            $data = array();
    95104        }
    96105        $keys = array_keys($data);
    97106        if (isset($value) === true) {
    98107            array_push($group, intval($value));
    99108        }
    100109
    101110        if ($i >= count($data)) {
    102111            array_push($all, $group);
    103112        } else {
    104113            $currentKey = $keys[$i];
    105114            $currentElement = $data[$currentKey];
    106115            foreach ($currentElement as $val) {
    107116                $this->generate_combinations($data, $all, $group, $val, $i + 1);
    108117            }
    109118        }
    110119
    111120        return $all;
    112121    }
    113122
    114123    //ajax
    115124    public function woobe_bulkoperations_get_prod_count() {
    116 
     125        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     126            die('0');
     127        }
    117128        if (!current_user_can('manage_woocommerce')) {
    118129            die('0');
    119130        }
    120131
    121132        //***
    122133
    123134        $products = $this->products->gets(array(
    124135            'fields' => 'ids',
    125136            'no_found_rows' => true
    126137        ));
    127138        echo json_encode($products->posts);
    128139
    129140        exit;
    130141    }
    131142
    132143    //ajax
    133144    public function woobe_bulkoperations_apply_combinations() {
    134 
     145        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     146            die('0');
     147        }
    135148        if (!isset($_REQUEST['products_ids'])) {
    136149            die('0');
    137150        }
    138151
    139152        //***
    140153
    141154        $products_ids = array_map(function($item) {
    142155            return intval($item); //sanitize intval
    143156        }, $_REQUEST['products_ids']);
    144157
    145158        $combinations = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combinations']);
    146159
    147160        $possible_attributes = array();
    148161        //for set_product_attributes
    149162        $set_product_attributes = array();
    150163
    151164        //***
    152165
    153166        if (!empty($products_ids) AND!empty($combinations)) {
    154167
    155168            //lets prepare combinations for applying
    156169            $terms = array();
    157170            foreach ($combinations as $comb) {
    158171                if (!empty($comb)) {
    159172                    foreach ($comb as $term_id) {
    160173                        $terms[$term_id] = get_term($term_id, '', ARRAY_A);
    161174                    }
    162175                }
    163176            }
    164177
    165178            //***
    166179
    167180            foreach ($terms as $t) {
    168181                $set_product_attributes[$t['taxonomy']][] = $t['term_id'];
    169182            }
    170183
    171184            foreach ($combinations as $k => $comb) {
    172185                if (!empty($comb)) {
    173186                    foreach ($comb as $term_id) {
    174187                        $possible_attributes[$k][strtolower(urlencode($terms[$term_id]['taxonomy']))] = $terms[$term_id]['slug'];
    175188                    }
    176189                }
    177190            }
    178191
    179192            //***
    180193            //wp-content\plugins\woocommerce\includes\class-wc-ajax.php -> public static function link_all_variations
    181194            wc_maybe_define_constant('WC_MAX_LINKED_VARIATIONS', 50);
    182195            wc_set_time_limit(0);
    183196
    184197
    185198            /*
    186199             * Leaved as an example of data structure
    187200              $possible_attributes = array(
    188201              'pa_color' => array('black', 'blue', 'green'),
    189202              'pa_size' => array('xl', '2xxl')
    190203              );
    191204
    192205              $set_product_attributes = array(
    193206              'pa_color' => array(1, 2, 3),
    194207              'pa_size' => array(4, 5)
    195208              );
    196209             */
    197210
    198211
    199212            if (!empty($possible_attributes)) {
    200213
    201214                foreach ($products_ids as $product_id) {
    202215
    203216                    $product_id = intval($product_id);
    204217                    $product = $this->products->get_product($product_id);
    205218
    206219                    //if product is not variable - no variations!!
    207220                    if (!$product->is_type('variable')) {
    208221                        continue;
    209222                    }
    210223
    211224                    //attach attributes if they not been attached
    212225                    foreach ($set_product_attributes as $field_key => $value) {
    213226                        $this->products->set_product_attributes($product_id, $field_key, $value, 'append', array('set_variation'));
    214227
    215228                    }
    216229
    217230                    //***
    218231                    // Get existing variations so we don't create duplicates.
    219232                    $existing_variations = array_map('wc_get_product', $product->get_children());
    220233                    $parent_sku = $product->get_sku();
    221234                    $existing_attributes = array();
    222235
    223236                    foreach ($existing_variations as $existing_variation) {
    224237                        if ($existing_variation) {
    225238                            $existing_attributes[] = $existing_variation->get_attributes();
    226239                        }
    227240                    }
    228241
    229242
    230243                    $regular_price = get_post_meta($product_id, '_regular_price', true);
    231244                    $sale_price = get_post_meta($product_id, '_sale_price', true);
    232245                    //***
    233246
    234247                    foreach ($possible_attributes as $possible_attribute) {
    235248
    236249                        if (in_array($possible_attribute, $existing_attributes)) {
    237250                            continue;
    238251                        }
    239252
    240253                        $variation = new WC_Product_Variation();
    241254                        $variation->set_parent_id($product_id);
    242255                        $variation->set_attributes($possible_attribute);
    243256                       
    244257                        $variation->set_status(apply_filters('woobe_new_variation_product_status', 'publish'));
    245258                       
    246259
    247260                        if ($regular_price) {
    248261                            $variation->set_regular_price($regular_price);
    249262                        }
    250263                        if ($sale_price) {
    251264                            $variation->set_sale_price($sale_price);
    252265                        }
    253266
    254267
    255268                        //do_action('product_variation_linked', $variation->save());
    256269                        $variation->save();
    257270
    258271                        //to avoid the same SKU as in the parent
    259272                        if (empty($parent_sku)) {
    260273                            $variation->set_sku('sku-' . $variation->get_id());
    261274                        } else {
    262275                            $variation->set_sku($parent_sku . '-' . $variation->get_id());
    263276                        }
    264277                        $variation->set_manage_stock(0);
    265278                        $variation->set_stock_quantity(0);
    266279                        $variation->save();
    267280
    268281                        //clean_post_cache($variation->get_id());
    269282                        //wp_cache_flush();
    270283                    }
    271284
    272285
    273286                    //***
    274287                    //set order of variations
    275288                    $data_store = $product->get_data_store();
    276289                    $data_store->sort_all_product_variations($product->get_id());
    277290                }
    278291            }
    279292        }
    280293
    281294        die('done');
    282295    }
    283296
    284297    //************ TAB 2
    285298    //ajax
    286299    public function woobe_bulkoperations_apply_default_combination() {
    287 
     300        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     301            die('0');
     302        }
    288303        if (!isset($_REQUEST['products_ids'])) {
    289304            die('0');
    290305        }
    291306
    292307        //***
    293308
    294309        $products_ids = array_map(function($item) {
    295310            return intval($item); //sanitize intval
    296311        }, $_REQUEST['products_ids']);
    297312       
    298313        $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']);
    299314
    300315        if (!empty($combination) AND!empty($products_ids)) {
    301316            foreach ($products_ids as $product_id) {
    302317                $product = $this->products->get_product($product_id);
    303318                $product->set_props(array(
    304319                    'default_attributes' => $combination
    305320                ));
    306321                $product->save();
    307322
    308323                //*** also lets set order of attributes which customer will see on the product page
    309324                $meta = get_post_meta($product_id, '_product_attributes', true);
    310325
    311326                $new_meta = array();
    312327                $counter = 0;
    313328                foreach (array_keys($combination) as $meta_key) {
    314329                    if (isset($meta[$meta_key])) {
    315330                        $new_meta[$meta_key] = $meta[$meta_key];
    316331                        $new_meta[$meta_key]['position'] = $counter;
    317332                        unset($meta[$meta_key]);
    318333                        $counter++;
    319334                    }
    320335                }
    321336
    322337                //if we have more attributes than trying to save in order
    323338                if (!empty($meta)) {
    324339                    foreach ($meta as $meta_key => $value) {
    325340                        $new_meta[$meta_key] = $value;
    326341                        $new_meta[$meta_key]['position'] = $counter;
    327342                        $counter++;
    328343                    }
    329344                }
    330345
    331346                update_post_meta($product_id, '_product_attributes', $new_meta);
    332347
    333348                //woocommerce\includes\admin\meta-boxes\class-wc-meta-box-product-data.php
    334349                //public static function save($post_id, $post)
    335350                do_action('woocommerce_process_product_meta_' . $product->get_type(), $product_id);
    336351            }
    337352        }
    338353
    339354        exit;
    340355    }
    341356
    342357    //************ TAB 3
    343358    //ajax
    344359    public function woobe_bulkoperations_delete() {
    345 
     360        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     361            die('0');
     362        }
    346363        if (!isset($_REQUEST['products_ids'])) {
    347364            die('0');
    348365        }
    349366
    350367        //***
    351368
    352369        $removed_ids = array();
    353370        $products_ids = array_map(function($item) {
    354371            return intval($item); //sanitize intval
    355372        }, $_REQUEST['products_ids']);
    356373
    357374        $combination = array();
    358375        if (isset($_REQUEST['combination'])) {
    359376            $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']);
    360377        }
    361378
    362379        if (!empty($products_ids)) {
    363380            foreach ($products_ids as $product_id) {
    364381                $product = $this->products->get_product($product_id);
    365382
    366383                if (method_exists($product, 'is_type') AND $product->is_type('variable')) {
    367384                    $vars = $product->get_children();
    368385
    369386                    if (!empty($vars)) {
    370387                        foreach ($vars as $var_id) {
    371388
    372389                            $var = $this->products->get_product($var_id);
    373390                            $av = $product->get_available_variation($var);
    374391
    375392                            if ($_REQUEST['delete_how'] == 'combo') {
    376393
    377394                                if (!empty($combination)) {
    378395                                    if (count($av['attributes']) === count($combination)) {
    379396                                        $is = FALSE;
    380397                                        $attributes = array();
    381398
    382399                                        //fix for non-latin symbols
    383400                                        if (!empty($av['attributes'])) {
    384401                                            foreach ($av['attributes'] as $k => $v) {
    385402                                                $attributes[urldecode($k)] = urldecode($v);
    386403                                            }
    387404                                        }
    388405
    389406
    390407                                        foreach ($combination as $comb_key => $comb_value) {
    391408                                            if (isset($attributes['attribute_' . urldecode($comb_key)]) AND $attributes['attribute_' . urldecode($comb_key)] == urldecode($comb_value)) {
    392409                                                $is = TRUE;
    393410                                            } else {
    394411                                                $is = FALSE;
    395412                                                break;
    396413                                            }
    397414                                        }
    398415
    399416                                        //removing
    400417                                        if ($is) {
    401418                                            $var_prod = $this->products->get_product($av['variation_id']);
    402419                                            $removed_ids[] = $av['variation_id'];
    403420                                            $var_prod->delete(true);
    404421                                        }
    405422                                    }
    406423                                }
    407424                            } else {
    408425                                //all
    409426                                $var_prod = $this->products->get_product($av['variation_id']);
    410427                                $removed_ids[] = $av['variation_id'];
    411                                 $var_prod->delete(true);
     428                                if ($var_prod){
     429                                    $var_prod->delete(true);
     430                                }
    412431                            }
    413432                        }
    414433                    }
    415434                }
    416435
    417436                //***
    418437            }
    419438        }
    420439
    421440        //***
    422441        $removed_ids = json_encode($removed_ids);
    423442        die($removed_ids);
    424443    }
    425444
    426445    //************ TAB 4
    427446    //ajax
    428447    public function woobe_bulkoperations_get_product_variations() {
     448        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     449            die('0');
     450        }       
    429451        $product_id = intval($_REQUEST['product_id']);
    430452        $available_variations = array();
    431453
    432454        if ($product_id > 0) {
    433455            try {
    434456                $product = $this->products->get_product($product_id);
    435457
    436458                if (is_object($product) AND $product->is_type('variable')) {
    437459                    $vars = $product->get_children();
    438460
    439461                    if (!empty($vars)) {
    440462                        foreach ($vars as $var_id) {
    441463                            $var = $this->products->get_product($var_id);
    442464                            $av = $product->get_available_variation($var);
    443465                            $available_variations[$var_id]['title'] = str_replace($product->get_title(), '', $this->products->generate_product_title($var));
    444466                            $available_variations[$var_id]['attributes'] = $av['attributes'];
    445467                        }
    446468                    }
    447469
    448470                    //print_r($available_variations);
    449471                }
    450472            } catch (Exception $e) {
    451473                //***
    452474            }
    453475        }
    454476
    455477        die(json_encode($available_variations));
    456478    }
    457479
    458480    //ajax
    459481    public function woobe_bulkoperations_ordering() {
    460 
     482        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     483            die('0');
     484        }
    461485        $products_ids = array_map(function($item) {
    462486            return intval($item); //sanitize intval
    463487        }, $_REQUEST['products_ids']);
    464488
    465489        $combination = WOOBE_HELPER::sanitize_array((array) $_REQUEST['combination']);
    466490
    467491        //***
    468492
    469493        if (!empty($products_ids) AND!empty($combination)) {
    470494            foreach ($products_ids as $product_id) {
    471495                $available_variations = array();
    472496                $product = $this->products->get_product($product_id);
    473497                $childrens = $product->get_children();
    474498
    475499                if (!empty($childrens)) {
    476500                    foreach ($childrens as $child_id) {
    477501                        $variation = $this->products->get_product($child_id);
    478502                        $available_variations[] = $product->get_available_variation($variation);
    479503                    }
    480504
    481505                    //***
    482506
    483507                    if (!empty($available_variations)) {
    484508                        foreach ($available_variations as $var) {
    485509                            $att = $var['attributes'];
    486510                            $num = -1;
    487511                            foreach ($combination as $n => $comb_var) {
    488512                                //lets look is it the same set of attributes as in $var
    489513                                $ak_att = array_keys($att);
    490514                                $ak_cv = array_keys($comb_var);
    491515                                sort($ak_att);
    492516                                sort($ak_cv);
    493517                                if ($ak_att === $ak_cv) {
    494518                                    $av_att = array_values($att);
    495519                                    $av_cv = array_values($comb_var);
    496520                                    sort($av_att);
    497521                                    sort($av_cv);
    498522                                    if ($av_att === $av_cv) {
    499523                                        $num = $n;
    500524                                        break;
    501525                                    }
    502526                                }
    503527                            }
    504528
    505529                            //***
    506530                            if ($num > -1) {
    507531                                $this->products->update_page_field(intval($var['variation_id']), 'menu_order', $num);
    508532                            }
    509533                        }
    510534                    }
    511535                }
    512536            }
    513537        }
    514538
    515539
    516540        exit;
    517541    }
    518542
    519543    //************ TAB 5 swap
    520544    //ajax
    521545    public function woobe_bulkoperations_swap() {
    522 
     546        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     547            die('0');
     548        }
    523549        $do = true;
    524550        if (!empty($_REQUEST['from']) AND!empty($_REQUEST['to'])) {
    525551            if ($_REQUEST['from']['attribute'] == $_REQUEST['to']['attribute']) {
    526552                if ($_REQUEST['from']['term'] == $_REQUEST['to']['term']) {
    527553                    $do = false;
    528554                }
    529555            }
    530556        } else {
    531557            $do = false;
    532558        }
    533559
    534560        //***
    535561
    536562        if ($do) {
    537563            $from_att = sanitize_text_field($_REQUEST['from']['attribute']);
    538564            $to_att = sanitize_text_field($_REQUEST['to']['attribute']);
    539565
    540566            //***
    541567
    542568            if (!empty($_REQUEST['products_ids'])) {
    543569                foreach ($_REQUEST['products_ids'] as $product_id) {
    544570                    $product_id = intval($product_id);
    545571                    $product = $this->products->get_product($product_id);
    546572
    547573                    //***
    548574
    549575                    if (!in_array($product->get_type(), array('variable', 'variation'))) {
    550576                        continue;
    551577                    }
    552578
    553579                    //***
    554580
    555581                    if ($product->is_type('variable')) {
    556582                        $childrens = $product->get_children();
    557583                        $parent_id = $product_id;
    558584                        $parent_product = $product;
    559585                    } else {
    560586                        $childrens = array($product_id);
    561587                        $parent_id = $product->get_parent_id();
    562588                        $parent_product = $this->products->get_product($parent_id);
    563589                    }
    564590
    565591
    566592                    $parent_terms = wc_get_product_terms($parent_id, $to_att, array('fields' => 'slugs'));
    567593
    568594                    if (!empty($childrens)) {
    569595                        foreach ($childrens as $child_id) {
    570596                            $variation = $this->products->get_product($child_id);
    571597                            $available_variations = $parent_product->get_available_variation($variation);
    572598
    573599                            //***
    574600
    575601                            if (isset($available_variations['attributes']) AND!empty($available_variations['attributes'])) {
    576602                                if (isset($available_variations['attributes']['attribute_' . $from_att])) {
    577603                                    if ($available_variations['attributes']['attribute_' . $from_att] === sanitize_text_field($_REQUEST['from']['term'])) {
    578604
    579605                                        $possible_attributes = $available_variations['attributes'];
    580606
    581607                                        if ($from_att !== $to_att) {
    582608                                            unset($possible_attributes['attribute_' . $from_att]);
    583609                                        }
    584610
    585611                                        $possible_attributes['attribute_' . $to_att] = sanitize_text_field($_REQUEST['to']['term']);
    586612
    587613                                        //if such attribute not selected in the parent product Attributes tab lets attach it here
    588614                                        if (!in_array($_REQUEST['to']['term'], $parent_terms)) {
    589615                                            $p_terms = [];
    590616                                            foreach($parent_terms as $t_slug){
    591617                                                $t_term= get_term_by('slug', sanitize_text_field($t_slug), $to_att);
    592618                                                if($t_term){
    593619                                                    $p_terms[] = $t_term->term_id;
    594620                                                }
    595621                                               
    596622                                            }
    597623                                            $t = get_term_by('slug', sanitize_text_field($_REQUEST['to']['term']), $to_att);
    598624                                            $p_terms[] =  $t->term_id;
    599625
    600626                                            $this->products->update_page_field($parent_id, $to_att, $p_terms);
    601627                                        }
    602628
    603629                                        $variation->set_attributes($possible_attributes);
    604630                                        $variation->save();
    605631                                    }
    606632                                }
    607633                            }
    608634                        }
    609635                    }
    610636                }
    611637            }
    612638        }
    613639
    614640        die('done');
    615641    }
    616642
    617643    //************ TAB 6 attaching
    618644    //ajax
    619645    public function woobe_bulkoperations_attaching() {
    620 
     646        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     647            die('0');
     648        }
    621649        $selected_attribute = sanitize_text_field($_REQUEST['selected_attribute']);
    622650        $attaching_att = $_REQUEST['attaching_att']; //sanitizing in cycle
    623651        $products_ids = $_REQUEST['products_ids']; //sanitizing in cycle
    624652        //***
    625653
    626654        if (!empty($products_ids)) {
    627655            if (!empty($selected_attribute) AND $attaching_att) {
    628656                foreach ($products_ids as $product_id) {
    629657                    $product_id = intval($product_id); //sanitizing
    630658
    631659                    $product = $this->products->get_product($product_id);
    632660
    633661                    if ($product->is_type('variable')) {
    634662                        $childrens = $product->get_children();
    635663
    636664                        //***
    637665                        if (!empty($childrens)) {
    638666                            foreach ($childrens as $child_id) {
    639667                                $variation = $this->products->get_product($child_id);
    640668                                $available_variations = $product->get_available_variation($variation);
    641669
    642670                                //***
    643671
    644672                                if (isset($available_variations['attributes']) AND!empty($available_variations['attributes'])) {
    645673
    646674                                    //if its empty - will be filled up, if not empty will be replaced if not selected 'ignore'
    647675                                    $possible_attributes = $available_variations['attributes'];
    648676
    649677                                    //***
    650678
    651679                                    foreach ($attaching_att as $set) {
    652680
    653681                                        if (isset($possible_attributes['attribute_' . $selected_attribute])) {
    654682                                            unset($possible_attributes['attribute_' . $selected_attribute]);
    655683                                        }
    656684
    657685                                        if (isset($set['attributes']['attribute_' . $selected_attribute])) {
    658686                                            unset($set['attributes']['attribute_' . $selected_attribute]);
    659687                                        }
    660688
    661689                                        //***
    662690
    663691                                        $att_set_now_keys = array_keys($possible_attributes);
    664692                                        $set_keys = array_keys($set['attributes']);
    665693                                        sort($att_set_now_keys);
    666694                                        sort($set_keys);
    667695
    668696                                        if ($att_set_now_keys === $set_keys) {
    669697                                            $att_set_now_vals = array_values($possible_attributes);
    670698                                            $set_vals = array_values($set['attributes']);
    671699                                            sort($att_set_now_vals);
    672700                                            sort($set_vals);
    673701                                            if ($att_set_now_vals === $set_vals AND $set['value'] !== 'woobe-ignore') {
    674702                                                $possible_attributes['attribute_' . $selected_attribute] = sanitize_text_field($set['value']);
    675703
    676704                                                //if such attribute not selected in the parent product Attributes tab lets attach it here
    677705                                                $parent_terms = wc_get_product_terms($product_id, $selected_attribute, array('fields' => 'ids'));
    678706                                                if (!in_array($set['value'], $parent_terms)) {
    679707                                                    $t = get_term_by('slug', sanitize_text_field($set['value']), $selected_attribute);
    680708                                                    $parent_terms[] = $t->term_id;
    681709                                                    $this->products->update_page_field($product_id, $selected_attribute, $parent_terms, '', array('set_variation'));
    682710
    683711                                                }
    684712
    685713
    686714                                                $variation->set_attributes($possible_attributes);
    687715                                                $variation->save();
    688716                                                break;
    689717                                            }
    690718                                        }
    691719                                    }
    692720                                }
    693721                            }
    694722                        }
    695723                       
    696724                        //fix  visibility
    697725                        $meta = get_post_meta($product_id, '_product_attributes', true);
    698726                        if (is_array($meta)) {
    699727                            foreach ($meta as $pa_key => $item) {
    700728                                if ($item['name'] == 'pa_size') {
    701729                                    $meta[$pa_key]['is_visible'] = 1;
    702730                                    $meta[$pa_key]['is_variation'] = 1;
    703731                                }
    704732                            }
    705733                        }
    706734                        update_post_meta($product_id, '_product_attributes', $meta);                       
    707735                    }
    708736                }
    709737            }
    710738        }
    711739
    712740        //***
    713741
    714742        die('done');
    715743    }
    716744
    717745    //************ TAB 7 visibility
    718746    //ajax
    719747    public function woobe_bulkoperations_visibility() {
    720 
     748        if (!isset($_REQUEST['bulkoperations_nonce']) || !wp_verify_nonce($_REQUEST['bulkoperations_nonce'], 'woobe_bulkoperations_nonce')) {
     749            die('0');
     750        }
    721751        if (isset($_REQUEST['products_ids'])) {
    722752            $products_ids = $_REQUEST['products_ids']; //sanitizing in cycle
    723753            $vis_data = $_REQUEST['vis_data']; //sanitizing in cycle
    724754
    725755            if (!empty($products_ids) AND!empty($vis_data)) {
    726756                foreach ($products_ids as $product_id) {
    727757                    $product_id = intval($product_id);
    728758
    729759                    $product = $this->products->get_product($product_id);
    730760                    if ($product->is_type('variable')) {
    731761                        $meta = get_post_meta($product_id, '_product_attributes', true);
    732762
    733763
    734764                        foreach ($vis_data as $vis) {
    735765                            if (is_array($meta)) {
    736766                                foreach ($meta as $pa_key => $item) {
    737767                                    if ($item['name'] == $vis['attribute']) {
    738768                                        $meta[$pa_key]['is_visible'] = intval($vis['is_visible']);
    739769                                        $meta[$pa_key]['is_variation'] = intval($vis['is_variation']);
    740770                                    }
    741771                                }
    742772                            }
    743773                        }
    744774                        update_post_meta($product_id, '_product_attributes', $meta);
    745775                    }
    746776                }
    747777            }
    748778        }
    749779
    750780        die('done');
    751781    }
    752782
    753783}
Note: See TracChangeset for help on using the changeset viewer.