daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jan 21st, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Removes all sermon preached dates. Cannot be reverted!
  3.  *
  4.  * @author WP For Church (Nikola Miljković)
  5.  * @since  2019-01-21
  6.  */
  7. function sm_remove_all_sermon_dates() {
  8.     global $wpdb;
  9.  
  10.     // Check if it's requested.
  11.     if ( ! isset( $_GET['clear_sm_dates'] ) ) {
  12.         return;
  13.     }
  14.  
  15.     // Check if it's confirmed.
  16.     if ( ! isset( $_GET['clear_sm_dates_confirm'] ) ) {
  17.         wp_die(
  18.             <<<CONFIRMATION
  19. <h2>Are you sure?</h2>
  20. <p>Are you sure that you want to clear all sermon preached dates? It cannot be reverted.</p>
  21. <form method="get" style="display: inline;">
  22. <input type="submit" value="Yes" name="clear_sm_dates_confirm">
  23. <input type="hidden" name="clear_sm_dates" value="1">
  24. </form>
  25. <form method="get" style="display: inline;">
  26. <input type="submit" value="No" name="clear_sm_dates_confirm">
  27. </form>
  28. CONFIRMATION
  29.         );
  30.     }
  31.  
  32.     ob_start();
  33.  
  34.     if ( ! function_exists( 'sm_log' ) ) {
  35.         /**
  36.          * Logs a message. Temporary function.
  37.          *
  38.          * @param string $message The message to log.
  39.          * @param bool   $end     Specifies if this is the end of log.
  40.          */
  41.         function sm_temp_log_9012( $message, $end = false ) {
  42.             if ( ! isset( $GLOBALS[ __FUNCTION__ . '_start' ] ) ) {
  43.                 echo '<pre>';
  44.                 $GLOBALS[ __FUNCTION__ . '_start' ] = true; // phpcs:ignore
  45.             }
  46.  
  47.             echo '[OK] ' . $message . PHP_EOL;
  48.  
  49.             if ( $end ) {
  50.                 echo '</pre>';
  51.             }
  52.         }
  53.     }
  54.  
  55.     sm_temp_log_9012( 'Getting all sermons.' );
  56.  
  57.     // Get all sermons.
  58.     $sermons      = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s", 'wpfc_sermon' ) );
  59.     $sermon_count = is_array( $sermons ) ? count( $sermons ) : null;
  60.  
  61.     if ( $sermons ) {
  62.         sm_temp_log_9012( "Found $sermon_count " . _n( 'sermon', 'sermons', $sermon_count, 'sermon-manager-for-wordpress' ) );
  63.         sm_temp_log_9012( 'Starting date removal.' );
  64.  
  65.         foreach ( $sermons as $sermon ) {
  66.             $id = $sermon->ID;
  67.  
  68.             delete_post_meta( $id, 'sermon_date' );
  69.             update_post_meta( $id, 'sermon_date_auto', 1 );
  70.         }
  71.  
  72.         sm_temp_log_9012( 'Finished removing dates.' );
  73.  
  74.         // Try to include the file with reset function.
  75.         if ( file_exists( SM_PATH . 'includes/sm-update-functions.php' ) ) {
  76.             include_once SM_PATH . 'includes/sm-update-functions.php';
  77.         }
  78.  
  79.         // Check if we can automatically reset dates.
  80.         if ( function_exists( 'sm_update_28_fill_out_empty_dates' ) ) {
  81.             sm_temp_log_9012( 'Starting date filling.' );
  82.             sm_update_28_fill_out_empty_dates();
  83.             sm_temp_log_9012( 'Date filling finished.' );
  84.         } else {
  85.             $sm_settings_url = admin_url( 'edit.php?post_type=wpfc_sermon&page=sm-settings&tab=debug' );
  86.             sm_temp_log_9012(
  87.                 <<<MESSAGE
  88. Unfortunately, we could not fill out dates automatically. You will have to
  89.      go to <a href="$sm_settings_url">Sermon Manager settings -> Advanced</a> tab and select
  90.      "sm_update_28_fill_out_empty_dates (2.8)" in "Specific Background
  91.      Updates" setting. After that, just click on "Save changes", and you
  92.      are done.
  93. MESSAGE
  94.             );
  95.         }
  96.     } else {
  97.         sm_temp_log_9012( 'No sermons found. Aborting.' );
  98.     }
  99.  
  100.     sm_temp_log_9012( 'All done.', true );
  101.  
  102.     wp_die( ob_get_clean() );
  103. }
  104.  
  105. sm_remove_all_sermon_dates();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top