function wpsc_st_clear_dataset($dataset, $data = null) { global $wpdb; switch ($dataset) { // WP e-Commerce case 'products': $post_type = 'wpsc-product'; $products = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($products) { foreach ($products as $product) { if ($product->ID) { wp_delete_post($product->ID, true); } } } break; case 'variations': // Products $post_type = 'wpsc-product'; $variations_sql = "SELECT `ID` FROM `" . $wpdb->posts . "` WHERE `post_type` = '" . $post_type . "' AND `post_parent` <> 0"; $variations = $wpdb->get_results($variations_sql); if ($variations) { foreach ($variations as $variation) { if ($variation->ID) { wp_delete_post($variation->ID, true); } } } // Terms $term_taxonomy = 'wpsc-variation'; $variations = get_terms($term_taxonomy, array('hide_empty' => false)); if ($variations) { foreach ($variations as $variation) { if ($variation->term_id) { wp_delete_term($variation->term_id, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $variation->term_id)); } } } delete_option('wpsc-variation_children'); break; case 'categories': $term_taxonomy = 'wpsc_product_category'; if ($data) { foreach ($data as $single_category) { $post_type = 'wpsc-product'; $args = array('post_type' => $post_type, 'tax_query' => array(array('taxonomy' => $term_taxonomy, 'field' => 'id', 'terms' => $single_category)), 'numberposts' => -1); $products = get_posts($args); if ($products) { foreach ($products as $product) { if ($product->ID) { wp_delete_post($product->ID, true); } } } } } else { $categories = get_terms($term_taxonomy, array('hide_empty' => false)); if ($categories) { foreach ($categories as $category) { if ($category->term_id) { wp_delete_term($category->term_id, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $category->term_id)); } if ($category->term_taxonomy_id) { $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = %d", $category->term_taxonomy_id)); } } } $wpdb->query("DELETE FROM `" . $wpdb->prefix . "wpsc_meta` WHERE `object_type` = 'wpsc_category'"); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '%s'", $term_taxonomy)); } break; case 'tags': $term_taxonomy = 'product_tag'; $tags = get_terms($term_taxonomy, array('hide_empty' => false)); if ($tags) { foreach ($tags as $tag) { if ($tag->term_id) { wp_delete_term($tag->term_id, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $tag->term_id)); } } } break; case 'images': $post_type = 'wpsc-product'; $products = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($products) { $upload_dir = wp_upload_dir(); foreach ($products as $product) { $args = array('post_type' => 'attachment', 'post_parent' => $product->ID, 'post_status' => 'inherit', 'post_mime_type' => 'image', 'numberposts' => -1); $images = get_children($args); if ($images) { // $intermediate_sizes = wpsc_intermediate_image_sizes_advanced( $intermediate_sizes ); foreach ($images as $image) { wp_delete_attachment($image->ID, true); /* $image->filepath = dirname( $upload_dir['basedir'] . '/' . get_post_meta( $image->ID, '_wp_attached_file', true ) ); chdir( $image->filepath ); $image->filename = basename( get_post_meta( $image->ID, '_wp_attached_file', true ) ); $image->extension = strrchr( $image->filename, '.' ); $image->filebase = wpsc_st_remove_filename_extension( $image->filename ); foreach( $intermediate_sizes as $intermediate_size ) { if( file_exists( $image->filebase . '-' . $intermediate_size['width'] . 'x' . $intermediate_size['height'] . $image->extension ) ) @unlink( $image->filebase . '-' . $intermediate_size['width'] . 'x' . $intermediate_size['height'] . $image->extension ); } if( file_exists( $image->filename ) ) @unlink( basename( $image->filename ) ); wp_delete_post( $image->ID ); */ } unset($images, $image); } } } break; case 'files': $post_type = 'wpsc-product-file'; $files = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($files) { foreach ($files as $file) { if ($file->ID) { wp_delete_post($file->ID, true); } } } break; case 'orders': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_purchase_logs`"); $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_cart_contents`"); $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_submited_form_data`"); $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_download_status`"); $wpdb->query("DELETE FROM `" . $wpdb->prefix . "wpsc_meta` WHERE `object_type` = 'wpsc_cart_item'"); break; case 'coupons': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_coupon_codes`"); break; case 'wpsc_pages': $wpsc_pages = array('[productspage]', '[shoppingcart]', '[transactionresults]', '[userlog]', '[download-manager]', '[order-tracking]'); $size = count($wpsc_pages); for ($i = 0; $i < $sizes; $i++) { if ($wpsc_pages[$i]) { $post_id = wpsc_st_get_page_by_shortcode($wpsc_pages[$i]); if ($post_id) { wp_delete_post($wishlist->ID, true); } } } break; case 'wpsc_options': $options = array(); $wpec_options_sql = "SELECT `option_name` FROM `" . $wpdb->prefix . "options` WHERE `option_name` LIKE 'wpec_%'"; $wpec_options = $wpdb->get_results($wpec_options_sql); if ($wpec_options) { foreach ($wpec_options as $wpec_option) { $options[] = $wpec_option; } // $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "options` WHERE `option_name` LIKE 'wpec_%'" ); } $wpsc_options_sql = "SELECT `option_name` FROM `" . $wpdb->prefix . "options` WHERE `option_name` LIKE 'wpsc_%'"; $wpsc_options = $wpdb->get_results($wpsc_options_sql); if ($wpsc_options) { foreach ($wpsc_options as $wpsc_option) { $options[] = $wpsc_option; } // $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "options` WHERE `option_name` LIKE 'wpsc_%'" ); } break; // 3rd Party // 3rd Party case 'wishlist': $post_type = 'wpsc-wishlist'; $wishlists = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($wishlists) { foreach ($wishlists as $wishlist) { if (isset($wishlist->ID)) { wp_delete_post($wishlist->ID, true); } } } break; case 'enquiries': $post_type = 'wpsc-enquiry'; $enquiries = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($enquiries) { foreach ($enquiries as $enquiry) { if (isset($enquiry->ID)) { wp_delete_post($enquiry->ID, true); } } } break; case 'credit-cards': $post_type = 'offline_payment'; $credit_cards = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($credit_cards) { foreach ($credit_cards as $credit_card) { if (isset($credit_card->ID)) { wp_delete_post($credit_card->ID, true); } } } break; case 'custom-fields': delete_option('wpsc_cf_data'); break; case 'preview-files': $post_type = 'wpsc-preview-file'; $preview_files = (array) get_posts(array('post_type' => $post_type, 'post_status' => 'inherit', 'numberposts' => -1)); if ($preview_files) { foreach ($preview_files as $preview_file) { if (isset($preview_file->ID)) { wp_delete_post($preview_file->ID, true); } } } break; // WordPress // WordPress case 'posts': $post_type = 'post'; $posts = (array) get_posts(array('post_type' => $post_type, 'post_status' => wpsc_st_post_statuses(), 'numberposts' => -1)); if ($posts) { foreach ($posts as $post) { if (isset($post->ID)) { wp_delete_post($post->ID, true); } } } break; case 'post_categories': $term_taxonomy = 'category'; $post_categories = get_terms($term_taxonomy, array('hide_empty' => false)); if ($post_categories) { foreach ($post_categories as $post_category) { if ($post_category->term_id) { wp_delete_term($post_category->term_id, $term_taxonomy); $wpdb->query("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = " . $post_category->term_id); } if ($post_category->term_taxonomy_id) { $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = %d", $post_category->term_taxonomy_id)); } } } $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '%s'", $term_taxonomy)); break; case 'post_tags': $term_taxonomy = 'post_tag'; $post_tags = get_terms($term_taxonomy, array('hide_empty' => false)); if ($post_tags) { foreach ($post_tags as $post_tag) { if ($post_tag->term_id) { wp_delete_term($post_tag->term_id, $term_taxonomy); $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->terms . "` WHERE `term_id` = %d", $post_tag->term_id)); } if ($post_tag->term_taxonomy_id) { $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_relationships . "` WHERE `term_taxonomy_id` = %d", $post_tag->term_taxonomy_id)); } } } $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->term_taxonomy . "` WHERE `taxonomy` = '%s'", $term_taxonomy)); break; case 'links': $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "links`"); break; case 'comments': $comments = get_comments(); if ($comments) { foreach ($comments as $comment) { if ($comment->comment_ID) { wp_delete_comment($comment->comment_ID, true); } } } break; } }
function wpsc_st_admin_init() { global $wpdb, $wpsc_st; $action = wpsc_get_action(); if (!$action && isset($_POST['wpsc_admin_action'])) { $action = $_POST['wpsc_admin_action']; } if (!$action && isset($_GET['wpsc_admin_action'])) { $action = $_GET['wpsc_admin_action']; } switch ($action) { case 'tools': if (isset($_POST['maximum_cart_quantity'])) { $maximum_cart_quantity = $_POST['maximum_cart_quantity']; wpsc_st_update_option('maximum_cart_quantity', $maximum_cart_quantity); $message = __('Settings saved.', 'wpsc_st'); wpsc_st_admin_notice($message); } break; case 'relink-pages': $product_page = wpsc_st_get_page_by_shortcode('[productspage]'); $checkout = wpsc_st_get_page_by_shortcode('[shoppingcart]'); $transaction_results = wpsc_st_get_page_by_shortcode('[transactionresults]'); $my_account = wpsc_st_get_page_by_shortcode('[userlog]'); if ($product_page) { update_option('product_list_url', get_bloginfo('url') . "/?page_id=" . $product_page); } if ($checkout) { update_option('shopping_cart_url', get_bloginfo('url') . "/?page_id=" . $checkout); } if ($checkout) { update_option('checkout_url', get_bloginfo('url') . "/?page_id=" . $checkout); } if ($transaction_results) { update_option('transact_url', get_bloginfo('url') . "/?page_id=" . $transaction_results); } if ($my_account) { update_option('user_account_url', get_bloginfo('url') . "/?page_id=" . $my_account); } $message = __('Default WP e-Commerce Pages have been restored.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'relink-existing-preregistered-sales': $sales = wpsc_st_get_unlinked_sales(); $size = count($sales); if ($sales) { $adjusted_sales = 0; foreach ($sales as $sale) { $sale_email = wpsc_st_get_email_from_sale($sale->ID); if ($sale_email) { $sale_user = get_user_by('email', $sale_email); if ($sale_user) { $wpdb->update($wpdb->prefix . 'wpsc_purchase_logs', array('user_ID' => $sale_user->ID), array('id' => $sale->ID)); } $adjusted_sales++; } } } $notice_status = ''; if ($adjusted_sales > 0) { $message = sprintf(__('%s of %s unlinked Sale\'s from pre-registered Users have been re-linked.', 'wpsc_st'), '<strong>' . $adjusted_sales . '</strong>', '<strong>' . $size . '</strong>'); } else { $message = __('No existing Sales from pre-registered Users have been re-linked.', 'wpsc_st'); $notice_status = 'error'; } wpsc_st_admin_notice($message, $notice_status); break; case 'reset-file-downloads-sale': if (version_compare(wpsc_get_minor_version(), '3.8.8', '>=')) { $purchase_id = (int) $_GET['id']; } else { $purchase_id = (int) $_GET['purchaselog_id']; } $file_downloads_sql = $wpdb->prepare("SELECT `id`, `downloads` FROM `" . $wpdb->prefix . "wpsc_download_status` WHERE `purchid` = %d", $purchase_id); $file_downloads = $wpdb->get_results($file_downloads_sql); $notice_status = ''; if ($file_downloads) { $max_downloads = get_option('wpsc_max_downloads'); if ($max_downloads) { foreach ($file_downloads as $file_download) { if ($file_download->downloads < $max_downloads) { $wpdb->update($wpdb->prefix . 'wpsc_download_status', array('downloads' => $max_downloads), array('id' => $file_download->id)); } } $message = __('File Downloads have been reset for this Sale.', 'wpsc_st'); } else { $message = __('Max downloads per file has not been set - or is set to 0 - within Settings > Store > Admin screen.', 'wpsc_st'); $notice_status = 'error'; } } else { $message = __('No File Downloads are assigned to this Sale.', 'wpsc_st'); $notice_status = 'error'; } wpsc_st_admin_notice($message, $notice_status); break; case 'demo': $options = false; if (isset($_POST['options'])) { $options = $_POST['options']; if (isset($options['demo_store'])) { $options['demo_store'] = 1; } else { $options['demo_store'] = 0; } if ($options) { foreach ($options as $key => $option) { wpsc_st_update_option($key, $option); } } } $message = __('Settings saved.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'nuke': if (!ini_get('safe_mode')) { set_time_limit(0); } // WP e-Commerce if (isset($_POST['wpsc_st_products'])) { wpsc_st_clear_dataset('products'); } if (isset($_POST['wpsc_st_product_variations'])) { wpsc_st_clear_dataset('variations'); } if (isset($_POST['wpsc_st_product_tags'])) { wpsc_st_clear_dataset('tags'); } if (isset($_POST['wpsc_st_categories'])) { $categories = $_POST['wpsc_st_categories']; wpsc_st_clear_dataset('categories', $categories); } else { if (isset($_POST['wpsc_st_product_categories'])) { wpsc_st_clear_dataset('categories'); } } if (isset($_POST['wpsc_st_product_images'])) { wpsc_st_clear_dataset('images'); } if (isset($_POST['wpsc_st_product_files'])) { wpsc_st_clear_dataset('files'); } if (isset($_POST['wpsc_st_sales_orders'])) { wpsc_st_clear_dataset('orders'); } if (isset($_POST['wpsc_st_coupons'])) { wpsc_st_clear_dataset('coupons'); } // 3rd Party if (isset($_POST['wpsc_st_wishlist'])) { wpsc_st_clear_dataset('wishlist'); } if (isset($_POST['wpsc_st_enquiries'])) { wpsc_st_clear_dataset('enquiries'); } if (isset($_POST['wpsc_st_creditcards'])) { wpsc_st_clear_dataset('credit-cards'); } if (isset($_POST['wpsc_st_customfields'])) { wpsc_st_clear_dataset('custom-fields'); } if (isset($_POST['wpsc_st_previewfiles'])) { wpsc_st_clear_dataset('preview-files'); } // WordPress if (isset($_POST['wpsc_st_posts'])) { wpsc_st_clear_dataset('posts'); } if (isset($_POST['wpsc_st_post_categories'])) { wpsc_st_clear_dataset('post_categories'); } if (isset($_POST['wpsc_st_post_tags'])) { wpsc_st_clear_dataset('post_tags'); } if (isset($_POST['wpsc_st_links'])) { wpsc_st_clear_dataset('links'); } if (isset($_POST['wpsc_st_comments'])) { wpsc_st_clear_dataset('comments'); } $message = __('Chosen WP e-Commerce details have been permanently erased from your store.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'uninstall': global $uninstall; $uninstall = new stdClass(); $uninstall->log = ''; $uninstall->log .= "<br />" . __('Removing WP e-Commerce tables...', 'wpsc_st'); $wpsc_tables = array(); $wpsc_tables[] = array('label' => __('Also Bought', 'wpsc_st'), 'table' => 'wpsc_also_bought'); $wpsc_tables[] = array('label' => __('Cart Contents', 'wpsc_st'), 'table' => 'wpsc_cart_contents'); $wpsc_tables[] = array('label' => __('Checkout Forms', 'wpsc_st'), 'table' => 'wpsc_checkout_forms'); $wpsc_tables[] = array('label' => __('Claimed Stock', 'wpsc_st'), 'table' => 'wpsc_claimed_stock'); $wpsc_tables[] = array('label' => __('Coupons', 'wpsc_st'), 'table' => 'wpsc_coupon_codes'); $wpsc_tables[] = array('label' => __('Currency List', 'wpsc_st'), 'table' => 'wpsc_currency_list'); $wpsc_tables[] = array('label' => __('Download Status', 'wpsc_st'), 'table' => 'wpsc_download_status'); $wpsc_tables[] = array('label' => __('Meta', 'wpsc_st'), 'table' => 'wpsc_meta'); $wpsc_tables[] = array('label' => __('Product Rating', 'wpsc_st'), 'table' => 'wpsc_product_rating'); $wpsc_tables[] = array('label' => __('Purchase Logs', 'wpsc_st'), 'table' => 'wpsc_purchase_logs'); $wpsc_tables[] = array('label' => __('Tax Regions', 'wpsc_st'), 'table' => 'wpsc_region_tax'); $wpsc_tables[] = array('label' => __('Submited Form Data', 'wpsc_st'), 'table' => 'wpsc_submited_form_data'); $size = count($wpsc_tables); for ($i = 0; $i < $size; $i++) { // Check that the WP e-Commerce table exists if (wpsc_st_check_table_exists($wpsc_tables[$i]['table'])) { // $wpdb->query( "DROP TABLE `" . $wpdb->prefix . $wpsc_tables[$i]['table'] . "`" ); $uninstall->log .= "<br />>>> " . sprintf(__('Removed %s (%s)', 'wpsc_st'), $wpsc_tables[$i]['label'], $wpdb->prefix . $wpsc_tables[$i]['table']); } } $uninstall->log .= "<br /><br />" . __('Removing WP e-Commerce details...', 'wpsc_st'); $wpsc_dataset = array(); $wpsc_dataset[] = array('label' => __('Products', 'wpsc_st'), 'dataset' => 'products'); $wpsc_dataset[] = array('label' => __('Product Variations', 'wpsc_st'), 'dataset' => 'variations'); $wpsc_dataset[] = array('label' => __('Product Tags', 'wpsc_st'), 'dataset' => 'tags'); $wpsc_dataset[] = array('label' => __('Product Categories', 'wpsc_st'), 'dataset' => 'categories'); $wpsc_dataset[] = array('label' => __('Product Images', 'wpsc_st'), 'dataset' => 'images'); $wpsc_dataset[] = array('label' => __('Product Files', 'wpsc_st'), 'dataset' => 'files'); $wpsc_dataset[] = array('label' => __('Sales', 'wpsc_st'), 'dataset' => 'orders'); $wpsc_dataset[] = array('label' => __('Coupons', 'wpsc_st'), 'dataset' => 'coupons'); $wpsc_dataset[] = array('label' => __('Add to Wishlist', 'wpsc_st'), 'dataset' => 'wishlist'); $wpsc_dataset[] = array('label' => __('Enquiries', 'wpsc_st'), 'dataset' => 'enquiries'); $wpsc_dataset[] = array('label' => __('Offline Payments', 'wpsc_st'), 'dataset' => 'credit-cards'); $wpsc_dataset[] = array('label' => __('Custom Fields', 'wpsc_st'), 'dataset' => 'custom-fields'); $wpsc_dataset[] = array('label' => __('Preview Files', 'wpsc_st'), 'dataset' => 'preview-files'); $wpsc_dataset[] = array('label' => __('Plugin Pages', 'wpsc_st'), 'dataset' => 'wpsc_pages'); $wpsc_dataset[] = array('label' => __('Plugin Options', 'wpsc_st'), 'dataset' => 'wpsc_options'); $size = count($wpsc_dataset); for ($i = 0; $i < $size; $i++) { if ($wpsc_dataset[$i]['dataset']) { // wpsc_st_clear_dataset( $wpsc_dataset[$i] ); $uninstall->log .= "<br />>>> " . sprintf(__('Removed %s', 'wpsc_st'), $wpsc_dataset[$i]['label']); } } break; case 'wpsc_update_session_id': global $wpdb; $session_id = $_POST['session_id']; $purchase_id = $_POST['purchase_id']; if (isset($session_id) && $purchase_id) { $wpdb->update($wpdb->prefix . 'wpsc_purchase_logs', array('sessionid' => $session_id), array('id' => $purchase_id)); } break; case 'update': $options = false; if (isset($_POST['options'])) { $options = $_POST['options']; } if ($options) { foreach ($options as $key => $option) { wpsc_st_update_option($key, $option); } } $message = __('Settings saved.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'uninstall': $message = __('Uninstall complete.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'clear-claimed_stock': global $wpdb; $wpdb->query("TRUNCATE TABLE `" . $wpdb->prefix . "wpsc_claimed_stock`"); $message = __('The \'claimed stock\' table has been emptied.', 'wpsc_st'); wpsc_st_admin_notice($message); break; case 'fix-wpsc_version': $notice_status = ''; if (wpsc_get_major_version() == '3.8' && WPSC_VERSION == '3.7') { update_option('wpsc_version', '3.7'); $message = __('WordPress option \'wpsc_version\' has been repaired.', 'wpsc_st'); } else { $notice_status = 'error'; $message = __('WordPress option \'wpsc_version\' did not require attention.', 'wpsc_st'); } wpsc_st_admin_notice($message, $notice_status); break; } }