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... ( 1ace21...8f3dfe )
by Brad
02:32
created

preview_no_attachments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
590
591
					$foogallery_gallery_preview = true;
592
593
					$args = array(
594
						'template'       => $template,
595
						'attachment_ids' => $_POST['foogallery_attachments']
596
					);
597
598
					$args = apply_filters( 'foogallery_preview_arguments', $args, $_POST, $template );
599
					$args = apply_filters( 'foogallery_preview_arguments-' . $template, $args, $_POST );
600
601
					foogallery_render_gallery( $foogallery_id, $args );
602
603
					$foogallery_gallery_preview = false;
604
605
				} else {
606
					echo '<div style="padding:20px 50px 50px 50px; text-align: center">';
607
					echo '<h3>' . __( 'Preview not available!', 'foogallery' ) . '</h3>';
608
					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' );
609
					echo '</div>';
610
				}
611
			}
612
613
			die();
614
		}
615
616
		/**
617
		 * Handle gallery previews where there are no attachments
618
		 *
619
		 * @param $foogallery FooGallery
620
		 */
621
		public function preview_no_attachments( $foogallery ) {
0 ignored issues
show
Unused Code introduced by
The parameter $foogallery is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
622
			global $foogallery_gallery_preview;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
623
624
			if ( isset( $foogallery_gallery_preview ) && true === $foogallery_gallery_preview ) {
625
				$this->render_empty_gallery_preview();
626
			}
627
		}
628
	}
629
}
630