예제 #1
0
 /**
  * Loads the tables available to run a search replace, prefilling if already
  * selected the tables.
  * @access public
  */
 public static function load_tables()
 {
     // Get the tables and their sizes.
     $tables = BSR_DB::get_tables();
     $sizes = BSR_DB::get_sizes();
     echo '<select id="select_tables" name="select_tables[]" multiple="multiple" style="width:25em;">';
     foreach ($tables as $table) {
         // Try to get the size for this specific table.
         $table_size = isset($sizes[$table]) ? $sizes[$table] : '';
         if (isset($_GET['result']) && get_transient('bsr_results')) {
             $result = get_transient('bsr_results');
             if (isset($result['table_reports'][$table])) {
                 echo "<option value='{$table}' selected>{$table} {$table_size}</option>";
             } else {
                 echo "<option value='{$table}'>{$table} {$table_size}</option>";
             }
         } else {
             echo "<option value='{$table}'>{$table} {$table_size}</option>";
         }
     }
     echo '</select>';
 }
 /**
  * Processes the search/replace form submitted by the user.
  * @access public
  */
 public function process_search_replace()
 {
     // Bail if not authorized.
     if (!check_admin_referer('bsr_ajax_nonce', 'bsr_ajax_nonce')) {
         return;
     }
     $args = array();
     if (isset($_POST['bsr_data'])) {
         parse_str($_POST['bsr_data'], $args);
     }
     // Initialize the DB class.
     $db = new BSR_DB();
     $step = isset($_POST['bsr_step']) ? absint($_POST['bsr_step']) : 0;
     $page = isset($_POST['bsr_page']) ? absint($_POST['bsr_page']) : 0;
     // Build the arguements for this run.
     $args = array('select_tables' => isset($args['select_tables']) ? $args['select_tables'] : array(), 'case_insensitive' => isset($args['case_insensitive']) ? $args['case_insensitive'] : 'off', 'replace_guids' => isset($args['replace_guids']) ? $args['replace_guids'] : 'off', 'dry_run' => isset($args['dry_run']) ? $args['dry_run'] : 'off', 'search_for' => isset($args['search_for']) ? stripslashes($args['search_for']) : '', 'replace_with' => isset($args['replace_with']) ? stripslashes($args['replace_with']) : '', 'completed_pages' => isset($args['completed_pages']) ? absint($args['completed_pages']) : 0);
     $args['total_pages'] = isset($args['total_pages']) ? absint($args['total_pages']) : $db->get_total_pages($args['select_tables']);
     // Any operations that should only be performed at the beginning.
     if ($step === 0 && $page === 0) {
         // Clear the results of the last run.
         delete_transient('bsr_results');
     }
     // Start processing data.
     if (isset($args['select_tables'][$step])) {
         $result = $db->srdb($args['select_tables'][$step], $page, $args);
         $this->append_report($args['select_tables'][$step], $result['table_report'], $args);
         if (false === $result['table_complete']) {
             $page++;
         } else {
             $step++;
             $page = 0;
         }
         // Check if isset() again as the step may have changed since last check.
         if (isset($args['select_tables'][$step])) {
             $message = sprintf(__('Processing table %d of %d: %s', 'better-search-replace'), $step + 1, count($args['select_tables']), esc_html($args['select_tables'][$step]));
         }
         $args['completed_pages']++;
         $percentage = $args['completed_pages'] / $args['total_pages'] * 100 . '%';
     } else {
         $db->maybe_update_site_url();
         $step = 'done';
         $percentage = '100%';
     }
     // Store results in an array.
     $result = array('step' => $step, 'page' => $page, 'percentage' => $percentage, 'url' => get_admin_url() . 'tools.php?page=better-search-replace&tab=bsr_search_replace&result=true', 'bsr_data' => http_build_query($args));
     if (isset($message)) {
         $result['message'] = $message;
     }
     // Send output as JSON for processing via AJAX.
     echo json_encode($result);
     exit;
 }