dbm = $dbm; $this->replace = $replace; $this->dbe = $dbe; $this->downloader = $downloader; } /** * shows the page contents */ public function render() { require_once( __DIR__ . '/../templates/search_replace.php' ); } /** * @return string */ public function get_page_title() { return esc_html__( 'Search & Replace', 'search-and-replace' ); } /** * Return the static slug string. * * @return string */ public function get_slug() { return 'search-replace'; } /** *prints a select with all the tables and their sizes * * @return void */ protected function show_table_list() { $tables = $this->dbm->get_tables(); $sizes = $this->dbm->get_sizes(); $table_count = count( $tables ); //adjust height of select according to table count, but max 20 rows $select_rows = $table_count < 20 ? $table_count : 20; //if we come from a dry run, we select the tables to the dry run again /** @var bool | string $selected_tables */ $selected_tables = FALSE; if ( isset( $_POST[ 'select_tables' ] ) ) { $selected_tables = $_POST[ 'select_tables' ]; } echo '' ); } /** * @return bool */ public function save() { //check for errors in form if ( ! $this->is_request_valid() ) { return FALSE; } $tables = isset( $_POST[ 'select_tables' ] ) ? $_POST[ 'select_tables' ] : ''; $dry_run = isset( $_POST[ 'dry_run' ] ) ? TRUE : FALSE; //remove wp_magic_quotes $search = stripslashes( filter_input( INPUT_POST, 'search' ) ); $replace = stripslashes( filter_input( INPUT_POST, 'replace' ) ); //if dry run is checked we run the replace function with dry run and return if ( TRUE === $dry_run ) { $this->run_replace( $search, $replace, $tables, $dry_run ); return FALSE; } $export_or_save = filter_input( INPUT_POST, 'export_or_save' ); if ( 'export' === $export_or_save ) { //'export'-button was checked $report = $this->dbe->db_backup( $search, $replace, $tables ); $this->downloader->show_modal( $report ); } else { //"Save changes to database" was checked $this->run_replace( $search, $replace, $tables, $dry_run ); } return TRUE; } /** * @return string */ protected function get_submit_button_title() { return esc_html__( 'Do Search & Replace', 'search-and-replace' ); } /** * calls run_replace_table() on each table provided in array $tables * * @param $search * @param $replace * @param $tables array of tables we want to search * @param $dry_run True if dry run (no changes are written to db) * * @return null */ protected function run_replace( $search, $replace, $tables, $dry_run ) { echo '
' . esc_html__( 'Dry run is selected. No changes were made to the database and no SQL file was written .', 'search-and-replace' ) . '
'; } else { echo '' . esc_html__( 'The following changes were made to the database: ', 'search-and-replace' ) . '
'; } $this->replace->set_dry_run( $dry_run ); $report = $this->replace->run_search_replace( $search, $replace, $tables ); if ( is_wp_error( $report ) ) { $this->add_error( __( $report->get_error_message(), 'search-and-replace' ) ); $this->display_errors(); } else { if ( count( $report[ 'changes' ] ) > 0 ) { $this->downloader->show_changes( $report ); } //if no changes found report that if ( 0 === count( $report [ 'changes' ] ) ) { echo '' . esc_html__( 'Search pattern not found.', 'search-and-replace' ) . '
'; } } echo '