Skip to content

Instantly share code, notes, and snippets.

@lukecav

lukecav/reset-demo-site.php Secret

Created May 26, 2020
Embed
What would you like to do?
Reset WooCommerce Demo Site
<?php
/**
* Plugin Name: Reset WooCommerce Demo Site
* Plugin URI: https://www.nexcess.net
* Description: Wipe out all products, orders, pages, and more from a WooCommerce site.
* Author: Nexcess
* Author URI: https://www.nexcess.net
*/
namespace Nexcess\ResetDemoSite;
use WP_Admin_Bar;
use WP_Query;
/**
* Register the custom admin bar menu item.
*
* @param \WP_Admin_Bar The WP Admin Bar, passed by reference.
*/
function register_admin_bar( WP_Admin_Bar $admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$admin_bar->add_menu( [
'id' => 'nexcess-reset-demo-site',
'title' => sprintf(
'<form method="post" action="%1$s"><button name="action" type="submit" value="%2$s" class="button" style="min-height: 0; padding: 0; line-height: 32px; color: red; background: none; border: none;" onclick="return confirm(\'This will reset all content on this site. Are you sure you wish to proceed?\')">%3$s</button>%4$s</form>',
is_admin() ? '' : admin_url( 'admin-post.php' ),
'nexcess-reset-demo-site',
'Reset Demo Site',
wp_nonce_field( 'nexcess-reset-demo-site', 'nonce', true, false )
),
] );
}
// Register custom menus.
add_action( 'admin_bar_menu', __NAMESPACE__ . '\register_admin_bar', 1001 );
/**
* Clear out demo site content.
*/
function reset_demo_site() {
// Remove all pages, products, orders, and attachments.
$query = new WP_Query( [
'post_type' => [
'page',
'product',
'shop_order',
'attachment',
],
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
] );
array_walk( $query->posts, function ( $post_id ) {
wp_delete_post( $post_id, true );
} );
// Delete nav menus.
array_map( 'wp_delete_nav_menu', wp_get_nav_menus() );
// Delete custom options.
delete_option( '_storebuilder_created_on' );
delete_option( 'nexcess_mapps_storebuilder_industry' );
return wp_safe_redirect( admin_url( '/' ) );
}
add_action( 'admin_action_nexcess-reset-demo-site', __NAMESPACE__ . '\reset_demo_site' );
add_action( 'admin_post_nexcess-reset-demo-site', __NAMESPACE__ . '\reset_demo_site' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment