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', ''); }
function woo_st_tab_template($tab = '') { if (!$tab) { $tab = 'overview'; } switch ($tab) { case 'nuke': // Check if a previous nuke failed mid-drop $in_progress = woo_st_get_option('in_progress', ''); if (!empty($in_progress)) { $message = sprintf(__('It looks like a previous nuke failed to clear that dataset, this is common in large catalogues and is likely due to WordPress hitting a memory limit or server timeout. Don\'t stress, <a href="%s">retry %s nuke?</a>', 'woo_st'), add_query_arg(array('action' => 'nuke', 'dataset' => $in_progress)), ucfirst($in_progress)); woo_st_admin_notice_html($message, 'error'); woo_st_update_option('in_progress', ''); } $products = woo_st_return_count('product'); $images = woo_st_return_count('product_image'); $tags = woo_st_return_count('product_tag'); $categories = woo_st_return_count('product_category'); if ($categories) { $term_taxonomy = 'product_cat'; $args = array('hide_empty' => 0); $categories_data = get_terms($term_taxonomy, $args); } $orders = woo_st_return_count('order'); if ($orders) { // Check if this is a WooCommerce 2.2+ instance $woocommerce_version = woo_get_woo_version(); $orders_data = false; if (version_compare($woocommerce_version, '2.2', '<')) { $term_taxonomy = 'shop_order_status'; $args = array('hide_empty' => 0); $orders_data = get_terms($term_taxonomy, $args); } } $tax_rates = woo_st_return_count('tax_rate'); $download_permissions = woo_st_return_count('download_permission'); $coupons = woo_st_return_count('coupon'); $attributes = woo_st_return_count('attribute'); $brands = woo_st_return_count('product_brand'); $vendors = woo_st_return_count('product_vendor'); $credit_cards = woo_st_return_count('credit_card'); $posts = woo_st_return_count('post'); $post_categories = woo_st_return_count('post_category'); $post_tags = woo_st_return_count('post_tag'); $links = woo_st_return_count('link'); $comments = woo_st_return_count('comment'); $media_images = woo_st_return_count('media_image'); $show_table = false; if ($products || $images || $tags || $categories || $orders || $credit_cards || $attributes) { $show_table = true; } break; } if ($tab) { if (file_exists(WOO_ST_PATH . 'templates/admin/tabs-' . $tab . '.php')) { include_once WOO_ST_PATH . 'templates/admin/tabs-' . $tab . '.php'; } else { $message = sprintf(__('We couldn\'t load the export template file <code>%s</code> within <code>%s</code>, this file should be present.', 'woo_st'), 'tabs-' . $tab . '.php', WOO_CD_PATH . 'templates/admin/...'); woo_st_admin_notice_html($message, 'error'); ob_start(); ?> <p><?php _e('You can see this error for one of a few common reasons', 'woo_st'); ?> :</p> <ul class="ul-disc"> <li><?php _e('WordPress was unable to create this file when the Plugin was installed or updated', 'woo_st'); ?> </li> <li><?php _e('The Plugin files have been recently changed and there has been a file conflict', 'woo_st'); ?> </li> <li><?php _e('The Plugin file has been locked and cannot be opened by WordPress', 'woo_st'); ?> </li> </ul> <p><?php _e('Jump onto our website and download a fresh copy of this Plugin as it might be enough to fix this issue. If this persists get in touch with us.', 'woo_st'); ?> </p> <?php ob_end_flush(); } } }