Esempio n. 1
0
File: woo.php Progetto: bulats/chef
function batchUpdateWoo($post)
{
    global $post_status_update, $table_prefix, $wpdb, $sql_results, $woocommerce;
    $_POST = $post;
    // Fix: PHP 5.4
    if (!empty($wpdb->prefix)) {
        $wp_table_prefix = $wpdb->prefix;
    }
    if (isset($_POST['edited'])) {
        $_POST['active_module'] = $_POST['activeModule'];
        $result = woo_insert_update_data($_POST);
    }
    $ids = json_decode(stripslashes($_POST['ids']));
    $radioData = '';
    // For WP_Debug
    $flag = '';
    // For WP_Debug
    if ($_POST['activeModule'] == 'Products') {
        $active_module = 'Products';
        $actions = json_decode($_POST['updateDetails']);
        $sel_records = json_decode($_POST['selected']);
        $radioData = $wpdb->_real_escape($_POST['radio']);
        $flag = $wpdb->_real_escape($_POST['flag']);
        // create an array of ids (newly added products & modified products)
        $count = isset($result['updateCnt']) ? $result['updateCnt'] : 0;
        //to skip updated & unselected records from batch update
        for ($i = 0; $i < count($ids); $i++) {
            if (strstr($ids[$i], 'ext-record') != '') {
                $ids_temp[$i] = $result['productId'][$count];
                $count++;
            }
        }
        if (isset($sel_records) && $sel_records != null) {
            //collectin the variation product's id
            foreach ($sel_records as $record) {
                if (isset($record->id) && !empty($record->id) && $record->id != '') {
                    if ($record->post_parent != 0) {
                        $children_ids[] = $record->id;
                    } else {
                        $parent_ids[] = $record->id;
                    }
                } else {
                    $parent_ids = $result['productId'];
                }
            }
        }
    } else {
        if ($_POST['activeModule'] == 'Customers') {
            $active_module = 'Customers';
        } else {
            $active_module = 'Orders';
        }
        $actions = json_decode($_POST['values']);
    }
    //$idLength = count ( $ids );
    $idLength = json_decode(stripslashes($_POST['fupdatecnt']));
    // code to handle the message for different number of max. records
    $length = count($actions);
    $all_ids = get_all_ids($sql_results);
    $_POST['actions_count'] = $length;
    // For distributing ids based on product type, will help in reducing number of queries
    if ($active_module == 'Products') {
        $variation_parent_id = array();
        $selected_id = array();
        $selected_id_variation = array();
        $all_id = array();
        $all_id_variation = array();
        foreach ($sel_records as $sel_record) {
            $terms = wp_get_object_terms($sel_record->id, 'product_type', array('fields' => 'names'));
            // woocommerce gets product_type using this method
            $post_parent = $wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = {$sel_record->id};");
            if (sanitize_title($terms[0]) == 'variable' && $post_parent == 0) {
                $variation_parent_id[] = $sel_record->id;
            } else {
                if ($post_parent > 0 && sanitize_title($terms[0]) == 'simple') {
                    $selected_id[] = $sel_record->id;
                } elseif ($post_parent > 0) {
                    $selected_id_variation[] = $sel_record->id;
                } else {
                    $selected_id[] = $sel_record->id;
                }
            }
        }
        $all_ids_grouped = array();
        // Array used for passing ids to function for processing variations
        $all_ids_grouped['selected_id'] = $selected_id;
        $all_ids_grouped['variation_parent_id'] = $variation_parent_id;
        $all_ids_grouped['selected_id_variation'] = $selected_id_variation;
        $all_ids_grouped['all_id'] = $all_id;
        $all_ids_grouped['all_id_variation'] = $all_id_variation;
        $parent_ids = array_merge($all_ids_grouped['selected_id'], $all_ids_grouped['variation_parent_id']);
        $results = $wpdb->get_results("SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies", 'ARRAY_A');
        $attribute_names = array();
        $count = 0;
        foreach ($results as $result) {
            $attribute_names[] = $result['attribute_name'];
            $count++;
        }
        // For handling modification in attributes of parent product
        foreach ($parent_ids as $parent_id) {
            for ($i = 0; $i < count($actions); $i++) {
                $actions_colfilter = property_exists($actions[$i], 'colFilter') === true ? $actions[$i]->colFilter : '';
                // For WP_Debug
                if ($actions_colfilter == 'AttributeAdd') {
                    $action = $actions_colfilter;
                    $attribute_name = $actions[$i]->action;
                    $attribute_type = $wpdb->get_var("SELECT attribute_type FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name LIKE '{$attribute_name}'");
                    if ($attribute_name == 'custom') {
                        $custom_attribute_name = $actions[$i]->colValue;
                        $custom_attribute_terms = $actions[$i]->unit;
                        process_custom_add_attribute($parent_id, $custom_attribute_name, $custom_attribute_terms, $action, $attribute_names);
                    } else {
                        if ($attribute_type == 'text') {
                            $terms = explode('|', $actions[$i]->unit);
                            if (is_array($terms) && count($terms) > 0) {
                                foreach ($terms as $term) {
                                    $term_details = term_exists($term, 'pa_' . $attribute_name);
                                    if (!is_array($term_details) && $term_details == 0) {
                                        $term_details = wp_insert_term($term, 'pa_' . $attribute_name);
                                    }
                                    $term_taxonomy_id = $term_details['term_taxonomy_id'];
                                    process_add_attribute($parent_id, $attribute_name, $term_taxonomy_id, $action, $attribute_names);
                                }
                            }
                        } else {
                            $term_taxonomy_id = $actions[$i]->colValue;
                            process_add_attribute($parent_id, $attribute_name, $term_taxonomy_id, $action, $attribute_names);
                        }
                    }
                } elseif ($actions_colfilter == 'AttributeRemove') {
                    $attribute_name = $actions[$i]->action;
                    $term_taxonomy_id = $actions[$i]->colValue;
                    process_remove_attribute($parent_id, $attribute_name, $term_taxonomy_id);
                }
            }
        }
        // For handling changes in attributes of product variation
        foreach ($all_ids_grouped['selected_id_variation'] as $c_id) {
            for ($i = 0; $i < count($actions); $i++) {
                $actions_colfilter = property_exists($actions[$i], 'colFilter') === true ? $actions[$i]->colFilter : '';
                // For WP_Debug
                if ($actions_colfilter == 'AttributeChange') {
                    $attribute_name = $actions[$i]->action;
                    $term_taxonomy_id = $actions[$i]->colValue;
                    $taxonomy = "pa_" . $attribute_name;
                    $select = "select t.slug from {$wpdb->prefix}terms as t join {$wpdb->prefix}term_taxonomy as tt on (t.term_id = tt.term_id) where tt.taxonomy = '{$taxonomy}' and t.term_id in ( ";
                    $select .= "select term_id from {$wpdb->prefix}term_taxonomy where term_taxonomy_id = '{$term_taxonomy_id}' ) ";
                    $results = $wpdb->get_results($select, 'ARRAY_A');
                    $term_name = array();
                    foreach ($results as $result) {
                        $term_name[] = $result['slug'];
                    }
                    process_change_attribute($c_id, $attribute_name, $term_taxonomy_id, $term_name);
                }
            }
        }
        // generating string of comma separated ids
        $variation_parent_id = $wpdb->_real_escape(implode(',', $variation_parent_id));
        $selected_id = $wpdb->_real_escape(implode(',', $selected_id));
        $selected_id_variation = $wpdb->_real_escape(implode(',', $selected_id_variation));
        $all_id = $wpdb->_real_escape(implode(',', $all_id));
        //This is commented as the implode function dosent work properly with very long list of ids.
        $product_type_grouped = "";
        $product_type = "";
        $no_product_type = "";
        $price_variation = "";
        //$all_id_variation 		= $wpdb->_real_escape ( implode ( ',', $all_id_variation ) );
        //Query for handling the grouped products updation
        $query_grouped = "SELECT id FROM `{$wpdb->prefix}posts`\n                                    WHERE post_parent IN (SELECT posts.id as id\n                                            FROM `{$wpdb->prefix}posts` AS posts\n                                                JOIN {$wpdb->prefix}term_relationships AS term_relationships \n                                                                ON term_relationships.object_id = posts.id\n                                                JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n                                                                ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n                                                JOIN {$wpdb->prefix}terms AS terms \n                                                                ON term_taxonomy.term_id = terms.term_id \n                                            WHERE posts.post_parent = 0 \n                                                AND posts.post_type IN ('product')\n                                                AND posts.post_status IN ('publish', 'draft')\n                                                AND terms.slug IN ('grouped'))\n                                        AND id IN (" . implode(",", $ids) . ")\n                                    GROUP BY id\n                                    ORDER BY id desc ";
        $product_type_grouped = implode(",", $wpdb->get_col($query_grouped));
        //Query to get the post_id type of all the simple products
        $query = "SELECT posts.id as id\n                                            FROM `{$wpdb->prefix}posts` AS posts\n                                                JOIN {$wpdb->prefix}term_relationships AS term_relationships \n                                                                ON term_relationships.object_id = posts.id\n                                                JOIN {$wpdb->prefix}term_taxonomy AS term_taxonomy \n                                                                ON term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id \n                                                JOIN {$wpdb->prefix}terms AS terms \n                                                                ON term_taxonomy.term_id = terms.term_id \n                                            WHERE posts.post_parent = 0 \n                                                AND posts.post_type IN ('product')\n                                                AND posts.post_status IN ('publish', 'draft')\n                                                AND terms.slug NOT IN ('variable','grouped','external')\n                                                AND posts.id IN (" . implode(",", $ids) . ")\n                                            GROUP BY posts.id\n                                            ORDER BY posts.id desc ";
        $product_type_result = $wpdb->get_col($query);
        //Query to handle the products which have no type
        $query = "SELECT object_id FROM {$wpdb->prefix}term_relationships\n                                    WHERE object_id IN (" . implode(",", $ids) . ")";
        $result = $wpdb->get_col($query);
        $no_product_type = array_values(array_diff($ids, $result));
        for ($i = 0, $j = sizeof($product_type_result); $i < sizeof($no_product_type); $i++, $j++) {
            //Code to exclude variations from the product type update
            $post_parent = $wpdb->get_var("SELECT post_parent FROM {$wpdb->prefix}posts WHERE ID = {$no_product_type[$i]}");
            if ($post_parent == 0) {
                $product_type_result[$j] = $no_product_type[$i];
                //Code for adding the product type as simple for products having no type
                $terms = wp_set_object_terms($no_product_type[$i], 'simple', 'product_type');
            }
        }
        $product_type = implode(",", array_filter($product_type_result));
        //Query to get the post_id of all the variations for price updation
        $query_variation = "SELECT posts.id as id\n                                            FROM `{$wpdb->prefix}posts` AS posts\n                                            WHERE posts.post_parent > 0 \n                                                AND posts.post_type IN ('product_variation')\n                                                AND posts.post_status IN ('publish', 'draft')\n                                                AND posts.id IN (" . implode(",", $ids) . ")\n                                            GROUP BY posts.id\n                                            ORDER BY posts.id desc ";
        $price_variation = implode(",", $wpdb->get_col($query_variation));
    }
    //Code to get all the term_names along with the term_taxonomy_id in an array
    $query_terms = "SELECT terms.name,term_taxonomy.term_taxonomy_id \n                                FROM {$wpdb->prefix}term_taxonomy AS term_taxonomy\n                                    JOIN {$wpdb->prefix}terms AS terms ON terms.term_id = term_taxonomy.term_id\n                                WHERE taxonomy LIKE 'shop_order_status'";
    $terms = $wpdb->get_results($query_terms, 'ARRAY_A');
    for ($i = 0; $i < sizeof($terms); $i++) {
        $terms_name[$terms[$i]['name']] = $terms[$i]['term_taxonomy_id'];
    }
    // Building queries
    for ($i = 0; $i < $length; $i++) {
        $selected_ids = "";
        if ($active_module == 'Products') {
            $actions_colfilter = property_exists($actions[$i], 'colFilter') === true ? $actions[$i]->colFilter : '';
            // For WP_Debug
            $actions_updatecolname = property_exists($actions[$i], 'updateColName') === true ? $actions[$i]->updateColName : '';
            // For WP_Debug
            $actions_colname = property_exists($actions[$i], 'colName') === true ? $actions[$i]->colName : '';
            // For WP_Debug
            if (substr($actions_colfilter, 0, 8) == 'Attribute') {
                continue;
            }
            $table_name = "`{$wpdb->_real_escape($actions[$i]->tableName)}`";
            $col_id = $wpdb->_real_escape($actions[$i]->colId);
            $is_category = strstr($col_id, 'group') != '' ? true : false;
            $column_name = "{$wpdb->_real_escape($actions_colname)}";
            $action_name = $wpdb->_real_escape($actions[$i]->action);
            $column_filter = $wpdb->_real_escape($actions_colfilter);
            if ($column_name == 'thumbnail') {
                for ($j = 0; $j < sizeof($ids); $j++) {
                    update_post_meta($ids[$j], '_thumbnail_id', $actions[$i]->colValue);
                }
            }
            // populating all ids based on column to be updated
            if ($column_name == "_regular_price" || $column_name == "_sale_price") {
                $all_ids = $all_id;
            } elseif ($column_name == "post_title" || $column_name == "post_status" || $column_name == "post_content" || $column_name == "post_excerpt" || $is_category == true || $column_name == "_tax_status") {
                $all_ids = $variation_parent_id;
                $all_ids .= isset($all_id) && $all_id != '' ? ',' . $all_id : '';
            } else {
                $all_ids = $variation_parent_id;
                // $all_ids .= ( isset($all_id_variation) && $all_id_variation != '' ) ? ',' . $all_id_variation : '';
                $all_ids .= !empty($all_id_variation) ? ',' . implode(",", $all_id_variation) : '';
                // For WP_Debug
                $all_ids .= isset($all_id) && $all_id != '' ? ',' . $all_id : '';
            }
            $all_ids = trim($all_ids, ',');
            $update_column = $actions_updatecolname != '' ? "{$wpdb->_real_escape($actions_updatecolname)}" : "{$wpdb->_real_escape($actions_colname)}";
            $col_filter = "{$wpdb->_real_escape($actions_colfilter)}";
            $drop_down3_value = "{$wpdb->_real_escape($actions[$i]->unit)}";
            //@todo for state code for customers
            $row_filter = '';
            $filter_col = '';
            if ($col_filter != '') {
                $col_filter_arr = explode(':', $col_filter);
                $filter_col = "{$col_filter_arr['0']}";
                $row_filter = $col_filter_arr[1];
            }
            $text_cmp_value = $wpdb->_real_escape($actions[$i]->colValue);
            $selected_ids = implode(',', $ids);
            //Added by Tarun to cater the function to update only for ids passed
        } else {
            $actions[$i][0] = explode(',', $actions[$i][0]);
            $action_index = 0;
            foreach ($actions[$i][0] as $action) {
                // trimming the field names & table names
                $actions[$i][0][$action_index] = trim($actions[$i][0][$action_index]);
                $action_index++;
            }
            // getting values from POST
            $action_name = $wpdb->_real_escape($actions[$i][1]);
            $column_name = $wpdb->_real_escape($actions[$i][0][0]);
            $table_name = $wpdb->_real_escape($actions[$i][0][1]);
            $drop_down3_value = isset($actions[$i][3]) ? $wpdb->_real_escape($actions[$i][3]) : '';
            $selected_ids = $wpdb->_real_escape(implode(',', $ids));
            if ($active_module == 'Customers') {
                //Query for getting the email id and the customer_user for the selected ids
                $query_email = "SELECT DISTINCT(GROUP_CONCAT( meta_value\n                                     ORDER BY meta_id SEPARATOR '###' ) )AS meta_value,\n                                     GROUP_CONCAT(distinct meta_key\n                                     ORDER BY meta_id SEPARATOR '###' ) AS meta_key\n                                     FROM {$wpdb->prefix}postmeta \n                                     WHERE meta_key in ('_billing_email','_customer_user') \n                                        AND post_id IN ({$selected_ids})\n                                     GROUP BY post_id";
                $result_email = $wpdb->get_results($query_email, 'ARRAY_A');
                $email = "";
                $users = "";
                $index = 0;
                $index1 = 0;
                for ($j = 0; $j < sizeof($result_email); $j++) {
                    $meta_key = explode("###", $result_email[$j]['meta_key']);
                    $meta_value = explode("###", $result_email[$j]['meta_value']);
                    $postmeta[$j] = array_combine($meta_key, $meta_value);
                    if ($postmeta[$j]['_customer_user'] == 0) {
                        $email[$index] = $postmeta[$j]['_billing_email'];
                        $index++;
                    } elseif ($postmeta[$j]['_customer_user'] > 0) {
                        $users[$index1] = $postmeta[$j]['_customer_user'];
                        $index1++;
                    }
                    unset($meta_key);
                    unset($meta_value);
                }
                //For Guest Customers
                if ($email != "") {
                    $email = "'" . implode("','", $email) . "'";
                    //Query for getting all the post_ids w.r.to the email id of the edited customer record
                    $query_ids = "SELECT DISTINCT(post_id) FROM {$wpdb->prefix}postmeta WHERE meta_key='_billing_email' AND meta_value IN ({$email})";
                    $id = implode(", ", $wpdb->get_col($query_ids));
                    $selected_ids = $id;
                }
                //For Registered Customers
                if ($users != "") {
                    $users = implode(",", $users);
                }
            }
            $text_cmp_value = '';
            if ($column_name == '_billing_country' || $column_name == '_shipping_country') {
                $region = !empty($actions[$i][4]) ? $wpdb->_real_escape($actions[$i][4]) : $wpdb->_real_escape($actions[$i][2]);
                // For WP_Debug
                $text_cmp_value = $drop_down3_value;
                $state_column = $column_name == '_billing_country' ? '_billing_state' : '_shipping_state';
                $region_query = "UPDATE " . $wpdb->_real_escape($table_name) . " SET meta_value = '" . $wpdb->_real_escape($region) . "' WHERE post_id IN ( " . $wpdb->_real_escape($selected_ids) . " ) AND meta_key = '{$state_column}'";
                $result = $wpdb->query($region_query);
                // if ( $result < 1 ) {
                // 	$updated_rows_cnt = _e('Batch Updation of Region not successful','smart-manager');
                // }
            } else {
                if ($table_name == "`{$wpdb->prefix}term_relationships`" && $actions[$i][4] != '') {
                    $term_id = $terms_name[$actions[$i][4]];
                    $query = "UPDATE `{$wpdb->prefix}term_relationships` SET term_taxonomy_id = {$term_id} \n                                                WHERE object_id IN ( " . $wpdb->_real_escape($selected_ids) . " )";
                    $result = $wpdb->query($query);
                    continue;
                } else {
                    $text_cmp_value = $actions[$i][2] == '' ? $drop_down3_value : $wpdb->_real_escape($actions[$i][2]);
                }
            }
        }
        if ($table_name == "`{$wpdb->prefix}postmeta`" || $table_name == "`{$wpdb->prefix}usermeta`") {
            $update_column = 'meta_value';
            $reference_column = 'meta_key';
        }
        $flag_query = 0;
        //Flag for handling the 'Set To Sales Price' and 'Set To Regular Price' batch update actions
        switch ($action_name) {
            case 'SET_TO':
                if ($table_name == "{$wpdb->prefix}posts" || $table_name == "`{$wpdb->prefix}postmeta`" || $table_name == "`{$wpdb->prefix}usermeta`") {
                    //version 3.8
                    //condition for handling the decimal places
                    if (!empty($text_cmp_value) && ($column_name == '_regular_price' || $column_name == '_sale_price')) {
                        $update_value = $update_column . ' = ROUND(' . $text_cmp_value . ',' . get_option('woocommerce_price_num_decimals') . ')';
                    } else {
                        if (!empty($text_cmp_value)) {
                            $update_value = $update_column . ' = \'' . $text_cmp_value . '\'';
                            //is array for weight
                        } else {
                            $update_value = $update_column . ' = ""';
                        }
                    }
                } else {
                    if ($is_category) {
                        $delete_query = "DELETE FROM " . $table_name . " WHERE `object_id` in (";
                        $insert_query = "INSERT INTO " . $table_name . " (object_id,`" . $update_column . "`) VALUES ";
                        $delete_query .= $selected_ids;
                        $sub_query = array();
                        $category_selected_ids = explode(',', $selected_ids);
                        foreach ($category_selected_ids as $category_selected_id) {
                            $sub_query[] = "(" . $category_selected_id . "," . $text_cmp_value . ")";
                        }
                        $insert_query .= implode(',', $sub_query);
                        $delete_query .= ") AND `term_taxonomy_id` IN ( SELECT term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy WHERE taxonomy = 'product_cat' )";
                        $delete_sql_result = $wpdb->query($delete_query);
                        $insert_sql_result = $wpdb->query($insert_query);
                    }
                }
                break;
            case 'PREPEND':
                if ($table_name == "{$wpdb->prefix}posts" || $table_name == "`{$wpdb->prefix}postmeta`" || $table_name == "`{$wpdb->prefix}usermeta`") {
                    //version 3.8
                    $update_value = $update_column . ' = concat(\'' . $text_cmp_value . '\',' . $update_column . ')';
                }
                break;
            case 'APPEND':
                if ($table_name == "{$wpdb->prefix}posts" || $table_name == "`{$wpdb->prefix}postmeta`" || $table_name == "`{$wpdb->prefix}usermeta`") {
                    //version 3.8
                    $update_value = $update_column . ' = concat(' . $update_column . ',\'' . $text_cmp_value . '\')';
                }
                break;
            case 'INCREASE_BY_NUMBER':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    //condition for handling the decimal places
                    if (!empty($text_cmp_value) && ($column_name == '_regular_price' || $column_name == '_sale_price')) {
                        $update_value = $update_column . ' = ROUND(' . $update_column . '+' . $text_cmp_value . ',' . get_option('woocommerce_price_num_decimals') . ')';
                    } else {
                        if (!empty($text_cmp_value)) {
                            $update_value = $update_column . ' = ' . $update_column . '+' . $text_cmp_value;
                        } else {
                            $text_cmp_value = "0";
                            $update_value = $update_column . ' = ' . $update_column . '+' . $text_cmp_value;
                        }
                    }
                }
                break;
            case 'DECREASE_BY_NUMBER':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    //condition for handling the decimal places
                    if (!empty($text_cmp_value) && ($column_name == '_regular_price' || $column_name == '_sale_price')) {
                        $update_value = $update_column . ' = ROUND(' . $update_column . '-' . $text_cmp_value . ',' . get_option('woocommerce_price_num_decimals') . ')';
                    } else {
                        if (!empty($text_cmp_value)) {
                            $update_value = $update_column . ' = ' . $update_column . '-' . $text_cmp_value;
                        } else {
                            $text_cmp_value = "0";
                            $update_value = $update_column . ' = ' . $update_column . '-' . $text_cmp_value;
                        }
                    }
                }
                break;
            case 'INCREASE_BY_%':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    //condition for handling the decimal places
                    if (!empty($text_cmp_value) && ($column_name == '_regular_price' || $column_name == '_sale_price')) {
                        $update_value = $update_column . ' = ROUND(' . $update_column . '+' . ($update_column . '*' . $text_cmp_value / 100) . ',' . get_option('woocommerce_price_num_decimals') . ')';
                    } else {
                        if (!empty($text_cmp_value)) {
                            $update_value = $update_column . ' =' . $update_column . '+' . ($update_column . '*' . $text_cmp_value / 100);
                        } else {
                            $update_value = $update_column . ' =' . $update_column;
                        }
                    }
                }
                break;
            case 'DECREASE_BY_%':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    //condition for handling the decimal places
                    if (!empty($text_cmp_value) && ($column_name == '_regular_price' || $column_name == '_sale_price')) {
                        $update_value = $update_column . ' = ROUND(' . $update_column . '-' . ($update_column . '*' . $text_cmp_value / 100) . ',' . get_option('woocommerce_price_num_decimals') . ')';
                    } else {
                        if (!empty($text_cmp_value)) {
                            $update_value = $update_column . ' =' . $update_column . '-' . ($update_column . '*' . $text_cmp_value / 100);
                        } else {
                            $update_value = $update_column . ' =' . $update_column;
                        }
                    }
                }
                break;
            case 'YES':
                if ($column_name == 'post_status') {
                    $update_value = $update_column . ' = \'publish\'';
                } elseif ($column_name == '_stock') {
                    $update_value = $update_column . ' = 0';
                } else {
                    $update_value = $update_column . ' = 1';
                }
                break;
            case 'NO':
                if ($column_name == 'post_status') {
                    $update_value = $update_column . ' = \'draft\'';
                } elseif ($column_name == '_stock') {
                    $update_value = $update_column . ' = ""';
                } else {
                    $update_value = $update_column . ' = 0';
                }
                break;
            case 'ADD_TO':
                $sub_query = array();
                if (!$is_category) {
                    // Need to be reworked
                    for ($j = 0; $j < count($ids); $j++) {
                        $sub_query[] = "( " . $wpdb->_real_escape($ids[$j]) . "," . $text_cmp_value . ")";
                    }
                    $sub_query = implode(',', $sub_query);
                    $query = "INSERT INTO " . $table_name . " (object_id,`" . $update_column . "`) VALUES " . $sub_query;
                    $sql_result = $wpdb->query($query);
                } else {
                    $delete_query = "DELETE FROM " . $table_name . " WHERE `object_id` in (";
                    $insert_query = "INSERT INTO " . $table_name . " (`object_id`,`" . $update_column . "`) VALUES ";
                    $delete_query .= $selected_ids;
                    $sub_query = array();
                    $category_selected_ids = explode(',', $selected_ids);
                    foreach ($category_selected_ids as $category_selected_id) {
                        $sub_query[] = "(" . $category_selected_id . "," . $text_cmp_value . ")";
                    }
                    $insert_query .= implode(',', $sub_query);
                    $delete_query .= ") AND `term_taxonomy_id`=" . $text_cmp_value;
                    $delete_sql_result = $wpdb->query($delete_query);
                    $insert_sql_result = $wpdb->query($insert_query);
                }
                break;
            case 'REMOVE_FROM':
                if (!$is_category) {
                    // Need to be reworked
                    $query = "DELETE FROM " . $table_name . " WHERE object_id in (" . $wpdb->_real_escape(implode(',', $ids)) . ")\n\t\t\t\t\t\t              AND `" . $update_column . "` = " . $text_cmp_value;
                    $sql_result = $wpdb->query($query);
                } else {
                    $delete_query = "DELETE FROM " . $table_name . " WHERE `object_id` in (";
                    $delete_query .= $selected_ids;
                    $delete_query .= ") AND `term_taxonomy_id` = {$text_cmp_value}";
                    $delete_sql_result = $wpdb->query($delete_query);
                }
                break;
            case 'CATALOG & SEARCH':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    $update_value = $update_column . ' = \'visible\'';
                }
                break;
            case 'CATALOG':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    $update_value = $update_column . ' = \'catalog\'';
                }
                break;
            case 'SEARCH':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    $update_value = $update_column . ' = \'search\'';
                }
                break;
            case 'HIDDEN':
                if ($table_name == "{$wpdb->prefix}postmeta") {
                    $update_value = $update_column . ' = \'hidden\'';
                }
                break;
            case 'SET_TO_SALES_PRICE':
                for ($j = 0; $j < sizeof($ids); $j++) {
                    $type = wp_get_object_terms($ids[$j], 'product_type', array('fields' => 'slugs'));
                    $query = "SELECT post_parent FROM `{$wpdb->prefix}posts` WHERE ID =" . $ids[$j];
                    $result_parent = $wpdb->get_col($query);
                    if (!empty($result_parent)) {
                        $product_type_parent = wp_get_object_terms($result_parent, 'product_type', array('fields' => 'slugs'));
                    }
                    if ($type[0] == 'simple' && $result_parent[0] == 0 || $product_type_parent[0] == "grouped" || $_POST['SM_IS_WOO16'] == "false") {
                        $flag_success = 1;
                        $query = "SELECT meta_value FROM `{$wpdb->prefix}postmeta`\n                                            WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                        $result = $wpdb->get_col($query);
                        $result[0] = trim($result[0]);
                        // For handling when both price and sales price are null
                        if (!empty($result[0])) {
                            $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = ROUND(" . $result[0] . ',' . get_option('woocommerce_price_num_decimals') . ')' . " WHERE meta_key = '_regular_price' AND post_id = " . $ids[$j];
                        } else {
                            $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = '' WHERE meta_key = '_regular_price' AND post_id = " . $ids[$j];
                        }
                        $result1 = $wpdb->query($query);
                    } else {
                        if ($result_parent[0] > 0 && $_POST['SM_IS_WOO16'] == "true") {
                            $flag_success = 1;
                            $query = "SELECT meta_value FROM `{$wpdb->prefix}postmeta`\n                                            WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                            $result = $wpdb->get_col($query);
                            $result[0] = trim($result[0]);
                            // For handling when both price and sales price are null
                            if (!empty($result[0])) {
                                $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = ROUND(" . $result[0] . ',' . get_option('woocommerce_price_num_decimals') . ')' . " WHERE meta_key = '_price' AND post_id = " . $ids[$j];
                            } else {
                                $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = '' WHERE meta_key = '_price' AND post_id = " . $ids[$j];
                            }
                            $result1 = $wpdb->query($query);
                        }
                    }
                }
                $flag_query = 1;
                break;
            case 'SET_TO_REGULAR_PRICE':
                for ($j = 0; $j < sizeof($ids); $j++) {
                    $type = wp_get_object_terms($ids[$j], 'product_type', array('fields' => 'slugs'));
                    $query = "SELECT post_parent FROM `{$wpdb->prefix}posts` WHERE ID =" . $ids[$j];
                    $result_parent = $wpdb->get_col($query);
                    if (!empty($result_parent)) {
                        $product_type_parent = wp_get_object_terms($result_parent, 'product_type', array('fields' => 'slugs'));
                    }
                    if ($type[0] == 'simple' && $result_parent[0] == 0 || $product_type_parent[0] == "grouped" || $_POST['SM_IS_WOO16'] == "false") {
                        $query = "SELECT meta_value FROM `{$wpdb->prefix}postmeta`\n                                            WHERE meta_key = '_regular_price' AND post_id = " . $ids[$j];
                        $result = $wpdb->get_col($query);
                        $result[0] = trim($result[0]);
                        // For handling when both price and sales price are null
                        if (!empty($result[0])) {
                            $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = ROUND(" . $result[0] . ',' . get_option('woocommerce_price_num_decimals') . ')' . " WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                        } else {
                            $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = '' WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                        }
                        $result1 = $wpdb->query($query);
                    } else {
                        if ($result_parent[0] > 0 && $_POST['SM_IS_WOO16'] == "true") {
                            $query = "SELECT meta_value FROM `{$wpdb->prefix}postmeta`\n                                            WHERE meta_key = '_price' AND post_id = " . $ids[$j];
                            $result = $wpdb->get_col($query);
                            $result[0] = trim($result[0]);
                            // For handling when both price and sales price are null
                            if (!empty($result[0])) {
                                $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = ROUND(" . $result[0] . ',' . get_option('woocommerce_price_num_decimals') . ')' . " WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                            } else {
                                $query = "UPDATE `{$wpdb->prefix}postmeta` SET meta_value = '' WHERE meta_key = '_sale_price' AND post_id = " . $ids[$j];
                            }
                            $result1 = $wpdb->query($query);
                        }
                    }
                }
                $flag_query = 1;
                break;
        }
        if ($table_name != "`{$wpdb->prefix}term_relationships`") {
            if ($flag_query == 0) {
                if (is_array($update_value)) {
                    $update_value = implode(',', $update_value);
                }
                $update_price_meta = false;
                $query = "UPDATE " . $table_name . " SET " . $update_value;
                if ($table_name == "{$wpdb->prefix}posts") {
                    if ($active_module == 'Products' && $update_column == 'post_status') {
                        $query .= " WHERE `post_type` IN ('product') AND `ID` in (" . $selected_ids . ")";
                    } else {
                        $query .= ' WHERE `ID` in (' . $selected_ids . ')';
                    }
                } else {
                    if ($table_name == "`{$wpdb->prefix}postmeta`") {
                        if ($column_name == '_regular_price') {
                            //Query for updating the price for the simple products
                            if (!empty($product_type)) {
                                sm_insert_metakey($product_type, '_regular_price');
                                // Code for inserting the meta_key if not present
                                $query_simple = $query;
                                $query_simple = $query_simple . " WHERE meta_key LIKE '_regular_price' AND post_id IN ({$product_type})";
                                $result = $wpdb->query($query_simple);
                                if (empty($price_variation)) {
                                    $query = "";
                                }
                            }
                            //                                                    $query = "INSERT INTO ". $table_name . "VALUES"
                            //Query for updating the price for the grouped products
                            if (!empty($product_type_grouped)) {
                                sm_insert_metakey($product_type_grouped, '_regular_price');
                                // Code for inserting the meta_key if not present
                                $query_grouped = $query;
                                $query_grouped = $query_grouped . " WHERE meta_key LIKE '_regular_price' AND post_id IN ({$product_type_grouped})";
                                $result_grouped = $wpdb->query($query_grouped);
                                if (empty($price_variation)) {
                                    $query = "";
                                }
                            }
                            //                                                    $query .= " WHERE meta_key LIKE '_price' AND post_id IN (" . $selected_ids . ")";
                            //Woo 2.0 Compatibility check
                            if ($_POST['SM_IS_WOO16'] == "true") {
                                if (!empty($price_variation)) {
                                    sm_insert_metakey($price_variation, '_price');
                                    // Code for inserting the meta_key if not present
                                    $query .= " WHERE meta_key LIKE '_price' AND post_id IN ({$price_variation})";
                                } else {
                                    $query = "";
                                }
                            } else {
                                if (!empty($price_variation)) {
                                    sm_insert_metakey($price_variation, '_regular_price');
                                    // Code for inserting the meta_key if not present
                                    $query .= " WHERE meta_key LIKE '_regular_price' AND post_id IN ({$price_variation})";
                                } else {
                                    $query = "";
                                }
                            }
                        } else {
                            if (!empty($selected_ids)) {
                                sm_insert_metakey($selected_ids, $column_name);
                                // Code for inserting the meta_key if not present
                                $query .= " WHERE `post_id` in (" . $selected_ids . ") AND meta_key = '" . $column_name . "'";
                            }
                        }
                    }
                }
                if (!empty($query) && $flag_query == 0) {
                    $result = $wpdb->query($query);
                }
            }
            // Code for Updating the '_price' for All Products
            if ($column_name == '_regular_price' || $column_name == '_sale_price') {
                $simple_ids = explode(",", $product_type);
                $grouped_ids = explode(",", $product_type_grouped);
                $variation_ids = explode(",", $price_variation);
                if (!empty($product_type) && !empty($product_type_grouped)) {
                    $final_ids = array_merge($simple_ids, $grouped_ids);
                } else {
                    if (!empty($product_type)) {
                        $final_ids = $simple_ids;
                    } else {
                        $final_ids = $grouped_ids;
                    }
                }
                if (!empty($price_variation) && $_POST['SM_IS_WOO16'] == "false") {
                    if (!empty($final_ids)) {
                        $final_ids = array_merge($final_ids, $variation_ids);
                    } else {
                        $final_ids = $variation_ids;
                    }
                }
                update_price_meta(implode(",", array_filter($final_ids)));
                // For Updating Variable Parent Price
                if (!empty($price_variation)) {
                    variable_price_sync($variation_ids);
                }
            }
            //Condition for handling the Batch Update for the registered Customers
            if ($active_module == 'Customers' && $users != "") {
                $table_name = "`{$wpdb->prefix}usermeta`";
                $column_name = substr($column_name, 1);
                $user_ids = $users;
                if ($column_name == 'billing_email') {
                    $update_column = 'user_email';
                    switch ($action_name) {
                        case 'SET_TO':
                            $user_value = $update_column . ' = \'' . $text_cmp_value . '\'';
                            break;
                        case 'PREPEND':
                            $user_value = $update_column . ' = concat(\'' . $text_cmp_value . '\',' . $update_column . ')';
                            break;
                        case 'APPEND':
                            $user_value = $update_column . ' = concat(' . $update_column . ',\'' . $text_cmp_value . '\')';
                            break;
                    }
                    $query_users = "UPDATE `{$wpdb->prefix}users` SET " . $user_value . "WHERE `id` in (" . $user_ids . ")";
                    $result_users = $wpdb->query($query_users);
                }
                if ($column_name == 'billing_country') {
                    $region = !empty($actions[$i][4]) ? $wpdb->_real_escape($actions[$i][4]) : $wpdb->_real_escape($actions[$i][2]);
                    $region_query = "UPDATE " . $wpdb->_real_escape($table_name) . " SET meta_value = '" . $wpdb->_real_escape($region) . "' WHERE user_id IN ( " . $wpdb->_real_escape($user_ids) . " ) AND meta_key = 'billing_state'";
                    $result = $wpdb->query($region_query);
                    if ($result < 1) {
                        $updated_rows_cnt = _e('Batch Updation of Region not successful', 'smart-manager');
                    }
                }
                $query = "UPDATE " . $table_name . " SET " . $update_value . "WHERE `user_id` in (" . $user_ids . ") AND meta_key = '" . $column_name . "'";
                $result = $wpdb->query($query);
            }
        } elseif ($table_name == "`{$wpdb->prefix}term_relationships`") {
            if ($active_module != 'Products') {
                $term_taxonomy_id = get_term_taxonomy_id($text_cmp_value);
                $query = "UPDATE " . $table_name . " SET term_taxonomy_id = " . $wpdb->_real_escape($term_taxonomy_id) . " WHERE object_id IN (" . $selected_ids . ") ";
                $result = $wpdb->query($query);
                $order_ids = explode(',', $selected_ids);
                if ($text_cmp_value == 'processing' || $text_cmp_value == 'completed') {
                    foreach ($order_ids as $order_id) {
                        $order = new WC_Order($wpdb->_real_escape($order_id));
                        $order->update_status($text_cmp_value);
                    }
                }
            }
        }
        $update_value = '';
    }
    // Handled with a different ajax request
    if ($radioData == 2 && $flag == 1) {
        $updated_rows_cnt = 'All';
    } else {
        $updated_rows_cnt = $idLength;
    }
    //Clearing the transients to handle the proper functioning of the widgets
    $woocommerce->clear_product_transients();
    return $updated_rows_cnt;
}
Esempio n. 2
0
function modify_permission_action($session_uid, $uid, $id)
{
    // needed to set the tab active
    $readers_active = true;
    //check if the user is admin
    if (user_is_admin($session_uid)) {
        $permission = get_permission($uid, $id);
        // Fetch all the readers IDs
        $ids = get_all_ids();
        // Fetch all the users UIDs
        $uids = get_all_uids();
        require 'templates/permission.php';
    } else {
        require 'templates/login.php';
    }
}