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.

FooGallery_NextGen_Helper   F
last analyzed

Complexity

Total Complexity 91

Size/Duplication

Total Lines 572
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 572
rs 2
c 0
b 0
f 0
wmc 91
lcom 1
cbo 5

24 Methods

Rating   Name   Duplication   Size   Complexity  
A is_nextgen_installed() 0 3 2
A get_galleries() 0 10 1
A get_albums() 0 5 1
A get_album() 0 6 1
A get_gallery() 0 10 1
A get_gallery_images() 0 6 1
A get_import_progress() 0 10 4
A set_import_progress() 0 5 1
A init_import_progress() 0 5 1
A start_import() 0 4 1
A cancel_import() 0 4 1
A reset_import() 0 5 1
A import_in_progress() 0 3 1
A continue_import() 0 34 5
A get_overall_progress() 0 15 4
A get_next_gallery_to_import() 0 11 3
A ignore_previously_imported_galleries() 0 9 3
A import_picture() 0 37 2
F render_import_form() 0 156 28
F render_album_import_form() 0 135 19
A get_import_progress_for_album() 0 10 4
A import_album() 0 12 1
A reset_album_import() 0 3 1
A nextgen_unserialize() 0 22 4

How to fix   Complexity   

Complex Class

Complex classes like FooGallery_NextGen_Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FooGallery_NextGen_Helper, and based on these observations, apply Extract Interface, too.

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