version = $this->get_plugin_version();
$this->plugin_dir = plugin_dir_path(__FILE__);
$this->plugin_url = plugin_dir_url(__FILE__);
$this->load_options();
$this->license = new WF_Licensing(array(
'prefix' => 'wpr',
'licensing_servers' => $this->licensing_servers,
'version' => $this->version,
'plugin_file' => __FILE__,
'plugin_page' => 'tools_page_wp-reset',
'skip_hooks' => false,
'debug' => false,
'js_folder' => plugin_dir_url(__FILE__) . '/js/'
));
add_action('admin_menu', array($this, 'admin_menu'));
add_action('admin_init', array($this, 'do_all_actions'));
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('admin_action_wpr_dismiss_notice', array($this, 'action_dismiss_notice'));
add_action('wp_ajax_wp_reset_dismiss_notice', array($this, 'ajax_dismiss_notice'));
add_action('wp_ajax_wp_reset_run_tool', array($this, 'ajax_run_tool'));
add_action('admin_print_scripts', array($this, 'remove_admin_notices'));
add_action('admin_action_wpr_install_wpfssl', array($this, 'install_wpfssl'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
add_filter('plugin_row_meta', array($this, 'plugin_meta_links'), 10, 2);
add_filter('admin_footer_text', array($this, 'admin_footer_text'));
$this->core_tables = array_map(function ($tbl) {
global $wpdb;
return $wpdb->prefix . $tbl;
}, $this->core_tables);
} // __construct
/**
* Get plugin version from file header
*
* @return string
*/
function get_plugin_version()
{
$plugin_data = get_file_data(__FILE__, array('version' => 'Version'), 'plugin');
return $plugin_data['version'];
} // get_plugin_version
/**
* Load and prepare the options array
* If needed create a new DB entry
*
* @return array
*/
private function load_options()
{
$options = get_option('wp-reset', array());
$change = false;
if (!isset($options['meta'])) {
$options['meta'] = array('first_version' => $this->version, 'first_install' => current_time('timestamp', true), 'reset_count' => 0);
$change = true;
}
if (!isset($options['dismissed_notices'])) {
$options['dismissed_notices'] = array();
$change = true;
}
if (!isset($options['last_run'])) {
$options['last_run'] = array();
$change = true;
}
if (!isset($options['options'])) {
$options['options'] = array();
$change = true;
}
if ($change) {
update_option('wp-reset', $options, true);
}
$this->options = $options;
return $options;
} // load_options
/**
* Get meta part of plugin options
*
* @return array
*/
function get_meta()
{
return $this->options['meta'];
} // get_meta
/**
* Get all dismissed notices, or check for one specific notice
*
* @param string $notice_name Optional. Check if specified notice is dismissed.
*
* @return bool|array
*/
function get_dismissed_notices($notice_name = '')
{
$notices = $this->options['dismissed_notices'];
if (empty($notice_name)) {
return $notices;
} else {
if (empty($notices[$notice_name])) {
return false;
} else {
return true;
}
}
} // get_dismissed_notices
/**
* Get options part of plugin options
*
* @param string $key Optional.
*
* @return array
*/
function get_options()
{
return $this->options['options'];
} // get_options
/**
* Update specified plugin options key
*
* @param string $key Data to save.
* @param string $data Option key.
*
* @return bool
*/
function update_options($key, $data)
{
if (false === in_array($key, array('meta', 'license', 'dismissed_notices', 'options'))) {
user_error('Unknown options key.', E_USER_ERROR);
return false;
}
$this->options[$key] = $data;
$tmp = update_option('wp-reset', $this->options);
return $tmp;
} // update_options
/**
* Add plugin menu entry under Tools menu
*
* @return null
*/
function admin_menu()
{
add_management_page(__('WP Reset', 'wp-reset'), __('WP Reset', 'wp-reset'), 'administrator', 'wp-reset', array($this, 'plugin_page'));
} // admin_menu
/**
* Dismiss notice via AJAX call
*
* @return null
*/
function ajax_dismiss_notice()
{
check_ajax_referer('wp-reset_dismiss_notice');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You are not allowed to run this action.', 'wp-reset'));
}
$notice_name = trim(sanitize_text_field(@$_GET['notice_name']));
if (!$this->dismiss_notice($notice_name)) {
wp_send_json_error(__('Notice is already dismissed.', 'wp-reset'));
} else {
wp_send_json_success();
}
} // ajax_dismiss_notice
/**
* Dismiss notice via admin action
*
* @return null
*/
function action_dismiss_notice()
{
if (false == wp_verify_nonce(sanitize_text_field(@$_GET['_wpnonce']), 'wpr_dismiss_notice')) {
wp_die('Please reload the page and try again.');
}
if (empty($_GET['notice'])) {
wp_safe_redirect(admin_url());
exit;
}
$notice_name = trim(sanitize_text_field(@$_GET['notice']));
$this->dismiss_notice($notice_name);
if (!empty($_GET['redirect'])) {
wp_safe_redirect($_GET['redirect']);
} else {
wp_safe_redirect(admin_url());
}
exit;
} // action_dismiss_notice
/**
* Dismiss notice by adding it to dismissed_notices options array
*
* @param string $notice_name Notice to dismiss.
*
* @return bool
*/
function dismiss_notice($notice_name)
{
if ($this->get_dismissed_notices($notice_name)) {
return false;
} else {
$notices = $this->get_dismissed_notices();
$notices[$notice_name] = true;
$this->update_options('dismissed_notices', $notices);
return true;
}
} // dismiss_notice
/**
* Returns all WP pointers
*
* @return array
*/
function get_pointers()
{
$pointers = array();
$pointers['welcome'] = array('target' => '#menu-tools', 'edge' => 'left', 'align' => 'right', 'content' => 'Thank you for installing the WP Reset plugin! Open Tools - WP Reset to access resetting tools and start developing & debugging faster.');
return $pointers;
} // get_pointers
/**
* Enqueue CSS and JS files
*
* @return null
*/
function admin_enqueue_scripts($hook)
{
// welcome pointer is shown on all pages except WPR to admins, until dismissed
$pointers = $this->get_pointers();
$dismissed_notices = $this->get_dismissed_notices();
foreach ($dismissed_notices as $notice_name => $tmp) {
if ($tmp) {
unset($pointers[$notice_name]);
}
} // foreach
if (!empty($pointers) && !$this->is_plugin_page() && current_user_can('manage_options')) {
$pointers['_nonce_dismiss_pointer'] = wp_create_nonce('wp-reset_dismiss_notice');
wp_enqueue_style('wp-pointer');
wp_enqueue_script('wp-reset-pointers', $this->plugin_url . 'js/wp-reset-pointers.js', array('jquery'), $this->version, true);
wp_enqueue_script('wp-pointer');
wp_localize_script('wp-pointer', 'wp_reset_pointers', $pointers);
}
// exit early if not on WP Reset page
if (!$this->is_plugin_page()) {
return;
}
$js_localize = array(
'undocumented_error' => __('An undocumented error has occurred. Please refresh the page and try again.', 'wp-reset'),
'documented_error' => __('An error has occurred.', 'wp-reset'),
'plugin_name' => __('WP Reset', 'wp-reset'),
'settings_url' => admin_url('tools.php?page=wp-reset'),
'wpfssl_install_url' => add_query_arg(array('action' => 'wpr_install_wpfssl', '_wpnonce' => wp_create_nonce('install_wpfssl'), 'rnd' => rand()), admin_url('admin.php')),
'icon_url' => $this->plugin_url . 'img/wp-reset-icon.png',
'invalid_confirmation' => __('Please type "reset" in the confirmation field.', 'wp-reset'),
'invalid_confirmation_title' => __('Invalid confirmation', 'wp-reset'),
'cancel_button' => __('Cancel', 'wp-reset'),
'ok_button' => __('OK', 'wp-reset'),
'confirm_button' => __('Reset WordPress', 'wp-reset'),
'confirm_title' => __('Are you sure you want to proceed?', 'wp-reset'),
'confirm_title_reset' => __('Are you sure you want to reset the site?', 'wp-reset'),
'confirm1' => __('Clicking "Reset WordPress" will reset your site to default values. All content will be lost. Always create a snapshot if you want to be able to undo.', 'wp-reset'),
'confirm2' => __('Click "Cancel" to abort.', 'wp-reset'),
'doing_reset' => __('Resetting in progress. Please wait.', 'wp-reset'),
'snapshot_success' => __('Snapshot created', 'wp-reset'),
'snapshot_wait' => __('Creating snapshot. Please wait.', 'wp-reset'),
'snapshot_confirm' => __('Create snapshot', 'wp-reset'),
'snapshot_placeholder' => __('Snapshot name or brief description, ie: before plugin install', 'wp-reset'),
'snapshot_text' => __('Enter snapshot name or brief description', 'wp-reset'),
'snapshot_title' => __('Create a new snapshot', 'wp-reset'),
'nonce_dismiss_notice' => wp_create_nonce('wp-reset_dismiss_notice'),
'activating' => __('Activating', 'wp-reset'),
'deactivating' => __('Deactivating', 'wp-reset'),
'deleting' => __('Deleting', 'wp-reset'),
'installing' => __('Installing', 'wp-reset'),
'activate_failed' => __('Could not activate', 'wp-reset'),
'deactivate_failed' => __('Could not deactivate', 'wp-reset'),
'delete_failed' => __('Could not delete', 'wp-reset'),
'install_failed' => __('Could not install', 'wp-reset'),
'install_failed_existing' => __('is already installed', 'wp-reset'),
'nonce_run_tool' => wp_create_nonce('wp-reset_run_tool'),
'nonce_do_reset' => wp_create_nonce('wp-reset_do_reset'),
);
wp_enqueue_style('plugin-install');
wp_enqueue_style('wp-jquery-ui-dialog');
wp_enqueue_style('wp-reset', $this->plugin_url . 'css/wp-reset.css', array(), $this->version);
wp_enqueue_style('wp-reset-sweetalert2', $this->plugin_url . 'css/sweetalert2.min.css', array(), $this->version);
wp_enqueue_style('wp-reset-tooltipster', $this->plugin_url . 'css/tooltipster.bundle.min.css', array(), $this->version);
wp_enqueue_script('plugin-install');
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script('jquery-ui-dialog');
wp_enqueue_script('wp-reset-sweetalert2', $this->plugin_url . 'js/wp-reset-libs.min.js', array('jquery'), $this->version, true);
wp_enqueue_script('wp-reset', $this->plugin_url . 'js/wp-reset.js', array('jquery'), $this->version, true);
wp_localize_script('wp-reset', 'wp_reset', $js_localize);
add_thickbox();
// fix for aggressive plugins that include their CSS on all pages
wp_dequeue_style('uiStyleSheet');
wp_dequeue_style('wpcufpnAdmin');
wp_dequeue_style('unifStyleSheet');
wp_dequeue_style('wpcufpn_codemirror');
wp_dequeue_style('wpcufpn_codemirrorTheme');
wp_dequeue_style('collapse-admin-css');
wp_dequeue_style('jquery-ui-css');
wp_dequeue_style('tribe-common-admin');
wp_dequeue_style('file-manager__jquery-ui-css');
wp_dequeue_style('file-manager__jquery-ui-css-theme');
wp_dequeue_style('wpmegmaps-jqueryui');
wp_dequeue_style('wp-botwatch-css');
wp_dequeue_style('uap_main_admin_style');
wp_dequeue_style('uap_font_awesome');
wp_dequeue_style('uap_jquery-ui.min.css');
} // admin_enqueue_scripts
/**
* Remove all WP notices on WPR page
*
* @return null
*/
function remove_admin_notices()
{
if (!$this->is_plugin_page()) {
return false;
}
global $wp_filter;
unset($wp_filter['user_admin_notices'], $wp_filter['admin_notices']);
} // remove_admin_notices
/**
* Check if WP-CLI is available and running
*
* @return bool
*/
static function is_cli_running()
{
if (!is_null($value = apply_filters('wp-reset-override-is-cli-running', null))) {
return (bool) $value;
}
if (defined('WP_CLI') && WP_CLI) {
return true;
} else {
return false;
}
} // is_cli_running
/**
* Check if given plugin is installed
*
* @param [string] $slug Plugin slug
* @return boolean
*/
function is_plugin_installed($slug)
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
if (!empty($all_plugins[$slug])) {
return true;
} else {
return false;
}
} // is_plugin_installed
/**
* Deletes all transients.
*
* @return int Number of deleted transient DB entries
*/
function do_delete_transients()
{
global $wpdb;
$count = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '\_transient\_%' OR option_name LIKE '\_site\_transient\_%'");
wp_cache_flush();
do_action('wp_reset_delete_transients', $count);
return $count;
} // do_delete_transients
/**
* Purge all cache for popular caching plugins
*
* @return bool true
*/
function do_purge_cache()
{
global $wp_reset;
wp_cache_flush();
$wp_reset->do_delete_transients();
if (function_exists('w3tc_flush_all')) {
w3tc_flush_all();
}
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache();
}
if (method_exists('LiteSpeed_Cache_API', 'purge_all')) {
LiteSpeed_Cache_API::purge_all();
}
if (class_exists('Endurance_Page_Cache')) {
$epc = new Endurance_Page_Cache;
$epc->purge_all();
}
if (class_exists('SG_CachePress_Supercacher') && method_exists('SG_CachePress_Supercacher', 'purge_cache')) {
SG_CachePress_Supercacher::purge_cache(true);
}
if (class_exists('SiteGround_Optimizer\Supercacher\Supercacher')) {
SiteGround_Optimizer\Supercacher\Supercacher::purge_cache();
}
if (isset($GLOBALS['wp_fastest_cache']) && method_exists($GLOBALS['wp_fastest_cache'], 'deleteCache')) {
$GLOBALS['wp_fastest_cache']->deleteCache(true);
}
if (is_callable(array('Swift_Performance_Cache', 'clear_all_cache'))) {
Swift_Performance_Cache::clear_all_cache();
}
if (is_callable(array('Hummingbird\WP_Hummingbird', 'flush_cache'))) {
Hummingbird\WP_Hummingbird::flush_cache(true, false);
}
do_action('wp_reset_purge_cache');
return true;
} // do_purge_cache
/**
* Resets all theme options (mods).
*
* @param bool $all_themes Delete mods for all themes or just the current one
*
* @return int Number of deleted mod DB entries
*/
function do_reset_theme_options($all_themes = true)
{
global $wpdb;
$query = $wpdb->prepare("DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s", array('mods\_%', 'theme_mods\_%'));
$count = $wpdb->query($query);
do_action('wp_reset_reset_theme_options', $count);
return $count;
} // do_reset_theme_options
/**
* Deletes all files in uploads folder.
*
* @return int Number of deleted files and folders.
*/
function do_delete_uploads()
{
$upload_dir = wp_get_upload_dir();
$this->delete_count = 0;
$this->delete_folder($upload_dir['basedir'], $upload_dir['basedir']);
do_action('wp_reset_delete_uploads', $this->delete_count);
return $this->delete_count;
} // do_delete_uploads
/**
* Recursively deletes a folder
*
* @param string $folder Recursive param.
* @param string $base_folder Base folder.
*
* @return bool
*/
private function delete_folder($folder, $base_folder)
{
$files = array_diff(scandir($folder), array('.', '..'));
foreach ($files as $file) {
if (is_dir($folder . DIRECTORY_SEPARATOR . $file)) {
$this->delete_folder($folder . DIRECTORY_SEPARATOR . $file, $base_folder);
} else {
$tmp = @unlink($folder . DIRECTORY_SEPARATOR . $file);
$this->delete_count = $this->delete_count + (int) $tmp;
}
} // foreach
if ($folder != $base_folder) {
$tmp = @rmdir($folder);
$this->delete_count = $this->delete_count + (int) $tmp;
return $tmp;
} else {
return true;
}
} // delete_folder
/**
* Deactivate all plugins
*
* @param array keep_wp_reset - Keep WP Reset active and installed, silent_deactivate - Skip individual plugin deactivation functions when deactivating
*
* @return int Number of deactivated plugins.
*/
function do_deactivate_plugins($params = array())
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if (!function_exists('request_filesystem_credentials')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$wp_reset_basename = plugin_basename(WP_RESET_FILE);
$params = shortcode_atts(array('keep_wp_reset' => true, 'silent_deactivate' => false), (array) $params);
$active_plugins = (array) get_option('active_plugins', array());
if ($params['keep_wp_reset']) {
if (($key = array_search($wp_reset_basename, $active_plugins)) !== false) {
unset($active_plugins[$key]);
}
}
if (!empty($active_plugins)) {
deactivate_plugins($active_plugins, $params['silent_deactivate'], false);
}
do_action('wp_reset_deactivate_plugins', $active_plugins, $params);
return sizeof($active_plugins);
} // do_deactivate_plugins
/**
* Delete all plugins
*
* @param array keep_wp_reset - Keep WP Reset active and installed
*
* @return int Number of deleted plugins.
*/
function do_delete_plugins($params = array())
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if (!function_exists('request_filesystem_credentials')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$wp_reset_basename = plugin_basename(WP_RESET_FILE);
$params = shortcode_atts(array('keep_wp_reset' => true), (array) $params);
$all_plugins = get_plugins();
if ($params['keep_wp_reset']) {
unset($all_plugins[$wp_reset_basename]);
}
if (!empty($all_plugins)) {
delete_plugins(array_keys($all_plugins));
}
do_action('wp_reset_delete_plugins', $all_plugins, $params);
return sizeof($all_plugins);
} // do_delete_plugins
/**
* Delete all themes
*
* @param bool $keep_default_theme Keep default theme
*
* @return int Number of deleted themes.
*/
function do_delete_themes($keep_default_theme = true)
{
global $wp_version;
if (!function_exists('delete_theme')) {
require_once ABSPATH . 'wp-admin/includes/theme.php';
}
if (!function_exists('request_filesystem_credentials')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
if (version_compare($wp_version, '5.0', '<') === true) {
$default_theme = 'twentyseventeen';
} else {
$default_theme = 'twentytwentyone';
}
$all_themes = wp_get_themes(array('errors' => null));
if (true == $keep_default_theme) {
unset($all_themes[$default_theme]);
}
foreach ($all_themes as $theme_slug => $theme_details) {
$res = delete_theme($theme_slug);
}
if (false == $keep_default_theme) {
update_option('template', '');
update_option('stylesheet', '');
update_option('current_theme', '');
}
do_action('wp_reset_delete_themes', $all_themes);
return sizeof($all_themes);
} // do_delete_themes
/**
* Truncate custom tables
*
* @return int Number of truncated tables.
*/
function do_truncate_custom_tables()
{
global $wpdb;
$custom_tables = $this->get_custom_tables();
foreach ($custom_tables as $tbl) {
$wpdb->wpreset_custom_table = $tbl['name'];
$wpdb->query('SET foreign_key_checks = 0');
$wpdb->query("TRUNCATE TABLE " . $wpdb->wpreset_custom_table);
} // foreach
do_action('wp_reset_truncate_custom_tables', $custom_tables);
return sizeof($custom_tables);
} // do_truncate_custom_tables
/**
* Drop custom tables
*
* @return int Number of dropped tables.
*/
function do_drop_custom_tables()
{
global $wpdb;
$custom_tables = $this->get_custom_tables();
foreach ($custom_tables as $tbl) {
$wpdb->wpreset_custom_table = $tbl['name'];
$wpdb->query('SET foreign_key_checks = 0');
$wpdb->query("DROP TABLE IF EXISTS " . $wpdb->wpreset_custom_table);
} // foreach
do_action('wp_reset_drop_custom_tables', $custom_tables);
return sizeof($custom_tables);
} // do_drop_custom_tables
/**
* Delete .htaccess file
*
* @return bool|WP_Error Action status.
*/
function do_delete_htaccess()
{
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
$htaccess_path = $this->get_htaccess_path();
clearstatcache();
do_action('wp_reset_delete_htaccess', $htaccess_path);
if (!$wp_filesystem->is_readable($htaccess_path)) {
return new WP_Error(1, 'Htaccess file does not exist; there\'s nothing to delete.');
}
if (!$wp_filesystem->is_writable($htaccess_path)) {
return new WP_Error(1, 'Htaccess file is not writable.');
}
if ($wp_filesystem->delete($htaccess_path, false, 'f')) {
return true;
} else {
return new WP_Error(1, 'Unknown error. Unable to delete htaccess file.');
}
} // do_delete_htaccess
/**
* Get .htaccess file path.
*
* @return string
*/
function get_htaccess_path()
{
if (!function_exists('get_home_path')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
if ($this->is_cli_running()) {
$_SERVER['SCRIPT_FILENAME'] = ABSPATH;
}
$filepath = get_home_path() . '.htaccess';
return $filepath;
} // get_htaccess_path
/**
* Run one tool via AJAX call
*
* @return null
*/
function ajax_run_tool()
{
check_ajax_referer('wp-reset_run_tool');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('You are not allowed to run this action.', 'wp-reset'));
}
$tool = trim(sanitize_text_field(@$_GET['tool']));
$extra_data = trim(sanitize_text_field(@$_GET['extra_data']));
if ($tool == 'delete_transients') {
$cnt = $this->do_delete_transients();
wp_send_json_success($cnt);
} elseif ($tool == 'reset_theme_options') {
$cnt = $this->do_reset_theme_options(true);
wp_send_json_success($cnt);
} elseif ($tool == 'purge_cache') {
$this->do_purge_cache();
wp_send_json_success();
} elseif ($tool == 'delete_wp_cookies') {
wp_clear_auth_cookie();
wp_send_json_success();
} elseif ($tool == 'delete_themes') {
$cnt = $this->do_delete_themes(false);
wp_send_json_success($cnt);
} elseif ($tool == 'deactivate_plugins') {
$cnt = $this->do_deactivate_plugins($extra_data);
wp_send_json_success($cnt);
} elseif ($tool == 'delete_plugins') {
$cnt = $this->do_delete_plugins($extra_data);
wp_send_json_success($cnt);
} elseif ($tool == 'delete_uploads') {
$cnt = $this->do_delete_uploads();
wp_send_json_success($cnt);
} elseif ($tool == 'delete_htaccess') {
$tmp = $this->do_delete_htaccess();
if (is_wp_error($tmp)) {
wp_send_json_error($tmp->get_error_message());
} else {
wp_send_json_success($tmp);
}
} elseif ($tool == 'drop_custom_tables') {
$cnt = $this->do_drop_custom_tables();
wp_send_json_success($cnt);
} elseif ($tool == 'truncate_custom_tables') {
$cnt = $this->do_truncate_custom_tables();
wp_send_json_success($cnt);
} elseif ($tool == 'delete_snapshot') {
$res = $this->do_delete_snapshot($extra_data);
if (is_wp_error($res)) {
wp_send_json_error($res->get_error_message());
} else {
wp_send_json_success();
}
} elseif ($tool == 'download_snapshot') {
$res = $this->do_export_snapshot($extra_data);
if (is_wp_error($res)) {
wp_send_json_error($res->get_error_message());
} else {
$url = content_url() . '/' . $this->snapshots_folder . '/' . $res;
wp_send_json_success($url);
}
} elseif ($tool == 'restore_snapshot') {
$res = $this->do_restore_snapshot($extra_data);
if (is_wp_error($res)) {
wp_send_json_error($res->get_error_message());
} else {
wp_send_json_success();
}
} elseif ($tool == 'compare_snapshots') {
$res = $this->do_compare_snapshots($extra_data);
if (is_wp_error($res)) {
wp_send_json_error($res->get_error_message());
} else {
wp_send_json_success($res);
}
} elseif ($tool == 'create_snapshot') {
$res = $this->do_create_snapshot($extra_data);
if (is_wp_error($res)) {
wp_send_json_error($res->get_error_message());
} else {
wp_send_json_success();
}
} elseif ($tool == 'get_table_details') {
$res = WP_Reset_Utility::get_table_details();
wp_send_json_success($res);
} elseif (
$tool == 'check_deactivate_plugin' ||
$tool == 'check_delete_plugin' ||
$tool == 'check_install_plugin' ||
$tool == 'check_activate_plugin'
) {
$path = $this->get_plugin_path(sanitize_text_field($_GET['slug']));
if (false !== ($error = get_transient('wf_install_error_' . sanitize_text_field($_GET['slug'])))) {
delete_transient('wf_install_error_' . sanitize_text_field($_GET['slug']));
wp_send_json_success($error);
}
if (false !== $path) {
$active_plugins = (array) get_option('active_plugins', array());
if (false !== array_search($path, $active_plugins)) {
wp_send_json_success('active');
} else {
wp_send_json_success('inactive');
}
} else {
wp_send_json_success('deleted');
}
} elseif ($tool == 'install_plugin') {
$slug = sanitize_text_field($_GET['slug']);
@include_once ABSPATH . 'wp-admin/includes/plugin.php';
@include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
@include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
@include_once ABSPATH . 'wp-admin/includes/file.php';
@include_once ABSPATH . 'wp-admin/includes/misc.php';
wp_cache_flush();
$path = $this->get_plugin_path($slug);
if (false !== $path) {
// Plugin is already installed
wp_send_json_success();
} else {
// Install Plugin
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader($skin);
$upgrader->install('https://downloads.wordpress.org/plugin/' . $slug . '.latest-stable.zip');
wp_send_json_success();
}
} elseif ($tool == 'activate_plugin') {
$path = $this->get_plugin_path(sanitize_text_field($_GET['slug']));
activate_plugin($path);
wp_send_json_success();
} elseif ($tool == 'before_reset') {
$active_plugins = get_option('active_plugins');
set_transient('wpr_active_plugins', $active_plugins, 100);
remove_all_actions('update_option_active_plugins');
update_option('active_plugins', array(plugin_basename(__FILE__)));
wp_send_json_success();
} else {
wp_send_json_error(__('Unknown tool.', 'wp-reset'));
}
} // ajax_run_tool
/**
* Get plugin path from slug
*
* @return string path
*/
function get_plugin_path($slug)
{
$all_plugins = get_plugins();
foreach ($all_plugins as $plugin_path => $plugin) {
if (strpos($plugin_path, $slug . '/') === 0) {
return $plugin_path;
}
}
return false;
} // get_plugin_path
/**
* Reinstall / reset the WP site
* There are no failsafes in the function - it reinstalls when called
* Redirects when done
*
* @param array $params Optional.
*
* @return null
*/
function do_reinstall($params = array())
{
global $current_user, $wpdb;
// only admins can reset; double-check
if (!$this->is_cli_running() && !current_user_can('manage_options')) {
return false;
}
// make sure the function is available to us
if (!function_exists('wp_install')) {
require ABSPATH . '/wp-admin/includes/upgrade.php';
}
// save values that need to be restored after reset
$blogname = get_option('blogname');
$blog_public = get_option('blog_public');
$wplang = get_option('wplang');
$siteurl = get_option('siteurl');
$home = get_option('home');
$snapshots = $this->get_snapshots();
$active_plugins = get_transient('wpr_active_plugins');
$active_theme = wp_get_theme();
// for WP-CLI
if (!$current_user->ID) {
$tmp = get_users(array('role' => 'administrator', 'order' => 'ASC', 'order_by' => 'ID'));
if (empty($tmp[0]->user_login)) {
return new WP_Error(1, 'Reset failed. Unable to find any admin users in database.');
}
$current_user = $tmp[0];
}
// delete custom tables with WP's prefix
$prefix = str_replace('_', '\_', $wpdb->prefix);
$tables = $wpdb->get_col($wpdb->prepare("SHOW TABLES LIKE %s", array($prefix . '%')));
foreach ($tables as $table) {
$wpdb->wpreset_table = $table;
$wpdb->query("DROP TABLE " . $wpdb->wpreset_table);
}
$old_user_pass = $current_user->user_pass;
// suppress errors for WP_CLI
$result = @wp_install($blogname, $current_user->user_login, $current_user->user_email, $blog_public, '', md5(rand()), $wplang);
$user_id = $result['user_id'];
// restore user pass
$query = $wpdb->prepare("UPDATE {$wpdb->users} SET user_pass = %s, user_activation_key = %s WHERE ID = %d LIMIT 1", array($old_user_pass, '', $user_id));
$wpdb->query($query);
$current_user->user_pass = $old_user_pass;
// restore rest of the settings including WP Reset's
update_option('siteurl', $siteurl);
update_option('home', $home);
update_option('wp-reset', $this->options);
update_option('wp-reset-snapshots', $snapshots);
// remove password nag
if (get_user_meta($user_id, 'default_password_nag')) {
update_user_meta($user_id, 'default_password_nag', false);
}
if (get_user_meta($user_id, $wpdb->prefix . 'default_password_nag')) {
update_user_meta($user_id, $wpdb->prefix . 'default_password_nag', false);
}
$meta = $this->get_meta();
$meta['reset_count']++;
$this->update_options('meta', $meta);
// reactivate theme
if (!empty($params['reactivate_theme'])) {
switch_theme($active_theme->get_stylesheet());
}
// reactivate WP Reset
if (!empty($params['reactivate_wpreset'])) {
activate_plugin(plugin_basename(__FILE__));
}
// reactivate all plugins
if (!empty($params['reactivate_plugins'])) {
foreach ($active_plugins as $plugin_file) {
activate_plugin($plugin_file);
}
}
if (!$this->is_cli_running()) {
// log out and log in the old/new user
// since the password doesn't change this is potentially unnecessary
wp_clear_auth_cookie();
wp_set_auth_cookie($user_id);
wp_safe_redirect(admin_url() . '?wp-reset=success');
exit;
}
} // do_reinstall
/**
* Checks wp_reset post value and performs all actions
*
* @return null|bool
*/
function do_all_actions()
{
// only admins can perform actions
if (!current_user_can('manage_options')) {
return;
}
if (!empty($_GET['wp-reset']) && sanitize_text_field($_GET['wp-reset']) == 'success') {
add_action('admin_notices', array($this, 'notice_successful_reset'));
}
// check nonce
if (true === isset($_POST['wp_reset_confirm']) && false === wp_verify_nonce(sanitize_text_field(@$_POST['_wpnonce']), 'wp-reset')) {
add_settings_error('wp-reset', 'bad-nonce', __('Something went wrong. Please refresh the page and try again.', 'wp-reset'), 'error');
return false;
}
// check confirmation code
if (true === isset($_POST['wp_reset_confirm']) && 'reset' !== sanitize_text_field($_POST['wp_reset_confirm'])) {
add_settings_error('wp-reset', 'bad-confirm', __('Invalid confirmation code. Please type "reset" in the confirmation field.', 'wp-reset'), 'error');
return false;
}
// only one action at the moment
if (true === isset($_POST['wp_reset_confirm']) && 'reset' === sanitize_text_field($_POST['wp_reset_confirm'])) {
$params = array(
'reactivate_theme' => '0',
'reactivate_plugins' => '0',
'reactivate_wpreset' => '0',
);
if (isset($_POST['wpr-post-reset']['reactivate_theme'])) {
$params['reactivate_theme'] = true;
}
if (isset($_POST['wpr-post-reset']['reactivate_plugins'])) {
$params['reactivate_plugins'] = true;
}
if (isset($_POST['wpr-post-reset']['reactivate_wpreset'])) {
$params['reactivate_wpreset'] = true;
}
$this->do_reinstall($params);
}
} // do_all_actions
/**
* Add "Open WP Reset Tools" action link to plugins table, left part
*
* @param array $links Initial list of links.
*
* @return array
*/
function plugin_action_links($links)
{
$settings_link = '' . esc_html(__('Open WP Reset Tools', 'wp-reset')) . '';
array_unshift($links, $settings_link);
return $links;
} // plugin_action_links
/**
* Add links to plugin's description in plugins table
*
* @param array $links Initial list of links.
* @param string $file Basename of current plugin.
*
* @return array
*/
function plugin_meta_links($links, $file)
{
if ($file !== plugin_basename(__FILE__)) {
return $links;
}
$support_link = '' . __('Support', 'wp-reset') . '';
$home_link = '' . __('Plugin Homepage', 'wp-reset') . '';
$rate_link = '' . __('Rate the plugin ★★★★★', 'wp-reset') . '';
$links[] = $support_link;
$links[] = $home_link;
$links[] = $rate_link;
return $links;
} // plugin_meta_links
/**
* Test if we're on WPR's admin page
*
* @return bool
*/
function is_plugin_page()
{
$current_screen = get_current_screen();
if (!empty($current_screen->id) && $current_screen->id == 'tools_page_wp-reset') {
return true;
} else {
return false;
}
} // is_plugin_page
/**
* Add powered by text in admin footer
*
* @param string $text Default footer text.
*
* @return string
*/
function admin_footer_text($text)
{
if (!$this->is_plugin_page()) {
return $text;
}
$text = 'WP Reset v' . $this->version . '. Please rate the plugin ★★★★★ to help us spread the word. Thank you from the WP Reset team!';
return $text;
} // admin_footer_text
/**
* Loads plugin's translated strings
*
* @return null
*/
function load_textdomain()
{
load_plugin_textdomain('wp-reset');
} // load_textdomain
/**
* Inform the user that WordPress has been successfully reset
*
* @return null
*/
function notice_successful_reset()
{
global $current_user;
WP_Reset_Utility::wp_kses_wf('
' . sprintf(__('Site has been successfully reset to default settings. User "%s" was restored with the password unchanged. Open WP Reset to do another reset.', 'wp-reset'), esc_html($current_user->user_login), esc_url(admin_url('tools.php?page=wp-reset'))) . '
WP Force SSL is a free WP plugin maintained by the same team as this Maintenance plugin. It has +150,000 users, 5-star rating, and is hosted on the official WP repository.
';
echo '
';
}
echo '
';
echo '
'; // wrap
} // plugin_page
/**
* Echoes all custom plugin notitications
*
* @return null
*/
private function custom_notifications()
{
$notice_shown = false;
$meta = $this->get_meta();
$snapshots = $this->get_snapshots();
// update to PRO after activating the license
if ($this->license->is_active()) {
echo '
';
echo '
' . esc_html(__('Thank you for purchasing WP Reset PRO!', 'wp-reset')) . '
';
echo '
Your license has been verified & activated. To start using the PRO version, please follow these steps:';
echo '
If asked to replace (overwrite) the free version - confirm it.
';
echo '
Activate the plugin.
';
echo '
That\'s it, no more steps.
';
echo '';
echo '
';
$notice_shown = true;
}
// warn that WPR is not WPMU compatible
if (false === $notice_shown && is_multisite()) {
echo '
';
echo '
' . esc_html__('WP Reset is not compatible with multisite!', 'wp-reset') . '
';
WP_Reset_Utility::wp_kses_wf('
' . __('Please be careful when using WP Reset with multisite enabled. It\'s not recommended to reset the main site. Sub-sites should be OK. We\'re working on making it fully compatible with WP-MU. Till then please be careful. Thank you for understanding.', 'wp-reset') . '
' . esc_html__('Please help us spread the word & keep the plugin up-to-date', 'wp-reset') . '
';
WP_Reset_Utility::wp_kses_wf('
' . __('If you use & enjoy WP Reset, please rate it on WordPress.org. It only takes a second and helps us keep the plugin maintained. Thank you!', 'wp-reset') . '
The following table details what data will be deleted (reset or destroyed) when a selected reset tool is run. Please read it! ';
echo 'If something is not clear contact support before running any tools. It\'s better to ask than to be sorry!';
echo '
';
echo '
- tool WILL delete, reset or destroy the noted data ';
echo ' - tool will NOT touch the noted data in any way
' . esc_html__('What happens when I run any Reset tool?', 'wp-reset') . '
';
echo '
';
WP_Reset_Utility::wp_kses_wf('
' . __('remember, always make a backup first or use snapshots', 'wp-reset') . '
');
echo '
' . esc_html__('you will have to confirm the action one more time', 'wp-reset') . '
';
echo '
' . esc_html__('see the table above to find out what exactly will be reset or deleted', 'wp-reset') . '
';
echo '
' . esc_html__('site title, WordPress URL, site URL, site language, search engine visibility and current user will always be restored', 'wp-reset') . '
';
echo '
' . esc_html__('you will be logged out, automatically logged back in and taken to the admin dashboard', 'wp-reset') . '
';
echo '
' . esc_html__('WP Reset plugin will be reactivated if that option is chosen', 'wp-reset') . '
';
echo '
';
echo '
' . esc_html__('WP-CLI Support', 'wp-reset') . ' ';
echo '' . sprintf(esc_html__('All tools available via GUI are available in WP-CLI as well. To get the list of commands run %s. Instead of the active user, the first user with admin privileges found in the database will be restored. ', 'wp-reset'), 'wp help reset');
echo sprintf(esc_html__('All actions have to be confirmed. If you want to skip confirmation use the standard %s option. Please be careful and backup first.', 'wp-reset'), '--yes') . '
Options table will be reset to default values meaning all WP core settings, widgets, theme settings and customizations, and plugin settings will be gone. Other content and files will not be touched including posts, pages, custom post types, comments and other data stored in separate tables. Site URL and name will be kept as well. Please see the table above for details.
' . __('Type reset in the confirmation field to confirm the reset and then click the "Reset WordPress" button. Always create a snapshot before resetting if you want to be able to undo.', 'wp-reset') . '
All data will be deleted or reset (see the explanation table for details). All data stored in the database including custom tables with ' . esc_html($wpdb->prefix) . ' prefix, as well as all files in wp-content, themes and plugins folders. The only thing restored after reset will be your user account so you can log in again, and the basic WP settings like site URL. Please see the table above for details.
';
WP_Reset_Utility::wp_kses_wf($this->get_tool_icons(true, true));
if (is_multisite()) {
echo '
This tool is not compatible with WP multisite (WPMU). Using it would delete files shared by multiple sites in the WP network.
';
} else {
echo '
';
WP_Reset_Utility::wp_kses_wf('
' . __('Type reset in the confirmation field to confirm the reset and then click the "Reset WordPress & Delete All Custom Files & Data" button. There is NO UNDO.', 'wp-reset') . '
' . __('All options (mods) for all themes will be reset; not just for the active theme. The tool works only for themes that use the WordPress theme modification API. If options are saved in some other, custom way they won\'t be reset. Always create a snapshot before using this tool if you want to be able to undo its actions.', 'wp-reset') . '
create a snapshot if you want to be able to undo') . '." data-text-done="Options for %n themes have been reset." data-text-done-singular="Options for one theme have been reset." class="button button-delete" href="#" id="reset-theme-options">Reset theme options';
WP_Reset_Utility::wp_kses_wf($this->get_snapshot_button('reset-theme-options', 'Before resetting theme options') . '
Default user roles\' capatibilities will be reset to their default values. All custom roles will be deleted. Users that had custom roles will not be assigned any default ones and might not be able to log in. Roles have to be (re)assigned to them manually.
All transient related database entries will be deleted. Including expired and non-expired transients, and orphaned transient timeout entries. Always create a snapshot before using this tool if you want to be able to undo its actions.
create a snapshot if you want to be able to undo') . '." data-text-done="%n transient database entries have been deleted." data-text-done-singular="One transient database entry has been deleted." class="button button-delete" href="#" id="delete-transients">Delete all transients';
WP_Reset_Utility::wp_kses_wf($this->get_snapshot_button('delete-transients', 'Before deleting transients') . '
All cache objects stored in both files and the database will be deleted. Along with WP object cache and transients, cache from the following plugins will be purged: W3 Total Cache, WP Cache, LiteSpeed Cache, Endurance Page Cache, SiteGround Optimizer, WP Fastest Cache and Swift Performance.
All local storage and session storage data will be deleted. Cookies without a custom set path will be deleted as well. WP cookies are not touched, with Delete Local Data button. Deleting all WordPress cookies (including authentication cookies) will delete all WP related cookies and user (you) will be logged out on the next page reload.
Besides content, all linked or child records (for selected content) will be deleted to prevent creating orphaned rows in the database. For instance, for posts that\'s posts, post meta, and comments related to posts. Delete process does not call any WP hooks such as before_delete_post. Choosing a post type or taxonomy does not delete that parent object it deletes the child objects. Parent objects are defined in code. If you want to remove them, remove their code definition. When media is deleted, files are left in the uploads folder. To delete files use the Clean uploads Folder tool. Deleting users does not affect the current, logged in user account. All orphaned objects will be reassigned to him.
All widgets, orphaned, active and inactive ones, as well as widgets in active and inactive sidebars will be deleted including their settings. After deleting, WordPress will automatically recreate default, empty database entries related to widgets. So, no matter how many times users run the tool it will never return "no data deleted". That\'s expected and normal.
' . __('All themes will be deleted. Including the currently active theme - ' . esc_html($theme->get('Name')) . '. There is NO UNDO. WP Reset does not make any file backups.', 'wp-reset') . '
' . __('All plugins will be deleted except for WP Reset which will remain active. There is NO UNDO. WP Reset does not make any file backups.', 'wp-reset') . '
MU Plugins are located in /wp-content/mu-plugins/ and are, as the name suggests, must-use plugins that are automatically activated by WP and can\'t be deactiavated via the plugins interface, although if any are used, they are listed in the "Must Use" tab. ';
echo 'Drop-ins are pieces of code found in /wp-content/ that replace default, built-in WordPress functionality. Most often used are db.php and advanced-cache.php that implement custom DB and cache functionality. They can\'t be deactivated via the plugins interface but if any are present are listed in the "Drop-in" tab.
';
if (is_multisite()) {
echo '
This tool is not compatible with WP multisite (WPMU). Using it would delete plugins for all sites in the network since they all share the same plugin files.
' . __('All files in ' . esc_html($upload_dir['basedir']) . ' folder will be deleted. Including folders and subfolders, and files in subfolders. Files associated with media entries will be deleted too. There is NO UNDO. WP Reset does not make any file backups.', 'wp-reset') . '
');
WP_Reset_Utility::wp_kses_wf($this->get_tool_icons(true, false));
if (false != $upload_dir['error']) {
echo '
Tool is not available. Folder is not writeable by WordPress. Please check file and folder access rights.
All folders and their content in wp-content folder except the following ones will be deleted: mu-plugins, plugins, themes, uploads, wp-reset-autosnapshots, wp-reset-snapshots-export.
';
WP_Reset_Utility::wp_kses_wf($this->get_tool_icons(true, false));
if (false === is_writable(trailingslashit(WP_CONTENT_DIR))) {
echo '
Tool is not available. Folder is not writeable by WordPress. Please check file and folder access rights.
' . __('This action affects only custom tables with ' . esc_html($wpdb->prefix) . ' prefix. Core WP tables and other tables in the database that do not have that prefix will not be deleted/emptied. Deleting (dropping) tables completely removes them from the database. Emptying (truncating) removes all content from them, but keeps the structure intact. Always create a snapshot before using this tool if you want to be able to undo its actions.
', 'wp-reset'));
if ($custom_tables) {
WP_Reset_Utility::wp_kses_wf('
' . __('The following ' . sizeof($custom_tables) . ' custom tables are affected by this tool: ', 'wp-reset'));
foreach ($custom_tables as $tbl) {
echo '' . esc_html($tbl['name']) . '';
if (next($custom_tables)) {
echo ', ';
}
} // foreach
echo '.
';
$custom_tables_btns = '';
} else {
echo '
' . esc_html__('There are no custom tables. There\'s nothing for this tool to empty or delete.', 'wp-reset') . '
create a snapshot if you want to be able to undo') . '." data-text-done="%n custom tables have been emptied." data-text-done-singular="One custom table has been emptied." class="button button-delete' . esc_attr($custom_tables_btns) . '" href="#" id="truncate-custom-tables">Empty (truncate) custom tables';
echo 'create a snapshot if you want to be able to undo') . '." data-text-done="%n custom tables have been deleted." data-text-done-singular="One custom table has been deleted." class="button button-delete' . esc_attr($custom_tables_btns) . '" href="#" id="drop-custom-tables">Delete (drop) custom tables';
WP_Reset_Utility::wp_kses_wf($this->get_snapshot_button('drop-custom-tables', 'Before deleting custom tables'));
echo '
This tool is not compatible with WP multisite (WPMU). Using it would change the WP version for all sites in the network since they all share the same core files.
';
} else {
echo '
Replace current WordPress version with the selected new version. Switching from a previous version, to a newer version is mostly supported and properly handled by the WP installer. Reverting WordPress, rolling back WordPress to a previous version is not supported. Results may vary!
' . __('This action deletes the .htaccess file located in ' . esc_html($this->get_htaccess_path()) . ' There is NO UNDO. WP Reset does not make any file backups.
', 'wp-reset'));
echo '
If you need to edit .htaccess, install our free WP Htaccess Editor plugin. It automatically creates backups when you edit .htaccess as well as checks for syntax errors. To create the default .htaccess file open Settings - Permalinks and re-save settings. WordPress will recreate the file.
' . __('Have a set of plugins (and themes) that you install and activate after every reset? Or on every fresh WordPress installation? Well, no more clicking install & active for ten minutes! Build the collection once and install it with one click as many times as needed.
WP Reset stores collections in the cloud so they\'re accessible on every site you build. You can use free plugins and themes from the official repo, and PRO ones by uploading a ZIP file. We\'ll safely store your license keys too, so you have everything in one place.', 'wp-reset') . '
'; // collections-info
$plugins = array();
$plugins['eps-301-redirects'] = array('name' => '301 Redirects', 'desc' => 'Easiest way to manage redirects');
$plugins['classic-editor'] = array('name' => 'Classic Editor', 'desc' => 'Any easy fix for all your Gutenberg caused troubles');
$plugins['simple-author-box'] = array('name' => 'Simple Author Box', 'desc' => 'Simplest way to add responsive, great looking author boxes');
$plugins['sticky-menu-or-anything-on-scroll'] = array('name' => 'Sticky Menu (or Anything!) on Scroll', 'desc' => 'Make any element on the page sticky.');
$plugins['under-construction-page'] = array('name' => 'UnderConstructionPage', 'desc' => 'Working on your site? Put it in the under construction mode.');
$plugins['wp-external-links'] = array('name' => 'WP External Links', 'desc' => 'Manage all external & internal links. Control icons, nofollow, noopener, UGC, sponsored and if links open in new window or new tab.');
echo '
' . __('All tools and features are explained in detail in the documentation. We did our best to describe how things work on both the code level and an "average user" level.', 'wp-reset') . '
Emergency Recovery Script is a standalone, single-file, WordPress independent PHP script created to recover WordPress sites from the most difficult situations. When access to the admin is not possible when core files are compromised (accidental delete or malware related situations), when you get the white screen of death, can\'t log in for whatever reason or a plugin has killed your site - emergency recovery script can fix the problem! Some of the things ERS can do;
';
echo '
';
echo '
Test the integrity of all WP core files and reinstall them if needed
';
echo '
Detect and remove all files in core folders that are not a part of WP
';
echo '
Deactivate and activate plugins without logging in to WP admin
';
echo '
Deactivate and activate themes without logging in to WP admin
';
echo '
Reset user privileges and roles
';
echo '
Create new WP admin accounts without logging in to WP admin or knowing the admin username/password
';
echo '
Modify WordPress address and site address
';
echo '
';
echo '
You can install the script as a preventive measure, so it\'s always available in case of an emergency (don\'t worry, it\'s password protected), or upload it only when needed. On production sites, when big and potentially dangerous changes rarely happen, we suggest uploading it only when needed. On test sites, have it ready in advance because there\'s a higher probability that you\'ll need it. Emergency Recovery Script is a WP Reset PRO tool.
' . __('We are very active on the official WP Reset support forum. If you found a bug, have a feature idea or just want to say hi - please drop by. We love to hear back from our users.', 'wp-reset') . '
Need urgent support? Have one of our devs personally help you with your issue. All PRO license holders have access to premium email support. Get WP Reset PRO now.
';
echo '
';
echo '
'; // email support
echo '
';
WP_Reset_Utility::wp_kses_wf($this->get_card_header(__('Care to Help Out?', 'wp-reset'), 'support-help-out', array('collapse_button' => false)));
echo '
';
WP_Reset_Utility::wp_kses_wf('
' . __('No need for donations :) If you can give us a five star rating you\'ll help out more than you can imagine. A public mention @webfactoryltd also does wonders. Thank you!', 'wp-reset') . '
');
echo '
';
echo '
'; // help out
} // tab_support
/**
* Echoes content for pro tab
*
* @return null
*/
private function tab_pro()
{
$agency_lifetime = $this->generate_web_link('pricing-table', '/buy/', array('p' => 'wp-reset-pro-agency-ltd-launch'));
$team_lifetime = $this->generate_web_link('pricing-table', '/', array(), 'pricing');
$personal_lifetime = $this->generate_web_link('pricing-table', '/buy/', array('p' => 'wp-reset-pro-personal-ltd-launch'));
echo '
More ways to reset your site, more tools, automatic snapshots, collections, email support and the emergency recover script - that\'s WP Reset PRO in a nutshell. The same quality and easy-of-use you experienced in the free version is very much a part of the PRO one, but extended and upgraded with more tools that will save you even more time.
';
echo '
WP Reset PRO is aimed towards webmasters, agencies, and everyone who buildsa a lot of WordPress sites. It\'s much, much more than a "reset" tool. It\'s an easy way to start a new site, to test changes and to get out of the thickest jams. And thanks to its cloud features and the Dashboard it\'ll give you access to collections and snapshots on all the sites you\'re working on - instantly, without dragging any files along.
';
echo '
Give WP Reset PRO a go. It\'ll pay itself out in hours saved within the first few days!
';
echo '
If you already have a PRO license, activate it below.
License key is visible on the confirmation screen, right after purchasing. You can also find it in the confirmation email sent to the email address provided on purchase. Or use keys created with the license manager.
Thank you for purchasing WP Reset PRO! Your license has been verified and activated.';
echo ' To start using the PRO version, please follow these steps:
By attempting to activate a license you agree to share the following data with WebFactory Ltd: license key, site URL, site title, site WP version, site IP, and WP Reset plugin (free) version.';
echo '
';
echo '
';
echo '
'; // activate PRO
WP_Reset_Utility::wp_kses_wf($this->pro_dialog());
} // tab_pro
function pro_dialog()
{
$out = '';
$out .= '
';
$out .= '
';
$out .= 'Limited PRO Offer - all prices are LIFETIME! Pay once & use forever!';
$out .= '
A snapshot is a copy of all WP database tables, standard and custom ones, saved in the site\'s database. Watch a short video overview and tutorial about Snapshots.
';
echo '
Snapshots are primarily a development tool. When using various reset tools we advise using our 1-click snapshot tool available in every tool\'s confirmation dialog. If a full backup that includes files is needed, use one of the backup plugins from the repo.
';
echo '
Use snapshots to find out what changes a plugin made to your database or to quickly restore the dev environment after testing database related changes. Restoring a snapshot does not affect other snapshots, or WP Reset settings.
';
echo '
To automatically generate snapshots on plugin, theme, and core update, activate, deactivate and similar events enable automatic snapshots available in WP Reset PRO.
';
$tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
if (is_array($tables)) {
foreach ($tables as $table) {
if (0 !== stripos($table[0], $wpdb->prefix)) {
continue;
}
if (in_array($table[0], $this->core_tables)) {
$tbl_core++;
} else {
$tbl_custom++;
}
} // foreach
echo '
Currently used WordPress tables, prefixed with ' . esc_html($wpdb->prefix) . ', consist of ' . esc_html($tbl_core) . ' standard and ';
if ($tbl_custom) {
echo esc_attr($tbl_custom) . ' custom table' . ($tbl_custom == 1 ? '' : 's');
} else {
echo 'no custom tables';
}
echo ' (show details)';
} else {
echo 'Tables information is not available. Something is not working properly on your site. Snapshots won\'t work.';
}
echo '
WP Reset PRO creates automatic snapshots before significant events occur on your site that can cause it to stop working correctly. Plugin, theme and core updates, plugin and theme activations and deactivations all of those can happen in the background without your knowledge. With automatic snapshots, you can roll back any update with a single click. Snapshots can be uploaded to the WP Reset Cloud, Dropbox, Google Drive or pCloud, giving you an extra layer of security.
Upgrade to WP Reset PRO to enable automatic snapshots and give your site an extra layer of safety.
';
echo '
';
echo '
';
} // tab_snapshots
/**
* Helper function for generating links
*
* @param string $placement Optional. UTM content param.
* @param string $page Optional. Page to link to.
* @param array $params Optional. Extra URL params.
* @param string $anchor Optional. URL anchor part.
*
* @return string
*/
function generate_web_link($placement = '', $page = '/', $params = array(), $anchor = '')
{
$base_url = 'https://wpreset.com';
if ('/' != $page) {
$page = '/' . trim($page, '/') . '/';
}
if ($page == '//') {
$page = '/';
}
if ($placement) {
$placement = trim($placement);
$placement = '-' . $placement;
}
$parts = array_merge(array('ref' => 'wp-reset-free'. $placement), $params);
if (!empty($anchor)) {
$anchor = '#' . trim($anchor, '#');
}
$out = $base_url . $page . '?' . http_build_query($parts, '', '&') . $anchor;
return $out;
} // generate_web_link
/**
* Returns all saved snapshots from DB
*
* @return array
*/
function get_snapshots()
{
$snapshots = get_option('wp-reset-snapshots', array());
return $snapshots;
} // get_snapshots
/**
* Returns all custom table names, with prefix
*
* @return array
*/
function get_custom_tables()
{
global $wpdb;
$custom_tables = array();
$table_status = $wpdb->get_results('SHOW TABLE STATUS');
if (is_array($table_status)) {
foreach ($table_status as $index => $table) {
if (0 !== stripos($table->Name, $wpdb->prefix)) {
continue;
}
if (empty($table->Engine)) {
continue;
}
if (false === in_array($table->Name, $this->core_tables)) {
$custom_tables[] = array('name' => $table->Name, 'rows' => $table->Rows, 'data_length' => $table->Data_length, 'index_length' => $table->Index_length);
}
} // foreach
}
return $custom_tables;
} // get_custom tables
/**
* Creates snapshot of current tables by copying them in the DB and saving metadata.
*
* @param int $name Optional. Name for the new snapshot.
*
* @return array|WP_Error Snapshot details in array on success, or error object on fail.
*/
function do_create_snapshot($name = '')
{
global $wpdb;
$snapshots = $this->get_snapshots();
$snapshot = array();
$uid = $this->generate_snapshot_uid();
$tbl_core = $tbl_custom = $tbl_size = $tbl_rows = 0;
if (!$uid) {
return new WP_Error(1, 'Unable to generate a valid snapshot UID.');
}
if ($name) {
$snapshot['name'] = substr(trim($name), 0, 64);
} else {
$snapshot['name'] = '';
}
$snapshot['uid'] = $uid;
$snapshot['timestamp'] = current_time('mysql');
$table_status = $wpdb->get_results('SHOW TABLE STATUS');
if (is_array($table_status)) {
foreach ($table_status as $index => $table) {
if (0 !== stripos($table->Name, $wpdb->prefix)) {
continue;
}
if (empty($table->Engine)) {
continue;
}
$tbl_rows += $table->Rows;
$tbl_size += $table->Data_length + $table->Index_length;
if (in_array($table->Name, $this->core_tables)) {
$tbl_core++;
} else {
$tbl_custom++;
}
$wpdb->wpreset_snapshot_table_name = $table->Name;
$wpdb->wpreset_snapshot_table_copy_name = $uid . '_' . $table->Name;
$wpdb->query("OPTIMIZE TABLE " . $wpdb->wpreset_snapshot_table_name);
$wpdb->query("CREATE TABLE " . $wpdb->wpreset_snapshot_table_copy_name . " LIKE " . $wpdb->wpreset_snapshot_table_name);
$wpdb->query("INSERT " . $wpdb->wpreset_snapshot_table_copy_name . " SELECT * FROM " . $wpdb->wpreset_snapshot_table_name);
} // foreach
} else {
return new WP_Error(1, 'Can\'t get table status data.');
}
$snapshot['tbl_core'] = $tbl_core;
$snapshot['tbl_custom'] = $tbl_custom;
$snapshot['tbl_rows'] = $tbl_rows;
$snapshot['tbl_size'] = $tbl_size;
$snapshots[$uid] = $snapshot;
update_option('wp-reset-snapshots', $snapshots);
do_action('wp_reset_create_snapshot', $uid, $snapshot);
return $snapshot;
} // create_snapshot
/**
* Delete snapshot metadata and tables from DB
*
* @param string $uid Snapshot unique 6-char ID.
*
* @return bool|WP_Error True on success, or error object on fail.
*/
function do_delete_snapshot($uid = '')
{
global $wpdb;
$snapshots = $this->get_snapshots();
if (strlen($uid) != 6) {
return new WP_Error(1, 'Invalid UID format.');
}
if (!isset($snapshots[$uid])) {
return new WP_Error(1, 'Unknown snapshot ID.');
}
$tables = $wpdb->get_col($wpdb->prepare('SHOW TABLES LIKE %s', array($uid . '\_%')));
foreach ($tables as $table) {
$wpdb->wpreset_snapshot_table = $table;
$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->wpreset_snapshot_table);
}
$snapshot_copy = $snapshots[$uid];
unset($snapshots[$uid]);
update_option('wp-reset-snapshots', $snapshots);
do_action('wp_reset_delete_snapshot', $uid, $snapshot_copy);
return true;
} // delete_snapshot
/**
* Exports snapshot as SQL dump; saved in gzipped file in WP_CONTENT folder.
*
* @param string $uid Snapshot unique 6-char ID.
*
* @return string|WP_Error Export base filename, or error object on fail.
*/
function do_export_snapshot($uid = '')
{
$snapshots = $this->get_snapshots();
if (strlen($uid) != 6) {
return new WP_Error(1, 'Invalid snapshot ID format.');
}
if (!isset($snapshots[$uid])) {
return new WP_Error(1, 'Unknown snapshot ID.');
}
require_once $this->plugin_dir . 'libs/dumper.php';
$snapshot_file_uid = md5($this->generate_snapshot_uid(10));
try {
$world_dumper = WPR_Shuttle_Dumper::create(array(
'host' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASSWORD,
'db_name' => DB_NAME,
));
$folder = wp_mkdir_p(trailingslashit(WP_CONTENT_DIR) . $this->snapshots_folder);
if (!$folder) {
return new WP_Error(1, 'Unable to create wp-content/' . $this->snapshots_folder . '/ folder.');
}
$htaccess_content = 'AddType application/octet-stream .gz' . PHP_EOL;
$htaccess_content .= 'Options -Indexes' . PHP_EOL;
$htaccess_file = @fopen(trailingslashit(WP_CONTENT_DIR) . $this->snapshots_folder . '/.htaccess', 'w');
if ($htaccess_file) {
fputs($htaccess_file, $htaccess_content);
fclose($htaccess_file);
}
$world_dumper->dump(trailingslashit(WP_CONTENT_DIR) . $this->snapshots_folder . '/wp-reset-snapshot-' . $snapshot_file_uid . '.sql.gz', $uid . '_');
} catch (Shuttle_Exception $e) {
return new WP_Error(1, 'Couldn\'t create snapshot: ' . $e->getMessage());
}
do_action('wp_reset_export_snapshot', 'wp-reset-snapshot-' . $snapshot_file_uid . '.sql.gz');
return 'wp-reset-snapshot-' . $snapshot_file_uid . '.sql.gz';
} // export_snapshot
/**
* Replace current tables with ones in snapshot.
*
* @param string $uid Snapshot unique 6-char ID.
*
* @return bool|WP_Error True on success, or error object on fail.
*/
function do_restore_snapshot($uid = '')
{
global $wpdb;
$new_tables = array();
$snapshots = $this->get_snapshots();
if (($res = $this->verify_snapshot_integrity($uid)) !== true) {
return $res;
}
$table_status = $wpdb->get_results('SHOW TABLE STATUS');
if (is_array($table_status)) {
foreach ($table_status as $index => $table) {
if (0 !== stripos($table->Name, $uid . '_')) {
continue;
}
if (empty($table->Engine)) {
continue;
}
$new_tables[] = $table->Name;
} // foreach
} else {
return new WP_Error(1, 'Can\'t get table status data.');
}
foreach ($table_status as $index => $table) {
if (0 !== stripos($table->Name, $wpdb->prefix)) {
continue;
}
if (empty($table->Engine)) {
continue;
}
$wpdb->wpreset_snapshot_table = $table->Name;
$wpdb->query('DROP TABLE ' . $wpdb->wpreset_snapshot_table);
} // foreach
// copy snapshot tables to original name
foreach ($new_tables as $table) {
$wpdb->wpreset_snapshot_table = $table;
$wpdb->wpreset_snapshot_table_new = str_replace($uid . '_', '', $table);
$wpdb->query("CREATE TABLE " . $wpdb->wpreset_snapshot_table_new . " LIKE " . $wpdb->wpreset_snapshot_table);
$wpdb->query("INSERT " . $wpdb->wpreset_snapshot_table_new . " SELECT * FROM " . $wpdb->wpreset_snapshot_table);
}
wp_cache_flush();
update_option('wp-reset', $this->options);
update_option('wp-reset-snapshots', $snapshots);
do_action('wp_reset_restore_snapshot', $uid);
return true;
} // restore_snapshot
/**
* Verifies snapshot integrity by comparing metadata and data in DB
*
* @param string $uid Snapshot unique 6-char ID.
*
* @return bool|WP_Error True on success, or error object on fail.
*/
function verify_snapshot_integrity($uid)
{
global $wpdb;
$tbl_core = $tbl_custom = 0;
$snapshots = $this->get_snapshots();
if (strlen($uid) != 6) {
return new WP_Error(1, 'Invalid snapshot ID format.');
}
if (!isset($snapshots[$uid])) {
return new WP_Error(1, 'Unknown snapshot ID.');
}
$snapshot = $snapshots[$uid];
$table_status = $wpdb->get_results('SHOW TABLE STATUS');
if (is_array($table_status)) {
foreach ($table_status as $index => $table) {
if (0 !== stripos($table->Name, $uid . '_')) {
continue;
}
if (empty($table->Engine)) {
continue;
}
if (in_array(str_replace($uid . '_', '', $table->Name), $this->core_tables)) {
$tbl_core++;
} else {
$tbl_custom++;
}
} // foreach
if ($tbl_core != $snapshot['tbl_core'] || $tbl_custom != $snapshot['tbl_custom']) {
return new WP_Error(1, 'Snapshot data has been compromised. Saved metadata does not match data in the DB. Contact WP Reset support if data is critical, or restore it via a MySQL GUI.');
}
} else {
return new WP_Error(1, 'Can\'t get table status data.');
}
return true;
} // verify_snapshot_integrity
/**
* Compares a selected snapshot with the current table set in DB
*
* @param string $uid Snapshot unique 6-char ID.
*
* @return string|WP_Error Formatted table with details on success, or error object on fail.
*/
function do_compare_snapshots($uid)
{
global $wpdb;
$current = $snapshot = array();
$out = $out2 = $out3 = '';
if (($res = $this->verify_snapshot_integrity($uid)) !== true) {
return $res;
}
$table_status = $wpdb->get_results('SHOW TABLE STATUS');
foreach ($table_status as $index => $table) {
if (empty($table->Engine)) {
continue;
}
if (0 !== stripos($table->Name, $uid . '_') && 0 !== stripos($table->Name, $wpdb->prefix)) {
continue;
}
$info = array();
$info['rows'] = $table->Rows;
$info['size_data'] = $table->Data_length;
$info['size_index'] = $table->Index_length;
$wpdb->wpreset_table_name = $table->Name;
$schema = $wpdb->get_row('SHOW CREATE TABLE ' . $wpdb->wpreset_table_name, ARRAY_N);
$info['schema'] = $schema[1];
$info['engine'] = $table->Engine;
$info['fullname'] = $table->Name;
$basename = str_replace(array($uid . '_'), array(''), $table->Name);
$info['basename'] = $basename;
$info['corename'] = str_replace(array($wpdb->prefix), array(''), $basename);
$info['uid'] = $uid;
if (0 === stripos($table->Name, $uid . '_')) {
$snapshot[$basename] = $info;
}
if (0 === stripos($table->Name, $wpdb->prefix)) {
$info['uid'] = '';
$current[$basename] = $info;
}
} // foreach
$in_both = array_keys(array_intersect_key($current, $snapshot));
$in_current_only = array_diff_key($current, $snapshot);
$in_snapshot_only = array_diff_key($snapshot, $current);
$out .= '
';
foreach ($in_current_only as $table) {
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
' . $table['fullname'] . '
';
$out .= '
table is not present in snapshot
';
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
' . number_format($table['rows']) . ' row' . ($table['rows'] == 1 ? '' : 's') . ' totaling ' . WP_Reset_Utility::format_size($table['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($table['size_index']) . ' in index.
';
$out .= '
' . $table['schema'] . '
';
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
';
} // foreach in current only
foreach ($in_snapshot_only as $table) {
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
table is not present in current tables
';
$out .= '
' . esc_html($table['fullname']) . '
';
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
';
$out .= '
' . number_format($table['rows']) . ' row' . ($table['rows'] == 1 ? '' : 's') . ' totaling ' . WP_Reset_Utility::format_size($table['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($table['size_index']) . ' in index.
' . $tbl_current['fullname'] . ' table schemas do not match
';
$out2 .= '
' . $tbl_snapshot['fullname'] . ' table schemas do not match
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
' . number_format($tbl_current['rows']) . ' rows totaling ' . WP_Reset_Utility::format_size($tbl_current['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($tbl_current['size_index']) . ' in index.
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
' . number_format($tbl_snapshot['rows']) . ' rows totaling ' . WP_Reset_Utility::format_size($tbl_snapshot['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($tbl_snapshot['size_index']) . ' in index.
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= $diff->Render($renderer);
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
} else {
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
' . $tbl_current['fullname'] . ' data in tables does not match
';
$out2 .= '
' . $tbl_snapshot['fullname'] . ' data in tables does not match
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
' . number_format($tbl_current['rows']) . ' rows totaling ' . WP_Reset_Utility::format_size($tbl_current['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($tbl_current['size_index']) . ' in index.
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
' . number_format($tbl_snapshot['rows']) . ' rows totaling ' . WP_Reset_Utility::format_size($tbl_snapshot['size_data']) . ' in data and ' . WP_Reset_Utility::format_size($tbl_snapshot['size_index']) . ' in index.
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
$out2 .= '
';
if ($tbl_current['corename'] == 'options') {
$ss_prefix = $tbl_snapshot['uid'] . '_' . $wpdb->prefix;
$diff_rows = $wpdb->get_results("SELECT {$wpdb->prefix}options.option_name, {$wpdb->prefix}options.option_value AS current_value, {$ss_prefix}options.option_value AS snapshot_value FROM {$wpdb->prefix}options LEFT JOIN {$ss_prefix}options ON {$ss_prefix}options.option_name = {$wpdb->prefix}options.option_name WHERE {$wpdb->prefix}options.option_value != {$ss_prefix}options.option_value LIMIT 100;");
$only_current = $wpdb->get_results("SELECT {$wpdb->prefix}options.option_name, {$wpdb->prefix}options.option_value AS current_value, {$ss_prefix}options.option_value AS snapshot_value FROM {$wpdb->prefix}options LEFT JOIN {$ss_prefix}options ON {$ss_prefix}options.option_name = {$wpdb->prefix}options.option_name WHERE {$ss_prefix}options.option_value IS NULL LIMIT 100;");
$only_snapshot = $wpdb->get_results("SELECT {$wpdb->prefix}options.option_name, {$wpdb->prefix}options.option_value AS current_value, {$ss_prefix}options.option_value AS snapshot_value FROM {$wpdb->prefix}options LEFT JOIN {$ss_prefix}options ON {$ss_prefix}options.option_name = {$wpdb->prefix}options.option_name WHERE {$wpdb->prefix}options.option_value IS NULL LIMIT 100;");
$out2 .= '
';
wp_cache_flush();
$upgrader = new Plugin_Upgrader();
echo 'Check if WP Force SSL is already installed ... ';
if ($this->is_plugin_installed($plugin_slug)) {
echo 'WP Force SSL is already installed!
Making sure it\'s the latest version. ';
$upgrader->upgrade($plugin_slug);
$installed = true;
} else {
echo 'Installing WP Force SSL. ';
$installed = $upgrader->install($plugin_zip);
}
wp_cache_flush();
if (!is_wp_error($installed) && $installed) {
echo 'Activating WP Force SSL. ';
$activate = activate_plugin($plugin_slug);
if (is_null($activate)) {
echo 'WP Force SSL Activated. ';
echo '';
echo ' If you are not redirected in a few seconds - click here.';
}
} else {
echo 'Could not install WP Force SSL. You\'ll have to download and install manually.';
}
echo '
';
} // install_wpfssl
/**
* Clean up on uninstall; no action on deactive at the moment
*
* @return null
*/
static function uninstall()
{
delete_option('wp-reset');
delete_option('wp-reset-snapshots');
} // uninstall
/**
* Disabled; we use singleton pattern so magic functions need to be disabled
*
* @return null
*/
function __clone()
{
}
/**
* Disabled; we use singleton pattern so magic functions need to be disabled
*
* @return null
*/
function __sleep()
{
}
/**
* Disabled; we use singleton pattern so magic functions need to be disabled
*
* @return null
*/
function __wakeup()
{
}
} // WP_Reset class
// Create plugin instance and hook things up
// Only if in admin - plugin has no frontend functionality
if (is_admin() || WP_Reset::is_cli_running()) {
global $wp_reset;
$wp_reset = WP_Reset::getInstance();
add_action('plugins_loaded', array($wp_reset, 'load_textdomain'));
register_uninstall_hook(__FILE__, array('WP_Reset', 'uninstall'));
}