GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9c0c8a...f2b063 )
by Brad
05:17 queued 02:45
created

migrate_attachment()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
/**
3
 * FooVideo Migration Helpers Class
4
 */
5
if ( ! class_exists( 'FooGallery_Pro_Video_Migration_Helper' ) ) {
6
7
	class FooGallery_Pro_Video_Migration_Helper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
		public function get_migration_state() {
10
			$state = get_option( 'foogallery-video-migration' );
11
12
			//check for the default state
13
			if ( false === $state ) {
14
				$state = array(
15
					'step' => 0,
16
					'button_text' => __( 'Start Migration', 'foogallery' ),
17
					'message' => __('The migration will only take a few minutes. Click "Start Migration" to begin.', 'foogallery')
18
				);
19
			}
20
21
			return $state;
22
		}
23
24
		public function save_migration_state( $state ) {
25
			if ( get_option( 'foogallery-video-migration' ) !== false ) {
26
				update_option( 'foogallery-video-migration', $state );
27
			} else {
28
				add_option( 'foogallery-video-migration', $state, null, 'no' );
29
			}
30
		}
31
32
		function find_galleries_to_migrate() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
			//first take all galleries, as all templates share the video settings
34
			$gallery_posts = get_posts( array(
35
				'fields' => 'ids',
36
				'post_type'     => FOOGALLERY_CPT_GALLERY,
37
				'post_status'	=> array( 'publish', 'draft' ),
38
				'cache_results' => false,
39
				'nopaging'      => true
40
			) );
41
42
			$galleries_to_migrate = array();
43
44
			//loop through the galleries and determine if
45
			// a. they use the Video Slider gallery template
46
			// b. have a video count
47
			foreach ($gallery_posts as $gallery_id) {
48
				$gallery_template = get_post_meta( $gallery_id, FOOGALLERY_META_TEMPLATE, true );
49
50
				if ( 'videoslider' === $gallery_template ) {
51
					$galleries_to_migrate[] = $gallery_id;
52
				} else {
53
54
					$video_count = intval( get_post_meta( $gallery_id , '_foovideo_video_count', true ) );
55
56
					if ( $video_count > 0 ) {
57
						$galleries_to_migrate[] = $gallery_id;
58
					}
59
				}
60
			}
61
62
			return $galleries_to_migrate;
63
		}
64
65
		public function run_next_migration_step() {
66
			$state = $this->get_migration_state();
67
68
			if ( 0 === $state['step'] ) {
69
				//first we need to identify what needs to be migrated.
70
				$gallery_posts = $this->find_galleries_to_migrate();
71
72
				//how many videos were imported into the media library with the legacy importer?
73
				$attachment_posts = get_posts( array(
74
					'fields' => 'ids',
75
					'post_type'     => 'attachment',
76
					'cache_results' => false,
77
					'nopaging'      => true,
78
					'meta_query' => array(
79
						'relation' => 'AND',
80
						array(
81
							'key' => FOOGALLERY_FOOVIDEO_MIGRATED,
82
							'compare' => 'NOT EXISTS', // works!
83
							'value' => '' // This is ignored, but is necessary...
84
						),
85
						array(
86
							'key' => '_foovideo_video_data',
87
							'compare' => 'EXISTS',
88
						)
89
					)
90
				) );
91
92
				$gallery_count = count( $gallery_posts );
93
				$attachment_count = count( $attachment_posts );
94
95
				$state['gallery_data'] = $gallery_posts;
96
				$state['attachment_data'] = $attachment_posts;
97
98
				if ( $gallery_count > 0 && $attachment_count > 0 ) {
99
					$state['step'] = 1;
100
					$state['button_text'] =  __( 'Migrate Galleries', 'foogallery' );
101
					$state['message'] = sprintf( __('We found %d galleries and %d videos that need to be migrated. Click "Migrate Galleries" to continue.', 'foogallery' ), $gallery_count, $attachment_count );
102
				} else if ( $gallery_count === 0 && $attachment_count === 0 ) {
103
					$state['step'] = 3;
104
					$state['button_text'] =  __( 'Finalize Migration', 'foogallery' );
105
					$state['message'] = __('We found nothing that needs to be migrated. Click "Finalize Migration" to uninstall FooVideo.', 'foogallery' );
106
				} else if ( $attachment_count > 0 ) {
107
					$state['step'] = 2;
108
					$state['button_text'] =  __( 'Migrate Videos', 'foogallery' );
109
					$state['message'] = sprintf( __('We found %d videos that need to be migrated. Click "Migrate Videos" to continue.', 'foogallery' ), $attachment_count );
110
				} else if ( $gallery_count > 0 ) {
111
					$state['step'] = 1;
112
					$state['button_text'] =  __( 'Migrate Galleries', 'foogallery' );
113
					$state['message'] = sprintf( __('We found %d galleries that need to be migrated. Click "Migrate Galleries" to continue.', 'foogallery' ), $gallery_count );
114
				}
115
116
			} else if ( 1 === $state['step'] ) {
117
				//migrate the galleries
118
				$count = 0;
119
				foreach ($state['gallery_data'] as $gallery_id) {
120
					$gallery = FooGallery::get_by_id( $gallery_id );
121
					//migrate the gallery settings
122
					$this->migrate_gallery( $gallery, true );
123
					//migrate video counts
124
					$this->migrate_video_counts( $gallery_id );
125
					$count++;
126
				}
127
128
				$attachment_count = count( $state['attachment_data'] );
129
130
				if ( $attachment_count > 0 ) {
131
					$state['step'] = 2;
132
					$state['button_text'] =  __( 'Migrate Videos', 'foogallery' );
133
					$state['message'] = sprintf( __('%d galleries were migrated. %d videos still need to be migrated. Click "Migrate Videos" to continue.', 'foogallery' ), $count, $attachment_count );
134
				} else {
135
					$state['step'] = 3;
136
					$state['button_text'] =  __( 'Finalize Migration', 'foogallery' );
137
					$state['message'] = sprintf( __('%d galleries were migrated. Click "Finalize Migration" to uninstall FooVideo.', 'foogallery' ), $count );
138
				}
139
140
			} else if ( 2 === $state['step'] ) {
141
				//migrate the attachments
142
				$count = 0;
143
				foreach ($state['attachment_data'] as $attachment_id) {
144
					$this->migrate_attachment( $attachment_id );
145
					$count++;
146
				}
147
148
				$state['step'] = 3;
149
				$state['button_text'] =  __( 'Finalize Migration', 'foogallery' );
150
				$state['message'] = sprintf( __('%d video attachments were migrated. Click "Finalize Migration" to uninstall FooVideo.', 'foogallery' ), $count );
151
			} else if ( 3 === $state['step'] ) {
152
153
				//delete the option for migrations so we no longer see the migration admin message
154
				delete_option( FOOGALLERY_FOOVIDEO_MIGRATION_REQUIRED );
155
156
				//DEACTIVATE FOOVIDEO!
157
				$api = foogallery_extensions_api();
158
				$api->deactivate('foovideo', true);
159
160
				$state['step'] = 4;
161
				$state['message'] = __('The migration has completed. Click "Check" to ensure the migration was a success.', 'foogallery' );
162
				$state['button_text'] =  __( 'Check', 'foogallery' );
163
			} else if ( 4 === $state['step'] ) {
164
165
				if ( class_exists( 'Foo_Video' ) ) {
166
					$state['button_text'] =  __( 'Check Again', 'foogallery' );
167
					$state['message'] = __('FooVideo cannot be deactivated automatically. Please manually deactivate "FooGallery - Video Extension" from the plugins listing, and then restart this migration.', 'foogallery' );
168
				} else {
169
					//delete the option for migrations so we no longer see the migration admin message
170
					delete_option( FOOGALLERY_FOOVIDEO_MIGRATION_REQUIRED );
171
172
					$state['button_text'] =  __( 'All Done!', 'foogallery' );
173
					$state['message'] = __('The migration was successful. You can now navigate away from this page.', 'foogallery' );
174
				}
175
			}
176
177
			$this->save_migration_state( $state );
178
179
			return $state;
180
		}
181
182
		public function reset_state() {
183
			delete_option( 'foogallery-video-migration' );
184
			return $this->get_migration_state();
185
		}
186
187
		/**
188
		 * Checks if the gallery needs to be migrated
189
		 *
190
		 * @param $gallery_id
191
		 *
192
		 * @return bool
193
		 */
194
		public function check_gallery_needs_migration( $gallery_id ) {
195
196
			//check if the gallery has legacy videos
197
			$video_count = get_post_meta( $gallery_id , '_foovideo_video_count', true );
198
199
			if ( $video_count !== '' ) {
200
				return true;
201
			}
202
203
			return false;
204
		}
205
206
		/**
207
		 * Cleans up the legacy video counts and sets the new counts
208
		 *
209
		 * @param $gallery_id
210
		 */
211
		public function migrate_video_counts( $gallery_id ) {
212
			//get the legacy video count
213
			$video_count = intval( get_post_meta( $gallery_id , '_foovideo_video_count', true ) );
214
215
			//clear the video count so we do not migrate the gallery again
216
			delete_post_meta( $gallery_id, '_foovideo_video_count' );
217
218
			//update the new video count
219
			update_post_meta( $gallery_id, FOOGALLERY_VIDEO_POST_META_VIDEO_COUNT, $video_count );
220
		}
221
222
		/**
223
		 * Migrate a gallery's settings
224
		 *
225
		 * @param FooGallery $gallery
226
		 *
227
		 * @param bool       $save_changes
228
		 *
229
		 * @return FooGallery
230
		 */
231
		public function migrate_gallery( &$gallery, $save_changes ) {
232
233
			//get the old settings, so we can migrate to the new
234
			$settings = $gallery->settings;
235
236
			if ( 'videoslider' === $gallery->gallery_template ) {
237
238
				if ( $save_changes ) {
239
					//update the gallery template
240
					update_post_meta( $gallery->ID, FOOGALLERY_META_TEMPLATE, 'slider' );
241
				}
242
243
				//we need to port all settings from 'videoslider' across to 'slider'
244
				foreach ( $settings as $name => $value) {
245
					if ( strpos( $name, 'videoslider_' ) === 0 ) {
246
						$new_name = str_replace( 'videoslider_', 'slider_', $name );
247
						$settings[$new_name] = $value;
248
					}
249
				}
250
251
				//update the layout setting
252
				$this->migrate_setting( $settings, 'videoslider_layout', array(
253
					'rvs-vertical' => '',
254
					'rvs-horizontal' => 'fgs-horizontal'
255
				), 'slider_layout' );
0 ignored issues
show
Documentation introduced by
'slider_layout' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
256
257
				//update the viewport setting
258
				$this->migrate_setting( $settings, 'videoslider_viewport', array(
259
					'' => '',
260
					'rvs-use-viewport' => 'yes'
261
				), 'slider_viewport' );
0 ignored issues
show
Documentation introduced by
'slider_viewport' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
262
263
				//update the theme setting
264
				$this->migrate_setting( $settings, 'videoslider_theme', array(
265
					'' => 'fg-dark',
266
					'rvs-light' => 'fg-light',
267
					'rvs-custom' => 'fg-custom'
268
				), 'slider_theme' );
0 ignored issues
show
Documentation introduced by
'slider_theme' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
269
270
				//update the highlight setting
271
				$this->migrate_setting( $settings, 'videoslider_highlight', array(
272
					'' => 'fgs-purple',
273
					'rvs-blue-highlight' => 'fgs-blue',
274
					'rvs-green-highlight' => 'fgs-green',
275
					'rvs-orange-highlight' => 'fgs-orange',
276
					'rvs-red-highlight' => 'fgs-red',
277
					'rvs-custom-highlight' => 'fgs-custom'
278
				), 'slider_highlight' );
0 ignored issues
show
Documentation introduced by
'slider_highlight' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
279
280
				$gallery->settings = $settings;
281
282
				if ( $save_changes ) {
283
					update_post_meta( $gallery->ID, FOOGALLERY_META_SETTINGS, $settings );
284
				}
285
286
				$gallery->gallery_template = 'slider';
287
			}
288
289
			//we need to migrate the old foovideo settings that are saved on all galleries
290
			$this->migrate_setting(
291
				$settings, $gallery->gallery_template . '_foovideo_video_overlay', array(
292
				'video-icon-default' => 'fg-video-default',
293
				'video-icon-1'       => 'fg-video-1',
294
				'video-icon-2'       => 'fg-video-2',
295
				'video-icon-3'       => 'fg-video-3',
296
				'video-icon-4'       => 'fg-video-4'
297
			), $gallery->gallery_template . '_video_hover_icon' );
0 ignored issues
show
Documentation introduced by
$gallery->gallery_template . '_video_hover_icon' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
298
299
			$this->migrate_setting(
300
				$settings, $gallery->gallery_template . '_foovideo_sticky_icon', array(
301
				'video-icon-sticky' => 'fg-video-sticky',
302
				''                  => ''
303
			), $gallery->gallery_template . '_video_sticky_icon' );
0 ignored issues
show
Documentation introduced by
$gallery->gallery_template . '_video_sticky_icon' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
304
305
			$this->migrate_setting(
306
				$settings, $gallery->gallery_template . '_foovideo_video_size', array(
307
				'640x360'   => '640x360',
308
				'854x480'   => '854x480',
309
				'960x540'   => '960x540',
310
				'1024x576'  => '1024x576',
311
				'1280x720'  => '1280x720',
312
				'1366x768'  => '1366x768',
313
				'1600x900'  => '1600x900',
314
				'1920x1080' => '1920x1080',
315
			), $gallery->gallery_template . '_video_size' );
0 ignored issues
show
Documentation introduced by
$gallery->gallery_template . '_video_size' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
316
317
			$this->migrate_setting( $settings, $gallery->gallery_template . '_foovideo_autoplay', array(
318
				'yes' => 'yes',
319
				'no' => 'no'
320
			), $gallery->gallery_template . '_video_autoplay' );
0 ignored issues
show
Documentation introduced by
$gallery->gallery_template . '_video_autoplay' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
321
322
			$gallery->settings = $settings;
323
324
			if  ( $save_changes ) {
325
				//update the gallery settings
326
				update_post_meta( $gallery->ID, FOOGALLERY_META_SETTINGS, $settings );
327
			}
328
329
			return $gallery;
330
		}
331
332
		/**
333
		 * Migrate settings and the choice mappings
334
		 *
335
		 * @param array $settings
336
		 * @param string $setting_name
337
		 * @param array $mappings
338
		 */
339
		function migrate_setting( &$settings, $setting_name, $mappings, $override_setting_name = false ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
340
			if ( false === $settings ) {
341
				return;
342
			}
343
344
			$old_setting_name = $setting_name;
345
			foreach ( $settings as $name => $value) {
346
				if ( $old_setting_name === $name ) {
347
					foreach( $mappings as $mapping_key => $mapping_value ) {
348
						if ( $mapping_key === $value ) {
349
							if ( false === $override_setting_name ) {
350
								$override_setting_name = $setting_name;
351
							}
352
							$settings[$override_setting_name] = $mapping_value;
353
							return;
354
						}
355
					}
356
				}
357
			}
358
		}
359
360
		/**
361
		 * Migrate a single attachment
362
		 * @param $attachment_id
363
		 */
364
		function migrate_attachment( $attachment_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
365
			$video_info = get_post_meta( $attachment_id, '_foovideo_video_data', true );
366
367
			if ( isset( $video_info ) && !empty( $video_info ) ) {
368
				$is_migrated = get_post_meta( $attachment_id, FOOGALLERY_FOOVIDEO_MIGRATED, true );
369
370
				if ( '1' === $is_migrated ) return;
371
372
				//need to update the post mime type
373
				$update_attachment = array(
374
					'ID'             => $attachment_id,
375
					'post_mime_type' => 'image/foogallery'
376
				);
377
378
				wp_update_post( $update_attachment );
379
380
				//set the new data
381
				update_post_meta( $attachment_id, FOOGALLERY_VIDEO_POST_META, $video_info );
382
383
				//mark as migrated
384
				update_post_meta( $attachment_id, FOOGALLERY_FOOVIDEO_MIGRATED, 1 );
385
			}
386
		}
387
388
	}
389
}