Changeset 1390214
- Timestamp:
- 04/08/2016 12:55:51 PM (10 years ago)
- Location:
- search-and-replace/trunk/inc
- Files:
-
- 8 edited
-
Database/Exporter.php (modified) (2 diffs)
-
Database/Replace.php (modified) (2 diffs)
-
FileDownloader.php (modified) (6 diffs)
-
Page/BackupDatabase.php (modified) (2 diffs)
-
Page/Credits.php (modified) (1 diff)
-
Page/ReplaceDomain.php (modified) (2 diffs)
-
Page/SearchReplace.php (modified) (3 diffs)
-
Page/SqlImport.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
search-and-replace/trunk/inc/Database/Exporter.php
r1390032 r1390214 78 78 * $report ['changes'] : Array with replacements in tables 79 79 */ 80 public function db_backup( $search , $replace, $tables = array(), $domain_replace = FALSE, $new_table_prefix) {80 public function db_backup( $search = '', $replace = '', $tables = array(), $domain_replace = FALSE, $new_table_prefix = '' ) { 81 81 82 82 if ( count( $tables ) < 1 ) { … … 584 584 } 585 585 586 /**587 * Trims a given string to 50 chars before and after the search string, if the string is longer than 199 chars.588 *589 * @param $needle string590 * @param $haystack string591 * @param $delimiter array $delimiter[0]=start delimiter, $delimiter[1] = end delimiter592 *593 * @return string The trimmed $haystack594 */595 protected function trim_search_results( $needle, $haystack, $delimiter ) {596 597 //if result has <200 characters we return the whole string598 if ( strlen( $haystack ) < 100 ) {599 return $haystack;600 }601 $trimmed_results = NULL;602 // Get all occurrences of $needle with up to 50 chars front & back.603 preg_match_all( '@.{0,50}' . $needle . '.{0,50}@', $haystack, $trimmed_results );604 $return_value = '';605 /** @var array $trimmed_results */606 $imax = count( $trimmed_results );607 for ( $i = 0; $i < $imax; $i ++ ) {608 //reset delimiter, might have been changed609 $local_delimiter = $delimiter;610 //check if the first trimmmed result is the beginning of $haystack. if so remove leading delimiter611 if ( $i === 0 ) {612 $pos = strpos( $haystack, $trimmed_results[ 0 ][ $i ] );613 if ( $pos === 0 ) {614 $local_delimiter[ 0 ] = '';615 }616 }617 //check if the last trimmed result is the end of $haystack. if so, remove trailing delimiter618 $last_index = count( $trimmed_results ) - 1;619 if ( $i === $last_index ) {620 $trimmed_result_length = strlen( $trimmed_results[ 0 ][ $i ] );621 $substring = substr( $haystack, - $trimmed_result_length );622 if ( $substring === $trimmed_results[ 0 ][ $i ] ) {623 $local_delimiter[ 1 ] = '';624 }625 }626 $return_value .= $local_delimiter[ 0 ] . $trimmed_results[ 0 ][ $i ] . $local_delimiter[ 1 ];627 }628 629 return $return_value;630 }631 586 632 587 } -
search-and-replace/trunk/inc/Database/Replace.php
r1390032 r1390214 73 73 public function run_search_replace( $search, $replace, $tables ) { 74 74 75 if ( $search== $replace ){75 if ( $search === $replace ){ 76 76 return new \WP_Error( 'error', __( "Search and replace pattern can't be the same!" ) ); 77 77 } … … 127 127 //split columns array in primary key string and columns array 128 128 $columns = $this->dbm->get_columns( $table ); 129 list( $primary_key, $columns ) = $columns; 129 $primary_key = $columns[ 0 ]; 130 $columns = $columns[ 1 ]; 130 131 131 132 if ( NULL === $primary_key ) { -
search-and-replace/trunk/inc/FileDownloader.php
r1390032 r1390214 11 11 class FileDownloader { 12 12 13 /** 14 * @var string 15 */ 13 16 private $nonce_action = 'download_sql'; 14 17 18 /** 19 * @var string 20 */ 15 21 private $nonce_name = 'insr_nonce'; 16 22 … … 35 41 * @param array $report 36 42 */ 37 public function show_ download_modal( $report ) {43 public function show_modal( $report ) { 38 44 39 45 if ( ! isset( $report[ 'changes' ] ) ) { 40 46 echo '<p>' . esc_html__( 'Search pattern not found.', 'search-and-replace' ) . '</p>'; 47 41 48 return; 42 49 } … … 51 58 $this->show_changes( $report ); 52 59 } 53 60 54 61 //if no changes found report that 55 62 if ( 0 === count( $report [ 'changes' ] ) ) { … … 60 67 61 68 <div class="updated notice is-dismissible insr_sql_button_wrap"> 62 <p><?php esc_html_e( 'Your SQL file was created!', 'search-and-replace');?> </p>69 <p><?php esc_html_e( 'Your SQL file was created!', 'search-and-replace' ); ?> </p> 63 70 <form action method="post"> 64 71 <?php wp_nonce_field( $this->nonce_action, $this->nonce_name ); ?> 65 72 <input type="hidden" name="action" value="download_file" /> 66 <input type ="hidden" name="sql_file" value="<?php echo esc_attr( $report[ 'filename' ] ); ?>"> 67 <input type ="hidden" name="compress" value="<?php echo esc_attr( $compress ); ?>"> 68 <input id ="insr_submit" type="submit" value="<?php esc_attr_e( 'Download SQL File', 'search-and-replace' ) ?>" class="button" /> 73 <input type="hidden" name="sql_file" value="<?php echo esc_attr( $report[ 'filename' ] ); ?>"> 74 <input type="hidden" name="compress" value="<?php echo esc_attr( $compress ); ?>"> 75 <input id="insr_submit" type="submit" value="<?php esc_attr_e( 76 'Download SQL File', 'search-and-replace' 77 ) ?>" class="button" /> 69 78 </form> 70 79 </div> 71 80 <?php 72 81 } 73 74 82 75 83 /** … … 90 98 * @return string 91 99 */ 92 p rivatefunction show_changes( $report ) {100 public function show_changes( $report ) { 93 101 94 102 //get search & replace values in order to highlight them in the results … … 129 137 </div> 130 138 <div class="search-replace-changes-modal-content"> 139 <?php 140 foreach ( $report[ 'changes' ] as $table_report ) : 141 $changes = $table_report[ 'changes' ]; 142 $changes_made = count( $changes ); 143 144 if ( $changes_made < 1 ) : 145 continue; 146 endif; 147 148 $table = $table_report[ 'table_name' ]; 149 ?> 150 <h2 class="search-replace-modal-table-headline"> 151 <strong><?php esc_html_e( 'Table:', 'search-and-replace' ); ?></strong> 152 <?php echo $table; ?> 153 <strong><?php esc_html_e( 'Changes:', 'search-and-replace' ); ?></strong> 154 <?php echo $changes_made; ?> 155 </h2> 156 157 <table class="search-replace-modal-table"> 158 159 <?php foreach ( $changes as $change ) : ?> 160 161 <tr> 162 <th class="search-replace-narrow"> 163 <?php esc_html_e( 'row', 'search-and-replace' ); ?> 164 </th> 165 <td class="search-replace-narrow"><?php echo esc_html( $change [ 'row' ] ); ?></td> 166 <th><?php esc_html_e( 'column', 'search-and-replace' ); ?></th> 167 <td><?php echo esc_html( $change [ 'column' ] ); ?></td> 168 <?php 169 //trim results and wrap with highlight class 170 $old_value = esc_html( $change [ 'from' ] ); 171 $old_value = $this->trim_search_results( $search, $old_value, $delimiter ); 172 $old_value = str_replace( $search, $search_highlight, $old_value ); 173 174 $new_value = esc_html( $change[ 'to' ] ); 175 $new_value = $this->trim_search_results( $replace, $new_value, $delimiter ); 176 $new_value = str_replace( $replace, $replace_highlight, $new_value ); 177 ?> 178 <th><?php esc_html_e( 'Old value:', 'search-and-replace' ); ?></th> 179 <td><?php echo wp_kses( $old_value, [ 'span' => [ 'class' => [] ] ] ); ?></td> 180 <th><?php esc_html_e( 'New value:', 'search-and-replace' ); ?></th> 181 <td><?php echo wp_kses( $new_value, [ 'span' => [ 'class' => [] ] ] ); ?></td> 182 </tr> 183 184 <?php endforeach ?> 185 186 </table> 187 188 <?php endforeach; ?> 189 190 </div> 191 </div> 131 192 <?php 132 foreach ( $report[ 'changes' ] as $table_report ) { 133 $changes = $table_report[ 'changes' ]; 134 $changes_made = count( $changes ); 135 136 if ( $changes_made > 0 ) { 137 $table = $table_report[ 'table_name' ]; 138 $html = '<h2 class = "search-replace-modal-table-headline">'; 139 $html .= '<strong>' . esc_attr__( 'Table:', 'search-and-replace' ) . '</strong> ' . $table; 140 $html .= '<strong>' . esc_attr__( 'Changes:', 'search-and-replace' ) . '</strong> ' . $changes_made; 141 $html .= '</h2>'; 142 143 $html .= '<table class="search-replace-modal-table"><colgroup><col><col><col><col><col><col><col><col></colgroup>'; 144 145 foreach ( $changes as $change ) { 146 147 $html .= '<tr>'; 148 $html .= '<th class="search-replace-narrow">' . __( 'row', 'search-and-replace' ) . '</th> 149 <td class="search-replace-narrow">' . $change [ 'row' ] . '</td> 150 <th> ' . __( 'column', 'search-and-replace' ) . '</th> 151 <td>' . $change [ 'column' ] . '</td> '; 152 153 //trim results and wrap with highlight class 154 $old_value = esc_html( $change [ 'from' ] ); 155 $old_value = $this->trim_search_results( $search, $old_value, $delimiter ); 156 $old_value = str_replace( $search, $search_highlight, $old_value ); 157 158 $new_value = esc_html( $change[ 'to' ] ); 159 $new_value = $this->trim_search_results( $replace, $new_value, $delimiter ); 160 $new_value = str_replace( $replace, $replace_highlight, $new_value ); 161 162 $html .= '<th>' . __( 'Old value:', 'search-and-replace' ) . '</th> 163 <td>' . $old_value . '</td> 164 <th> ' . __( 'New value:', 'search-and-replace' ) . '</th><td>' . $new_value . '</td>'; 165 $html .= '</tr>'; 193 } 194 195 /** 196 * Trims a given string to 50 chars before and after the search string, if the string is longer than 199 chars. 197 * 198 * @param $needle string 199 * @param $haystack string 200 * @param $delimiter array $delimiter[0]=start delimiter, $delimiter[1] = end delimiter 201 * 202 * @return string The trimmed $haystack 203 */ 204 public function trim_search_results( $needle, $haystack, $delimiter ) { 205 206 //if result has <200 characters we return the whole string 207 if ( strlen( $haystack ) < 100 ) { 208 return $haystack; 209 } 210 $trimmed_results = NULL; 211 // Get all occurrences of $needle with up to 50 chars front & back. 212 preg_match_all( '@.{0,50}' . $needle . '.{0,50}@', $haystack, $trimmed_results ); 213 $return_value = ''; 214 /** @var array $trimmed_results */ 215 $imax = count( $trimmed_results ); 216 for ( $i = 0; $i < $imax; $i ++ ) { 217 //reset delimiter, might have been changed 218 $local_delimiter = $delimiter; 219 //check if the first trimmmed result is the beginning of $haystack. if so remove leading delimiter 220 if ( $i === 0 ) { 221 $pos = strpos( $haystack, $trimmed_results[ 0 ][ $i ] ); 222 if ( $pos === 0 ) { 223 $local_delimiter[ 0 ] = ''; 166 224 } 167 $html .= '</table>'; 168 169 echo $html; 170 } 171 } 172 173 echo '</div></div>'; 174 } 175 176 225 } 226 //check if the last trimmed result is the end of $haystack. if so, remove trailing delimiter 227 $last_index = count( $trimmed_results ) - 1; 228 if ( $i === $last_index ) { 229 $trimmed_result_length = strlen( $trimmed_results[ 0 ][ $i ] ); 230 $substring = substr( $haystack, - $trimmed_result_length ); 231 if ( $substring === $trimmed_results[ 0 ][ $i ] ) { 232 $local_delimiter[ 1 ] = ''; 233 } 234 } 235 $return_value .= $local_delimiter[ 0 ] . $trimmed_results[ 0 ][ $i ] . $local_delimiter[ 1 ]; 236 } 237 238 return $return_value; 239 } 177 240 /** 178 241 * calls the file delivery in Class DatabaseExporter -
search-and-replace/trunk/inc/Page/BackupDatabase.php
r1390032 r1390214 43 43 44 44 /** 45 * Return the static slug string. 46 * 47 * @return string 48 */ 49 public function get_slug() { 50 51 return 'backup-database'; 52 } 53 54 /** 45 55 *shows the page template 46 56 */ … … 64 74 65 75 $report = $this->dbe->db_backup(); 66 $this->downloader->show_ download_modal( $report );76 $this->downloader->show_modal( $report ); 67 77 68 78 return TRUE; -
search-and-replace/trunk/inc/Page/Credits.php
r1390032 r1390214 26 26 27 27 /** 28 * Return the static slug string. 29 * 30 * @return string 31 */ 32 public function get_slug() { 33 34 return 'credits'; 35 } 36 37 /** 28 38 * @return boolean 29 39 */ -
search-and-replace/trunk/inc/Page/ReplaceDomain.php
r1390032 r1390214 59 59 60 60 $report = $this->dbe->db_backup( $search, $replace, array(), TRUE, $new_db_prefix ); 61 $this->downloader->show_ download_modal( $report );61 $this->downloader->show_modal( $report ); 62 62 63 63 return TRUE; … … 88 88 } 89 89 90 /** 91 * Return the static slug string. 92 * 93 * @return string 94 */ 95 public function get_slug() { 96 97 return 'replace-domain-url'; 98 } 90 99 } -
search-and-replace/trunk/inc/Page/SearchReplace.php
r1390032 r1390214 63 63 64 64 return esc_html__( 'Search & Replace', 'search-and-replace' ); 65 } 66 67 /** 68 * Return the static slug string. 69 * 70 * @return string 71 */ 72 public function get_slug() { 73 74 return 'search-replace'; 65 75 } 66 76 … … 134 144 //'export'-button was checked 135 145 $report = $this->dbe->db_backup( $search, $replace, $tables ); 136 $this->downloader->show_ download_modal( $report );146 $this->downloader->show_modal( $report ); 137 147 } else { 138 148 //"Save changes to database" was checked … … 190 200 191 201 if ( count( $report[ 'changes' ] ) > 0 ) { 192 $this->d be->show_changes( $report );202 $this->downloader->show_changes( $report ); 193 203 } 194 204 -
search-and-replace/trunk/inc/Page/SqlImport.php
r1390032 r1390214 33 33 34 34 return esc_html__( 'SQL Import', 'search-and-replace' ); 35 } 36 37 /** 38 * Return the static slug string. 39 * 40 * @return string 41 */ 42 public function get_slug() { 43 44 return 'sql-import'; 35 45 } 36 46
Note: See TracChangeset
for help on using the changeset viewer.