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... ( 275fef...a33793 )
by Brad
02:29
created

ajax_gallery_preview()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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