SimpleCalendarUpdate

Update script.

Defined (1)

The class is defined in the following location(s).

/includes/update.php  
  1. class Update { 
  2.  
  3. /** 
  4. * Previous version. 
  5. * @access protected 
  6. * @var string 
  7. */ 
  8. private $installed_ver = '0.0.0'; 
  9.  
  10. /** 
  11. * Current version. 
  12. * @access private 
  13. * @var string 
  14. */ 
  15. private $new_ver = '0.0.0'; 
  16.  
  17. /** 
  18. * Existing posts. 
  19. * @access private 
  20. * @var array 
  21. */ 
  22. private $posts = array(); 
  23.  
  24. /** 
  25. * Update path. 
  26. * @access private 
  27. * @var array 
  28. */ 
  29. private $update_path = array( 
  30. '2.1.0',  
  31. '2.2.0',  
  32. '3.0.0',  
  33. '3.0.13',  
  34. ); 
  35.  
  36. /** 
  37. * Constructor. 
  38. * @since 3.0.0 
  39. * @param string $version (optional) Current plugin version, defaults to value in plugin constant. 
  40. */ 
  41. public function __construct( $version = SIMPLE_CALENDAR_VERSION ) { 
  42. // Look for previous version in current or legacy option, null for fresh install. 
  43. $installed = get_option( 'simple-calendar_version', null ); 
  44. $this->installed_ver = is_null( $installed ) ? get_option( 'gce_version', null ) : $installed; 
  45. $this->new_ver = $version; 
  46.  
  47. if ( version_compare( $this->installed_ver, $this->new_ver, '<' ) ) { 
  48. $this->run_updates(); 
  49.  
  50. /** 
  51. * Update to current version. 
  52. * Runs all the update scripts through version steps. 
  53. * @since 3.0.0 
  54. */ 
  55. public function run_updates() { 
  56.  
  57. do_action( 'simcal_before_update', $this->installed_ver ); 
  58.  
  59. if ( ! is_null( $this->installed_ver ) ) { 
  60.  
  61. if ( version_compare( $this->installed_ver, $this->new_ver ) === -1 ) { 
  62.  
  63. $post_type = version_compare( $this->installed_ver, '3.0.0' ) === -1 ? 'gce_feed' : 'calendar'; 
  64. $this->posts = $this->get_posts( $post_type ); 
  65.  
  66. foreach ( $this->update_path as $update_to ) { 
  67. if ( version_compare( $this->installed_ver, $update_to, '<' ) ) { 
  68. $this->update( $update_to ); 
  69.  
  70.  
  71. simcal_delete_feed_transients(); 
  72.  
  73. } else { 
  74.  
  75. new Post_Types(); 
  76. flush_rewrite_rules(); 
  77.  
  78.  
  79. do_action( 'simcal_updated', $this->new_ver ); 
  80.  
  81. // Redirect to a welcome page if new install or major update. 
  82. if ( is_null( $this->installed_ver ) ) { 
  83. set_transient( '_simple-calendar_activation_redirect', 'fresh', 60 ); 
  84. } else { 
  85. $major_new = substr( $this->new_ver, 0, strrpos( $this->new_ver, '.' ) ); 
  86. $major_old = substr( $this->installed_ver, 0, strrpos( $this->installed_ver, '.' ) ); 
  87. if ( version_compare( $major_new, $major_old, '>' ) ) { 
  88. set_transient( '_simple-calendar_activation_redirect', 'update', 60 ); 
  89. } elseif ( $major_old == $major_new ) { 
  90. $version = explode( '.', $this->new_ver ); 
  91. end( $version ); 
  92. if ( 0 === intval( current( $version ) ) ) { 
  93. set_transient( '_simple-calendar_activation_redirect', 'update', 60 ); 
  94.  
  95. $this->admin_redirects(); 
  96.  
  97. update_option( 'simple-calendar_version', $this->new_ver ); 
  98.  
  99. /** 
  100. * Handle redirects to welcome page after install and updates. 
  101. * Transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. 
  102. * @since 3.0.0 
  103. */ 
  104. public function admin_redirects() { 
  105.  
  106. $transient = get_transient( '_simple-calendar_activation_redirect' ); 
  107.  
  108. if ( ! $transient || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { 
  109. return; 
  110.  
  111. delete_transient( '_simple-calendar_activation_redirect' ); 
  112.  
  113. // Do not redirect if already on welcome page screen. 
  114. if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'simple-calendar_about' ) ) ) { 
  115. return; 
  116.  
  117. $url = add_query_arg( 
  118. 'simcal_install',  
  119. esc_attr( $transient ),  
  120. admin_url( 'index.php?page=simple-calendar_about' ) 
  121. ); 
  122. wp_safe_redirect( $url ); 
  123. exit; 
  124.  
  125. /** 
  126. * Get posts. 
  127. * @since 3.0.0 
  128. * @param $post_type 
  129. * @return array 
  130. */ 
  131. private function get_posts( $post_type ) { 
  132.  
  133. $posts = array(); 
  134.  
  135. if ( ! empty( $post_type ) ) { 
  136.  
  137. // https://core.trac.wordpress.org/ticket/18408 
  138. $posts = get_posts( array( 
  139. 'post_type' => $post_type,  
  140. 'post_status' => array( 
  141. 'draft',  
  142. 'future',  
  143. 'publish',  
  144. 'pending',  
  145. 'private',  
  146. 'trash',  
  147. ),  
  148. 'nopaging' => true,  
  149. ) ); 
  150.  
  151. wp_reset_postdata(); 
  152.  
  153. return $posts; 
  154.  
  155. /** 
  156. * Update. 
  157. * Runs an update script for the specified version passed in argument. 
  158. * @since 3.0.0 
  159. * @param string $version 
  160. */ 
  161. private function update( $version ) { 
  162.  
  163. $update_v = '\\' . __NAMESPACE__ . '\Updates\\Update_V' . str_replace( '.', '', $version ); 
  164.  
  165. if ( class_exists( $update_v ) ) { 
  166. new $update_v( $this->posts ); 
  167.