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 ( e1c9dd...dff668 )
by Brad
02:14
created

FooGallery_Admin_Gallery_MetaBoxes   C

Complexity

Total Complexity 73

Size/Duplication

Total Lines 607
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 607
rs 5.5105
c 0
b 0
f 0
wmc 73
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A whitelist_metaboxes() 0 22 1
B add_meta_boxes_to_gallery() 0 76 2
A get_gallery() 0 10 2
C save_gallery() 0 47 9
C attach_gallery_to_post() 0 30 8
B render_gallery_media_metabox() 0 37 3
B render_gallery_item() 0 29 4
F render_gallery_settings_metabox() 0 103 18
B render_gallery_shortcode_metabox() 0 30 1
B render_gallery_usage_metabox() 0 29 4
A render_sorting_metabox() 0 18 3
B render_retina_metabox() 0 24 4
A render_thumb_settings_metabox() 0 18 1
B include_required_scripts() 0 28 6
A render_customcss_metabox() 0 19 1
A ajax_create_gallery_page() 0 18 2
B ajax_clear_gallery_thumb_cache() 0 25 3

How to fix   Complexity   

Complex Class

Complex classes like FooGallery_Admin_Gallery_MetaBoxes 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_Admin_Gallery_MetaBoxes, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * FooGallery Admin Gallery MetaBoxes class
5
 */
6
7
if ( ! class_exists( 'FooGallery_Admin_Gallery_MetaBoxes' ) ) {
8
9
	class FooGallery_Admin_Gallery_MetaBoxes {
10
11
		private $_gallery;
12
13
		public function __construct() {
14
			//add our foogallery metaboxes
15
			add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes_to_gallery' ) );
16
17
			//save extra post data for a gallery
18
			add_action( 'save_post', array( $this, 'save_gallery' ) );
19
20
			//save custom field on a page or post
21
			add_Action( 'save_post', array( $this, 'attach_gallery_to_post' ), 10, 2 );
22
23
			//whitelist metaboxes for our gallery postype
24
			add_filter( 'foogallery_metabox_sanity', array( $this, 'whitelist_metaboxes' ) );
25
26
			//add scripts used by metaboxes
27
			add_action( 'admin_enqueue_scripts', array( $this, 'include_required_scripts' ) );
28
29
			// Ajax calls for creating a page for the gallery
30
			add_action( 'wp_ajax_foogallery_create_gallery_page', array( $this, 'ajax_create_gallery_page' ) );
31
32
			// Ajax call for clearing thumb cache for the gallery
33
			add_action( 'wp_ajax_foogallery_clear_gallery_thumb_cache', array( $this, 'ajax_clear_gallery_thumb_cache' ) );
34
		}
35
36
		public function whitelist_metaboxes() {
37
			return array(
38
				FOOGALLERY_CPT_GALLERY => array(
39
					'whitelist'  => apply_filters( 'foogallery_metabox_sanity_foogallery',
40
						array(
41
							'submitdiv',
42
							'slugdiv',
43
							'postimagediv',
44
							'foogallery_items',
45
							'foogallery_settings',
46
							'foogallery_help',
47
							'foogallery_pages',
48
							'foogallery_customcss',
49
							'foogallery_sorting',
50
							'foogallery_thumb_settings',
51
							'foogallery_retina'
52
						) ),
53
					'contexts'   => array( 'normal', 'advanced', 'side', ),
54
					'priorities' => array( 'high', 'core', 'default', 'low', ),
55
				)
56
			);
57
		}
58
59
		public function add_meta_boxes_to_gallery() {
60
			global $post;
61
62
			add_meta_box(
63
				'foogallery_items',
64
				__( 'Gallery Items', 'foogallery' ),
65
				array( $this, 'render_gallery_media_metabox' ),
66
				FOOGALLERY_CPT_GALLERY,
67
				'normal',
68
				'high'
69
			);
70
71
			add_meta_box(
72
				'foogallery_settings',
73
				__( 'Gallery Settings', 'foogallery' ),
74
				array( $this, 'render_gallery_settings_metabox' ),
75
				FOOGALLERY_CPT_GALLERY,
76
				'normal',
77
				'high'
78
			);
79
80
			add_meta_box(
81
				'foogallery_help',
82
				__( 'Gallery Shortcode', 'foogallery' ),
83
				array( $this, 'render_gallery_shortcode_metabox' ),
84
				FOOGALLERY_CPT_GALLERY,
85
				'side',
86
				'default'
87
			);
88
89
			if ( 'publish' == $post->post_status ) {
90
				add_meta_box( 'foogallery_pages',
91
					__( 'Gallery Usage', 'foogallery' ),
92
					array( $this, 'render_gallery_usage_metabox' ),
93
					FOOGALLERY_CPT_GALLERY,
94
					'side',
95
					'default'
96
				);
97
			}
98
99
			add_meta_box(
100
				'foogallery_customcss',
101
				__( 'Custom CSS', 'foogallery' ),
102
				array( $this, 'render_customcss_metabox' ),
103
				FOOGALLERY_CPT_GALLERY,
104
				'normal',
105
				'low'
106
			);
107
108
			add_meta_box(
109
				'foogallery_retina',
110
				__( 'Retina Support', 'foogallery' ),
111
				array( $this, 'render_retina_metabox' ),
112
				FOOGALLERY_CPT_GALLERY,
113
				'side',
114
				'default'
115
			);
116
117
			add_meta_box(
118
				'foogallery_sorting',
119
				__( 'Gallery Sorting', 'foogallery' ),
120
				array( $this, 'render_sorting_metabox' ),
121
				FOOGALLERY_CPT_GALLERY,
122
				'side',
123
				'default'
124
			);
125
126
			add_meta_box(
127
				'foogallery_thumb_settings',
128
				__( 'Thumbnails', 'foogallery' ),
129
				array( $this, 'render_thumb_settings_metabox' ),
130
				FOOGALLERY_CPT_GALLERY,
131
				'side',
132
				'default'
133
			);
134
		}
135
136
		public function get_gallery( $post ) {
137
			if ( ! isset($this->_gallery) ) {
138
				$this->_gallery = FooGallery::get( $post );
139
140
				//attempt to load default gallery settings from another gallery, as per FooGallery settings page
141
				$this->_gallery->load_default_settings_if_new();
142
			}
143
144
			return $this->_gallery;
145
		}
146
147
		public function save_gallery( $post_id ) {
148
			// check autosave
149
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
150
				return $post_id;
151
			}
152
153
			// verify nonce
154
			if ( array_key_exists( FOOGALLERY_CPT_GALLERY . '_nonce', $_POST ) &&
155
				wp_verify_nonce( $_POST[FOOGALLERY_CPT_GALLERY . '_nonce'], plugin_basename( FOOGALLERY_FILE ) )
156
			) {
157
				//if we get here, we are dealing with the Gallery custom post type
158
				do_action( 'foogallery_before_save_gallery', $post_id, $_POST );
159
160
				$attachments = apply_filters( 'foogallery_save_gallery_attachments', explode( ',', $_POST[FOOGALLERY_META_ATTACHMENTS] ) );
161
				update_post_meta( $post_id, FOOGALLERY_META_ATTACHMENTS, $attachments );
162
163
				$settings = isset($_POST[FOOGALLERY_META_SETTINGS]) ?
164
					$_POST[FOOGALLERY_META_SETTINGS] : array();
165
166
				$settings = apply_filters( 'foogallery_save_gallery_settings', $settings );
167
168
				update_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, $_POST[FOOGALLERY_META_TEMPLATE] );
169
170
				update_post_meta( $post_id, FOOGALLERY_META_SETTINGS, $settings );
171
172
				update_post_meta( $post_id, FOOGALLERY_META_SORT, $_POST[FOOGALLERY_META_SORT] );
173
174
				$custom_css = isset($_POST[FOOGALLERY_META_CUSTOM_CSS]) ?
175
					$_POST[FOOGALLERY_META_CUSTOM_CSS] : '';
176
177
				if ( empty( $custom_css ) ) {
178
					delete_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS );
179
				} else {
180
					update_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, $custom_css );
181
				}
182
183
				update_post_meta( $post_id, FOOGALLERY_META_RETINA, $_POST[FOOGALLERY_META_RETINA] );
184
185
				if ( isset( $_POST[FOOGALLERY_META_FORCE_ORIGINAL_THUMBS] ) ) {
186
					update_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, $_POST[FOOGALLERY_META_FORCE_ORIGINAL_THUMBS] );
187
				} else {
188
					delete_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS );
189
				}
190
191
				do_action( 'foogallery_after_save_gallery', $post_id, $_POST );
192
			}
193
		}
194
195
		public function attach_gallery_to_post( $post_id, $post ) {
196
197
			// check autosave
198
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
199
				return $post_id;
200
			}
201
202
			//only do this check for a page or post
203
			if ( 'post' == $post->post_type ||
204
				'page' == $post->post_type ) {
205
206
                do_action( 'foogallery_start_attach_gallery_to_post', $post_id );
207
208
				//Clear any foogallery usages that the post might have
209
				delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE );
210
211
				//get all foogallery shortcodes that are on the page/post
212
				$gallery_shortcodes = foogallery_extract_gallery_shortcodes( $post->post_content );
213
214
                if ( is_array( $gallery_shortcodes ) && count( $gallery_shortcodes ) > 0 ) {
215
216
                    foreach ( $gallery_shortcodes as $id => $shortcode ) {
217
                        //if the content contains the foogallery shortcode then add a custom field
218
                        add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, $id, false );
219
220
                        do_action( 'foogallery_attach_gallery_to_post', $post_id, $id );
221
                    }
222
                }
223
			}
224
		}
225
226
		public function render_gallery_media_metabox( $post ) {
227
			$gallery = $this->get_gallery( $post );
228
229
			wp_enqueue_media();
230
231
			?>
232
			<input type="hidden" name="<?php echo FOOGALLERY_CPT_GALLERY; ?>_nonce"
233
				   id="<?php echo FOOGALLERY_CPT_GALLERY; ?>_nonce"
234
				   value="<?php echo wp_create_nonce( plugin_basename( FOOGALLERY_FILE ) ); ?>"/>
235
			<input type="hidden" name='foogallery_attachments' id="foogallery_attachments"
236
				   value="<?php echo $gallery->attachment_id_csv(); ?>"/>
237
			<div>
238
				<ul class="foogallery-attachments-list">
239
				<?php
240
				if ( $gallery->has_attachments() ) {
241
					foreach ( $gallery->attachments() as $attachment ) {
242
						$this->render_gallery_item( $attachment );
243
					}
244
				} ?>
245
					<li class="add-attachment">
246
						<a href="#" data-uploader-title="<?php _e( 'Add Media To Gallery', 'foogallery' ); ?>"
247
						   data-uploader-button-text="<?php _e( 'Add Media', 'foogallery' ); ?>"
248
						   data-post-id="<?php echo $post->ID; ?>" class="upload_image_button"
249
						   title="<?php _e( 'Add Media To Gallery', 'foogallery' ); ?>">
250
							<div class="dashicons dashicons-format-gallery"></div>
251
							<span><?php _e( 'Add Media', 'foogallery' ); ?></span>
252
						</a>
253
					</li>
254
				</ul>
255
				<div style="clear: both;"></div>
256
			</div>
257
			<textarea style="display: none" id="foogallery-attachment-template">
258
				<?php $this->render_gallery_item(); ?>
259
			</textarea>
260
		<?php
261
262
		}
263
264
		public function render_gallery_item( $attachment_post = false ) {
265
			if ( $attachment_post != false ) {
266
				$attachment_id = $attachment_post->ID;
267
				$attachment = wp_get_attachment_image_src( $attachment_id );
268
			} else {
269
				$attachment_id = '';
270
				$attachment = '';
271
			}
272
			$data_attribute = empty($attachment_id) ? '' : "data-attachment-id=\"{$attachment_id}\"";
273
			$img_tag        = empty($attachment) ? '<img width="150" height="150" />' : "<img width=\"150\" height=\"150\" src=\"{$attachment[0]}\" />";
274
			?>
275
			<li class="attachment details" <?php echo $data_attribute; ?>>
276
				<div class="attachment-preview type-image">
277
					<div class="thumbnail">
278
						<div class="centered">
279
							<?php echo $img_tag; ?>
280
						</div>
281
					</div>
282
					<a class="info" href="#" title="<?php _e( 'Edit Info', 'foogallery' ); ?>">
283
						<span class="dashicons dashicons-info"></span>
284
					</a>
285
					<a class="remove" href="#" title="<?php _e( 'Remove from gallery', 'foogallery' ); ?>">
286
						<span class="dashicons dashicons-dismiss"></span>
287
					</a>
288
				</div>
289
				<!--				<input type="text" value="" class="describe" data-setting="caption" placeholder="Caption this image…" />-->
290
			</li>
291
		<?php
292
		}
293
294
		public function render_gallery_settings_metabox( $post ) {
295
			//gallery settings including:
296
			//gallery images link to image or attachment page
297
			//default template to use
298
			$gallery             = $this->get_gallery( $post );
299
			$available_templates = foogallery_gallery_templates();
300
301
			//check if we have no templates
302
			if ( 0 === count( $available_templates ) ) {
303
				//force the default template to activate if there are no other gallery templates
304
				foogallery_activate_default_templates_extension();
305
				$available_templates = foogallery_gallery_templates();
306
			}
307
308
			$gallery_template = foogallery_default_gallery_template();
309
			if ( ! empty($gallery->gallery_template) ) {
310
				$gallery_template = $gallery->gallery_template;
311
			}
312
			$hide_help = 'on' == foogallery_get_setting( 'hide_gallery_template_help' );
313
			?>
314
			<table class="foogallery-metabox-settings">
315
				<tbody>
316
				<tr class="gallery_template_field gallery_template_field_selector">
317
					<th>
318
						<label for="FooGallerySettings_GalleryTemplate"><?php _e( 'Gallery Template', 'foogallery' ); ?></label>
319
					</th>
320
					<td>
321
						<select id="FooGallerySettings_GalleryTemplate" name="<?php echo FOOGALLERY_META_TEMPLATE; ?>">
322
							<?php
323
							foreach ( $available_templates as $template ) {
324
								$selected = ($gallery_template === $template['slug']) ? 'selected' : '';
325
326
								$preview_css = '';
327
								if ( isset( $template['preview_css'] ) ) {
328
									if ( is_array( $template['preview_css'] ) ) {
329
										//dealing with an array of css files to include
330
										$preview_css = implode( ',', $template['preview_css'] );
331
									} else {
332
										$preview_css = $template['preview_css'];
333
									}
334
								}
335
								$preview_css = empty( $preview_css ) ? '' : ' data-preview-css="' . $preview_css . '" ';
336
337
								echo "<option {$selected}{$preview_css} value=\"{$template['slug']}\">{$template['name']}</option>";
338
							}
339
							?>
340
						</select>
341
						<br />
342
						<small><?php _e( 'The gallery template that will be used when the gallery is output to the frontend.', 'foogallery' ); ?></small>
343
					</td>
344
				</tr>
345
				<?php
346
				foreach ( $available_templates as $template ) {
347
					$field_visibility = ($gallery_template !== $template['slug']) ? 'style="display:none"' : '';
348
349
					//allow for extensions to override fields for every gallery template.
350
					// Also passes the $template along so you can inspect and conditionally alter fields based on the template properties
351
					$fields = apply_filters( 'foogallery_override_gallery_template_fields', $template['fields'], $template );
352
353
					$section = '';
354
					foreach ( $fields as $field ) {
355
356
						//allow for the field to be altered by extensions. Also used by the build-in fields, e.g. lightbox
357
						$field = apply_filters( 'foogallery_alter_gallery_template_field', $field, $gallery );
358
359
						$class = "gallery_template_field gallery_template_field-{$template['slug']} gallery_template_field-{$template['slug']}-{$field['id']}";
360
361
						if ( isset($field['section']) && $field['section'] !== $section ) {
362
							$section = $field['section'];
363
							?>
364
							<tr class="<?php echo $class; ?>" <?php echo $field_visibility; ?>>
365
								<td colspan="2"><h4><?php echo $section; ?></h4></td>
366
							</tr>
367
						<?php }
368
						if (isset($field['type']) && 'help' == $field['type'] && $hide_help) {
369
							continue; //skip help if the 'hide help' setting is turned on
370
						}
371
						?>
372
						<tr class="<?php echo $class; ?>" <?php echo $field_visibility; ?>>
373
							<?php if ( isset($field['type']) && 'help' == $field['type'] ) { ?>
374
							<td colspan="2">
375
								<div class="foogallery-help">
376
									<?php echo $field['desc']; ?>
377
								</div>
378
							</td>
379
							<?php } else { ?>
380
							<th>
381
								<label
382
									for="FooGallerySettings_<?php echo $template['slug'] . '_' . $field['id']; ?>"><?php echo $field['title']; ?></label>
383
							</th>
384
							<td>
385
								<?php do_action( 'foogallery_render_gallery_template_field', $field, $gallery, $template ); ?>
386
							</td>
387
							<?php } ?>
388
						</tr>
389
					<?php
390
					}
391
				}
392
				?>
393
				</tbody>
394
			</table>
395
		<?php
396
		}
397
398
		public function render_gallery_shortcode_metabox( $post ) {
399
			$gallery = $this->get_gallery( $post );
400
			$shortcode = $gallery->shortcode();
401
			?>
402
			<p class="foogallery-shortcode">
403
				<input type="text" id="foogallery-copy-shortcode" size="<?php echo strlen( $shortcode ); ?>" value="<?php echo htmlspecialchars( $shortcode ); ?>" readonly="readonly" />
404
			</p>
405
			<p>
406
				<?php _e( 'Paste the above shortcode into a post or page to show the gallery.', 'foogallery' ); ?>
407
			</p>
408
			<script>
409
				jQuery(function($) {
410
					var shortcodeInput = document.querySelector('#foogallery-copy-shortcode');
411
					shortcodeInput.addEventListener('click', function () {
412
						try {
413
							// select the contents
414
							shortcodeInput.select();
415
							//copy the selection
416
							document.execCommand('copy');
417
							//show the copied message
418
							$('.foogallery-shortcode-message').remove();
419
							$(shortcodeInput).after('<p class="foogallery-shortcode-message"><?php _e( 'Shortcode copied to clipboard :)','foogallery' ); ?></p>');
420
						} catch(err) {
421
							console.log('Oops, unable to copy!');
422
						}
423
					}, false);
424
				});
425
			</script>
426
			<?php
427
		}
428
429
		public function render_gallery_usage_metabox( $post ) {
430
			$gallery = $this->get_gallery( $post );
431
			$posts = $gallery->find_usages();
432
			if ( $posts && count( $posts ) > 0 ) { ?>
433
				<p>
434
					<?php _e( 'This gallery is used on the following posts or pages:', 'foogallery' ); ?>
435
				</p>
436
				<ul class="ul-disc">
437
				<?php foreach ( $posts as $post ) {
438
					$url = get_permalink( $post->ID );
439
					echo '<li>' . $post->post_title . '&nbsp;';
440
					edit_post_link( __( 'Edit', 'foogallery' ), '<span class="edit">', ' | </span>', $post->ID );
441
					echo '<span class="view"><a href="' . esc_url( $url ) . '" target="_blank">' . __( 'View', 'foogallery' ) . '</a></li>';
442
				} ?>
443
				</ul>
444
			<?php } else { ?>
445
				<p>
446
					<?php _e( 'This gallery is not used on any pages or pages yet. Quickly create a page:', 'foogallery' ); ?>
447
				</p>
448
				<div class="foogallery_metabox_actions">
449
					<button class="button button-primary button-large" id="foogallery_create_page"><?php _e( 'Create Gallery Page', 'foogallery' ); ?></button>
450
					<span id="foogallery_create_page_spinner" class="spinner"></span>
451
					<?php wp_nonce_field( 'foogallery_create_gallery_page', 'foogallery_create_gallery_page_nonce', false ); ?>
452
				</div>
453
				<p>
454
					<?php _e( 'A draft page will be created which includes the gallery shortcode in the content. The title of the page will be the same title as the gallery.', 'foogallery' ); ?>
455
				</p>
456
			<?php }
457
		}
458
459
		public function render_sorting_metabox( $post ) {
460
			$gallery = $this->get_gallery( $post );
461
			$sorting_options = foogallery_sorting_options();
462
			if ( empty( $gallery->sorting ) ) {
463
				$gallery->sorting = '';
464
			}
465
			?>
466
			<p>
467
				<?php _e('Change the way images are sorted within your gallery. By default, they are sorted in the order you see them.', 'foogallery'); ?>
468
			</p>
469
			<?php
470
			foreach ( $sorting_options as $sorting_key => $sorting_label ) { ?>
471
				<p>
472
				<input type="radio" value="<?php echo $sorting_key; ?>" <?php checked( $sorting_key === $gallery->sorting ); ?> id="FooGallerySettings_GallerySort_<?php echo $sorting_key; ?>" name="<?php echo FOOGALLERY_META_SORT; ?>" />
473
				<label for="FooGallerySettings_GallerySort_<?php echo $sorting_key; ?>"><?php echo $sorting_label; ?></label>
474
				</p><?php
475
			}
476
		}
477
478
		public function render_retina_metabox( $post ) {
479
			$gallery = $this->get_gallery( $post );
480
			$retina_options = foogallery_retina_options();
481
			if ( empty( $gallery->retina ) ) {
482
				$gallery->retina = foogallery_get_setting( 'default_retina_support', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, 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...
483
			}
484
			?>
485
			<p>
486
				<?php _e('Add retina support to this gallery by choosing the different pixel densities you want to enable.', 'foogallery'); ?>
487
			</p>
488
			<?php
489
			foreach ( $retina_options as $retina_key => $retina_label ) {
490
				$checked = array_key_exists( $retina_key, $gallery->retina ) ? ('true' === $gallery->retina[$retina_key]) : false;
491
				?>
492
				<p>
493
				<input type="checkbox" value="true" <?php checked( $checked ); ?> id="FooGallerySettings_Retina_<?php echo $retina_key; ?>" name="<?php echo FOOGALLERY_META_RETINA; ?>[<?php echo $retina_key; ?>]" />
494
				<label for="FooGallerySettings_Retina_<?php echo $retina_key; ?>"><?php echo $retina_label; ?></label>
495
				</p><?php
496
			} ?>
497
			<p class="foogallery-help">
498
				<?php _e('PLEASE NOTE : thumbnails will be generated for each of the pixel densities chosen, which will increase your website\'s storage space!', 'foogallery'); ?>
499
			</p>
500
			<?php
501
		}
502
503
		public function render_thumb_settings_metabox( $post ) {
504
			$gallery = $this->get_gallery( $post );
0 ignored issues
show
Unused Code introduced by
$gallery 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...
505
			$force_use_original_thumbs = get_post_meta( $post->ID, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
506
			$checked = 'true' === $force_use_original_thumbs; ?>
507
			<p>
508
				<?php _e( 'Clear all the previously cached thumbnails that have been generated for this gallery.', 'foogallery' ); ?>
509
			</p>
510
			<div class="foogallery_metabox_actions">
511
				<button class="button button-primary button-large" id="foogallery_clear_thumb_cache"><?php _e( 'Clear Thumbnail Cache', 'foogallery' ); ?></button>
512
				<span id="foogallery_clear_thumb_cache_spinner" class="spinner"></span>
513
				<?php wp_nonce_field( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce', false ); ?>
514
			</div>
515
			<p>
516
				<input type="checkbox" value="true" <?php checked( $checked ); ?> id="FooGallerySettings_ForceOriginalThumbs" name="<?php echo FOOGALLERY_META_FORCE_ORIGINAL_THUMBS; ?>" />
517
				<label for="FooGallerySettings_ForceOriginalThumbs"><?php _e('Force Original Thumbs', 'foogallery'); ?></label>
518
			</p>
519
			<?php
520
		}
521
522
		public function include_required_scripts() {
523
			$screen_id = foo_current_screen_id();
524
525
			//only include scripts if we on the foogallery add/edit page
526
			if ( FOOGALLERY_CPT_GALLERY === $screen_id ||
527
			     'edit-' . FOOGALLERY_CPT_GALLERY === $screen_id ) {
528
529
				//spectrum needed for the colorpicker field
530
				$url = FOOGALLERY_URL . 'lib/spectrum/spectrum.js';
531
				wp_enqueue_script( 'foogallery-spectrum', $url, array('jquery'), FOOGALLERY_VERSION );
532
				$url = FOOGALLERY_URL . 'lib/spectrum/spectrum.css';
533
				wp_enqueue_style( 'foogallery-spectrum', $url, array(), FOOGALLERY_VERSION );
534
535
				//include any admin js required for the templates
536
				foreach ( foogallery_gallery_templates() as $template ) {
537
					$admin_js = foo_safe_get( $template, 'admin_js' );
538
					if ( is_array( $admin_js ) ) {
539
						//dealing with an array of js files to include
540
						foreach( $admin_js as $admin_js_key => $admin_js_src ) {
541
							wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'] . '-' . $admin_js_key, $admin_js_src, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION );
542
						}
543
					} else {
544
						//dealing with a single js file to include
545
						wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'], $admin_js, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION );
546
					}
547
				}
548
			}
549
		}
550
551
		public function render_customcss_metabox( $post ) {
552
			$gallery = $this->get_gallery( $post );
553
			$custom_css = $gallery->custom_css;
554
			$example = '<code>#foogallery-gallery-' . $post->ID . ' { }</code>';
555
			?>
556
			<p>
557
				<?php printf( __( 'Add any custom CSS to target this specific gallery. For example %s', 'foogallery' ), $example ); ?>
558
			</p>
559
			<table id="table_styling" class="form-table">
560
				<tbody>
561
				<tr>
562
					<td>
563
						<textarea class="foogallery_metabox_custom_css" name="<?php echo FOOGALLERY_META_CUSTOM_CSS; ?>" type="text"><?php echo $custom_css; ?></textarea>
564
					</td>
565
				</tr>
566
				</tbody>
567
			</table>
568
		<?php
569
		}
570
571
		public function ajax_create_gallery_page() {
572
			if ( check_admin_referer( 'foogallery_create_gallery_page', 'foogallery_create_gallery_page_nonce' ) ) {
573
574
				$foogallery_id = $_POST['foogallery_id'];
575
576
				$foogallery = FooGallery::get_by_id( $foogallery_id );
577
578
				$post = array(
579
					'post_content' => $foogallery->shortcode(),
580
					'post_title'   => $foogallery->name,
581
					'post_status'  => 'draft',
582
					'post_type'    => 'page',
583
				);
584
585
				wp_insert_post( $post );
586
			}
587
			die();
588
		}
589
590
		public function ajax_clear_gallery_thumb_cache() {
591
			if ( check_admin_referer( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce' ) ) {
592
593
				$foogallery_id = $_POST['foogallery_id'];
594
595
				$foogallery = FooGallery::get_by_id( $foogallery_id );
596
597
				ob_start();
598
599
				//loop through all images, get the full sized file
600
				foreach ( $foogallery->attachments() as $attachment ) {
601
					$meta_data = wp_get_attachment_metadata( $attachment->ID );
602
603
					$file = $meta_data['file'];
604
605
					wpthumb_delete_cache_for_file( $file );
606
				}
607
608
				ob_end_clean();
609
610
				echo __( 'The thumbnail cache has been cleared!', 'foogallery' );
611
			}
612
613
			die();
614
		}
615
	}
616
}
617