Advertisement
urosevic

Stock Ticker Reset script

Sep 28th, 2020 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.03 KB | None | 0 0
  1. <?php
  2. if ( ! empty( $_GET['delete_me'] ) && 'yes' === $_GET['delete_me'] ) {
  3.     $file_to_delete = basename( __FILE__ );
  4.     printf( 'Deleting Stock Ticker Reset script file <strong>%s</strong>... ', $file_to_delete );
  5.     unlink( $file_to_delete );
  6.     exit();
  7. }
  8. /**
  9.  * Tells WordPress to load the WordPress theme and output it.
  10.  *
  11.  * @var bool
  12.  */
  13. define( 'WP_USE_THEMES', false );
  14.  
  15. /** Loads the WordPress Environment and Template */
  16. require __DIR__ . '/wp-blog-header.php';
  17.  
  18. /** Start Stock Ticker Reset Magic */
  19. global $wpdb;
  20.  
  21. $is_deletion_confirmed = ! empty( $_GET['confirm_reset'] ) && 'dabome' === $_GET['confirm_reset'] ? true : false;
  22. $nl = '<br/><br/>';
  23. ?>
  24. <h1>DANGEROUS ZONE</h1>
  25. <p>Below you can see what will be cleaned up from your WordPress installation if you enable checkbox and click on <strong>CLEANUP</strong> button.</p>
  26. <form action="" method="GET">
  27.     <label for="confirm_reset"><input type="checkbox" name="confirm_reset" id="confirm_reset" value="dabome" <?php checked( $is_deletion_confirmed ); ?> /> I understand that real data cleanup will be done without possibility to undo this action! <string>BEFORE YOU CONTINUE make sure you backed up your database!</strong></label>
  28.     <input type="submit" name="submit" value="RESET" />
  29. </form>
  30. <hr/>
  31. <?php
  32.  
  33. printf(
  34.     '<h2>Stock Ticker Reset Log (%s)</h2>',
  35.     $is_deletion_confirmed ? 'Doing real cleanup' : 'Dry run without deletion'
  36. );
  37. echo '<pre>';
  38.  
  39. // --- TABLE
  40. echo '<h3>Deleting stock table...</h3>';
  41. $stock_ticker_table = $wpdb->prefix . 'stock_ticker_data';
  42. $sql = "DROP TABLE IF EXISTS $stock_ticker_table";
  43. printf( 'Executing SQL query <strong>%s</strong>' . $nl, $sql );
  44. if ( $is_deletion_confirmed ) {
  45.     $wpdb->query( $sql );
  46. }
  47.  
  48. // --- OPTIONS
  49. echo '<h3>Deleting settings options...</h3>';
  50. $stock_ticker_option_keys = [
  51.     'stockticker_version',
  52.     'stockticker_db_ver',
  53.     'st_symbols',
  54.     'st_show',
  55.     'st_quote_zero',
  56.     'st_quote_minus',
  57.     'st_quote_plus',
  58.     'stock_ticker_defaults',
  59.     'stockticker_defaults',
  60.     'stockticker_av_latest',
  61.     'stockticker_av_latest_timestamp',
  62.     'stockticker_defaults',
  63. ];
  64. foreach ( $stock_ticker_option_keys as $option_key ) {
  65.     $option_value = get_option( $option_key );
  66.     if ( $option_value ) {
  67.         printf(
  68.             'Deleting Stock Ticker option <strong>%1$s</strong> which has value: %2$s' . $nl,
  69.             $option_key,
  70.             print_r( $option_value, 1 )
  71.         );
  72.         if ( $is_deletion_confirmed ) {
  73.             delete_option( $option_key );
  74.         }
  75.     } else {
  76.         printf( 'Stock Ticker option <strong>%s</strong> does not exists. Skip deletion option.' . $nl, $option_key );
  77.     }
  78. }
  79.  
  80. // --- TRANSIENTS
  81. echo '<h3>Deleting transients...</h3>';
  82. $results = $wpdb->get_results(
  83.     $wpdb->prepare( "SELECT option_name,option_value FROM $wpdb->options WHERE option_name REGEXP %s", '_transient_st_avdata_|_transient_timeout_st_avdata_|_transient_stockticker_av_|_transient_timeout_stockticker_av_' )
  84. );
  85. foreach ( $results as $transient ) {
  86.     printf(
  87.         'Deleting transient <strong>%1$s</strong> with value: %2$s' . $nl,
  88.         $transient->option_name,
  89.         print_r( $transient->option_value, 1 )
  90.     );
  91.     if ( $is_deletion_confirmed ) {
  92.         delete_option( $transient->option_name );
  93.     }
  94. }
  95.  
  96. // --- FILES in wp-content/uploads
  97. echo '<h3>Deleting custom files...</h3>';
  98. $stock_ticker_file_names = [
  99.     'stock-ticker-custom.css',
  100.     'stock-ticker-refresh.js',
  101. ];
  102. $upload_dir = wp_upload_dir();
  103. foreach ( $stock_ticker_file_names as $file_name ) {
  104.     $file_to_delete = $upload_dir['basedir'] . '/' . $file_name;
  105.     if ( is_file( $file_to_delete ) ) {
  106.         printf( 'Deleting custom Stock Ticker file <strong>%s</strong>' . $nl, $file_to_delete );
  107.         if ( $is_deletion_confirmed ) {
  108.             unlink( "$file_to_delete" );
  109.         }
  110.     } else {
  111.         printf( 'Custom Stock Ticker file <strong>%s</strong> does not exists. Skip file deletion.' . $nl, $file_name );
  112.     }
  113. }
  114.  
  115. echo '<h3>DONE!</h3>';
  116. echo '</pre>';
  117.  
  118. if ( $is_deletion_confirmed ) {
  119.     ?>
  120. <h1>Do not forget to DELETE CLEANUP SCRIPT!</h1>
  121. <p>Once you finished Stock Ticker Reset, make sure you delete this script manually or <a href="?delete_me=yes">CLICK HERE</a></p>
  122.     <?php
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement