Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created January 3, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/6578ea7ec427fa1287a2ddb904754a5a to your computer and use it in GitHub Desktop.
Save panoslyrakis/6578ea7ec427fa1287a2ddb904754a5a to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Complete removal of MarketPress
Plugin URI: https://premium.wpmudev.org/
Description: Removes all products orders and options of MarketPress from your WordPress installation. We strongly recommend to keep a backup before using this plugin. In order to start removing MarketPress options please click here <a >Remove MarketPress options</a>
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
/**
* Complete removal of MarketPress
*
* This will remove all
* MarketPress Tables from db
* MarketPress settings from options table
* MarketPress Products, Orders and Terms from db
*
* Therefore it is recommended to keep a backup before using this file.
*/
if( !class_exists( 'WPMUDEV_RM_MP' ) ){
class WPMUDEV_RM_MP{
private static $_instance = null;
public static $post_type = null;
public static $single_install_options;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_RM_MP();
}
return self::$_instance;
}
private function __construct() {
//add_menu_page( 'Remove MarketPress completely', 'Remove MarketPress', 'manage_options', 'wpmudev-rm-mp', array( $this, 'admin_page' ) );
add_submenu_page('tools.php', 'Remove MarketPress completely', 'Remove MarketPress', 'manage_options', 'wpmudev-rm-mp', array( $this, 'admin_page' ) );
add_action( 'wp_ajax_wpmudev_rm_mp_options', array( $this, 'exec_delete' ) );
}
public function admin_page(){
?>
<form>
<?php $this->show_deletion_options( true ); ?>
</form>
<?php
}
public function show_deletion_options( $print = false ){
$out = '';
wp_nonce_field( 'wpmudev_rm_mp', 'rm_mp_nonce' );
ob_start();
?>
<div class="wrap">
<h1>
Delete MarketPres options
</h1>
<h3>
Select which from the following options you would like to remove
</h3>
<div class="wpmudev-removal-options">
<p>
<label>
<input type="checkbox" class="mp-rm-option" id="mp_rm_options" value="options" checked="checked" />
Delete settings from options table
</label>
</p>
<p>
<label>
<input type="checkbox" class="mp-rm-option" id="mp_rm_tables" value="tables" checked="checked" />
Delete db tables
</label>
</p>
<p>
<label>
<input type="checkbox" class="mp-rm-option" id="mp_rm_terms" value="terms" checked="checked" />
Delete terms
</label>
</p>
<p>
<label>
<input type="checkbox" class="mp-rm-option" id="mp_rm_products" value="products" checked="checked" />
Delete all products
</label>
</p>
<p>
<label>
<input type="checkbox" class="mp-rm-option" id="mp_rm_orders" value="orders" checked="checked" />
Delete all orders
</label>
</p>
<p>
<label>
<input type="checkbox" value="pages" id="mp_rm_pages" />
Delete marketpress pages
</label>
</p>
<p>
<label>
Product post type: <input type="text" value="product" id="mp_post_type" />
</label>
</p>
<p>
<label>
Variation post type: <input type="text" value="mp_product_variation" id="mp_variation_type" />
</label>
</p>
<p>
<button class="button button-primary" id="mp-remover">
Delete
</button>
</p>
</div>
<div id="wpmudev_mp_rm_report"></div>
</div>
<script type="text/javascript">
(function($){
$( document ).ready(function(){
$( '#mp-remover' ).on('click', function( e ){
e.preventDefault();
var checked_options_arr = $('.mp-rm-option:checkbox:checked').map(function() {
return this.value;
}).get();
//var checked_options = ( checked_options_arr.join(",") );
//if( checked_options_arr.filter(function(item){ return item == 'pages' } ).length ){
//1st check for pages. If settings have already been deleted we can't find which pages to delete
if( $( '#mp_rm_pages' ).is(' :checked' ) ) wpmudev_mp_rm_option( 'pages' );
//Then continue deleleting the rest
for( var i=0; i < checked_options_arr.length; i++ ){
wpmudev_mp_rm_option( checked_options_arr[i] )
}
});
});
function wpmudev_mp_rm_option( option ){
//alert( 'option: ' + option ) ; return;
var data = {
action: 'wpmudev_rm_mp_options',
security: $( '#rm_mp_nonce' ).val(),
option: option,
post_type: $('#mp_post_type').val(),
variation_type: $( '#mp_variation_type' ).val()
};
$.ajax({
url:ajaxurl,
type:"POST",
data: data,
dataType: "json",
success: function( report ){
console.log( report );
if( report.success ){
var data = report.data;
var title = '<strong class="report-title">' + data.title + '</strong>';
var msg = '<div class="report-content">' + data.msg + '</div>';
$( '#wpmudev_mp_rm_report' ).append( '<div class="report-'+ data.option +'">' + title + msg + '</div>' );
}
}
});
}
})(jQuery)
</script>
<?php
$out .= ob_get_clean();
if( $print ) echo $out;
else return $out;
}
public function exec_delete(){
check_ajax_referer( 'wpmudev_rm_mp', 'security' );
$product_type = isset( $_POST['post_type'] ) && $_POST['post_type'] != '' ? sanitize_text_field( $_POST['post_type'] ) : 'product';
$variation_type = isset( $_POST['variation_type'] ) && $_POST['variation_type'] != '' ? sanitize_text_field( $_POST['variation_type'] ) : 'mp_product_variation';
$types = array(
'product' => $product_type,
'variation' => $variation_type
);
$option = isset( $_POST['option'] ) && $_POST['option'] != '' ? sanitize_text_field( $_POST['option'] ) : '';
$resp = array(
'option' => $option,
'msg' => ''
);
switch( $option ){
case 'pages': $resp['title'] = 'Deleted Pages'; $resp['msg'] = $this->deletePages(); break;
case 'options': $resp['title'] = 'Deleted Settings'; $resp['msg'] = $this->deleteOptions(); break;
case 'tables': $resp['title'] = 'Deleted Tables'; $resp['msg'] = $this->deleteTables(); break;
case 'terms': $resp['title'] = 'Deleted Terms'; $resp['msg'] = $this->deleteTerms(); break;
case 'products': $resp['title'] = 'Deleted Products'; $resp['msg'] = $this->deleteProducts( $types ); break;
case 'orders': $resp['title'] = 'Deleted Orders'; $resp['msg'] = $this->deleteOrders(); break;
}
do_action( 'wpmudev_rm_mp/after_exec_delete', $option, $product_type );
wp_send_json_success($resp);
wp_die();
}
public function deletePages(){
$mp_settings = get_option( 'mp_settings' );
$report = '';
if( isset( $mp_settings['pages'] ) ){
foreach( $mp_settings['pages'] as $page_id ){
wp_delete_post( $page_id, true );
$report .= '<p>Page ' . $page_id . ' deleted succesfully</p>';
}
}
return $report;
}
public function deleteOptions(){
$report = '';
foreach( $this->get_single_options() as $option ){
delete_option( $option );
$report .= "<p>Option {$option} deleted succesfully</p>";
}
if( is_multisite() ){
$sites = get_sites();
foreach( $sites as $site ){
switch_to_blog( $site->blog_id );
foreach( $this->get_single_options() as $option ){
delete_option( $option );
$report .= "<p>Option {$option} deleted succesfully from site_id: {$site->blog_id}</p>";
}
restore_current_blog();
}
foreach( $this->get_mu_options() as $option ){
delete_site_option( $option );
$report .= "<p>Global option {$option} deleted succesfully</p>";
}
}
return $report;
}
public function deleteTables(){
global $wpdb;
$report = '';
foreach ( $this->get_single_tables() as $table ) {
$table = $wpdb->prefix . $table;
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
$report .= "<p>Table {$table} deleted succesfully</p>";
}
if( is_multisite() ){
foreach ( $this->get_mu_tables() as $table ) {
$table = $wpdb->base_prefix . $table;
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
$report .= "<p>Global table {$table} deleted succesfully</p>";
}
$sites = get_sites();
foreach( $sites as $site ){
switch_to_blog( $site->blog_id );
foreach ( $this->get_single_tables() as $table ) {
$table = $wpdb->prefix . $table;
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
$report .= "<p>Table {$table} deleted succesfully from site_id: {$site->blog_id}</p>";
}
restore_current_blog();
}
}
return $report;
}
public function deleteTerms(){
//Taxonomy set staticaly: product_category
$report = '';
$terms = get_terms('product_category');
foreach ($terms as $term) {
wp_delete_term( $term->ID, 'product_category' );
$report .= "<p>Product category {$term->name} deleted succesfully</p>";
}
if( is_multisite() ){
$sites = get_sites();
foreach( $sites as $site ){
switch_to_blog( $site->blog_id );
$terms = get_terms('product_category');
foreach ($terms as $term) {
wp_delete_term( $term->ID, 'product_category' );
$report .= "<p>Product category {$term->name} deleted succesfully from site_id: {$site->blog_id}</p>";
}
restore_current_blog();
}
}
return $report;
}
public function deleteProducts( $types = false ){
if( ! $types || ! is_array( $types ) ) return "<p>Can't delete products</p>";
$report = '';
if( !is_multisite() ){
$report .= $this->deleteProducts_exec( $types );
}
else{
$sites = get_sites();
foreach( $sites as $site ){
switch_to_blog( $site->blog_id );
$report .= $this->deleteProducts_exec( $types, $site->blog_id );
restore_current_blog();
}
}
return $report;
}
public function deleteProducts_exec( $types = false, $site_id = false ){
if( ! $types ) return "<p>Can't delete products</p>";
$report = '';
$mu_report = $site_id ? "from site {$site_id}" : '';
$prod_args = array(
'post_type' => $types['product'],
'posts_per_page' => -1,
'post_status' => 'any'
);
$products = get_posts( $prod_args );
foreach( $products as $product ){
wp_delete_post( $product->ID, true );
$report .= "<p>Product {$product->post_title} has been deleted succesfully {$mu_report}</p>";
}
$variation_args = array(
'post_type' => $types['variation'],
'posts_per_page' => -1,
'post_status' => 'any'
);
$variations = get_posts( $variation_args );
foreach( $variations as $variation ){
wp_delete_post( $variation->ID, true );
$report .= "<p>Variation {$variation->post_title} has been deleted succesfully {$mu_report}</p>";
}
return $report;
}
public function deleteOrders(){
$report = '';
if( !is_multisite() ){
$report .= $this->deleteOrders_exec();
}
else{
$sites = get_sites();
foreach( $sites as $site ){
switch_to_blog( $site->blog_id );
$report .= $this->deleteOrders_exec( $site->blog_id );
restore_current_blog();
}
}
return $report;
}
public function deleteOrders_exec( $site_id = false ){
$report = '';
$mu_report = $site_id ? "from site {$site_id}" : '';
$args = array(
'post_type' => 'mp_order',
'posts_per_page' => -1,
'post_status' => 'any'
);
$orders = get_posts( $args );
foreach( $orders as $order ){
wp_delete_post( $order->ID, true );
$report .= "<p>Order {$order->post_title} has been deleted succesfully {$mu_report}</p>";
}
return $report;
}
public function get_single_options(){
return apply_filters( 'wpmudev_rm_mp/get_single_options',array(
'mp_deprecated_gateway_notice_showed',
'mp_flush_rewrites_30',
'mp_settings',
'mp_db_update_required',
'mp_previous_version',
'mp_version',
'mp_subsite_need_redirect',
'mp_needs_quick_setup',
'wpmudev_metabox_states',
'mp_needs_pages',
'mp_plugin_do_activation_redirect'
) );
}
public function get_mu_options(){
return apply_filters( 'wpmudev_rm_mp/get_mu_options',array(
'mp_network_settings',
'wdp_un_autoactivated',
'wdp_un_local_themes',
'wdp_un_local_projects',
'wdp_un_updates_available',
'mp_product_category',
'mp_product_tag',
'mp_network_build',
'wpmudev_metabox_states'
) );
}
public function get_single_tables(){
$tables = array(
'mp_product_attributes',
'mp_product_attributes_terms'
);
return apply_filters( 'wpmudev_rm_mp/get_single_tables', $tables );
}
public function get_mu_tables(){
$tables = array(
'mp_products',
//'mp_product_attributes', //Might get added in future ;)
//'mp_product_attributes_terms', //This one also
'mp_terms',
'mp_term_relationships'
);
return apply_filters( 'wpmudev_rm_mp/get_mu_tables', $tables );
}
}
function WPMUDEV_RM_MP_load(){
$GLOBALS['WPMUDEV_RM_MP'] = WPMUDEV_RM_MP::get_instance();
}
add_action( 'plugins_loaded', 'WPMUDEV_RM_MP_load' );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment