/** * uninstall - clean up database removing plugin stuff * * @since 1.0 */ function wcl_delete_plugin() { /** * unfortunatelly, this has to be a bit painful due to woocommerce delete * function. * * Loop through all categories and remove plugin options * * @var [type] */ if (function_exists('delete_woocommerce_term_meta')) { $terms = get_terms('product_cat'); if (!empty($terms)) { foreach ($terms as $term) { delete_woocommerce_term_meta($term_id, 'wcl_cat_password_protected'); delete_woocommerce_term_meta($term_id, 'wcl_cat_password'); } } } }
/** * When a post is updated and terms recounted (called by _update_post_term_count), clear the ids. * @param int $object_id Object ID. * @param array $terms An array of object terms. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. * @param array $old_tt_ids Old array of term taxonomy IDs. */ function wc_clear_term_product_ids($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) { foreach ($old_tt_ids as $term_id) { delete_woocommerce_term_meta($term_id, 'product_ids'); } foreach ($tt_ids as $term_id) { delete_woocommerce_term_meta($term_id, 'product_ids'); } }
function woo_st_clear_dataset($export_type = '', $data = false) { global $wpdb; if (empty($export_type)) { return false; } // Commence the drop woo_st_update_option('in_progress', $export_type); switch ($export_type) { // WooCommerce case 'product': $post_type = array('product', 'product_variation'); $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => woo_st_post_statuses(), 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product')) { $products = get_posts($args); if (!empty($products)) { foreach ($products as $product) { wp_delete_post($product, true); // Product Category if (taxonomy_exists('product_cat')) { wp_set_object_terms($product, null, 'product_cat'); } // Product Tag if (taxonomy_exists('product_tag')) { wp_set_object_terms($product, null, 'product_tag'); } // Product Brand if (taxonomy_exists('product_brand')) { wp_set_object_terms($product, null, 'product_brand'); } // Product Vendor if (taxonomy_exists('shop_vendor')) { wp_set_object_terms($product, null, 'shop_vendor'); } // Attributes $attributes_sql = "SELECT `attribute_id` as ID, `attribute_name` as name, `attribute_label` as label, `attribute_type` as type FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies`"; $attributes = $wpdb->get_results($attributes_sql); if (!empty($attributes)) { foreach ($attributes as $attribute) { if (taxonomy_exists('pa_' . $attribute->name)) { wp_set_object_terms($product, null, 'pa_' . $attribute->name); } } } } unset($products, $product, $attributes, $attribute); } } break; case 'product_category': $term_taxonomy = 'product_cat'; if (!empty($data)) { foreach ($data as $single_category) { $post_type = 'product'; $args = array('post_type' => $post_type, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => $term_taxonomy, 'field' => 'id', 'terms' => $single_category)), 'numberposts' => -1); $products = get_posts($args); if ($products) { foreach ($products as $product) { wp_delete_post($product, true); } unset($products, $product); } } unset($data, $single_category); } else { $args = array('hide_empty' => false, 'number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product_category')) { $categories = get_terms($term_taxonomy, $args); if (!empty($categories)) { foreach ($categories as $category) { wp_delete_term($category->term_id, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $category->term_id)); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = %d", $category->term_taxonomy_id)); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->prefix . "woocommerce_termmeta` WHERE `woocommerce_term_id` = %d", $category->term_id)); if (function_exists('delete_woocommerce_term_meta')) { delete_woocommerce_term_meta($category->term_id, 'thumbnail_id'); } } unset($categories, $category); } } $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '%s'", $term_taxonomy)); } break; case 'product_tag': $term_taxonomy = 'product_tag'; $args = array('fields' => 'ids', 'hide_empty' => false, 'number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product_tag')) { $tags = get_terms($term_taxonomy, $args); if (!empty($tags)) { foreach ($tags as $tag) { wp_delete_term($tag, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $tag)); } } } break; case 'product_brand': $term_taxonomy = 'product_brand'; $args = array('fields' => 'ids', 'hide_empty' => false, 'number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product_brand')) { $tags = get_terms($term_taxonomy, $args); if ($tags) { foreach ($tags as $tag) { wp_delete_term($tag, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $tag)); } } } break; case 'product_vendor': $term_taxonomy = 'shop_vendor'; $args = array('fields' => 'ids', 'hide_empty' => false); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product_vendor')) { $tags = get_terms($term_taxonomy, $args); if ($tags) { foreach ($tags as $tag) { wp_delete_term($tag, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $tag)); } } } break; case 'product_image': $post_type = array('product', 'product_variation'); $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => 'any', 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('product_image')) { $products = get_posts($args); // Check each Product for images if (!empty($products)) { $upload_dir = wp_upload_dir(); foreach ($products as $product) { $args = array('post_type' => 'attachment', 'post_parent' => $product, 'post_status' => 'inherit', 'post_mime_type' => 'image', 'numberposts' => -1); $images = get_children($args); if (!empty($images)) { foreach ($images as $image) { wp_delete_attachment($image->ID, true); } unset($images, $image); } } } else { // Check for WooCommerce-related images $images_sql = "SELECT `post_id` AS `ID` FROM `" . $wpdb->postmeta . "` WHERE `meta_key` = '_woocommerce_exclude_image' AND `meta_value` = 0"; $images = $wpdb->get_col($images_sql); if (!empty($images)) { foreach ($images as $image) { wp_delete_attachment($image, true); } unset($images, $image); } } } break; case 'order': $post_type = 'shop_order'; $term_taxonomy = 'shop_order_status'; $woocommerce_version = woo_get_woo_version(); if (!empty($data)) { foreach ($data as $single_order) { $args = array('post_type' => $post_type, 'fields' => 'ids', 'numberposts' => -1); // Check if this is a pre-WooCommerce 2.2 instance if (version_compare($woocommerce_version, '2.2', '<')) { $args['tax_query'] = array(array('taxonomy' => $term_taxonomy, 'field' => 'id', 'terms' => $single_order)); } else { $args['status'] = 'any'; } $orders = get_posts($args); if (!empty($orders)) { foreach ($orders as $order) { wp_delete_post($order, true); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `order_id` = %d", $order)); } unset($orders, $order); } } unset($data, $single_order); } else { $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => 'any', 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('order')) { $orders = get_posts($args); if (!empty($orders)) { foreach ($orders as $order) { wp_delete_post($order, true); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `order_id` = %d", $order)); } unset($orders, $order); } } $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "woocommerce_order_items`"); $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "woocommerce_order_itemmeta`"); } break; case 'tax_rate': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "woocommerce_tax_rates`"); $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "woocommerce_tax_rate_locations`"); break; case 'download_permission': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "woocommerce_downloadable_product_permissions`"); break; case 'coupon': $post_type = 'shop_coupon'; $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => woo_st_post_statuses(), 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('coupon')) { $coupons = get_posts($args); if (!empty($coupons)) { foreach ($coupons as $coupon) { wp_delete_post($coupon, true); } unset($coupons, $coupon); } } break; case 'attribute': if (isset($_POST['woo_st_attributes'])) { $attributes_sql = "SELECT `attribute_id` as ID, `attribute_name` as name, `attribute_label` as label, `attribute_type` as type FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies`"; $attributes = $wpdb->get_results($attributes_sql); if ($attributes) { foreach ($attributes as $attribute) { $terms_sql = $wpdb->prepare("SELECT `term_id` FROM `" . $wpdb->prefix . "term_taxonomy` WHERE `taxonomy` = %s", 'pa_' . $attribute->name); $terms = $wpdb->get_results($terms_sql); if ($terms) { foreach ($terms as $term) { wp_delete_term($term->term_id, 'pa_' . $attribute->name); } } $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->prefix . "woocommerce_termmeta` WHERE `meta_key` = 'order_pa_%s'", $attribute->name)); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = %d", $attribute->ID)); } } $wpdb->query("DELETE FROM `" . $wpdb->prefix . "woocommerce_attribute_taxonomies`"); } break; // 3rd Party // 3rd Party case 'credit_card': $post_type = 'offline_payment'; $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => woo_st_post_statuses(), 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('credit_card')) { $credit_cards = get_posts($args); if (!empty($credit_cards)) { foreach ($credit_cards as $credit_card) { wp_delete_post($credit_card, true); } unset($credit_cards, $credit_card); } } break; // WordPress // WordPress case 'post': $post_type = 'post'; $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_status' => woo_st_post_statuses(), 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('post')) { $posts = get_posts($args); if ($posts) { foreach ($posts as $post) { wp_delete_post($post, true); } unset($posts, $post); } } break; case 'post_category': $term_taxonomy = 'category'; $args = array('hide_empty' => false, 'number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('post_category')) { $post_categories = get_terms($term_taxonomy, $args); if ($post_categories) { foreach ($post_categories as $post_category) { wp_delete_term($post_category->term_id, $term_taxonomy); $wpdb->query("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $post_category->term_id); $wpdb->query("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = " . $post_category->term_taxonomy_id); } } } $wpdb->query("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '" . $term_taxonomy . "'"); break; case 'post_tag': $term_taxonomy = 'post_tag'; $args = array('hide_empty' => false, 'number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('post_tag')) { $post_tags = get_terms($term_taxonomy, $args); if ($post_tags) { foreach ($post_tags as $post_tag) { wp_delete_term($post_tag->term_id, $term_taxonomy); $wpdb->query("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $post_tag->term_id); $wpdb->query("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = " . $post_tag->term_taxonomy_id); } } } $wpdb->query("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '" . $term_taxonomy . "'"); break; case 'link': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "links`"); break; case 'comment': $args = array('number' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('comment')) { $comments = get_comments($args); if (!empty($comments)) { foreach ($comments as $comment) { wp_delete_comment($comment->comment_ID, true); } unset($comments, $comment); } } break; case 'media_image': $post_type = 'attachment'; $post_mime_types = array('image/jpg', 'image/jpeg', 'image/jpe', 'image/gif', 'image/png'); $args = array('post_type' => $post_type, 'fields' => 'ids', 'post_mime_type' => $post_mime_types, 'post_status' => woo_st_post_statuses(), 'numberposts' => 100); // Loop through every 100 records until 0 is returned, might take awhile while (woo_st_return_count('media_image')) { $images = get_posts($args); if (!empty($images)) { foreach ($images as $image) { wp_delete_attachment($image, true); } unset($images, $image); } } break; } // Mission accomplished woo_st_update_option('in_progress', ''); }
/** * Change Pending Status -> Table row actions * * @Author Andrea Grillo <*****@*****.**> * @since 1.2 * * @return void * @use admin_action_switch-selling-capability action */ public function switch_pending_status($vendor_id = 0, $direct_call = false) { $check = $direct_call ? $direct_call : !empty($_GET['action']) && 'switch-pending-status' == $_GET['action']; if ($check) { $vendor_id = $direct_call ? $vendor_id : $_GET['vendor_id']; $vendor = yith_get_vendor($vendor_id); if ('yes' == $vendor->pending) { $vendor->enable_selling = 'yes'; delete_woocommerce_term_meta($vendor->id, 'pending'); /* Send Email notification to New vendor */ do_action('yith_vendors_account_approved', $vendor->get_owner()); } if (!$direct_call) { $redirect = remove_query_arg(array('action', 'vendor_id', '_wpnonce')); $paged = isset($_GET['paged']) ? $_GET['paged'] : 1; wp_redirect(esc_url_raw(add_query_arg(array('paged' => $paged), $redirect))); exit; } } }
/** * Save the points earned / maximum discount fields * * @since 1.0 * @param int $term_id term ID being saved */ public function save_product_category_points_field($term_id) { // points earned if (isset($_POST['_wc_points_earned']) && '' !== $_POST['_wc_points_earned']) { update_woocommerce_term_meta($term_id, '_wc_points_earned', $_POST['_wc_points_earned']); } else { delete_woocommerce_term_meta($term_id, '_wc_points_earned'); } // max points discount if (isset($_POST['_wc_points_max_discount']) && '' !== $_POST['_wc_points_max_discount']) { update_woocommerce_term_meta($term_id, '_wc_points_max_discount', $_POST['_wc_points_max_discount']); } else { delete_woocommerce_term_meta($term_id, '_wc_points_max_discount'); } }
public function deleteData($dataId) { delete_woocommerce_term_meta($dataId, $this->getOptionName()); }
function category_field_save($term_id, $tt_id, $taxonomy) { $restrictions_enabled = isset($_POST['_wc_restrictions']) ? $_POST['_wc_restrictions'] : false; if (empty($restrictions_enabled) || $restrictions_enabled == 'no-restriction-setting') { delete_woocommerce_term_meta($term_id, '_wc_restrictions'); delete_woocommerce_term_meta($term_id, '_wc_restrictions_allowed'); } else { update_woocommerce_term_meta($term_id, '_wc_restrictions', $restrictions_enabled); delete_woocommerce_term_meta($term_id, '_wc_restrictions_allowed'); if ($restrictions_enabled == 'restricted') { $restrictions = isset($_POST['wc_restrictions_allowed']) ? $_POST['wc_restrictions_allowed'] : array(''); foreach ($restrictions as $role) { add_woocommerce_term_meta($term_id, '_wc_restrictions_allowed', $role); } } } $restrictions_location_enabled = isset($_POST['_wc_restrictions_location']) ? $_POST['_wc_restrictions_location'] : false; if (empty($restrictions_location_enabled) || $restrictions_location_enabled == 'no-restriction-setting') { delete_woocommerce_term_meta($term_id, '_wc_restrictions_location'); delete_woocommerce_term_meta($term_id, '_wc_restrictions_locations'); } else { update_woocommerce_term_meta($term_id, '_wc_restrictions_location', $restrictions_location_enabled); delete_woocommerce_term_meta($term_id, '_wc_restrictions_locations'); if ($restrictions_location_enabled == 'restricted') { $restrictions = isset($_POST['wc_restrictions_locations']) ? $_POST['wc_restrictions_locations'] : array(''); foreach ($restrictions as $location) { add_woocommerce_term_meta($term_id, '_wc_restrictions_locations', $location); } } } }
function category_field_save($term_id, $tt_id, $taxonomy) { $restrictions_enabled = filter_input(INPUT_POST, '_wc_restrictions', FILTER_SANITIZE_STRIPPED); if ($restrictions_enabled == 'no-restriction-setting') { delete_woocommerce_term_meta($term_id, '_wc_restrictions'); delete_woocommerce_term_meta($term_id, '_wc_restrictions_allowed'); } else { update_woocommerce_term_meta($term_id, '_wc_restrictions', $restrictions_enabled); delete_woocommerce_term_meta($term_id, '_wc_restrictions_allowed'); if ($restrictions_enabled == 'restricted') { $restrictions = isset($_POST['wc_restrictions_allowed']) ? $_POST['wc_restrictions_allowed'] : array(''); foreach ($restrictions as $role) { add_woocommerce_term_meta($term_id, '_wc_restrictions_allowed', $role); } } } $restrictions_location_enabled = filter_input(INPUT_POST, '_wc_restrictions_location', FILTER_SANITIZE_STRIPPED); if ($restrictions_location_enabled == 'no-restriction-setting') { delete_woocommerce_term_meta($term_id, '_wc_restrictions_location'); delete_woocommerce_term_meta($term_id, '_wc_restrictions_locations'); } else { update_woocommerce_term_meta($term_id, '_wc_restrictions_location', $restrictions_location_enabled); delete_woocommerce_term_meta($term_id, '_wc_restrictions_locations'); if ($restrictions_location_enabled == 'restricted') { $restrictions = isset($_POST['wc_restrictions_locations']) ? $_POST['wc_restrictions_locations'] : array(''); foreach ($restrictions as $location) { add_woocommerce_term_meta($term_id, '_wc_restrictions_locations', $location); } } } }
/** * Update term meta fields. * * @param WP_Term $term * @param WP_REST_Request $request * @return bool|WP_Error */ protected function update_term_meta_fields($term, $request) { $id = (int) $term->term_id; if (isset($request['display'])) { update_woocommerce_term_meta($id, 'display_type', 'default' === $request['display'] ? '' : $request['display']); } if (isset($request['menu_order'])) { update_woocommerce_term_meta($id, 'order', $request['menu_order']); } if (isset($request['image'])) { if (empty($request['image']['id']) && !empty($request['image']['src'])) { $upload = wc_rest_upload_image_from_url(esc_url_raw($request['image']['src'])); if (is_wp_error($upload)) { return $upload; } $image_id = wc_rest_set_uploaded_image_as_attachment($upload); } else { $image_id = isset($request['image']['id']) ? absint($request['image']['id']) : 0; } // Check if image_id is a valid image attachment before updating the term meta. if ($image_id && wp_attachment_is_image($image_id)) { update_woocommerce_term_meta($id, 'thumbnail_id', $image_id); // Set the image alt. if (!empty($request['image']['alt'])) { update_post_meta($image_id, '_wp_attachment_image_alt', wc_clean($request['image']['alt'])); } // Set the image title. if (!empty($request['image']['title'])) { wp_update_post(array('ID' => $image_id, 'post_title' => wc_clean($request['image']['title']))); } } else { delete_woocommerce_term_meta($id, 'thumbnail_id'); } } return true; }
protected function associate_terms($pid, $assign_taxes, $tx_name, $logger = false) { $terms = wp_get_object_terms($pid, $tx_name); $term_ids = array(); $assign_taxes = is_array($assign_taxes) ? array_filter($assign_taxes) : false; if (!empty($terms)) { if (!is_wp_error($terms)) { foreach ($terms as $term_info) { $term_ids[] = $term_info->term_taxonomy_id; delete_woocommerce_term_meta($term_info->term_taxonomy_id, 'product_ids'); $this->wpdb->query($this->wpdb->prepare("UPDATE {$this->wpdb->term_taxonomy} SET count = count - 1 WHERE term_taxonomy_id = %d", $term_info->term_taxonomy_id)); } $in_tt_ids = "'" . implode("', '", $term_ids) . "'"; $this->wpdb->query($this->wpdb->prepare("DELETE FROM {$this->wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_tt_ids})", $pid)); delete_transient('wc_ln_count_' . md5(sanitize_key($tx_name) . sanitize_key($term_info->term_taxonomy_id))); clean_term_cache($term_ids, '', false); } } if (empty($assign_taxes)) { return; } $values = array(); $term_order = 0; foreach ($assign_taxes as $tt) { $values[] = $this->wpdb->prepare("(%d, %d, %d)", $pid, $tt, ++$term_order); $this->wpdb->query("UPDATE {$this->wpdb->term_taxonomy} SET count = count + 1 WHERE term_taxonomy_id = {$tt}"); delete_transient('wc_ln_count_' . md5(sanitize_key($tx_name) . sanitize_key($tt))); delete_woocommerce_term_meta($tt, 'product_ids'); } if ($values) { if (false === $this->wpdb->query("INSERT INTO {$this->wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)")) { $logger and call_user_func($logger, __('<b>ERROR</b> Could not insert term relationship into the database', 'wpai_woocommerce_addon_plugin') . ': ' . $this->wpdb->last_error); } } wp_cache_delete($pid, $tx_name . '_relationships'); }
/** * Delete review qualifier options after a term has been deleted * * @since 1.0.0 * @param int $term_id */ public function delete_qualifier_options($term_id) { delete_woocommerce_term_meta($term_id, 'options'); }
public function save_brand_fields($term_id, $tt_id, $taxonomy) { if (!empty($_POST['dhvc_woo_brand_page_id'])) { update_woocommerce_term_meta($term_id, 'dhvc_woo_brand_page_id', absint($_POST['dhvc_woo_brand_page_id'])); } else { delete_woocommerce_term_meta($term_id, 'dhvc_woo_brand_page_id'); } }