Last active
August 3, 2023 10:02
-
-
Save rickhurst/252167df5d38ebf831eef2787ce25153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Option Compare with Backup | |
Description: Compares option values between wp_options and a backup table. | |
Version: 1.0 | |
Author: Rick Hurst | |
*/ | |
/** | |
* example usage | |
* wp option-compare-with-backup --backup-table=wp_opt_backup --recent-table=wp_opt_current --specific-options=sidebars_widgets --output-php | |
*/ | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
WP_CLI::add_command( 'option-compare-with-backup', 'option_compare_with_backup_command' ); | |
} | |
function option_compare_with_backup_command( $args, $assoc_args ) { | |
global $wpdb; | |
$original_table = isset($assoc_args['recent-table']) ? $assoc_args['recent-table'] : "wp_options"; | |
$backup_table = $assoc_args['backup-table']; | |
$output_php = isset( $assoc_args['output-php'] ) ? $assoc_args['output-php'] : false; | |
$specific_options = isset( $assoc_args['specific-options'] ) ? $assoc_args['specific-options'] : false; | |
$sql = "SELECT option_name, option_value FROM $original_table"; | |
if($specific_options){ | |
$opts = explode(",", $specific_options); | |
$opts_formatted = "'" . implode("','", $opts ) . "'"; | |
$sql = $sql . " where option_name in ( $opts_formatted )"; | |
} | |
$results = $wpdb->get_results( | |
$sql, | |
ARRAY_A | |
); | |
if ( ! empty( $results ) ) { | |
$rows = array(); | |
$different_count = 0; | |
foreach ( $results as $result ) { | |
$option_name = $result['option_name']; | |
$option_value = $result['option_value']; | |
$backup_value = $wpdb->get_var( | |
$wpdb->prepare( | |
"SELECT option_value FROM $backup_table WHERE option_name = %s", | |
$option_name | |
) | |
); | |
$same = ( $option_value === $backup_value ) ? 'yes' : 'no'; | |
if( $option_value !== $backup_value && trim($option_value) != ''){ | |
$different_count++; | |
$rows[] = array( | |
'count' => $different_count, | |
'option_name' => $option_name, | |
'option_value' => $option_value, | |
'option_value_backup' => $backup_value, | |
'same' => $same, | |
); | |
} | |
} | |
$formatter = new \WP_CLI\Formatter( $assoc_args, ['count', 'option_name','option_value','option_value_backup','same'], 'option_comparison' ); | |
// this seems to stall if there's more than 80 rows - might be specific to data in my test data | |
$output_rows = array_slice( $rows, 0, 80 ); | |
$formatter->display_items( $output_rows ); | |
if($different_count > 80 ){ | |
\WP_CLI::line('output was truncated to 80 records of '. $different_count); | |
} | |
if($output_php){ | |
$php_out = "<" . "?" . "php" . PHP_EOL; | |
foreach($rows as $row){ | |
// so horrible... | |
$php_out .= '$' . "serialized = '" . $row['option_value_backup'] ."';" . PHP_EOL; | |
$php_out .= '$data = maybe_unserialize($serialized);' . PHP_EOL; | |
$php_out .= 'if(is_array($data)){'. PHP_EOL; | |
$php_out .= ' print_r($data);' .PHP_EOL; | |
$php_out .= " update_option('". $row['option_name']."'," . '$data);' . PHP_EOL; | |
$php_out .= '}' .PHP_EOL . PHP_EOL; | |
} | |
$filename = 'update_wp_options.php'; | |
file_put_contents($filename, $php_out); | |
\WP_CLI::line('php commands written to file: ' . $filename ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment