version = $this->get_plugin_version(); $this->plugin_dir = plugin_dir_path(__FILE__); $this->plugin_url = plugin_dir_url(__FILE__); $this->load_options(); 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('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('wp_ajax_wp_reset_submit_survey', array($this, 'ajax_submit_survey')); add_action('admin_action_install_webhooks', array($this, 'install_webhooks')); add_action('admin_print_scripts', array($this, 'remove_admin_notices')); 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')); add_filter('install_plugins_table_api_args_featured', array($this, 'featured_plugins_tab')); $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['last_backup_filename'])) { $options['last_backup_filename'] = ''; $change = true; } if (!isset($options['last_backup_timestamp'])) { $options['last_backup_timestamp'] = 0; $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 * * todo: not completed * * @param string $key Optional. * * @return array */ function get_options($key = '') { return $this->options['options']; } // get_options /** * Update plugin options, currently entire array * * todo: this handles the entire options array although it should only do the options part - it's confusing * * @param string $key Data to save. * @param string $data Option key. * * @return bool */ function update_options($key, $data) { $this->options[$key] = $data; $tmp = update_option('wp-reset', $this->options); return $tmp; } // set_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('administrator')) { wp_send_json_error(__('You are not allowed to run this action.', 'wp-reset')); } $notice_name = trim(@$_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 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(); $meta = $this->get_meta(); foreach ($dismissed_notices as $notice_name => $tmp) { if ($tmp) { unset($pointers[$notice_name]); } } // foreach if (!empty($pointers) && !$this->is_plugin_page() && current_user_can('administrator')) { $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; } // features survey is shown 5min after install or after first reset $survey = false; if ($this->is_survey_active('features')) { $survey = true; } $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'), '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'), 'open_survey' => $survey, 'ok_button' => __('OK', 'wp-reset'), 'creating_backup' => __('Creating backup. Please wait.', '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. There is NO UNDO. Always create a backup.', 'wp-reset'), 'confirm2' => __('Click "Cancel" to abort.', 'wp-reset'), 'doing_reset' => __('Resetting in progress. Please wait.', 'wp-reset'), 'backup_not_accessible' => __('Backup was created but it\'s not accessible.', 'wp-reset'), 'backup_not_accessible_details' => __('Please check your folder access rights. File should be accessible on this URL.', 'wp-reset'), 'nonce_dismiss_notice' => wp_create_nonce('wp-reset_dismiss_notice'), 'nonce_run_tool' => wp_create_nonce('wp-reset_run_tool'), 'nonce_do_reset' => wp_create_nonce('wp-reset_do_reset'), ); if ($survey) { $js_localize['nonce_submit_survey'] = wp_create_nonce('wp-reset_submit_survey'); } if (!$this->is_webhooks_active()) { $js_localize['webhooks_install_url'] = add_query_arg(array('action' => 'install_webhooks'), admin_url('admin.php')); } 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_script('plugin-install'); wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_script('jquery-ui-tabs'); 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'); } // 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 /** * Submit user selected survey answers to WPR servers * * @return null */ function ajax_submit_survey() { check_ajax_referer('wp-reset_submit_survey'); $meta = $this->get_meta(); $vars = wp_parse_args($_POST, array('survey' => '', 'answers' => '', 'custom_answer' => '', 'emailme' => '')); $vars['answers'] = trim($vars['answers'], ','); $vars['custom_answer'] = substr(trim(strip_tags($vars['custom_answer'])), 0, 256); if (empty($vars['survey']) || empty($vars['answers'])) { wp_send_json_error(); } $request_params = array('sslverify' => false, 'timeout' => 15, 'redirection' => 2); $request_args = array( 'action' => 'submit_survey', 'survey' => $vars['survey'], 'email' => $vars['emailme'], 'answers' => $vars['answers'], 'custom_answer' => $vars['custom_answer'], 'first_version' => $meta['first_version'], 'version' => $this->version, 'codebase' => 'free', 'site' => get_home_url() ); $url = add_query_arg($request_args, $this->licensing_servers[0]); $response = wp_remote_get(esc_url_raw($url), $request_params); if (is_wp_error($response) || !wp_remote_retrieve_body($response)) { $url = add_query_arg($request_args, $this->licensing_servers[1]); $response = wp_remote_get(esc_url_raw($url), $request_params); } $this->dismiss_notice('survey-' . $vars['survey']); wp_send_json_success(); } // ajax_submit_survey /** * Check if named survey should be shown or not * * @param [string] $survey_name Name of the survey to check * @return boolean */ function is_survey_active($survey_name) { if (empty($survey_name)) { return false; } // all surveys are curently disabled return false; if ($this->get_dismissed_notices('survey-' . $survey_name)) { return false; } $meta = $this->get_meta(); if (current_time('timestamp', true) - $meta['first_install'] > 300 || $meta['reset_count'] > 0) { return true; } return false; } // is_survey_active /** * 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 core WP Webhooks and WPR addon plugins are installed and activated * * @return bool */ function is_webhooks_active() { if (!function_exists('is_plugin_active') || !function_exists('get_plugin_data')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if (false == is_plugin_active('wp-webhooks/wp-webhooks.php')) { return false; } if (false == is_plugin_active('wpwh-wp-reset-webhook-integration/wpwhpro-wp-reset-webhook-integration.php')) { return false; } return true; } // is_webhooks_active /** * 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 /** * Auto download/install/upgrade/activate WP Webhooks plugin * * @return null */ static function install_webhooks() { $plugin_slug = 'wp-webhooks/wp-webhooks.php'; $plugin_zip = 'https://downloads.wordpress.org/plugin/wp-webhooks.latest-stable.zip'; @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'; echo ''; echo '
'; echo 'If things are not done in a minute install the plugin manually via Plugins page

'; wp_cache_flush(); $upgrader = new Plugin_Upgrader(); echo 'Check if WP Webhooks plugin is already installed ...
'; if (self::is_plugin_installed($plugin_slug)) { echo 'WP Webhooks is already installed!
Making sure it\'s the latest version.
'; $upgrader->upgrade($plugin_slug); $installed = true; } else { echo 'Installing WP Webhooks.
'; $installed = $upgrader->install($plugin_zip); } wp_cache_flush(); if (!is_wp_error($installed) && $installed) { echo 'Activating WP Webhooks.
'; $activate = activate_plugin($plugin_slug); if (is_null($activate)) { echo 'WP Webhooks activated.
'; } } else { echo 'Could not install WP Webhooks. You\'ll have to download and install manually.'; } $plugin_slug = 'wpwh-wp-reset-webhook-integration/wpwhpro-wp-reset-webhook-integration.php'; $plugin_zip = 'https://downloads.wordpress.org/plugin/wpwh-wp-reset-webhook-integration.latest-stable.zip'; wp_cache_flush(); $upgrader = new Plugin_Upgrader(); echo '
Check if WP Webhooks WPR addon plugin is already installed ...
'; if (self::is_plugin_installed($plugin_slug)) { echo 'WP Webhooks WPR addon is already installed!
Making sure it\'s the latest version.
'; $upgrader->upgrade($plugin_slug); $installed = true; } else { echo 'Installing WP Webhooks WPR addon.
'; $installed = $upgrader->install($plugin_zip); } wp_cache_flush(); if (!is_wp_error($installed) && $installed) { echo 'Activating WP Webhooks WPR addon.
'; $activate = activate_plugin($plugin_slug); if (is_null($activate)) { echo 'WP Webhooks WPR addon activated.
'; echo ''; echo '
If you are not redirected in a few seconds - click here.'; } } else { echo 'Could not install WP Webhooks WPR addon. You\'ll have to download and install manually.'; } echo '
'; } // install_webhooks /** * 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 /** * 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; $count = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'theme_mods\_%' OR option_name LIKE 'mods\_%'"); 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 and delete all plugins * * @param bool $keep_wp_reset Keep WP Reset active and installed * @param bool $silent_deactivate Skip individual plugin deactivation functions when deactivating * * @return int Number of deleted plugins. */ function do_delete_plugins($keep_wp_reset = true, $silent_deactivate = false) { 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(__FILE__); $all_plugins = get_plugins(); $active_plugins = (array) get_option('active_plugins', array()); if (true == $keep_wp_reset) { if (($key = array_search($wp_reset_basename, $active_plugins)) !== false) { unset($active_plugins[$key]); } unset($all_plugins[$wp_reset_basename]); } if (!empty($active_plugins)) { deactivate_plugins($active_plugins, $silent_deactivate, false); } if (!empty($all_plugins)) { delete_plugins(array_keys($all_plugins)); } do_action('wp_reset_delete_plugins', $all_plugins, $all_plugins); 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 = 'twentynineteen'; } $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->query('TRUNCATE TABLE ' . $tbl['name']); } // 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->query('DROP TABLE IF EXISTS ' . $tbl['name']); } // 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('administrator')) { wp_send_json_error(__('You are not allowed to run this action.', 'wp-reset')); } $tool = trim(@$_GET['tool']); $extra_data = trim(@$_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 == 'delete_themes') { $cnt = $this->do_delete_themes(false); wp_send_json_success($cnt); } elseif ($tool == 'delete_plugins') { $cnt = $this->do_delete_plugins(true); 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 == 'download_backup') { $res = $this->do_create_backup(); if (is_wp_error($res)) { wp_send_json_error($res->get_error_message()); } else { $url = content_url() . '/' . $this->backups_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(); } } else { wp_send_json_error(__('Unknown tool.', 'wp-reset')); } } // ajax_run_tool /** * 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('administrator')) { 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 // todo: use params to determine what gets 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_option('active_plugins'); $active_theme = wp_get_theme(); if (!empty($params['reactivate_webhooks'])) { $wpwh1 = get_option('wpwhpro_active_webhooks'); $wpwh2 = get_option('wpwhpro_activate_translations'); $wpwh3 = get_option('ironikus_webhook_webhooks'); } // 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("SHOW TABLES LIKE '{$prefix}%'"); foreach ($tables as $table) { $wpdb->query("DROP TABLE $table"); } // supress errors for WP_CLI // todo: find a better way to supress errors and send/not send email on reset $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 = '' WHERE ID = %d LIMIT 1", array($current_user->user_pass, $user_id)); $wpdb->query($query); // 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 WP Webhooks if (!empty($params['reactivate_webhooks'])) { activate_plugin('wp-webhooks/wp-webhooks.php'); activate_plugin('wpwh-wp-reset-webhook-integration/wpwhpro-wp-reset-webhook-integration.php'); update_option('wpwhpro_active_webhooks', $wpwh1); update_option('wpwhpro_activate_translations', $wpwh2); update_option('ironikus_webhook_webhooks', $wpwh3); } // 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_redirect(admin_url() . '?wp-reset=success'); exit; } } // do_reinstall /** * Checks wp_reset post value and performs all actions * todo: handle messages for various actions * * @return null|bool */ function do_all_actions() { // only admins can perform actions if (!current_user_can('administrator')) { return; } if (!empty($_GET['wp-reset']) && stristr($_SERVER['HTTP_REFERER'], 'wp-reset')) { add_action('admin_notices', array($this, 'notice_successful_reset')); } // check nonce if (true === isset($_POST['wp_reset_confirm']) && false === wp_verify_nonce(@$_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' !== $_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' === $_POST['wp_reset_confirm']) { $defaults = array( 'reactivate_theme' => '0', 'reactivate_plugins' => '0', 'reactivate_wpreset' => '0', 'reactivate_webhooks' => '0' ); $params = shortcode_atts($defaults, (array) @$_POST['wpr-post-reset']); $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 = '' . __('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 ($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; echo '

' . sprintf(__('Site has been reset to default settings. User "%s" was restored with the password unchanged. Open WP Reset to do another reset.', 'wp-reset'), $current_user->user_login, admin_url('tools.php?page=wp-reset')) . '

'; } // notice_successful_reset /** * Generates a button that initiates backup creation and download * * @return string */ function get_backup_button($tool_id = '') { $out = ''; $out .= 'Create & download backup'; return $out; } // get_backup_button /** * Outputs complete plugin's admin page * * @return null */ function plugin_page() { // double check for admin privileges if (!current_user_can('administrator')) { wp_die(__('Sorry, you are not allowed to access this page.', 'wp-reset')); } echo '
'; echo '
'; echo '
'; echo '
'; echo '' . __('WP Reset', 'wp-reset') . ''; echo 'Grab the limited LTD offer!'; echo '
'; echo '
'; echo '
'; echo ''; echo '
'; echo '
'; $this->custom_notifications(); echo '
'; echo '
'; // wpr-notifications // tabs echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; // content echo '
'; // container echo '
'; // wp-reset-tabs echo '
'; echo '
'; // wrap // survey if ($this->is_survey_active('features')) { echo ''; } // survey if (!$this->is_webhooks_active()) { echo ''; } } // plugin_page /** * Echoes all custom plugin notitications * * @return null */ private function custom_notifications() { $notice_shown = false; $meta = $this->get_meta(); $snapshots = $this->get_snapshots(); // warn that WPR is not WPMU compatible if (false === $notice_shown && is_multisite()) { echo '
'; echo '

' . __('WP Reset is not compatible with multisite!', 'wp-reset') . '

'; echo '

' . __('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') . '

'; echo '
'; $notice_shown = true; } // ask for review if ((!empty($meta['reset_count']) || !empty($snapshots) || current_time('timestamp', true) - $meta['first_install'] > WEEK_IN_SECONDS) && false === $notice_shown && false == $this->get_dismissed_notices('rate') ) { echo '
'; echo '

' . __('Please help us spread the word & keep the plugin up-to-date', 'wp-reset') . '

'; echo '

' . __('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') . '

'; echo '

' . __('Rate the plugin ★★★★★', 'wp-reset') . ' ' . __('I\'ve already rated it', 'wp-reset') . '

'; echo '
'; $notice_shown = true; } } // custom_notifications /** * Echoes content for reset tab * * @return null */ private function tab_reset() { global $current_user, $wpdb; echo '
'; echo '

'; echo __('Please read carefully before proceeding. There is NO UNDO!', 'wp-reset'); echo '
'; echo '

'; echo '

' . __('Resetting will delete:', 'wp-reset') . '

'; echo ''; echo '

' . __('Resetting will not delete:', 'wp-reset') . '

'; echo ''; echo '

' . __('What happens when I click the Reset button?', 'wp-reset') . '

'; echo ''; echo '

' . __('WP-CLI Support', 'wp-reset') . '
'; echo '' . sprintf(__('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(__('All actions have to be confirmed. If you want to skip confirmation use the standard %s option. Please be careful - there is NO UNDO.', 'wp-reset'), '--yes') . '

'; echo '

' . __('WP Webhooks Support', 'wp-reset') . '
'; echo 'All WP Reset tools are integrated with WP Webhooks and available as (receive data) actions. Webhooks are a standard, platform-independent way of connecting WordPress to any 3rd party system. This article has more info, videos and use-cases so you can see just how powerful and easy to use webhooks are.
'; if ($this->is_webhooks_active()) { echo 'WP Webhooks are active. Make sure you enable WP Reset actions in settings.'; } else { echo 'Install WP Webhooks & WPR addon to automate your workflow, develop faster and connect WordPress to any web app or 3rd party system.'; } echo '

'; // card description $theme = wp_get_theme(); echo '
'; echo '

' . __('Reset', 'wp-reset') . '

'; echo '

'; echo '

'; if ($this->is_webhooks_active()) { echo '

'; } echo '

'; if ($this->is_webhooks_active()) { echo '

Configure WP Webhooks to run additional actions after reset, or connect to any 3rd party system.

'; } else { echo '

To run additional actions after reset or automate a complex workflow install WP Webhooks & WPR addon. It\'s a standard, platform-independent way of connecting WordPress to any web app. This short video explains it well.

'; } echo '


' . __('Type reset in the confirmation field to confirm the reset and then click the "Reset WordPress" button.
There is NO UNDO. Always create a backup before deleting anything.', 'wp-reset') . '

'; wp_nonce_field('wp-reset'); echo '

 '; echo '' . __('Reset WordPress', 'wp-reset') . '' . $this->get_backup_button('reset-wordpress') . '

'; echo '
'; } // tab_reset /** * Echoes content for tools tab * * @return null */ private function tab_tools() { echo '
'; echo '

' . __('Delete Transients', 'wp-reset') . '

'; echo '

All transient related database entries will be deleted. Including expired and non-expired transients, and orphaned transient timeout entries.
There is NO UNDO. Always create a backup before deleting anything.

'; echo '

create a backup') . '." 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' . $this->get_backup_button('delete-transients') . '

'; echo '
'; $upload_dir = wp_upload_dir(date('Y/m'), true); $upload_dir['basedir'] = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $upload_dir['basedir']); echo '
'; echo '

' . __('Clean Uploads Folder', 'wp-reset') . '

'; echo '

' . __('All files in ' . $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') . '

'; if (false != $upload_dir['error']) { echo '

Tool is not available. Folder is not writeable by WordPress. Please check file and folder access rights.

'; } else { echo '

Delete all files & folders in uploads folder

'; } echo '
'; echo '
'; echo '

' . __('Reset Theme Options', 'wp-reset') . '

'; echo '

' . __('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.
There is NO UNDO. Always create a backup before deleting anything.', 'wp-reset') . '

'; echo '

create a backup') . '." 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' . $this->get_backup_button('reset-theme-options') . '

'; echo '
'; $theme = wp_get_theme(); echo '
'; echo '

' . __('Delete Themes', 'wp-reset') . '

'; echo '

' . __('All themes will be deleted. Including the currently active theme - ' . $theme->get('Name') . '.
There is NO UNDO. WP Reset does not make any file backups.', 'wp-reset') . '

'; echo '

Delete all themes

'; echo '
'; echo '
'; echo '

' . __('Delete Plugins', 'wp-reset') . '

'; echo '

' . __('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') . '

'; echo '

Delete plugins

'; echo '
'; global $wpdb; $custom_tables = $this->get_custom_tables(); echo '
'; echo '

' . __('Empty or Delete Custom Tables', 'wp-reset') . '

'; echo '

' . __('This action affects only custom tables with ' . $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.
There is NO UNDO. Always create a backup before deleting anything.

', 'wp-reset'); if ($custom_tables) { echo '

' . __('The following ' . sizeof($custom_tables) . ' custom tables are affected by this tool: '); foreach ($custom_tables as $tbl) { echo '' . $tbl['name'] . ''; if (next($custom_tables)) { echo ', '; } } // foreach echo '.

'; $custom_tables_btns = ''; } else { echo '

' . __('There are no custom tables. There\'s nothing for this tool to empty or delete.', 'wp-reset') . '

'; $custom_tables_btns = ' disabled'; } echo '

create a backup') . '." data-text-done="%n custom tables have been emptied." data-text-done-singular="One custom table has been emptied." class="button button-delete' . $custom_tables_btns . '" href="#" id="truncate-custom-tables">Empty (truncate) custom tables'; echo 'create a backup') . '." data-text-done="%n custom tables have been deleted." data-text-done-singular="One custom table has been deleted." class="button button-delete' . $custom_tables_btns . '" href="#" id="drop-custom-tables">Delete (drop) custom tables' . $this->get_backup_button('drop-custom-tables') . '

'; echo '
'; echo '
'; echo '

' . __('Delete .htaccess File', 'wp-reset') . '

'; echo '

' . __('This action deletes the .htaccess file located in ' . $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.

'; echo 'Delete .htaccess file

'; echo '
'; } // tab_tools /** * Echoes content for collections tab * * @return null */ private function tab_collections() { echo '
'; echo '

' . __('What are Plugin & Theme Collections?', 'wp-reset') . '

'; echo '

' . __('A tool that\'s coming with WP Reset PRO that will save hours & hours of your precious time! Have a set of plugins (and themes) that you install and activate after every reset? Or on every fresh WP 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') . '

'; echo '


Sign up for the private LTD launch of WP Reset PRO in early November

'; echo '
'; } // tab_collections /** * Echoes content for support tab * * @return null */ private function tab_support() { echo '
'; echo '

' . __('Documentation', 'wp-reset') . '

'; echo '

' . __('All tools and functions 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') . '

'; echo '
'; echo '
'; echo '

' . __('Public support forum', 'wp-reset') . '

'; echo '

' . __('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') . '

'; echo '
'; echo '
'; echo '

' . __('Private contact', 'wp-reset') . '

'; echo '

' . __('If there\'s a need to contact us privately send emails to wpreset@webfactoryltd.com. Please know that although we\'ll gladly have a look at issues you are having with any site, we can\'t promise we\'ll fix them. Thank you for understanding.', 'wp-reset') . '

'; echo '
'; echo '
'; echo '

' . __('Care to help out?', 'wp-reset') . '

'; echo '

' . __('No need for donations or anything like that :) 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 '
'; } // tab_support /** * Echoes content for snapshots tab * * @return null */ private function tab_snapshots() { global $wpdb; $tbl_core = $tbl_custom = $tbl_size = $tbl_rows = 0; echo '
'; echo '

'; echo __('Database Snapshots', 'wp-reset'); echo '
'; echo '

'; echo '

A snapshot is a copy of all WP database tables, standard and custom ones, saved in your database. Files are not saved or included in snapshots in any way. Watch a short video overview and tutorial about Snapshots.

'; echo '

Snapshots are primarily a development tool. Although they can be used for backups (and downloaded), when using various reset tools we advise using our 1-click backup tool available in every tool\'s confirmation dialog. If you need a full backup that includes files, use something like UpdraftPlus.

'; 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.

'; $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++; } } // foreach echo '

Currently used WordPress tables, prefixed with ' . $wpdb->prefix . ', consist of ' . $tbl_core . ' standard and '; if ($tbl_custom) { echo $tbl_custom . ' custom table' . ($tbl_custom == 1 ? '' : 's'); } else { echo 'no custom tables'; } echo ' totaling ' . $this->format_size($tbl_size) . ' in ' . number_format($tbl_rows) . ' rows.

'; } echo ''; echo '
'; echo '
'; echo '

'; echo __('Saved Snapshots', 'wp-reset'); echo ''; echo '

'; if ($snapshots = $this->get_snapshots()) { echo ''; echo ''; foreach ($snapshots as $ss) { echo ''; if (!empty($ss['name'])) { echo ''; $name = $ss['name']; } else { echo ''; $name = 'created on ' . date(get_option('date_format'), strtotime($ss['timestamp'])) . ' @ ' . date(get_option('time_format'), strtotime($ss['timestamp'])); } echo ''; echo ''; echo ''; } // foreach echo '
NameInfo & SizeActions
' . $ss['name'] . '' . '' . date(get_option('date_format'), strtotime($ss['timestamp'])) . '
@ ' . date(get_option('time_format'), strtotime($ss['timestamp'])) . '
' . $ss['tbl_core'] . ' standard & '; if ($ss['tbl_custom']) { echo $ss['tbl_custom'] . ' custom table' . ($ss['tbl_custom'] == 1 ? '' : 's'); } else { echo 'no custom tables'; } echo ' totaling ' . $this->format_size($ss['tbl_size']) . ' in ' . number_format($ss['tbl_rows']) . ' rows'; echo ''; echo ''; echo ''; echo '
'; echo ''; } else { echo '

There are no saved snapshots. Create a new snapshot.

'; } echo '
'; } // tab_snapshots /** * Helper function for generating UTM tagged 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 = '/'; } $parts = array_merge(array('utm_source' => 'wp-reset-free', 'utm_medium' => 'plugin', 'utm_content' => $placement, 'utm_campaign' => 'wp-reset-free-v' . $this->version), $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 /** * Format file size to human readable string * * @param int $bytes Size in bytes to format. * * @return string */ function format_size($bytes) { if ($bytes > 1073741824) { return number_format_i18n($bytes / 1073741824, 2) . ' GB'; } elseif ($bytes > 1048576) { return number_format_i18n($bytes / 1048576, 1) . ' MB'; } elseif ($bytes > 1024) { return number_format_i18n($bytes / 1024, 1) . ' KB'; } else { return number_format_i18n($bytes, 0) . ' bytes'; } } // format_size /** * 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->query('OPTIMIZE TABLE ' . $table->Name); $wpdb->query('CREATE TABLE ' . $uid . '_' . $table->Name . ' LIKE ' . $table->Name); $wpdb->query('INSERT ' . $uid . '_' . $table->Name . ' SELECT * FROM ' . $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->query('DROP TABLE IF EXISTS ' . $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'; try { $world_dumper = 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-' . $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-' . $uid . '.sql.gz'); return 'wp-reset-snapshot-' . $uid . '.sql.gz'; } // export_snapshot /** * Exports DB tables as SQL dump; saved in gzipped file in WP_CONTENT folder. * * @return string|WP_Error Export base filename, or error object on fail. */ function do_create_backup($skip_timestamp_check = false) { require_once $this->plugin_dir . 'libs/dumper.php'; global $wpdb; if ( false == $skip_timestamp_check && !empty($this->options['last_backup_timestamp']) && !empty($this->options['last_backup_filename']) && time() - $this->options['last_backup_timestamp'] < 30 ) { return $this->options['last_backup_filename']; } try { $world_dumper = 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->backups_folder); if (!$folder) { return new WP_Error(1, 'Unable to create wp-content/' . $this->backups_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->backups_folder . '/.htaccess', 'w'); if ($htaccess_file) { fputs($htaccess_file, $htaccess_content); fclose($htaccess_file); } $rand_id = sprintf('%04x', mt_rand(0, 0xFFFF)); $filename = ''; $filename .= 'wp-reset-backup-' . str_replace(array('http://', 'https://', '.'), array('', '', '-'), home_url()); $filename .= '-' . date('Y-m-d-H-i-s', current_time('timestamp')) . '-' . $rand_id; $filename = sanitize_file_name($filename) . '.sql.gz'; $filename = apply_filters('wp_reset_backup_filename', $filename); $world_dumper->dump(trailingslashit(WP_CONTENT_DIR) . $this->backups_folder . '/' . $filename, $wpdb->prefix); } catch (Shuttle_Exception $e) { return new WP_Error(1, 'Couldn\'t create backup: ' . $e->getMessage()); } do_action('wp_reset_create_backup', $filename); $this->update_options('last_backup_timestamp', time()); $this->update_options('last_backup_filename', $filename); return $filename; } // create_backup /** * 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->query('DROP TABLE ' . $table->Name); } // foreach // copy snapshot tables to original name foreach ($new_tables as $table) { $new_name = str_replace($uid . '_', '', $table); $wpdb->query('CREATE TABLE ' . $new_name . ' LIKE ' . $table); $wpdb->query('INSERT ' . $new_name . ' SELECT * FROM ' . $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; $schema = $wpdb->get_row('SHOW CREATE TABLE ' . $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 .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= '
' . $table['fullname'] . 'table is not present in snapshot
'; $out .= '
'; } // foreach in current only foreach ($in_snapshot_only as $table) { $out .= '
'; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= ''; $out .= '
table is not present in current tables' . $table['fullname'] . '
'; $out .= '
'; } // foreach in snapshot only foreach ($in_both as $tablename) { $tbl_current = $current[$tablename]; $tbl_snapshot = $snapshot[$tablename]; $schema1 = preg_replace('/(auto_increment=)([0-9]*) /i', '${1}1 ', $tbl_current['schema'], 1); $schema2 = preg_replace('/(auto_increment=)([0-9]*) /i', '${1}1 ', $tbl_snapshot['schema'], 1); $tbl_snapshot['tmp_schema'] = str_replace($tbl_snapshot['uid'] . '_' . $tablename, $tablename, $tbl_snapshot['schema']); $schema2 = str_replace($tbl_snapshot['uid'] . '_' . $tablename, $tablename, $schema2); if ($tbl_current['rows'] == $tbl_snapshot['rows'] && $tbl_current['schema'] == $tbl_snapshot['tmp_schema']) { $out3 .= '
'; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= ''; $out3 .= '
' . $tbl_current['fullname'] . '' . $tbl_snapshot['fullname'] . '
'; $out3 .= '
'; } elseif ($schema1 != $schema2) { require_once $this->plugin_dir . 'libs/diff.php'; require_once $this->plugin_dir . 'libs/diff/Renderer/Html/SideBySide.php'; $diff = new Diff(explode("\n", $tbl_current['schema']), explode("\n", $tbl_snapshot['schema']), array('ignoreWhitespace' => false)); $renderer = new Diff_Renderer_Html_SideBySide; $out2 .= '
'; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= '
' . $tbl_current['fullname'] . ' table schemas do not match' . $tbl_snapshot['fullname'] . ' table schemas do not match
'; $out2 .= '
'; } else { $out2 .= '
'; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= ''; $out2 .= '
' . $tbl_current['fullname'] . ' data in tables does not match' . $tbl_snapshot['fullname'] . ' data in tables does not match
'; $out2 .= '
'; } } // foreach in both return $out . $out2 . $out3; } // do_compare_snapshots /** * Generates a unique 6-char snapshot ID; verified non-existing * * @return string */ function generate_snapshot_uid() { global $wpdb; $snapshots = $this->get_snapshots(); $cnt = 0; $uid = false; do { $cnt++; $uid = sprintf('%06x', mt_rand(0, 0xFFFFFF)); $verify_db = $wpdb->get_col($wpdb->prepare('SHOW TABLES LIKE %s', array('%' . $uid . '%'))); } while (!empty($verify_db) && isset($snapshots[$uid]) && $cnt < 30); if ($cnt == 30) { $uid = false; } return $uid; } // generate_snapshot_uid /** * Helper function for adding plugins to featured list * * @return array */ function featured_plugins_tab($args) { add_filter('plugins_api_result', array($this, 'plugins_api_result'), 10, 3); return $args; } // featured_plugins_tab /** * Add single plugin to featured list * * @return object */ function add_plugin_featured($plugin_slug, $res) { // check if plugin is already on the list if (!empty($res->plugins) && is_array($res->plugins)) { foreach ($res->plugins as $plugin) { if (is_object($plugin) && !empty($plugin->slug) && $plugin->slug == $plugin_slug) { return $res; } } // foreach } $plugin_info = get_transient('wf-plugin-info-' . $plugin_slug); if (!$plugin_info) { $plugin_info = plugins_api('plugin_information', array( 'slug' => $plugin_slug, 'is_ssl' => is_ssl(), 'fields' => array( 'banners' => true, 'reviews' => true, 'downloaded' => true, 'active_installs' => true, 'icons' => true, 'short_description' => true, ) )); if (!is_wp_error($plugin_info)) { set_transient('wf-plugin-info-' . $plugin_slug, $plugin_info, DAY_IN_SECONDS * 7); } } if ($plugin_info) { array_unshift($res->plugins, $plugin_info); } return $res; } // add_plugin_featured /** * Add plugins to featured plugins list * * @return object */ function plugins_api_result($res, $action, $args) { remove_filter('plugins_api_result', array($this, 'plugins_api_result'), 10, 3); $res = $this->add_plugin_featured('under-construction-page', $res); $res = $this->add_plugin_featured('wp-force-ssl', $res); $res = $this->add_plugin_featured('eps-301-redirects', $res); return $res; } // plugins_api_result /** * 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 */ private function __clone() { } /** * Disabled; we use singleton pattern so magic functions need to be disabled * * @return null */ private function __sleep() { } /** * Disabled; we use singleton pattern so magic functions need to be disabled * * @return null */ private 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')); }