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/gallery-template-clien... ( b693b9...cbf425 )
by Brad
02:40
created

render_gallery_media_metabox()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 32
nc 4
nop 1
dl 0
loc 57
rs 9.0309
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ) {
0 ignored issues
show
Coding Style introduced by
save_gallery uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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] ), $post_id, $_POST );
161
				update_post_meta( $post_id, FOOGALLERY_META_ATTACHMENTS, $attachments );
162
163
				$gallery_template = $_POST[FOOGALLERY_META_TEMPLATE];
164
				update_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, $gallery_template );
165
166
				$settings = isset($_POST[FOOGALLERY_META_SETTINGS]) ?
167
					$_POST[FOOGALLERY_META_SETTINGS] : array();
168
169
				$settings = apply_filters( 'foogallery_save_gallery_settings', $settings, $post_id, $_POST );
170
				$settings = apply_filters( 'foogallery_save_gallery_settings-'. $gallery_template, $settings, $post_id, $_POST );
171
172
				update_post_meta( $post_id, FOOGALLERY_META_SETTINGS, $settings );
173
174
				update_post_meta( $post_id, FOOGALLERY_META_SORT, $_POST[FOOGALLERY_META_SORT] );
175
176
				$custom_css = isset($_POST[FOOGALLERY_META_CUSTOM_CSS]) ?
177
					$_POST[FOOGALLERY_META_CUSTOM_CSS] : '';
178
179
				if ( empty( $custom_css ) ) {
180
					delete_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS );
181
				} else {
182
					update_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, $custom_css );
183
				}
184
185
				if ( isset( $_POST[FOOGALLERY_META_RETINA] ) ) {
186
					update_post_meta( $post_id, FOOGALLERY_META_RETINA, $_POST[FOOGALLERY_META_RETINA] );
187
				} else {
188
					delete_post_meta( $post_id, FOOGALLERY_META_RETINA );
189
				}
190
191
				if ( isset( $_POST[FOOGALLERY_META_FORCE_ORIGINAL_THUMBS] ) ) {
192
					update_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, $_POST[FOOGALLERY_META_FORCE_ORIGINAL_THUMBS] );
193
				} else {
194
					delete_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS );
195
				}
196
197
				update_post_meta( $post_id, FOOGALLERY_META_SETTINGS_VERSION, FOOGALLERY_SETTINGS_VERSION );
198
199
				do_action( 'foogallery_after_save_gallery', $post_id, $_POST );
200
			}
201
		}
202
203
		public function attach_gallery_to_post( $post_id, $post ) {
204
205
			// check autosave
206
			if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
207
				return $post_id;
208
			}
209
210
			//only do this check for a page or post
211
			if ( 'post' == $post->post_type ||
212
				'page' == $post->post_type ) {
213
214
                do_action( 'foogallery_start_attach_gallery_to_post', $post_id );
215
216
				//Clear any foogallery usages that the post might have
217
				delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE );
218
219
				//get all foogallery shortcodes that are on the page/post
220
				$gallery_shortcodes = foogallery_extract_gallery_shortcodes( $post->post_content );
221
222
                if ( is_array( $gallery_shortcodes ) && count( $gallery_shortcodes ) > 0 ) {
223
224
                    foreach ( $gallery_shortcodes as $id => $shortcode ) {
225
                        //if the content contains the foogallery shortcode then add a custom field
226
                        add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, $id, false );
227
228
                        do_action( 'foogallery_attach_gallery_to_post', $post_id, $id );
229
                    }
230
                }
231
			}
232
		}
233
234
		public function render_gallery_media_metabox( $post ) {
235
			$gallery = $this->get_gallery( $post );
236
237
			wp_enqueue_media();
238
239
			?>
240
			<div class="hidden foogallery-items-view-switch-container">
241
				<div class="foogallery-items-view-switch">
242
					<a href="#manage" data-container=".foogallery-items-view-manage" class="current1"><?php _e('Manage Items', 'foogallery'); ?></a>
243
					<a href="#preview" data-container=".foogallery-items-view-preview" class="current"><?php _e('Gallery Preview', 'foogallery'); ?></a>
244
				</div>
245
				<span id="foogallery_preview_spinner" class="spinner"></span>
246
			</div>
247
248
			<div class="foogallery-items-view foogallery-items-view-manage hidden">
249
				<input type="hidden" name="<?php echo FOOGALLERY_CPT_GALLERY; ?>_nonce"
250
					   id="<?php echo FOOGALLERY_CPT_GALLERY; ?>_nonce"
251
					   value="<?php echo wp_create_nonce( plugin_basename( FOOGALLERY_FILE ) ); ?>"/>
252
				<input type="hidden" name='foogallery_attachments' id="foogallery_attachments"
253
					   value="<?php echo $gallery->attachment_id_csv(); ?>"/>
254
				<div>
255
					<ul class="foogallery-attachments-list">
256
					<?php
257
					if ( $gallery->has_attachments() ) {
258
						foreach ( $gallery->attachments() as $attachment ) {
259
							$this->render_gallery_item( $attachment );
260
						}
261
					} ?>
262
						<li class="add-attachment">
263
							<a href="#" data-uploader-title="<?php _e( 'Add Media To Gallery', 'foogallery' ); ?>"
264
							   data-uploader-button-text="<?php _e( 'Add Media', 'foogallery' ); ?>"
265
							   data-post-id="<?php echo $post->ID; ?>" class="upload_image_button"
266
							   title="<?php _e( 'Add Media To Gallery', 'foogallery' ); ?>">
267
								<div class="dashicons dashicons-format-gallery"></div>
268
								<span><?php _e( 'Add Media', 'foogallery' ); ?></span>
269
							</a>
270
						</li>
271
					</ul>
272
					<div style="clear: both;"></div>
273
				</div>
274
				<textarea style="display: none" id="foogallery-attachment-template">
275
					<?php $this->render_gallery_item(); ?>
276
				</textarea>
277
			</div>
278
			<div class="foogallery-items-view foogallery-items-view-preview">
279
				<div class="foogallery_preview_container">
280
				<?php
281
				if ( $gallery->has_attachments() ) {
282
					foogallery_render_gallery( $gallery->ID );
283
				}
284
				?>
285
				</div>
286
				<?php wp_nonce_field( 'foogallery_preview', 'foogallery_preview', false ); ?>
287
			</div>
288
		<?php
289
290
		}
291
292
		public function render_gallery_item( $attachment_post = false ) {
293
			if ( $attachment_post != false ) {
294
				$attachment_id = $attachment_post->ID;
295
				$attachment = wp_get_attachment_image_src( $attachment_id );
296
			} else {
297
				$attachment_id = '';
298
				$attachment = '';
299
			}
300
			$data_attribute = empty($attachment_id) ? '' : "data-attachment-id=\"{$attachment_id}\"";
301
			$img_tag        = empty($attachment) ? '<img width="150" height="150" />' : "<img width=\"150\" height=\"150\" src=\"{$attachment[0]}\" />";
302
			?>
303
			<li class="attachment details" <?php echo $data_attribute; ?>>
304
				<div class="attachment-preview type-image">
305
					<div class="thumbnail">
306
						<div class="centered">
307
							<?php echo $img_tag; ?>
308
						</div>
309
					</div>
310
					<a class="info" href="#" title="<?php _e( 'Edit Info', 'foogallery' ); ?>">
311
						<span class="dashicons dashicons-info"></span>
312
					</a>
313
					<a class="remove" href="#" title="<?php _e( 'Remove from gallery', 'foogallery' ); ?>">
314
						<span class="dashicons dashicons-dismiss"></span>
315
					</a>
316
				</div>
317
				<!--				<input type="text" value="" class="describe" data-setting="caption" placeholder="Caption this image…" />-->
318
			</li>
319
		<?php
320
		}
321
322
		public function render_gallery_settings_metabox( $post ) {
323
            $gallery = FooGallery::get( $post );
324
325
            $settings = new FooGallery_Admin_Gallery_MetaBox_Settings_Helper( $gallery );
326
327
            $settings->render_hidden_gallery_template_selector();
328
329
            $settings->render_gallery_settings();
330
		}
331
332
		public function render_gallery_shortcode_metabox( $post ) {
333
			$gallery = $this->get_gallery( $post );
334
			$shortcode = $gallery->shortcode();
335
			?>
336
			<p class="foogallery-shortcode">
337
				<input type="text" id="foogallery-copy-shortcode" size="<?php echo strlen( $shortcode ) + 2; ?>" value="<?php echo htmlspecialchars( $shortcode ); ?>" readonly="readonly" />
338
			</p>
339
			<p>
340
				<?php _e( 'Paste the above shortcode into a post or page to show the gallery.', 'foogallery' ); ?>
341
			</p>
342
			<script>
343
				jQuery(function($) {
344
					var shortcodeInput = document.querySelector('#foogallery-copy-shortcode');
345
					shortcodeInput.addEventListener('click', function () {
346
						try {
347
							// select the contents
348
							shortcodeInput.select();
349
							//copy the selection
350
							document.execCommand('copy');
351
							//show the copied message
352
							$('.foogallery-shortcode-message').remove();
353
							$(shortcodeInput).after('<p class="foogallery-shortcode-message"><?php _e( 'Shortcode copied to clipboard :)','foogallery' ); ?></p>');
354
						} catch(err) {
355
							console.log('Oops, unable to copy!');
356
						}
357
					}, false);
358
				});
359
			</script>
360
			<?php
361
		}
362
363
		public function render_gallery_usage_metabox( $post ) {
364
			$gallery = $this->get_gallery( $post );
365
			$posts = $gallery->find_usages();
366
			if ( $posts && count( $posts ) > 0 ) { ?>
367
				<p>
368
					<?php _e( 'This gallery is used on the following posts or pages:', 'foogallery' ); ?>
369
				</p>
370
				<ul class="ul-disc">
371
				<?php foreach ( $posts as $post ) {
372
					$url = get_permalink( $post->ID );
373
					echo '<li>' . $post->post_title . '&nbsp;';
374
					edit_post_link( __( 'Edit', 'foogallery' ), '<span class="edit">', ' | </span>', $post->ID );
375
					echo '<span class="view"><a href="' . esc_url( $url ) . '" target="_blank">' . __( 'View', 'foogallery' ) . '</a></li>';
376
				} ?>
377
				</ul>
378
			<?php } else { ?>
379
				<p>
380
					<?php _e( 'This gallery is not used on any pages or pages yet. Quickly create a page:', 'foogallery' ); ?>
381
				</p>
382
				<div class="foogallery_metabox_actions">
383
					<button class="button button-primary button-large" id="foogallery_create_page"><?php _e( 'Create Gallery Page', 'foogallery' ); ?></button>
384
					<span id="foogallery_create_page_spinner" class="spinner"></span>
385
					<?php wp_nonce_field( 'foogallery_create_gallery_page', 'foogallery_create_gallery_page_nonce', false ); ?>
386
				</div>
387
				<p>
388
					<?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' ); ?>
389
				</p>
390
			<?php }
391
		}
392
393
		public function render_sorting_metabox( $post ) {
394
			$gallery = $this->get_gallery( $post );
395
			$sorting_options = foogallery_sorting_options();
396
			if ( empty( $gallery->sorting ) ) {
397
				$gallery->sorting = '';
398
			}
399
			?>
400
			<p>
401
				<?php _e('Change the way images are sorted within your gallery. By default, they are sorted in the order you see them.', 'foogallery'); ?>
402
			</p>
403
			<?php
404
			foreach ( $sorting_options as $sorting_key => $sorting_label ) { ?>
405
				<p>
406
				<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; ?>" />
407
				<label for="FooGallerySettings_GallerySort_<?php echo $sorting_key; ?>"><?php echo $sorting_label; ?></label>
408
				</p><?php
409
			} ?>
410
			<p class="foogallery-help">
411
				<?php _e('PLEASE NOTE : sorting randomly will force HTML Caching for the gallery to be disabled.', 'foogallery'); ?>
412
			</p>
413
			<?php
414
		}
415
416
		public function render_retina_metabox( $post ) {
417
			$gallery = $this->get_gallery( $post );
418
			$retina_options = foogallery_retina_options();
419
			if ( empty( $gallery->retina ) ) {
420
				$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...
421
			}
422
			?>
423
			<p>
424
				<?php _e('Add retina support to this gallery by choosing the different pixel densities you want to enable.', 'foogallery'); ?>
425
			</p>
426
			<?php
427
			foreach ( $retina_options as $retina_key => $retina_label ) {
428
				$checked = array_key_exists( $retina_key, $gallery->retina ) ? ('true' === $gallery->retina[$retina_key]) : false;
429
				?>
430
				<p>
431
				<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; ?>]" />
432
				<label for="FooGallerySettings_Retina_<?php echo $retina_key; ?>"><?php echo $retina_label; ?></label>
433
				</p><?php
434
			} ?>
435
			<p class="foogallery-help">
436
				<?php _e('PLEASE NOTE : thumbnails will be generated for each of the pixel densities chosen, which will increase your website\'s storage space!', 'foogallery'); ?>
437
			</p>
438
			<?php
439
		}
440
441
		public function render_thumb_settings_metabox( $post ) {
442
			$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...
443
			$force_use_original_thumbs = get_post_meta( $post->ID, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
444
			$checked = 'true' === $force_use_original_thumbs; ?>
445
			<p>
446
				<?php _e( 'Clear all the previously cached thumbnails that have been generated for this gallery.', 'foogallery' ); ?>
447
			</p>
448
			<div class="foogallery_metabox_actions">
449
				<button class="button button-primary button-large" id="foogallery_clear_thumb_cache"><?php _e( 'Clear Thumbnail Cache', 'foogallery' ); ?></button>
450
				<span id="foogallery_clear_thumb_cache_spinner" class="spinner"></span>
451
				<?php wp_nonce_field( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce', false ); ?>
452
			</div>
453
			<p>
454
				<input type="checkbox" value="true" <?php checked( $checked ); ?> id="FooGallerySettings_ForceOriginalThumbs" name="<?php echo FOOGALLERY_META_FORCE_ORIGINAL_THUMBS; ?>" />
455
				<label for="FooGallerySettings_ForceOriginalThumbs"><?php _e('Force Original Thumbs', 'foogallery'); ?></label>
456
			</p>
457
			<?php
458
		}
459
460
		public function include_required_scripts() {
461
			$screen_id = foo_current_screen_id();
462
463
			//only include scripts if we on the foogallery add/edit page
464
			if ( FOOGALLERY_CPT_GALLERY === $screen_id ||
465
			     'edit-' . FOOGALLERY_CPT_GALLERY === $screen_id ) {
466
467
				//enqueue any dependencies from extensions or gallery templates
468
				do_action( 'foogallery_enqueue_preview_dependencies' );
469
				//add core foogallery files for preview
470
				foogallery_enqueue_core_gallery_template_style();
471
				foogallery_enqueue_core_gallery_template_script();
472
473
				//spectrum needed for the colorpicker field
474
				$url = FOOGALLERY_URL . 'lib/spectrum/spectrum.js';
475
				wp_enqueue_script( 'foogallery-spectrum', $url, array('jquery'), FOOGALLERY_VERSION );
476
				$url = FOOGALLERY_URL . 'lib/spectrum/spectrum.css';
477
				wp_enqueue_style( 'foogallery-spectrum', $url, array(), FOOGALLERY_VERSION );
478
479
				//include any admin js required for the templates
480
				foreach ( foogallery_gallery_templates() as $template ) {
481
					$admin_js = foo_safe_get( $template, 'admin_js' );
482
					if ( is_array( $admin_js ) ) {
483
						//dealing with an array of js files to include
484
						foreach( $admin_js as $admin_js_key => $admin_js_src ) {
485
							wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'] . '-' . $admin_js_key, $admin_js_src, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION );
486
						}
487
					} else {
488
						//dealing with a single js file to include
489
						wp_enqueue_script( 'foogallery-gallery-admin-' . $template['slug'], $admin_js, array('jquery', 'media-upload', 'jquery-ui-sortable'), FOOGALLERY_VERSION );
490
					}
491
				}
492
			}
493
		}
494
495
		public function render_customcss_metabox( $post ) {
496
			$gallery = $this->get_gallery( $post );
497
			$custom_css = $gallery->custom_css;
498
			$example = '<code>#foogallery-gallery-' . $post->ID . ' { }</code>';
499
			?>
500
			<p>
501
				<?php printf( __( 'Add any custom CSS to target this specific gallery. For example %s', 'foogallery' ), $example ); ?>
502
			</p>
503
			<table id="table_styling" class="form-table">
504
				<tbody>
505
				<tr>
506
					<td>
507
						<textarea class="foogallery_metabox_custom_css" name="<?php echo FOOGALLERY_META_CUSTOM_CSS; ?>" type="text"><?php echo $custom_css; ?></textarea>
508
					</td>
509
				</tr>
510
				</tbody>
511
			</table>
512
		<?php
513
		}
514
515
		public function ajax_create_gallery_page() {
0 ignored issues
show
Coding Style introduced by
ajax_create_gallery_page uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
516
			if ( check_admin_referer( 'foogallery_create_gallery_page', 'foogallery_create_gallery_page_nonce' ) ) {
517
518
				$foogallery_id = $_POST['foogallery_id'];
519
520
				$foogallery = FooGallery::get_by_id( $foogallery_id );
521
522
				$post = array(
523
					'post_content' => $foogallery->shortcode(),
524
					'post_title'   => $foogallery->name,
525
					'post_status'  => 'draft',
526
					'post_type'    => 'page',
527
				);
528
529
				wp_insert_post( $post );
530
			}
531
			die();
532
		}
533
534
		public function ajax_clear_gallery_thumb_cache() {
0 ignored issues
show
Coding Style introduced by
ajax_clear_gallery_thumb_cache uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
535
			if ( check_admin_referer( 'foogallery_clear_gallery_thumb_cache', 'foogallery_clear_gallery_thumb_cache_nonce' ) ) {
536
537
				$foogallery_id = $_POST['foogallery_id'];
538
539
				$foogallery = FooGallery::get_by_id( $foogallery_id );
540
541
				ob_start();
542
543
				//loop through all images, get the full sized file
544
				foreach ( $foogallery->attachments() as $attachment ) {
545
					$meta_data = wp_get_attachment_metadata( $attachment->ID );
546
547
					$file = $meta_data['file'];
548
549
					wpthumb_delete_cache_for_file( $file );
550
				}
551
552
				ob_end_clean();
553
554
				echo __( 'The thumbnail cache has been cleared!', 'foogallery' );
555
			}
556
557
			die();
558
		}
559
	}
560
}
561