SimpleCalendarUpdatesUpdate_V300

Update to 3.0.0.

Defined (1)

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

/includes/updates/update-v300.php  
  1. class Update_V300 { 
  2.  
  3. /** 
  4. * Update posts and options. 
  5. * @param array $posts 
  6. */ 
  7. public function __construct( $posts ) { 
  8.  
  9. Post_Types::register_taxonomies(); 
  10. Post_Types::register_post_types(); 
  11.  
  12. if ( ! empty( $posts ) && is_array( $posts ) ) { 
  13. $this->update_posts( $posts ); 
  14. $this->update_post_type(); 
  15. $this->update_widgets(); 
  16. $this->update_options(); 
  17.  
  18. flush_rewrite_rules(); 
  19.  
  20. /** 
  21. * Update posts. 
  22. * @param $posts 
  23. */ 
  24. public function update_posts( $posts ) { 
  25.  
  26. foreach ( $posts as $post ) { 
  27.  
  28. $post_id = $post->ID; 
  29.  
  30. // Assign a feed taxonomy term (feed type) to legacy posts. 
  31. wp_set_object_terms( $post_id, 'google', 'calendar_feed' ); 
  32.  
  33. // Assign a calendar taxonomy term (calendar type) to legacy posts. 
  34. wp_set_object_terms( $post_id, 'default-calendar', 'calendar_type' ); 
  35.  
  36. // Convert legacy list/grid view to default calendar view. 
  37. $display = get_post_meta( $post_id, 'gce_display_mode', true ); 
  38. $views = array(); 
  39. $range = false; 
  40. if ( 'list' == $display ) { 
  41. $views['default-calendar'] = 'list'; 
  42. } elseif ( 'list-grouped' == $display ) { 
  43. $views['default-calendar'] = 'list'; 
  44. } elseif ( 'date-range-list' == $display ) { 
  45. $views['default-calendar'] = 'list'; 
  46. $range = true; 
  47. } elseif ( 'date-range-grid' == $display ) { 
  48. $views['default-calendar'] = 'grid'; 
  49. $range = true; 
  50. } else { 
  51. $views['default-calendar'] = 'grid'; 
  52. update_post_meta( $post_id, '_calendar_view', $views ); 
  53.  
  54. // List calendar settings. 
  55. $list_span = get_post_meta( $post_id, 'gce_events_per_page', true ); 
  56. $list_range = max( absint( get_post_meta( $post_id, 'gce_per_page_num', true ) ), 1 ); 
  57. if ( 'days' == $list_span ) { 
  58. $list_type = 'daily'; 
  59. } elseif ( 'week' == $list_span ) { 
  60. $list_type = 'weekly'; 
  61. $list_range = 1; 
  62. } elseif ( 'month' == $list_span ) { 
  63. $list_type = 'monthly'; 
  64. $list_range = 1; 
  65. } else { 
  66. $list_type = 'events'; 
  67. update_post_meta( $post_id, '_default_calendar_list_range_type', $list_type ); 
  68. update_post_meta( $post_id, '_default_calendar_list_range_span', $list_range ); 
  69.  
  70. $calendar_begins = 'today'; 
  71.  
  72. // Custom calendar range. 
  73. if ( $range === true ) { 
  74.  
  75. $begins = get_post_meta( $post_id, 'gce_feed_range_start', true ); 
  76. $ends = get_post_meta( $post_id, 'gce_feed_range_end', true ); 
  77.  
  78. if ( $begins && $ends ) { 
  79. update_post_meta( $post_id, '_calendar_begins', 'custom_date' ); 
  80. update_post_meta( $post_id, '_calendar_begins_custom_date', $this->convert_legacy_range( $begins ) ); 
  81. } else { 
  82. update_post_meta( $post_id, '_calendar_begins', $calendar_begins ); 
  83.  
  84. } else { 
  85.  
  86. // Legacy list calendars may have a start offset. 
  87. $offset = absint( get_post_meta( $post_id, 'gce_list_start_offset_num', true ) ); 
  88. if ( 'list' == $display && $offset > 0 ) { 
  89. $calendar_begins = 'back' == get_post_meta( $post_id, 'gce_list_start_offset_direction', true ) ? 'days_before' : 'days_after'; 
  90. update_post_meta( $post_id, '_calendar_begins_nth', $offset ); 
  91.  
  92. update_post_meta( $post_id, '_calendar_begins', $calendar_begins ); 
  93.  
  94. // Earliest event. 
  95. $start_before = get_post_meta( $post_id, 'gce_feed_start', true ); 
  96. $start_amt = absint( get_post_meta( $post_id, 'gce_feed_start_num', true ) ); 
  97. if ( $start_amt > 0 ) { 
  98. if ( 'years' == $start_before ) { 
  99. $earliest = 'years_before'; 
  100. } elseif ( 'months' == $start_before ) { 
  101. $earliest = 'months_before'; 
  102. } else { 
  103. $earliest = 'days_before'; 
  104. update_post_meta( $post_id, '_feed_earliest_event_date', $earliest ); 
  105. update_post_meta( $post_id, '_feed_earliest_event_date_range', $start_amt ); 
  106. } else { 
  107. update_post_meta( $post_id, '_feed_earliest_event_date', 'calendar_start' ); 
  108. update_post_meta( $post_id, '_feed_earliest_event_date_range', 1 ); 
  109.  
  110. // Latest event. 
  111. $end_after = get_post_meta( $post_id, 'gce_feed_end', true ); 
  112. $end_amt = absint( get_post_meta( $post_id, 'gce_feed_end_num', true ) ); 
  113. if ( $end_amt > 0 ) { 
  114. if ( 'years' == $end_after ) { 
  115. $latest = 'years_after'; 
  116. } elseif ( 'months' == $end_after ) { 
  117. $latest = 'months_after'; 
  118. } else { 
  119. $latest = 'days_after'; 
  120. update_post_meta( $post_id, '_feed_latest_event_date', $latest ); 
  121. update_post_meta( $post_id, '_feed_latest_event_date_range', $end_amt ); 
  122. } else { 
  123. update_post_meta( $post_id, '_feed_latest_event_date', 'calendar_start' ); 
  124. update_post_meta( $post_id, '_feed_latest_event_date_range', 1 ); 
  125.  
  126. // Static calendar. 
  127. if ( false === get_post_meta( $post_id, 'gce_paging', true ) ) { 
  128. update_post_meta( $post_id, '_calendar_is_static', 'yes' ); 
  129.  
  130. // Default calendar bubble trigger (click was unavailable before 3.0.0). 
  131. update_post_meta( $post_id, '_default_calendar_event_bubble_trigger', 'hover' ); 
  132.  
  133. // Default calendar multiple day events. 
  134. if ( get_post_meta( $post_id, 'gce_multi_day_events', true ) ) { 
  135. update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'yes' ); 
  136. } else { 
  137. update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'no' ); 
  138.  
  139. // Google Calendar ID. 
  140. $google_id = get_post_meta( $post_id, 'gce_feed_url', true ); 
  141. update_post_meta( $post_id, '_google_calendar_id', base64_encode( trim( $google_id ) ) ); 
  142.  
  143. // Google max results. 
  144. update_post_meta( $post_id, '_google_events_max_results', 2500 ); 
  145.  
  146. // Google calendar feed search terms. 
  147. $google_search = get_post_meta( $post_id, 'gce_search_query', true ); 
  148. if ( ! empty( $google_search ) ) { 
  149. update_post_meta( $post_id, '_google_events_search_query', trim( $google_search ) ); 
  150.  
  151. // Google recurring events. 
  152. if ( get_post_meta( $post_id, 'gce_expand_recurring', true ) ) { 
  153. update_post_meta( $post_id, '_google_events_recurring', 'show' ); 
  154. } else { 
  155. update_post_meta( $post_id, '_google_events_recurring', 'first-only' ); 
  156.  
  157. // Date and time format. 
  158. $date_format = get_post_meta( $post_id, 'gce_date_format', true ); 
  159. if ( ! empty( $date_format ) ) { 
  160. update_post_meta( $post_id, '_calendar_date_format_setting', 'use_custom_php' ); 
  161. update_post_meta( $post_id, '_calendar_date_format_php', $date_format ); 
  162. } else { 
  163. update_post_meta( $post_id, '_calendar_date_format_setting', 'use_site' ); 
  164. $time_format = get_post_meta( $post_id, 'gce_time_format', true ); 
  165. if ( ! empty( $time_format ) ) { 
  166. update_post_meta( $post_id, '_calendar_time_format_setting', 'use_custom_php' ); 
  167. update_post_meta( $post_id, '_calendar_time_format_php', $time_format ); 
  168. } else { 
  169. update_post_meta( $post_id, '_calendar_time_format_setting', 'use_site' ); 
  170. update_post_meta( $post_id, '_calendar_datetime_separator', '@' ); 
  171. update_post_meta( $post_id, '_calendar_week_starts_on_setting', 'use_site' ); 
  172.  
  173. // Feed transient cache duration. 
  174. $cache = get_post_meta( $post_id, 'gce_cache', true ); 
  175. if ( is_numeric( $cache ) ) { 
  176. $seconds = absint( $cache ); 
  177. if ( $seconds < 3600 ) { 
  178. $amount = $seconds / 60; 
  179. $unit = 60; 
  180. } elseif ( $seconds < 86400 ) { 
  181. $amount = $seconds / 3600; 
  182. $unit = 3600; 
  183. } elseif ( $seconds < 604800 ) { 
  184. $amount = $seconds / 86400; 
  185. $unit = 86400; 
  186. } else { 
  187. $amount = $seconds / 604800; 
  188. $unit = 604800; 
  189. $amount = max( ceil( $amount ), 1 ); 
  190. update_post_meta( $post_id, '_feed_cache_user_unit', $unit ); 
  191. update_post_meta( $post_id, '_feed_cache_user_amount', $amount ); 
  192. update_post_meta( $post_id, '_feed_cache', $unit * $amount ); 
  193. } else { 
  194. update_post_meta( $post_id, '_feed_cache_user_unit', 2 ); 
  195. update_post_meta( $post_id, '_feed_cache_user_amount', 3600 ); 
  196. update_post_meta( $post_id, '_feed_cache', 7200 ); 
  197.  
  198. $this->delete_post_meta( $post_id ); 
  199.  
  200. // Post updated. 
  201. update_post_meta( $post_id, '_calendar_version', '3.0.0' ); 
  202.  
  203.  
  204. /** 
  205. * Delete legacy post meta. 
  206. * @param int $post_id 
  207. */ 
  208. public function delete_post_meta( $post_id ) { 
  209.  
  210. $post_meta = array( 
  211. 'gce_cache',  
  212. 'gce_custom_from',  
  213. 'gce_custom_until',  
  214. 'gce_date_format',  
  215. 'gce_display_description',  
  216. 'gce_display_description_max',  
  217. 'gce_display_description_text',  
  218. 'gce_display_end',  
  219. 'gce_display_end_text',  
  220. 'gce_display_link',  
  221. 'gce_display_link_tab',  
  222. 'gce_display_link_text',  
  223. 'gce_display_location',  
  224. 'gce_display_location_text',  
  225. 'gce_display_mode',  
  226. 'gce_display_separator',  
  227. 'gce_display_simple',  
  228. 'gce_display_start',  
  229. 'gce_display_start_text',  
  230. 'gce_events_per_page',  
  231. 'gce_expand_recurring',  
  232. 'gce_feed_end',  
  233. 'gce_feed_end_custom',  
  234. 'gce_feed_end_num',  
  235. 'gce_feed_range_end',  
  236. 'gce_feed_range_start',  
  237. 'gce_feed_start',  
  238. 'gce_feed_start_custom',  
  239. 'gce_feed_start_num',  
  240. 'gce_feed_url',  
  241. 'gce_feed_use_range',  
  242. 'gce_list_start_offset_direction',  
  243. 'gce_list_start_offset_num',  
  244. 'gce_multi_day_events',  
  245. 'gce_paging',  
  246. 'gce_per_page_from',  
  247. 'gce_per_page_num',  
  248. 'gce_per_page_to',  
  249. 'gce_retrieve_from',  
  250. 'gce_retrieve_until',  
  251. 'gce_search_query',  
  252. 'gce_show_tooltips',  
  253. 'gce_time_format',  
  254. ); 
  255.  
  256. foreach ( $post_meta as $meta_key ) { 
  257. delete_post_meta( $post_id, $meta_key ); 
  258.  
  259. /** 
  260. * Update post type slug. 
  261. */ 
  262. public function update_post_type() { 
  263.  
  264. global $wpdb; 
  265. $table = $wpdb->prefix . 'posts'; 
  266.  
  267. $wpdb->query( 
  268. UPDATE {$table} SET `post_type`='calendar' WHERE `post_type`='gce_feed'; 
  269. ); 
  270.  
  271. /** 
  272. * Update options. 
  273. */ 
  274. public function update_options() { 
  275.  
  276. $old_settings = get_option( 'gce_settings_general' ); 
  277. $new_settings_feeds = get_option( 'simple-calendar_settings_feeds' ); 
  278. $new_settings_advanced = get_option( 'simple-calendar_settings_advanced' ); 
  279.  
  280. // If empty probably using a legacy hardcoded key (no longer recommended). 
  281. $new_settings_feeds['google']['api_key'] = ! empty( $old_settings['api_key'] ) ? $old_settings['api_key'] : ''; 
  282. update_option( 'simple-calendar_settings_feeds', $new_settings_feeds ); 
  283.  
  284. $new_settings_advanced['assets']['disable_css'] = ! empty( $old_settings['disable_css'] ) ? 'yes' : ''; 
  285. update_option( 'simple-calendar_settings_advanced', $new_settings_advanced ); 
  286.  
  287. // Delete legacy options. 
  288. delete_option( 'gce_version' ); 
  289. delete_option( 'gce_options' ); 
  290. delete_option( 'gce_upgrade_has_run' ); 
  291. delete_option( 'gce_v240_update_notices' ); 
  292. delete_option( 'gce_show_admin_install_notice' ); 
  293.  
  294. /** 
  295. * Update widgets. 
  296. */ 
  297. public function update_widgets() { 
  298.  
  299. $old_widgets = get_option( 'widget_gce_widget' ); 
  300.  
  301. if ( ! empty( $old_widgets ) && is_array( $old_widgets ) ) { 
  302.  
  303. $new_widgets = array(); 
  304.  
  305. foreach ( $old_widgets as $i => $old_widget ) { 
  306. if ( isset( $old_widget['id'] ) ) { 
  307.  
  308. $id = absint( substr( $old_widget['id'], 0, strspn( $old_widget['id'], '0123456789' ) ) ); 
  309.  
  310. if ( $id > 0 ) { 
  311. $new_widgets[ $i ]['title'] = isset( $old_widget['name'] ) ? $old_widget['name'] : 'Simple Calendar'; 
  312. $new_widgets[ $i ]['calendar_id'] = $id; 
  313.  
  314. if ( ! empty( $new_widgets ) ) { 
  315. update_option( 'widget_gce_widget', $new_widgets ); 
  316.  
  317. /** 
  318. * Convert legacy range date. 
  319. * Converts US format m/d/Y to international ISO 8601 Y-m-d. 
  320. * @param string $date 
  321. * @return string 
  322. */ 
  323. private function convert_legacy_range( $date ) { 
  324. $date = empty( $date ) ? date( 'm/d/Y', time() ) : $date; 
  325. $timestamp = Carbon::createFromFormat( 'm/d/Y', $date )->getTimestamp(); 
  326. return date( 'Y-m-d', $timestamp ); 
  327.