Update_V300::update_posts()   F
last analyzed

Complexity

Conditions 31
Paths > 20000

Size

Total Lines 198
Code Lines 134

Duplication

Lines 28
Ratio 14.14 %

Importance

Changes 0
Metric Value
cc 31
eloc 134
nc 512001
nop 1
dl 28
loc 198
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Update to 3.0.0
4
 *
5
 * @package SimpleCalendar/Updates
6
 */
7
namespace SimpleCalendar\Updates;
8
9
use Carbon\Carbon;
10
use SimpleCalendar\Post_Types;
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Update to 3.0.0
18
 */
19
class Update_V300 {
20
21
	/**
22
	 * Update posts and options.
23
	 *
24
	 * @param array $posts
25
	 */
26
	public function __construct( $posts ) {
27
28
		Post_Types::register_taxonomies();
29
		Post_Types::register_post_types();
30
31
		if ( ! empty( $posts ) && is_array( $posts ) ) {
32
			$this->update_posts( $posts );
33
			$this->update_post_type();
34
			$this->update_widgets();
35
		}
36
		$this->update_options();
37
38
		flush_rewrite_rules();
39
	}
40
41
	/**
42
	 * Update posts.
43
	 *
44
	 * @param $posts
45
	 */
46
	public function update_posts( $posts ) {
47
48
		foreach ( $posts as $post ) {
49
50
			$post_id = $post->ID;
51
52
			// Assign a feed taxonomy term (feed type) to legacy posts.
53
			wp_set_object_terms( $post_id, 'google', 'calendar_feed' );
54
55
			// Assign a calendar taxonomy term (calendar type) to legacy posts.
56
			wp_set_object_terms( $post_id, 'default-calendar', 'calendar_type' );
57
58
			// Convert legacy list/grid view to default calendar view.
59
			$display = get_post_meta( $post_id, 'gce_display_mode', true );
60
			$views = array();
61
			$range = false;
62
			if ( 'list' == $display ) {
63
				$views['default-calendar'] = 'list';
64
			} elseif ( 'list-grouped' == $display ) {
65
				$views['default-calendar'] = 'list';
66
			} elseif ( 'date-range-list' == $display ) {
67
				$views['default-calendar'] = 'list';
68
				$range                     = true;
69
			} elseif ( 'date-range-grid' == $display ) {
70
				$views['default-calendar'] = 'grid';
71
				$range                     = true;
72
			} else {
73
				$views['default-calendar'] = 'grid';
74
			}
75
			update_post_meta( $post_id, '_calendar_view', $views );
76
77
			// List calendar settings.
78
			$list_span  = get_post_meta( $post_id, 'gce_events_per_page', true );
79
			$list_range = max( absint( get_post_meta( $post_id, 'gce_per_page_num', true ) ), 1 );
80
			if ( 'days' == $list_span ) {
81
				$list_type = 'daily';
82
			} elseif ( 'week' == $list_span ) {
83
				$list_type = 'weekly';
84
				$list_range = 1;
85
			} elseif ( 'month' == $list_span ) {
86
				$list_type = 'monthly';
87
				$list_range = 1;
88
			} else {
89
				$list_type = 'events';
90
			}
91
			update_post_meta( $post_id, '_default_calendar_list_range_type', $list_type );
92
			update_post_meta( $post_id, '_default_calendar_list_range_span', $list_range );
93
94
			$calendar_begins = 'today';
95
96
			// Custom calendar range.
97
			if ( $range === true ) {
98
99
				$begins = get_post_meta( $post_id, 'gce_feed_range_start', true );
100
				$ends   = get_post_meta( $post_id, 'gce_feed_range_end', true );
101
102
				if ( $begins && $ends ) {
103
					update_post_meta( $post_id, '_calendar_begins', 'custom_date' );
104
					update_post_meta( $post_id, '_calendar_begins_custom_date', $this->convert_legacy_range( $begins ) );
105
				} else {
106
					update_post_meta( $post_id, '_calendar_begins', $calendar_begins );
107
				}
108
109
			} else {
110
111
				// Legacy list calendars may have a start offset.
112
				$offset = absint( get_post_meta( $post_id, 'gce_list_start_offset_num', true ) );
113
				if ( 'list' == $display && $offset > 0 ) {
114
					$calendar_begins = 'back' == get_post_meta( $post_id, 'gce_list_start_offset_direction', true ) ? 'days_before' : 'days_after';
115
					update_post_meta( $post_id, '_calendar_begins_nth', $offset );
116
				}
117
118
				update_post_meta( $post_id, '_calendar_begins', $calendar_begins );
119
			}
120
121
			// Earliest event.
122
			$start_before = get_post_meta( $post_id, 'gce_feed_start', true );
123
			$start_amt = absint( get_post_meta( $post_id, 'gce_feed_start_num', true ) );
124 View Code Duplication
			if ( $start_amt > 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
				if ( 'years' == $start_before ) {
126
					$earliest = 'years_before';
127
				} elseif ( 'months' == $start_before ) {
128
					$earliest = 'months_before';
129
				} else {
130
					$earliest = 'days_before';
131
				}
132
				update_post_meta( $post_id, '_feed_earliest_event_date', $earliest );
133
				update_post_meta( $post_id, '_feed_earliest_event_date_range', $start_amt );
134
			} else {
135
				update_post_meta( $post_id, '_feed_earliest_event_date', 'calendar_start' );
136
				update_post_meta( $post_id, '_feed_earliest_event_date_range', 1 );
137
			}
138
139
			// Latest event.
140
			$end_after = get_post_meta( $post_id, 'gce_feed_end', true );
141
			$end_amt   = absint( get_post_meta( $post_id, 'gce_feed_end_num', true ) );
142 View Code Duplication
			if ( $end_amt > 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
				if ( 'years' == $end_after ) {
144
					$latest = 'years_after';
145
				} elseif ( 'months' == $end_after ) {
146
					$latest = 'months_after';
147
				} else {
148
					$latest = 'days_after';
149
				}
150
				update_post_meta( $post_id, '_feed_latest_event_date', $latest );
151
				update_post_meta( $post_id, '_feed_latest_event_date_range', $end_amt );
152
			} else {
153
				update_post_meta( $post_id, '_feed_latest_event_date', 'calendar_start' );
154
				update_post_meta( $post_id, '_feed_latest_event_date_range', 1 );
155
			}
156
157
			// Static calendar.
158
			if ( false === get_post_meta( $post_id, 'gce_paging', true ) ) {
159
				update_post_meta( $post_id, '_calendar_is_static', 'yes' );
160
			}
161
162
			// Default calendar bubble trigger (click was unavailable before 3.0.0).
163
			update_post_meta( $post_id, '_default_calendar_event_bubble_trigger', 'hover' );
164
165
			// Default calendar multiple day events.
166
			if ( get_post_meta( $post_id, 'gce_multi_day_events', true ) ) {
167
				update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'yes' );
168
			} else {
169
				update_post_meta( $post_id, '_default_calendar_expand_multi_day_events', 'no' );
170
			}
171
172
			// Google Calendar ID.
173
			$google_id = get_post_meta( $post_id, 'gce_feed_url', true );
174
			update_post_meta( $post_id, '_google_calendar_id', base64_encode( trim( $google_id ) ) );
175
176
			// Google max results.
177
			update_post_meta( $post_id, '_google_events_max_results', 2500 );
178
179
			// Google calendar feed search terms.
180
			$google_search = get_post_meta( $post_id, 'gce_search_query', true );
181
			if ( ! empty( $google_search ) ) {
182
				update_post_meta( $post_id, '_google_events_search_query', trim( $google_search ) );
183
			}
184
185
			// Google recurring events.
186
			if ( get_post_meta( $post_id, 'gce_expand_recurring', true ) ) {
187
				update_post_meta( $post_id, '_google_events_recurring', 'show' );
188
			} else {
189
				update_post_meta( $post_id, '_google_events_recurring', 'first-only' );
190
			}
191
192
			// Date and time format.
193
			$date_format = get_post_meta( $post_id, 'gce_date_format', true );
194
			if ( ! empty( $date_format ) ) {
195
				update_post_meta( $post_id, '_calendar_date_format_setting', 'use_custom_php' );
196
				update_post_meta( $post_id, '_calendar_date_format_php', $date_format );
197
			} else {
198
				update_post_meta( $post_id, '_calendar_date_format_setting', 'use_site' );
199
			}
200
			$time_format = get_post_meta( $post_id, 'gce_time_format', true );
201
			if ( ! empty( $time_format ) ) {
202
				update_post_meta( $post_id, '_calendar_time_format_setting', 'use_custom_php' );
203
				update_post_meta( $post_id, '_calendar_time_format_php', $time_format );
204
			} else {
205
				update_post_meta( $post_id, '_calendar_time_format_setting', 'use_site' );
206
			}
207
			update_post_meta( $post_id, '_calendar_datetime_separator', '@' );
208
			update_post_meta( $post_id, '_calendar_week_starts_on_setting', 'use_site' );
209
210
			// Feed transient cache duration.
211
			$cache = get_post_meta( $post_id, 'gce_cache', true );
212
			if ( is_numeric( $cache ) ) {
213
				$seconds = absint( $cache );
214
				if ( $seconds < 3600 ) {
215
					$amount = $seconds / 60;
216
					$unit   = 60;
217
				} elseif ( $seconds < 86400 ) {
218
					$amount = $seconds / 3600;
219
					$unit   = 3600;
220
				} elseif ( $seconds < 604800 ) {
221
					$amount = $seconds / 86400;
222
					$unit   = 86400;
223
				} else {
224
					$amount = $seconds / 604800;
225
					$unit   = 604800;
226
				}
227
				$amount = max( ceil( $amount ), 1 );
228
				update_post_meta( $post_id, '_feed_cache_user_unit', $unit );
229
				update_post_meta( $post_id, '_feed_cache_user_amount', $amount );
230
				update_post_meta( $post_id, '_feed_cache', $unit * $amount );
231
			} else {
232
				update_post_meta( $post_id, '_feed_cache_user_unit', 2 );
233
				update_post_meta( $post_id, '_feed_cache_user_amount', 3600 );
234
				update_post_meta( $post_id, '_feed_cache', 7200 );
235
			}
236
237
			$this->delete_post_meta( $post_id );
238
239
			// Post updated.
240
			update_post_meta( $post_id, '_calendar_version', '3.0.0' );
241
		}
242
243
	}
244
245
	/**
246
	 * Delete legacy post meta.
247
	 *
248
	 * @param int $post_id
249
	 */
250
	public function delete_post_meta( $post_id ) {
251
252
		$post_meta = array(
253
			'gce_cache',
254
			'gce_custom_from',
255
			'gce_custom_until',
256
			'gce_date_format',
257
			'gce_display_description',
258
			'gce_display_description_max',
259
			'gce_display_description_text',
260
			'gce_display_end',
261
			'gce_display_end_text',
262
			'gce_display_link',
263
			'gce_display_link_tab',
264
			'gce_display_link_text',
265
			'gce_display_location',
266
			'gce_display_location_text',
267
			'gce_display_mode',
268
			'gce_display_separator',
269
			'gce_display_simple',
270
			'gce_display_start',
271
			'gce_display_start_text',
272
			'gce_events_per_page',
273
			'gce_expand_recurring',
274
			'gce_feed_end',
275
			'gce_feed_end_custom',
276
			'gce_feed_end_num',
277
			'gce_feed_range_end',
278
			'gce_feed_range_start',
279
			'gce_feed_start',
280
			'gce_feed_start_custom',
281
			'gce_feed_start_num',
282
			'gce_feed_url',
283
			'gce_feed_use_range',
284
			'gce_list_start_offset_direction',
285
			'gce_list_start_offset_num',
286
			'gce_multi_day_events',
287
			'gce_paging',
288
			'gce_per_page_from',
289
			'gce_per_page_num',
290
			'gce_per_page_to',
291
			'gce_retrieve_from',
292
			'gce_retrieve_until',
293
			'gce_search_query',
294
			'gce_show_tooltips',
295
			'gce_time_format',
296
		);
297
298
		foreach ( $post_meta as $meta_key ) {
299
			delete_post_meta( $post_id, $meta_key );
300
		}
301
	}
302
303
	/**
304
	 * Update post type slug.
305
	 */
306
	public function update_post_type() {
307
308
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
309
		$table = $wpdb->prefix . 'posts';
310
311
		$wpdb->query(
312
			"
313
UPDATE {$table} SET `post_type`='calendar' WHERE `post_type`='gce_feed';
314
"
315
		);
316
	}
317
318
	/**
319
	 * Update options.
320
	 */
321
	public function update_options() {
322
323
		$old_settings          = get_option( 'gce_settings_general' );
324
		$new_settings_feeds    = get_option( 'simple-calendar_settings_feeds' );
325
		$new_settings_advanced = get_option( 'simple-calendar_settings_advanced' );
326
327
		// If empty probably using a legacy hardcoded key (no longer recommended).
328
		$new_settings_feeds['google']['api_key'] = ! empty( $old_settings['api_key'] ) ? $old_settings['api_key'] : '';
329
		update_option( 'simple-calendar_settings_feeds', $new_settings_feeds );
330
331
		$new_settings_advanced['assets']['disable_css'] = ! empty( $old_settings['disable_css'] ) ? 'yes' : '';
332
		update_option( 'simple-calendar_settings_advanced', $new_settings_advanced );
333
334
		// Delete legacy options.
335
		delete_option( 'gce_version' );
336
		delete_option( 'gce_options' );
337
		delete_option( 'gce_upgrade_has_run' );
338
		delete_option( 'gce_v240_update_notices' );
339
		delete_option( 'gce_show_admin_install_notice' );
340
	}
341
342
	/**
343
	 * Update widgets.
344
	 */
345
	public function update_widgets() {
346
347
		$old_widgets = get_option( 'widget_gce_widget' );
348
349
		if ( ! empty( $old_widgets ) && is_array( $old_widgets ) ) {
350
351
			$new_widgets = array();
352
353
			foreach ( $old_widgets as $i => $old_widget ) {
354
				if ( isset( $old_widget['id'] ) ) {
355
356
					$id = absint( substr( $old_widget['id'], 0, strspn( $old_widget['id'], '0123456789' ) ) );
357
358
					if ( $id > 0 ) {
359
						$new_widgets[ $i ]['title']       = isset( $old_widget['name'] ) ? $old_widget['name'] : 'Simple Calendar';
360
						$new_widgets[ $i ]['calendar_id'] = $id;
361
					}
362
				}
363
			}
364
365
			if ( ! empty( $new_widgets ) ) {
366
				update_option( 'widget_gce_widget', $new_widgets );
367
			}
368
		}
369
	}
370
371
	/**
372
	 * Convert legacy range date.
373
	 *
374
	 * Converts US format m/d/Y to international ISO 8601 Y-m-d.
375
	 *
376
	 * @param  string $date
377
	 *
378
	 * @return string
379
	 */
380
	private function convert_legacy_range( $date ) {
381
		$date = empty( $date ) ? date( 'm/d/Y', time() ) : $date;
382
		$timestamp = Carbon::createFromFormat( 'm/d/Y', $date )->getTimestamp();
383
		return date( 'Y-m-d', $timestamp );
384
	}
385
386
}
387