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 — feature/video-support ( 96814b )
by Brad
05:36
created

migrate_attachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 14
rs 9.4285
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
		public function run_next_migration_step() {
33
			$state = $this->get_migration_state();
34
35
			if ( 0 === $state['step'] ) {
36
				//first we need to identify what needs to be migrated.
37
38
				//how many galleries are using the legacy videoslider template?
39
				$gallery_posts = get_posts( array(
40
					'fields' => 'ids',
41
					'post_type'     => FOOGALLERY_CPT_GALLERY,
42
					'post_status'	=> array( 'publish', 'draft' ),
43
					'cache_results' => false,
44
					'nopaging'      => true
45
				) );
46
47
				//how many videos were imported into the media library with the legacy importer?
48
				$attachment_posts = get_posts( array(
49
					'fields' => 'ids',
50
					'post_type'     => 'attachment',
51
					'cache_results' => false,
52
					'nopaging'      => true,
53
					'meta_query' => array(
54
						array(
55
							'key' => FOO_VIDEO_POST_META,
56
							'compare' => 'EXISTS',
57
						)
58
					)
59
				) );
60
61
				$state['step'] = 1;
62
				$state['button_text'] =  __( 'Next', 'foogallery' );
63
				$state['message'] = sprintf( __('We found %d galleries and %d videos that need to be migrated. Click Next to migrate the galleries first.', 'foogallery' ), count( $gallery_posts ), count( $attachment_posts ) );
64
				$state['gallery_data'] = $gallery_posts;
65
				$state['attachment_data'] = $attachment_posts;
66
67
			} else if ( 1 === $state['step'] ) {
68
				//migrate the galleries
69
70
				$count = 0;
71
				foreach ($state['gallery_data'] as $gallery_id) {
72
					$this->migrate_gallery( $gallery_id );
73
					$count++;
74
				}
75
76
				$state['step'] = 2;
77
				$state['message'] = sprintf( __('%d galleries were successfully migrated. Click Next to migrate the video attachments.', 'foogallery' ), $count );
78
			} else if ( 2 === $state['step'] ) {
79
				//migrate the attachments
80
81
				$count = 0;
82
				foreach ($state['attachment_data'] as $attachment_id) {
83
					$this->migrate_attachment( $attachment_id );
84
					$count++;
85
				}
86
87
				$state['step'] = 3;
88
				$state['button_text'] =  __( 'Deactivate FooVideo', 'foogallery' );
89
				$state['message'] = sprintf( __('%d video attachments were successfully migrated. You can now safely deactivate the FooVideo extension.', 'foogallery' ), $count );
90
			} else if ( 3 === $state['step'] ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
91
				//DEACTIVATE FOOVIDEO!
92
			}
93
94
			$this->save_migration_state( $state );
95
96
			return $state;
97
		}
98
99
		public function reset_state() {
100
			delete_option( 'foogallery-video-migration' );
101
			return $this->get_migration_state();
102
		}
103
104
		/**
105
		 * Migrate a gallery from the old video slider to the new slider
106
		 *
107
		 * @param $gallery_id
108
		 */
109
		public function migrate_gallery( $gallery_id ) {
110
			$gallery = FooGallery::get_by_id( $gallery_id );
111
112
			//get the old settings, so we can migrate to the new
113
			$settings = $gallery->settings;
114
115
			if ( 'videoslider' === $gallery->gallery_template ) {
116
117
				//update the gallery template
118
				update_post_meta( $gallery_id, FOOGALLERY_META_TEMPLATE, 'slider' );
119
120
				//update the layout setting
121
				$this->migrate_setting( $settings, 'videoslider_layout', array(
122
					'rvs-vertical' => '',
123
					'rvs-horizontal' => 'fgs-horizontal'
124
				), '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...
125
126
				//update the viewport setting
127
				$this->migrate_setting( $settings, 'videoslider_viewport', array(
128
					'' => '',
129
					'rvs-use-viewport' => 'yes'
130
				), '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...
131
132
				//update the theme setting
133
				$this->migrate_setting( $settings, 'videoslider_theme', array(
134
					'' => 'fg-dark',
135
					'rvs-light' => 'fg-light',
136
					'rvs-custom' => 'fg-custom'
137
				), '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...
138
139
				//update the highlight setting
140
				$this->migrate_setting( $settings, 'videoslider_highlight', array(
141
					'' => 'fgs-purple',
142
					'rvs-blue-highlight' => 'fgs-blue',
143
					'rvs-green-highlight' => 'fgs-green',
144
					'rvs-orange-highlight' => 'fgs-orange',
145
					'rvs-red-highlight' => 'fgs-red',
146
					'rvs-custom-highlight' => 'fgs-custom'
147
				), '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...
148
			}
149
150
			//we need to migrate the foovideo settings that are saved on all galleries
151
			$this->migrate_setting( $settings, $gallery->gallery_template . '_foovideo_video_overlay', array(
152
				'video-icon-default' => 'fg-video-default',
153
				'video-icon-1' => 'fg-video-1',
154
				'video-icon-2' => 'fg-video-2',
155
				'video-icon-3' => 'fg-video-3',
156
				'video-icon-4' => 'fg-video-4'
157
			), $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...
158
159
			$this->migrate_setting( $settings, $gallery->gallery_template . '_foovideo_sticky_icon', array(
160
				'video-icon-sticky' => 'fg-video-sticky',
161
				'' => ''
162
			), $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...
163
164
			$this->migrate_setting( $settings, $gallery->gallery_template . '_foovideo_video_size', array(
165
				'640x360' => '640x360',
166
				'854x480' => '854x480',
167
				'960x540' => '960x540',
168
				'1024x576' => '1024x576',
169
				'1280x720' => '1280x720',
170
				'1366x768' => '1366x768',
171
				'1600x900' => '1600x900',
172
				'1920x1080' => '1920x1080',
173
			), $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...
174
175
			$this->migrate_setting( $settings, $gallery->gallery_template . '_foovideo_autoplay', array(
176
				'yes' => 'yes',
177
				'no' => 'no'
178
			), $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...
179
180
			//update the gallery settings
181
			update_post_meta( $gallery_id, FOOGALLERY_META_SETTINGS, $settings );
182
		}
183
184
		/**
185
		 * Migrate settings and the choice mappings
186
		 *
187
		 * @param array $settings
188
		 * @param string $setting_name
189
		 * @param array $mappings
190
		 */
191
		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...
192
			$old_setting_name = $setting_name;
193
			foreach ( $settings as $name => $value) {
194
				if ( $old_setting_name === $name ) {
195
					foreach( $mappings as $mapping_key => $mapping_value ) {
196
						if ( $mapping_key === $value ) {
197
							if ( false === $override_setting_name ) {
198
								$override_setting_name = $setting_name;
199
							}
200
							$settings[$override_setting_name] = $mapping_value;
201
							return;
202
						}
203
					}
204
				}
205
			}
206
		}
207
208
		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...
209
			$video_info = get_post_meta( $attachment_id, FOO_VIDEO_POST_META, true );
210
			$type = $video_info['type'];
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
211
212
			//need to update the post mime type
213
			$update_attachment = array(
214
				'ID' => $attachment_id,
215
				'post_mime_type' => 'image/foogallery'
216
			);
217
218
			wp_update_post( $update_attachment );
219
220
			update_post_meta( $attachment_id, FOOGALLERY_VIDEO_POST_META, $video_info );
221
		}
222
223
	}
224
}