Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress Coding Standards updates & general code cleanup #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions includes/class-bsr-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public function enqueue_scripts( $hook ) {
wp_enqueue_script( 'thickbox' );

wp_localize_script( 'better-search-replace', 'bsr_object_vars', array(
'page_size' => get_option( 'bsr_page_size' ) ? absint( get_option( 'bsr_page_size' ) ) : 20000,
'endpoint' => BSR_AJAX::get_endpoint(),
'ajax_nonce' => wp_create_nonce( 'bsr_ajax_nonce' ),
'no_search' => __( 'No search string was defined, please enter a URL or string to search for.', 'better-search-replace' ),
'no_tables' => __( 'Please select the tables that you want to update.', 'better-search-replace' ),
'unknown' => __( 'An error occurred processing your request. Try decreasing the "Max Page Size", or contact support.', 'better-search-replace' ),
'processing' => __( 'Processing...', 'better-search-replace' )
'page_size' => get_option( 'bsr_page_size' ) ? absint( get_option( 'bsr_page_size' ) ) : 20000,
'endpoint' => BSR_AJAX::get_endpoint(),
'ajax_nonce' => wp_create_nonce( 'bsr_ajax_nonce' ),
'no_search' => __( 'No search string was defined, please enter a URL or string to search for.', 'better-search-replace' ),
'no_tables' => __( 'Please select the tables that you want to update.', 'better-search-replace' ),
'unknown' => __( 'An error occurred processing your request. Try decreasing the "Max Page Size", or contact support.', 'better-search-replace' ),
'processing' => __( 'Processing...', 'better-search-replace' )
) );
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ public static function render_result() {

if ( isset( $_GET['result'] ) && $result = get_transient( 'bsr_results' ) ) {

if ( isset( $result['dry_run'] ) && $result['dry_run'] === 'on' ) {
if ( isset( $result['dry_run'] ) && 'on' === $result['dry_run'] ) {
$msg = sprintf( __( '<p><strong>DRY RUN:</strong> <strong>%d</strong> tables were searched, <strong>%d</strong> cells were found that need to be updated, and <strong>%d</strong> changes were made.</p><p><a href="%s" class="thickbox" title="Dry Run Details">Click here</a> for more details, or use the form below to run the search/replace.</p>', 'better-search-replace' ),
$result['tables'],
$result['change'],
Expand Down Expand Up @@ -159,8 +159,8 @@ public static function prefill_value( $value, $type = 'text' ) {
public static function load_tables() {

// Get the tables and their sizes.
$tables = BSR_DB::get_tables();
$sizes = BSR_DB::get_sizes();
$tables = BSR_DB::get_tables();
$sizes = BSR_DB::get_sizes();

echo '<select id="bsr-table-select" name="select_tables[]" multiple="multiple" style="width:25em;">';

Expand Down Expand Up @@ -196,9 +196,9 @@ public static function load_tables() {
public function load_details() {

if ( get_transient( 'bsr_results' ) ) {
$results = get_transient( 'bsr_results' );
$min = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
$bsr_styles = BSR_URL . 'assets/css/better-search-replace.css?v=' . BSR_VERSION;
$results = get_transient( 'bsr_results' );
$min = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
$bsr_styles = BSR_URL . 'assets/css/better-search-replace.css?v=' . BSR_VERSION;

?>
<link href="<?php echo esc_url( get_admin_url( null, 'css/common' . $min . '.css' ) ); ?>" rel="stylesheet" type="text/css" />
Expand All @@ -215,11 +215,11 @@ public function load_details() {
foreach ( $results['table_reports'] as $table_name => $report ) {
$time = $report['end'] - $report['start'];

if ( $report['change'] !== 0 ) {
if ( 0 !== $report['change'] ) {
$report['change'] = '<strong>' . $report['change'] . '</strong>';
}

if ( $report['updates'] !== 0 ) {
if ( 0 !== $report['updates'] ) {
$report['updates'] = '<strong>' . $report['updates'] . '</strong>';
}

Expand Down
46 changes: 23 additions & 23 deletions includes/class-bsr-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ public function process_search_replace() {
$page = isset( $_POST['bsr_page'] ) ? absint( $_POST['bsr_page'] ) : 0;

// Any operations that should only be performed at the beginning.
if ( $step === 0 && $page === 0 ) {
if ( 0 === $step && 0 === $page ) {
$args = array();
parse_str( $_POST['bsr_data'], $args );

// 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,
'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'] );
Expand Down Expand Up @@ -162,19 +162,19 @@ public function process_search_replace() {

} else {
$db->maybe_update_site_url();
$step = 'done';
$step = 'done';
$percentage = '100%';
}

update_option( 'bsr_data', $args );

// 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' => build_query( $args )
'step' => $step,
'page' => $page,
'percentage' => $percentage,
'url' => get_admin_url() . 'tools.php?page=better-search-replace&tab=bsr_search_replace&result=true',
'bsr_data' => build_query( $args )
);

if ( isset( $message ) ) {
Expand Down Expand Up @@ -206,21 +206,21 @@ public function append_report( $table, $report, $args ) {
$results = get_transient( 'bsr_results' ) ? get_transient( 'bsr_results') : array();

// Grab any values from the run args.
$results['search_for'] = isset( $args['search_for'] ) ? $args['search_for'] : '';
$results['replace_with'] = isset( $args['replace_with'] ) ? $args['replace_with'] : '';
$results['dry_run'] = isset( $args['dry_run'] ) ? $args['dry_run'] : 'off';
$results['case_insensitive'] = isset( $args['case_insensitive'] ) ? $args['case_insensitive'] : 'off';
$results['replace_guids'] = isset( $args['replace_guids'] ) ? $args['replace_guids'] : 'off';
$results['search_for'] = isset( $args['search_for'] ) ? $args['search_for'] : '';
$results['replace_with'] = isset( $args['replace_with'] ) ? $args['replace_with'] : '';
$results['dry_run'] = isset( $args['dry_run'] ) ? $args['dry_run'] : 'off';
$results['case_insensitive'] = isset( $args['case_insensitive'] ) ? $args['case_insensitive'] : 'off';
$results['replace_guids'] = isset( $args['replace_guids'] ) ? $args['replace_guids'] : 'off';

// Sum the values of the new and existing reports.
$results['change'] = isset( $results['change'] ) ? $results['change'] + $report['change'] : $report['change'];
$results['change'] = isset( $results['change'] ) ? $results['change'] + $report['change'] : $report['change'];
$results['updates'] = isset( $results['updates'] ) ? $results['updates'] + $report['updates'] : $report['updates'];

// Append the table report, or create a new one if necessary.
if ( isset( $results['table_reports'] ) && isset( $results['table_reports'][$table] ) ) {
$results['table_reports'][$table]['change'] = $results['table_reports'][$table]['change'] + $report['change'];
$results['table_reports'][$table]['updates'] = $results['table_reports'][$table]['updates'] + $report['updates'];
$results['table_reports'][$table]['end'] = $report['end'];
$results['table_reports'][$table]['change'] = $results['table_reports'][$table]['change'] + $report['change'];
$results['table_reports'][$table]['updates'] = $results['table_reports'][$table]['updates'] + $report['updates'];
$results['table_reports'][$table]['end'] = $report['end'];
} else {
$results['table_reports'][$table] = $report;
}
Expand Down
8 changes: 4 additions & 4 deletions includes/class-bsr-compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,27 @@ public static function get_sysinfo() {
$plugins = get_plugins();
$active_plugins = get_option( 'active_plugins', array() );
foreach( $plugins as $plugin_path => $plugin ) {
if( !in_array( $plugin_path, $active_plugins ) )
if ( ! in_array( $plugin_path, $active_plugins ) )
continue;
$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
}

// WordPress inactive plugins
$return .= "\n" . '-- WordPress Inactive Plugins' . "\n\n";
foreach( $plugins as $plugin_path => $plugin ) {
if( in_array( $plugin_path, $active_plugins ) )
if ( in_array( $plugin_path, $active_plugins ) )
continue;
$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
}

if( is_multisite() ) {
if ( is_multisite() ) {
// WordPress Multisite active plugins
$return .= "\n" . '-- Network Active Plugins' . "\n\n";
$plugins = wp_get_active_network_plugins();
$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
foreach( $plugins as $plugin_path ) {
$plugin_base = plugin_basename( $plugin_path );
if( !array_key_exists( $plugin_base, $active_plugins ) )
if ( ! array_key_exists( $plugin_base, $active_plugins ) )
continue;
$plugin = get_plugin_data( $plugin_path );
$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
Expand Down
66 changes: 33 additions & 33 deletions includes/class-bsr-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static function get_tables() {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {

if ( is_main_site() ) {
$tables = $wpdb->get_col( 'SHOW TABLES' );
$tables = $wpdb->get_col( 'SHOW TABLES' );
} else {
$blog_id = get_current_blog_id();
$tables = $wpdb->get_col( "SHOW TABLES LIKE '" . $wpdb->base_prefix . absint( $blog_id ) . "\_%'" );
$blog_id = get_current_blog_id();
$tables = $wpdb->get_col( "SHOW TABLES LIKE '" . $wpdb->base_prefix . absint( $blog_id ) . "\_%'" );
}

} else {
Expand Down Expand Up @@ -106,9 +106,9 @@ public function get_page_size() {
* @return int
*/
public function get_pages_in_table( $table ) {
$table = esc_sql( $table );
$rows = $this->wpdb->get_var( "SELECT COUNT(*) FROM `$table`" );
$pages = ceil( $rows / $this->page_size );
$table = esc_sql( $table );
$rows = $this->wpdb->get_var( "SELECT COUNT(*) FROM `$table`" );
$pages = ceil( $rows / $this->page_size );
return absint( $pages );
}

Expand All @@ -126,7 +126,7 @@ public function get_total_pages( $tables ) {
$pages = $this->get_pages_in_table( $table );

// Always include 1 page in case we have to create schemas, etc.
if ( $pages == 0 ) {
if ( 0 == $pages ) {
$pages = 1;
}

Expand All @@ -143,14 +143,14 @@ public function get_total_pages( $tables ) {
* @return array
*/
public function get_columns( $table ) {
$primary_key = null;
$columns = array();
$fields = $this->wpdb->get_results( 'DESCRIBE ' . $table );
$primary_key = null;
$columns = array();
$fields = $this->wpdb->get_results( 'DESCRIBE ' . $table );

if ( is_array( $fields ) ) {
foreach ( $fields as $column ) {
$columns[] = $column->Field;
if ( $column->Key == 'PRI' ) {
if ( 'PRI' == $column->Key ) {
$primary_key = $column->Field;
}
}
Expand All @@ -176,21 +176,21 @@ public function get_columns( $table ) {
public function srdb( $table, $page, $args ) {

// Load up the default settings for this chunk.
$table = esc_sql( $table );
$current_page = absint( $page );
$pages = $this->get_pages_in_table( $table );
$done = false;
$table = esc_sql( $table );
$current_page = absint( $page );
$pages = $this->get_pages_in_table( $table );
$done = false;

$args['search_for'] = str_replace( '#BSR_BACKSLASH#', '\\', $args['search_for'] );
$args['replace_with'] = str_replace( '#BSR_BACKSLASH#', '\\', $args['replace_with'] );
$args['search_for'] = str_replace( '#BSR_BACKSLASH#', '\\', $args['search_for'] );
$args['replace_with'] = str_replace( '#BSR_BACKSLASH#', '\\', $args['replace_with'] );

$table_report = array(
'change' => 0,
'updates' => 0,
'start' => microtime( true ),
'end' => microtime( true ),
'errors' => array(),
'skipped' => false
'change' => 0,
'updates' => 0,
'start' => microtime( true ),
'end' => microtime( true ),
'errors' => array(),
'skipped' => false
);

// Get a list of columns in this table.
Expand All @@ -202,9 +202,9 @@ public function srdb( $table, $page, $args ) {
return array( 'table_complete' => true, 'table_report' => $table_report );
}

$current_row = 0;
$start = $page * $this->page_size;
$end = $this->page_size;
$current_row = 0;
$start = $page * $this->page_size;
$end = $this->page_size;

// Grab the content of the table.
$data = $this->wpdb->get_results( "SELECT * FROM `$table` LIMIT $start, $end", ARRAY_A );
Expand All @@ -213,8 +213,8 @@ public function srdb( $table, $page, $args ) {
foreach ( $data as $row ) {
$current_row++;
$update_sql = array();
$where_sql = array();
$upd = false;
$where_sql = array();
$upd = false;

foreach( $columns as $column ) {

Expand All @@ -240,8 +240,8 @@ public function srdb( $table, $page, $args ) {

// If the Site URL needs to be updated, let's do that last.
if ( isset( $update_later ) && true === $update_later ) {
$update_later = false;
$edited_data = $this->recursive_unserialize_replace( $args['search_for'], $args['replace_with'], $data_to_fix, false, $args['case_insensitive'] );
$update_later = false;
$edited_data = $this->recursive_unserialize_replace( $args['search_for'], $args['replace_with'], $data_to_fix, false, $args['case_insensitive'] );

if ( $edited_data != $data_to_fix ) {
$table_report['change']++;
Expand Down Expand Up @@ -274,11 +274,11 @@ public function srdb( $table, $page, $args ) {
}

// Determine what to do with updates.
if ( $args['dry_run'] === 'on' ) {
if ( 'on' === $args['dry_run'] ) {
// Don't do anything if a dry run
} elseif ( $upd && ! empty( $where_sql ) ) {
// If there are changes to make, run the query.
$sql = 'UPDATE ' . $table . ' SET ' . implode( ', ', $update_sql ) . ' WHERE ' . implode( ' AND ', array_filter( $where_sql ) );
$sql = 'UPDATE ' . $table . ' SET ' . implode( ', ', $update_sql ) . ' WHERE ' . implode( ' AND ', array_filter( $where_sql ) );
$result = $this->wpdb->query( $sql );

if ( ! $result ) {
Expand Down Expand Up @@ -338,7 +338,7 @@ public function recursive_unserialize_replace( $from = '', $to = '', $data = '',
// Submitted by Tina Matter
elseif ( is_object( $data ) ) {
// $data_class = get_class( $data );
$_tmp = $data; // new $data_class( );
$_tmp = $data; // new $data_class( );
$props = get_object_vars( $data );
foreach ( $props as $key => $value ) {
$_tmp->$key = $this->recursive_unserialize_replace( $from, $to, $value, false, $case_insensitive );
Expand Down
4 changes: 2 additions & 2 deletions includes/class-bsr-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class Better_Search_Replace {
* @since 1.0
*/
public function __construct() {
$this->plugin_name = 'better-search-replace';
$this->version = BSR_VERSION;
$this->plugin_name = 'better-search-replace';
$this->version = BSR_VERSION;
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
Expand Down
2 changes: 1 addition & 1 deletion templates/bsr-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Other settings.
$page_size = get_option( 'bsr_page_size' ) ? absint( get_option( 'bsr_page_size' ) ) : 20000;

?>
?>

<?php settings_fields( 'bsr_settings_fields' ); ?>

Expand Down