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 — develop ( fc9531...c44ae7 )
by Brad
02:36
created

FooGallery_NextGen_Helper::continue_import()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 8.439
cc 5
eloc 16
nc 9
nop 0
1
<?php
2
3
if ( ! class_exists( 'FooGallery_NextGen_Helper' ) ) {
4
5
	class FooGallery_NextGen_Helper {
6
7
		const NEXTGEN_TABLE_GALLERY          = 'ngg_gallery';
8
		const NEXTGEN_TABLE_PICTURES         = 'ngg_pictures';
9
		const NEXTGEN_TABLE_ALBUMS           = 'ngg_album';
10
11
		const NEXTGEN_OPTION_IMPORT_CURRENT  = 'foogallery_nextgen_import-current';
12
		const NEXTGEN_OPTION_IMPORT_PROGRESS = 'foogallery_nextgen_import-progress';
13
		const NEXTGEN_OPTION_IMPORT_IN_PROGRESS  = 'foogallery_nextgen_import-importing';
14
15
		const NEXTGEN_OPTION_IMPORT_CURRENT_ALBUM  = 'foogallery_nextgen_import-current-album';
16
		const NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM = 'foogallery_nextgen_import-progress-album';
17
18
		/**
19
		 * @TODO
20
		 */
21
		function is_nextgen_installed() {
22
			return class_exists( 'C_NextGEN_Bootstrap' ) || class_exists( 'nggLoader' );
23
		}
24
25
		function get_galleries() {
26
			global $wpdb;
27
			$gallery_table = $wpdb->prefix . self::NEXTGEN_TABLE_GALLERY;
28
			$picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES;
29
30
			return $wpdb->get_results( "select gid, name, title, galdesc,
31
(select count(*) from {$picture_table} where galleryid = gid) 'image_count'
32
from {$gallery_table}" );
33
		}
34
35
		function get_albums() {
36
			global $wpdb;
37
			$album_table = $wpdb->prefix . self::NEXTGEN_TABLE_ALBUMS;
38
			return $wpdb->get_results(" select * from {$album_table}");
39
		}
40
41
		function get_album( $id ) {
42
			global $wpdb;
43
			$album_table = $wpdb->prefix . self::NEXTGEN_TABLE_ALBUMS;
44
45
			return $wpdb->get_row( $wpdb->prepare( "select * from {$album_table} where id = %d", $id ) );
46
		}
47
48
		function get_gallery( $id ) {
49
			global $wpdb;
50
			$gallery_table = $wpdb->prefix . self::NEXTGEN_TABLE_GALLERY;
51
			$picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES;
52
53
			return $wpdb->get_row( $wpdb->prepare( "select gid, name, title, galdesc, path, author,
54
(select count(*) from {$picture_table} where galleryid = gid) 'image_count'
55
from {$gallery_table}
56
where gid = %d", $id ) );
57
		}
58
59
		function get_gallery_images( $id ) {
60
			global $wpdb;
61
			$picture_table = $wpdb->prefix . self::NEXTGEN_TABLE_PICTURES;
62
63
			return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$picture_table} WHERE galleryid = %d", $id ) );
64
		}
65
66
		/**
67
		 *
68
		 * @return FooGallery_NextGen_Import_Progress
69
		 */
70
		function get_import_progress( $nextgen_gallery_id ) {
71
			$progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS );
72
			if ( false !== $progress ) {
73
				if ( false !== $nextgen_gallery_id && array_key_exists( $nextgen_gallery_id, $progress ) ) {
74
					return $progress[ $nextgen_gallery_id ];
75
				}
76
			}
77
78
			return new FooGallery_NextGen_Import_Progress();
79
		}
80
81
		function set_import_progress( $nextgen_gallery_id, FooGallery_NextGen_Import_Progress $progress ) {
82
			$all_progress                        = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() );
83
			$all_progress[ $nextgen_gallery_id ] = $progress;
84
			update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, $all_progress );
85
		}
86
87
		/**
88
		 * @param int $nextgen_gallery_id
89
		 * @param string $foogallery_title
90
		 */
91
		function init_import_progress( $nextgen_gallery_id, $foogallery_title ) {
92
			$progress = new FooGallery_NextGen_Import_Progress();
93
			$progress->init( $nextgen_gallery_id, $foogallery_title );
94
			$this->set_import_progress( $nextgen_gallery_id, $progress );
95
		}
96
97
		function start_import() {
98
			delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT );
99
			update_option( self::NEXTGEN_OPTION_IMPORT_IN_PROGRESS, true );
100
		}
101
102
		function cancel_import() {
103
			delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT );
104
			delete_option( self::NEXTGEN_OPTION_IMPORT_IN_PROGRESS );
105
		}
106
107
		function reset_import() {
108
			delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT );
109
			delete_option( self::NEXTGEN_OPTION_IMPORT_IN_PROGRESS );
110
			delete_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS );
111
		}
112
113
		function import_in_progress() {
114
			return true == get_option( self::NEXTGEN_OPTION_IMPORT_IN_PROGRESS );
115
		}
116
117
		function continue_import() {
118
			//get the current gallery being imported
119
			$current_nextgen_id = get_option( self::NEXTGEN_OPTION_IMPORT_CURRENT, 0 );
120
121
			if ( 0 === $current_nextgen_id ) {
122
				//try and get the next gallery to import
123
				$current_nextgen_id = $this->get_next_gallery_to_import();
124
125
				//if we still have no current then do nothing
126
				if ( 0 === $current_nextgen_id ) {
127
					$this->cancel_import();
128
					return;
129
				} else {
130
					update_option( self::NEXTGEN_OPTION_IMPORT_CURRENT, $current_nextgen_id );
131
				}
132
			}
133
134
			$progress = $this->get_import_progress( $current_nextgen_id );
135
136
			if ( ! $progress->has_started() ) {
137
				$progress->start();
138
			}
139
140
			//import the next picture
141
			$progress->import_next_picture();
142
143
			//update our progress
144
			$this->set_import_progress( $current_nextgen_id, $progress );
145
146
			//if the percentage complete is 100 then clear the current gallery
147
			if ( $progress->is_completed() ) {
148
				delete_option( self::NEXTGEN_OPTION_IMPORT_CURRENT );
149
			}
150
		}
151
152
		function get_overall_progress() {
153
			$all_progress       = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() );
154
			$total = 0;
155
			$imported = 0;
156
			foreach ( $all_progress as $id => $progress ) {
157
				if ( $progress->is_part_of_current_import ) {
158
					$total += $progress->import_count;
159
					$imported += count( $progress->attachments );
160
				}
161
			}
162
			if ( 0 === $total ) {
163
				return 100;
164
			}
165
			return  $imported / $total * 100;
166
		}
167
168
		function get_next_gallery_to_import() {
169
			$all_progress       = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() );
170
171
			foreach ( $all_progress as $id => $progress ) {
172
				if ( $progress->can_import() ) {
173
					return $id;
174
				}
175
			}
176
177
			return 0;
178
		}
179
180
		function ignore_previously_imported_galleries() {
181
			$all_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, array() );
182
			foreach ( $all_progress as $id => $progress ) {
183
				if ( $progress->is_completed() ) {
184
					$progress->is_part_of_current_import = false;
185
				}
186
			}
187
			update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS, $all_progress );
188
		}
189
190
		function import_picture( $nextgen_gallery_path, $picture ) {
191
			$picture_url = trailingslashit( site_url() ) . trailingslashit( $nextgen_gallery_path ) . $picture->filename;
192
193
			// Get the contents of the picture
194
			$response = wp_remote_get( $picture_url );
195
			$contents = wp_remote_retrieve_body( $response );
196
197
			// Upload and get file data
198
			$upload    = wp_upload_bits( basename( $picture_url ), null, $contents );
199
			$guid      = $upload['url'];
200
			$file      = $upload['file'];
201
			$file_type = wp_check_filetype( basename( $file ), null );
202
203
			// Create attachment
204
			$attachment = array(
205
				'ID'             => 0,
206
				'guid'           => $guid,
207
				'post_title'     => $picture->alttext != '' ? $picture->alttext : $picture->image_slug,
208
				'post_excerpt'   => $picture->description,
209
				'post_content'   => $picture->description,
210
				'post_date'      => '',
211
				'post_mime_type' => $file_type['type'],
212
			);
213
214
			// Include image.php so we can call wp_generate_attachment_metadata()
215
			require_once( ABSPATH . 'wp-admin/includes/image.php' );
216
217
			// Insert the attachment
218
			$attachment_id   = wp_insert_attachment( $attachment, $file, 0 );
219
			$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file );
220
			wp_update_attachment_metadata( $attachment_id, $attachment_data );
221
222
			// Save alt text in the post meta
223
			update_post_meta( $attachment_id, '_wp_attachment_image_alt', $picture->alttext );
224
225
			return $attachment_id;
226
		}
227
228
		function render_import_form( $galleries = false ) {
229
			if ( false === $galleries ) {
230
				$galleries = $this->get_galleries();
231
			}
232
			$has_imports = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS );
233
			$overall_progress = $this->get_overall_progress();
234
			$all_imports_completed = 100 === $overall_progress;
235
			$import_has_started = $this->import_in_progress();
236
			$importing = $import_has_started && defined( 'DOING_AJAX' ) && DOING_AJAX;
237
			$current_nextgen_id = get_option( self::NEXTGEN_OPTION_IMPORT_CURRENT, 0 );
238
			?>
239
			<table class="wp-list-table widefat" cellspacing="0">
240
				<thead>
241
				<tr>
242
					<th scope="col" id="cb" class="manage-column column-cb check-column">
243
						<?php if ( ! $importing && $all_imports_completed ) { ?>
244
						<label class="screen-reader-text"
245
						       for="cb-select-all-1"><?php _e( 'Select All', 'foogallery' ); ?></label>
246
						<input id="cb-select-all-1" type="checkbox" <?php echo $importing ? 'disabled="disabled"' : ''; ?> checked="checked">
247
						<?php } ?>
248
					</th>
249
					<th scope="col" class="manage-column">
250
						<span><?php _e( 'NextGen Gallery', 'foogallery' ); ?></span>
251
					</th>
252
					<th scope="col" id="title" class="manage-column">
253
						<span><?php printf( __( '%s Name', 'foogallery' ), foogallery_plugin_name() ); ?></span>
254
					</th>
255
					<th scope="col" id="title" class="manage-column">
256
						<span><?php _e( 'Import Progress', 'foogallery' ); ?></span>
257
					</th>
258
				</tr>
259
				</thead>
260
				<tbody>
261
			<?php
262
			$counter = 0;
263
			foreach ( $galleries as $gallery ) {
264
				$counter++;
265
				$progress    = $this->get_import_progress( $gallery->gid );
266
				$done        = $progress->is_completed();
267
				$edit_link	 = '';
268
				$foogallery = false;
269
				if ( $progress->foogallery_id > 0 ) {
270
					$foogallery = FooGallery::get_by_id( $progress->foogallery_id );
271
					if ( $foogallery ) {
272
						$edit_link = '<a href="' . admin_url( 'post.php?post=' . $progress->foogallery_id . '&action=edit' ) . '">' . $foogallery->name . '</a>';
273
					} else {
274
						$done = false;
275
					}
276
				} ?>
277
				<tr class="<?php echo ($counter % 2 === 0) ? 'alternate' : ''; ?>">
278
					<?php if ( ! $importing && ! $done && $all_imports_completed ) { ?>
279
						<th scope="row" class="column-cb check-column">
280
							<input name="nextgen-id[]" type="checkbox" checked="checked" value="<?php echo $gallery->gid; ?>">
281
						</th>
282
					<?php } else if ( $importing && $gallery->gid == $current_nextgen_id ) { ?>
283
						<th>
284
							<div class="dashicons dashicons-arrow-right"></div>
285
						</th>
286
					<?php } else { ?>
287
						<th>
288
						</th>
289
					<?php } ?>
290
					<td>
291
						<?php echo $gallery->title . ' ' . sprintf( __( '(%s images)', 'foogallery' ), $gallery->image_count ); ?>
292
					</td>
293
					<td>
294
					<?php if ( $foogallery ) {
295
						echo $edit_link;
296
					} else { ?>
297
						<input name="foogallery-name-<?php echo $gallery->gid; ?>" value="<?php echo $gallery->title; ?>">
298
					<?php } ?>
299
					</td>
300
					<td class="nextgen-import-progress nextgen-import-progress-<?php echo $progress->status; ?>">
301
						<?php echo $progress->message(); ?>
302
					</td>
303
				</tr>
304
			<?php
305
			}
306
			?>
307
				</tbody>
308
			</table>
309
			<br/>
310
311
			<?php
312
			echo '<input type="hidden" id="nextgen_import_progress" value="' . $overall_progress . '" />';
313
			wp_nonce_field( 'foogallery_nextgen_import', 'foogallery_nextgen_import' );
314
			wp_nonce_field( 'foogallery_nextgen_import_refresh', 'foogallery_nextgen_import_refresh', false );
315
			wp_nonce_field( 'foogallery_nextgen_import_cancel', 'foogallery_nextgen_import_cancel', false );
316
			wp_nonce_field( 'foogallery_nextgen_import_reset', 'foogallery_nextgen_import_reset', false );
317
			if ( ! $import_has_started && ! $importing ) {
318
				?>
319
				<input type="submit" class="button button-primary start_import"
320
				       value="<?php _e( 'Start Import', 'foogallery' ); ?>">
321
			<?php } else if ( $import_has_started && ! $importing ) { ?>
322
				<input type="submit" class="button button-primary continue_import" value="<?php _e( 'Resume Import', 'foogallery' ); ?>">
323
			<?php } else { ?>
324
				<input type="submit" class="button cancel_import" value="<?php _e( 'Stop Import', 'foogallery' ); ?>">
325
			<?php
326
			}
327
			if ( $has_imports && ! $importing ) { ?>
328
				<input type="submit" name="foogallery_nextgen_reset" class="button reset_import" value="<?php _e( 'Reset All Gallery Imports', 'foogallery' ); ?>">
329
			<?php }
330
			?>
331
			<div id="import_spinner" style="width:20px">
332
				<span class="spinner"></span>
333
			</div>
334
			<?php if ( $importing ) { ?>
335
				<div class="nextgen-import-progressbar">
336
					<span style="width:<?php echo $overall_progress; ?>%"></span>
337
				</div>
338
				<?php echo intval( $overall_progress ); ?>%
339
				<div style="width:20px; display: inline-block;">
340
					<span class="spinner shown"></span>
341
				</div>
342
			<?php }
343
		}
344
345
		function render_album_import_form( $albums = false ) {
346
			if ( false === $albums ) {
347
				$albums = $this->get_albums();
348
			}
349
			$has_imports = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM );
350
			?>
351
			<table class="wp-list-table widefat" cellspacing="0">
352
				<thead>
353
				<tr>
354
					<th scope="col" class="manage-column">
355
						<span><?php _e( 'NextGen Album', 'foogallery' ); ?></span>
356
					</th>
357
					<th scope="col" class="manage-column">
358
						<span><?php _e( 'Album Name', 'foogallery' ); ?></span>
359
					</th>
360
					<th scope="col" class="manage-column">
361
						<span><?php _e( 'NextGen Galleries', 'foogallery' ); ?></span>
362
					</th>
363
					<th scope="col" class="manage-column">
364
						<span><?php _e( 'Import Options', 'foogallery' ); ?></span>
365
					</th>
366
				</tr>
367
				</thead>
368
				<tbody>
369
				<?php
370
				$counter = 0;
371
				foreach ( $albums as $album ) {
372
					$counter++;
373
					$progress    = $this->get_import_progress_for_album( $album->id );
374
					$done        = $progress->is_completed();
375
					$edit_link	 = '';
376
					$galleries   = $this->nextgen_unserialize( $album->sortorder );
377
					$foogallery_album = false;
378
					if ( $progress->foogallery_album_id > 0 ) {
379
						$foogallery_album = FooGalleryAlbum::get_by_id( $progress->foogallery_album_id );
380
						if ( $foogallery_album ) {
381
							$edit_link = '<a href="' . admin_url( 'post.php?post=' . $progress->foogallery_album_id . '&action=edit' ) . '">' . $foogallery_album->name . '</a>';
382
						} else {
383
							$done = false;
384
						}
385
					} ?>
386
					<tr class="<?php echo ($counter % 2 === 0) ? 'alternate' : ''; ?>">
387
						<td>
388
							<?php echo $album->name; ?>
389
							<input type="hidden" class="foogallery-album-id" value="<?php echo $album->id; ?>">
390
						</td>
391
						<td>
392
							<?php if ( $foogallery_album ) {
393
								echo $edit_link;
394
							} else { ?>
395
								<input class="foogallery-album-name" value="<?php echo $album->name; ?>">
396
							<?php } ?>
397
						</td>
398
						<td>
399
							<ul class="ul-disc" style="margin: 0 0 0 20px;">
400
							<?php
401
							$import_gallery_count = 0;
402
							foreach ( $galleries as $gallery_id ) {
403
								if ( 'a' === substr( $gallery_id, 0, 1 ) ) {
404
									//we are dealing with an album inside the album
405
									$nested_album = $this->get_album( substr( $gallery_id, 1 ) );
406
									if ( $nested_album ) {
407
										echo '<li>';
408
										echo __('[Album] ', 'foogallery');
409
										echo ' <span style="text-decoration:line-through">';
410
										echo $nested_album->name;
411
										echo '</span>';
412
										echo ' (<span class="nextgen-import-progress-' . FooGallery_NextGen_Import_Progress::PROGRESS_ERROR . '">';
413
										echo __( 'nested albums not supported', 'foogallery' );
414
										echo '</span>)</li>';
415
									}
416
								} else {
417
									$nextgen_gallery = $this->get_gallery( $gallery_id );
418
									echo '<li>';
419
									$gallery_progress  = $this->get_import_progress( $gallery_id );
420
									$gallery_completed = $gallery_progress->is_completed();
421
									if ( $gallery_completed ) {
422
										$import_gallery_count ++;
423
									}
424
									echo $gallery_completed ? '' : '<span style="text-decoration:line-through">';
425
									echo $nextgen_gallery->title;
426
									echo $gallery_completed ? '' : '</span>';
427
									echo ' (<span class="nextgen-import-progress-' . $gallery_progress->status . '">';
428
									echo $gallery_completed ? __( 'imported', 'foogallery' ) : __( 'not imported', 'foogallery' );
429
									echo '</span>)</li>';
430
								}
431
							}
432
							?>
433
							</ul>
434
						</td>
435
						<td>
436
							<span class="nextgen-import-progress nextgen-import-progress-<?php echo $progress->status; ?>">
437
								<?php echo $progress->message(); ?>
438
							</span>
439
							<?php
440
441
							echo '<br />';
442
							if ( !$progress->is_completed() ) {
443
								if ( $import_gallery_count > 0 ) {
444
									echo '<input type="submit" class="button button-primary start_album_import" value="Import Album">';
445
									echo '<div class="inline" style="width:20px"><span class="spinner"></span></div>';
446
									echo '<br />';
447
									if ( $import_gallery_count === count( $galleries ) ) {
448
										_e( 'All galleries will be linked', 'foogallery' );
449
									} else {
450
										echo sprintf( __( '%d/%d galleries will be linked', 'foogallery' ), $import_gallery_count, count( $galleries ) );
451
										echo '<br />';
452
										_e ( '(Only previously imported galleries can be linked)', 'foogallery' );
453
									}
454
								} else {
455
									_e( 'No galleries imported yet!!', 'foogallery' );
456
								}
457
							}
458
459
							?>
460
						</td>
461
					</tr>
462
				<?php
463
				}
464
				?>
465
				</tbody>
466
			</table>
467
			<?php
468
			wp_nonce_field( 'foogallery_nextgen_album_reset', 'foogallery_nextgen_album_reset', false );
469
			wp_nonce_field( 'foogallery_nextgen_album_import', 'foogallery_nextgen_album_import', false );
470
471
			if ( $has_imports ) { ?>
472
				<br />
473
				<input type="submit" name="foogallery_nextgen_reset_album" class="button reset_album_import" value="<?php _e( 'Reset All Album Imports', 'foogallery' ); ?>">
474
			<?php }
475
		}
476
477
		function get_import_progress_for_album( $nextgen_gallery_album_id ) {
478
			$progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM );
479
			if ( false !== $progress ) {
480
				if ( false !== $nextgen_gallery_album_id && array_key_exists( $nextgen_gallery_album_id, $progress ) ) {
481
					return $progress[ $nextgen_gallery_album_id ];
482
				}
483
			}
484
485
			return new FooGallery_NextGen_Import_Progress_Album();
486
		}
487
488
		function import_album( $nextgen_gallery_album_id, $foogallery_album_name ) {
489
			$progress = new FooGallery_NextGen_Import_Progress_Album();
490
			$progress->nextgen_album_id = $nextgen_gallery_album_id;
491
			$progress->foogallery_album_title = $foogallery_album_name;
492
493
			//create a new foogallery album
494
			$progress->import();
495
496
			$overall_progress = get_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM );
497
			$overall_progress[ $nextgen_gallery_album_id ] = $progress;
498
			update_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM, $overall_progress );
499
		}
500
501
		function reset_album_import() {
502
			delete_option( self::NEXTGEN_OPTION_IMPORT_PROGRESS_ALBUM );
503
		}
504
505
		/**
506
		 * Unserialize NextGEN data
507
		 */
508
		function nextgen_unserialize($value) {
509
			$retval = NULL;
510
511
			if ( is_string( $value ) ) {
512
				$retval = stripcslashes( $value );
513
514
				if ( strlen( $value ) > 1 ) {
515
					// We can't always rely on base64_decode() or json_decode() to return FALSE as their documentation
516
					// claims so check if $retval begins with a: as that indicates we have a serialized PHP object.
517
					if ( strpos( $retval, 'a:' ) === 0 ) {
518
						$er = error_reporting(0);
519
						$retval = unserialize($value);
520
						error_reporting($er);
521
					} else {
522
						// We use json_decode() here because PHP's unserialize() is not Unicode safe.
523
						$retval = json_decode(base64_decode($retval), TRUE);
524
					}
525
				}
526
			}
527
528
			return $retval;
529
		}
530
531
	}
532
533
}
534