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 — hotfix/unyson-support ( a83d09 )
by Brad
02:40
created

functions.php ➔ foogallery_gallery_template_setting()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 6
eloc 13
nc 3
nop 2
dl 0
loc 23
rs 8.5906
1
<?php
2
/**
3
 * FooGallery global functions
4
 *
5
 * @package   FooGallery
6
 * @author    Brad Vincent <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      https://github.com/fooplugins/foogallery
9
 * @copyright 2014 FooPlugins LLC
10
 */
11
12
/**
13
 * Returns the name of the plugin. (Allows the name to be overridden from extensions or functions.php)
14
 * @return string
15
 */
16
function foogallery_plugin_name() {
17
	return apply_filters( 'foogallery_plugin_name', 'FooGallery' );
18
}
19
20
/**
21
 * Return all the gallery templates used within FooGallery
22
 *
23
 * @return array
24
 */
25
function foogallery_gallery_templates() {
26
	return apply_filters( 'foogallery_gallery_templates', array() );
27
}
28
29
/**
30
 * Return the FooGallery extension API class
31
 *
32
 * @return FooGallery_Extensions_API
33
 */
34
function foogallery_extensions_api() {
35
	return new FooGallery_Extensions_API();
36
}
37
38
/**
39
 * Returns the default gallery template
40
 *
41
 * @return string
42
 */
43
function foogallery_default_gallery_template() {
44
	return foogallery_get_setting( 'gallery_template' );
45
}
46
47
/**
48
 * Returns if gallery permalinks are enabled
49
 *
50
 * @return bool
51
 */
52
function foogallery_permalinks_enabled() {
53
	return foogallery_get_setting( 'gallery_permalinks_enabled' );
54
}
55
56
/**
57
 * Returns the gallery permalink
58
 *
59
 * @return string
60
 */
61
function foogallery_permalink() {
62
	return foogallery_get_setting( 'gallery_permalink' );
63
}
64
65
/**
66
 * Return the FooGallery saved setting, or a default value
67
 *
68
 * @param string $key The key for the setting
69
 *
70
 * @param bool $default The default if no value is saved or found
71
 *
72
 * @return mixed
73
 */
74
function foogallery_get_setting( $key, $default = false ) {
75
	$foogallery = FooGallery_Plugin::get_instance();
76
77
	return $foogallery->options()->get( $key, foogallery_get_default( $key, $default ) );
78
}
79
80
/**
81
 * Builds up a FooGallery gallery shortcode
82
 *
83
 * @param $gallery_id
84
 *
85
 * @return string
86
 */
87
function foogallery_build_gallery_shortcode( $gallery_id ) {
88
	return '[' . foogallery_gallery_shortcode_tag() . ' id="' . $gallery_id . '"]';
89
}
90
91
/**
92
 * Returns the gallery shortcode tag
93
 *
94
 * @return string
95
 */
96
function foogallery_gallery_shortcode_tag() {
97
	return apply_filters( 'foogallery_gallery_shortcode_tag', FOOGALLERY_CPT_GALLERY );
98
}
99
100
/**
101
 * Helper method for getting default settings
102
 *
103
 * @param string $key The default config key to retrieve.
104
 *
105
 * @param bool $default The default if no default is set or found
106
 *
107
 * @return string Key value on success, false on failure.
108
 */
109
function foogallery_get_default( $key, $default = false ) {
110
111
	$defaults = array(
112
		'gallery_template'           => 'default',
113
		'gallery_permalinks_enabled' => false,
114
		'gallery_permalink'          => 'gallery',
115
		'lightbox'                   => 'none',
116
		'thumb_jpeg_quality'         => '80',
117
		'thumb_resize_animations'    => true,
118
		'gallery_sorting'            => ''
119
	);
120
121
	// A handy filter to override the defaults
122
	$defaults = apply_filters( 'foogallery_defaults', $defaults );
123
124
	// Return the key specified.
125
	return isset($defaults[ $key ]) ? $defaults[ $key ] : $default;
126
}
127
128
/**
129
 * Returns the FooGallery Add Gallery Url within the admin
130
 *
131
 * @return string The Url to the FooGallery Add Gallery page in admin
132
 */
133
function foogallery_admin_add_gallery_url() {
134
	return admin_url( 'post-new.php?post_type=' . FOOGALLERY_CPT_GALLERY );
135
}
136
137
/**
138
 * Returns the FooGallery help page Url within the admin
139
 *
140
 * @return string The Url to the FooGallery help page in admin
141
 */
142
function foogallery_admin_help_url() {
143
	return admin_url( add_query_arg( array( 'page' => 'foogallery-help' ), foogallery_admin_menu_parent_slug() ) );
144
}
145
146
/**
147
 * Returns the FooGallery settings page Url within the admin
148
 *
149
 * @return string The Url to the FooGallery settings page in admin
150
 */
151
function foogallery_admin_settings_url() {
152
	return admin_url( add_query_arg( array( 'page' => 'foogallery-settings' ), foogallery_admin_menu_parent_slug() ) );
153
}
154
155
/**
156
 * Returns the FooGallery extensions page Url within the admin
157
 *
158
 * @return string The Url to the FooGallery extensions page in admin
159
 */
160
function foogallery_admin_extensions_url() {
161
	return admin_url( add_query_arg( array( 'page' => 'foogallery-extensions' ), foogallery_admin_menu_parent_slug() ) );
162
}
163
164
/**
165
 * Returns the FooGallery system info page Url within the admin
166
 *
167
 * @return string The Url to the FooGallery system info page in admin
168
 */
169
function foogallery_admin_systeminfo_url() {
170
	return admin_url( add_query_arg( array( 'page' => 'foogallery-systeminfo' ), foogallery_admin_menu_parent_slug() ) );
171
}
172
173
/**
174
 * Get a foogallery template setting for the current foogallery that is being output to the frontend
175
 * @param string	$key
176
 * @param string	$default
177
 *
178
 * @return bool
179
 */
180
function foogallery_gallery_template_setting( $key, $default = '' ) {
181
	global $current_foogallery;
182
	global $current_foogallery_arguments;
183
	global $current_foogallery_template;
184
185
	$settings_key = "{$current_foogallery_template}_{$key}";
186
187
	if ( $current_foogallery_arguments && array_key_exists( $key, $current_foogallery_arguments ) ) {
188
		//try to get the value from the arguments
189
		$value = $current_foogallery_arguments[ $key ];
190
191
	} else if ( !empty( $current_foogallery ) && $current_foogallery->settings && array_key_exists( $settings_key, $current_foogallery->settings ) ) {
192
		//then get the value out of the saved gallery settings
193
		$value = $current_foogallery->settings[ $settings_key ];
194
	} else {
195
		//otherwise set it to the default
196
		$value = $default;
197
	}
198
199
	$value = apply_filters( 'foogallery_gallery_template_setting-' . $key, $value );
200
201
	return $value;
202
}
203
204
/**
205
 * Get the admin menu parent slug
206
 * @return string
207
 */
208
function foogallery_admin_menu_parent_slug() {
209
	return apply_filters( 'foogallery_admin_menu_parent_slug', FOOGALLERY_ADMIN_MENU_PARENT_SLUG );
210
}
211
212
/**
213
 * Helper function to build up the admin menu Url
214
 * @param array $extra_args
215
 *
216
 * @return string|void
217
 */
218
function foogallery_build_admin_menu_url( $extra_args = array() ) {
219
	$url = admin_url( foogallery_admin_menu_parent_slug() );
220
	if ( ! empty( $extra_args ) ) {
221
		$url = add_query_arg( $extra_args, $url );
222
	}
223
	return $url;
224
}
225
226
/**
227
 * Helper function for adding a foogallery sub menu
228
 *
229
 * @param $menu_title
230
 * @param string $capability
231
 * @param string $menu_slug
232
 * @param $function
233
 */
234
function foogallery_add_submenu_page( $menu_title, $capability, $menu_slug, $function ) {
235
	add_submenu_page(
236
		foogallery_admin_menu_parent_slug(),
237
		$menu_title,
238
		$menu_title,
239
		$capability,
240
		$menu_slug,
241
		$function
242
	);
243
}
244
245
/**
246
 * Returns all FooGallery galleries
247
 *
248
 * @return FooGallery[] array of FooGallery galleries
249
 */
250
function foogallery_get_all_galleries( $excludes = false ) {
251
	$args = array(
252
		'post_type'     => FOOGALLERY_CPT_GALLERY,
253
		'post_status'	=> array( 'publish', 'draft' ),
254
		'cache_results' => false,
255
		'nopaging'      => true,
256
	);
257
258
	if ( is_array( $excludes ) ) {
259
		$args['post__not_in'] = $excludes;
260
	}
261
262
	$gallery_posts = get_posts( $args );
263
264
	if ( empty( $gallery_posts ) ) {
265
		return array();
266
	}
267
268
	$galleries = array();
269
270
	foreach ( $gallery_posts as $post ) {
271
		$galleries[] = FooGallery::get( $post );
272
	}
273
274
	return $galleries;
275
}
276
277
/**
278
 * Parse some content and return an array of all gallery shortcodes that are used inside it
279
 *
280
 * @param $content The content to search for gallery shortcodes
281
 *
282
 * @return array An array of all the foogallery shortcodes found in the content
283
 */
284
function foogallery_extract_gallery_shortcodes( $content ) {
285
	$shortcodes = array();
286
287
	$regex_pattern = foogallery_gallery_shortcode_regex();
288
	if ( preg_match_all( '/' . $regex_pattern . '/s', $content, $matches ) ) {
289
		for ( $i = 0; $i < count( $matches[0] ); ++$i ) {
290
			$shortcode = $matches[0][$i];
291
			$args = $matches[3][$i];
292
			$attribure_string = str_replace( ' ', '&', trim( $args ) );
293
			$attribure_string = str_replace( '"', '', $attribure_string );
294
			$attributes = wp_parse_args( $attribure_string );
295
			if ( array_key_exists( 'id', $attributes ) ) {
296
				$id = intval( $attributes['id'] );
297
				$shortcodes[ $id ] = $shortcode;
298
			}
299
		}
300
	}
301
302
	return $shortcodes;
303
}
304
305
/**
306
 * Build up the FooGallery shortcode regex
307
 *
308
 * @return string
309
 */
310
function foogallery_gallery_shortcode_regex() {
311
	$tag = foogallery_gallery_shortcode_tag();
312
313
	return
314
		'\\['                              	 // Opening bracket
315
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
316
		. "($tag)"                     		 // 2: Shortcode name
317
		. '(?![\\w-])'                       // Not followed by word character or hyphen
318
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
319
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
320
		.     '(?:'
321
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
322
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
323
		.     ')*?'
324
		. ')'
325
		. '(?:'
326
		.     '(\\/)'                        // 4: Self closing tag ...
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
327
		.     '\\]'                          // ... and closing bracket
328
		. '|'
329
		.     '\\]'                          // Closing bracket
330
		.     '(?:'
331
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
332
		.             '[^\\[]*+'             // Not an opening bracket
333
		.             '(?:'
334
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
335
		.                 '[^\\[]*+'         // Not an opening bracket
336
		.             ')*+'
337
		.         ')'
338
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
339
		.     ')?'
340
		. ')'
341
		. '(\\]?)';                          // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
342
}
343
344
/**
345
 * Builds up a class attribute that can be used in a gallery template
346
 * @param $gallery FooGallery
347
 *
348
 * @return string the classname based on the gallery and any extra attributes
349
 */
350
function foogallery_build_class_attribute( $gallery ) {
351
	$classes[] = 'foogallery-container';
352
	$classes[] = "foogallery-{$gallery->gallery_template}";
353
	$num_args = func_num_args();
354
355
	if ( $num_args > 1 ) {
356
		$arg_list = func_get_args();
357
		for ( $i = 1; $i < $num_args; $i++ ) {
358
			$classes[] = $arg_list[$i];
359
		}
360
	}
361
362
	$classes = apply_filters( 'foogallery_build_class_attribute', $classes );
363
364
	return implode( ' ', $classes );
365
}
366
367
/**
368
 * Render a foogallery
369
 *
370
 * @param $gallery_id int The id of the foogallery you want to render
371
 */
372
function foogallery_render_gallery( $gallery_id ) {
373
	//create new instance of template engine
374
	$engine = new FooGallery_Template_Loader();
375
376
	$engine->render_template( array(
377
		'id' => $gallery_id
378
	) );
379
}
380
381
/**
382
 * Returns the available sorting options that can be chosen for galleries and albums
383
 */
384
function foogallery_sorting_options() {
385
	return apply_filters( 'foogallery_sorting_options', array(
386
		'' => __('Default', 'foogallery'),
387
		'date_desc' => __('Date created - newest first', 'foogallery'),
388
		'date_asc' => __('Date created - oldest first', 'foogallery'),
389
		'modified_desc' => __('Date modified - most recent first', 'foogallery'),
390
		'modified_asc' => __('Date modified - most recent last', 'foogallery'),
391
		'title_asc' => __('Title - alphabetically', 'foogallery'),
392
		'title_desc' => __('Title - reverse', 'foogallery'),
393
		'rand' => __('Random', 'foogallery')
394
	) );
395
}
396
397
function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
398
	$orderby_arg = 'post__in';
399
400
	switch ( $sorting_option ) {
401
		case 'date_desc':
402
		case 'date_asc':
403
			$orderby_arg = 'date';
404
			break;
405
		case 'modified_desc':
406
		case 'modified_asc':
407
			$orderby_arg = 'modified';
408
			break;
409
		case 'title_asc':
410
		case 'title_desc':
411
			$orderby_arg = 'title';
412
			break;
413
		case 'rand':
414
			$orderby_arg = 'rand';
415
			break;
416
	}
417
418
	return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
419
}
420
421
function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
422
	$order_arg = 'DESC';
423
424
	switch ( $sorting_option ) {
425
		case 'date_asc':
426
		case 'modified_asc':
427
		case 'title_asc':
428
		$order_arg = 'ASC';
429
			break;
430
	}
431
432
	return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
433
}
434
435
/**
436
 * Activate the default templates extension when there are no gallery templates loaded
437
 */
438
function foogallery_activate_default_templates_extension() {
439
	$api = foogallery_extensions_api();
440
	$api->activate( 'default_templates' );
441
}
442
443
/**
444
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
445
 *
446
 * @param $handle string
447
 * @param $src string
448
 * @param array $deps
449
 * @param bool $ver
450
 * @param string $media
451
 */
452
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
453
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
454
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
455
}
456
457
458
/**
459
 * Returns all foogallery post objects that are attached to the post
460
 *
461
 * @param $post_id int The ID of the post
462
 *
463
 * @return array List of foogallery posts.
464
 */
465
function foogallery_get_galleries_attached_to_post( $post_id ) {
466
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
467
468
	if ( !empty( $gallery_ids ) ) {
469
		return get_posts( array(
470
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
471
			'post_status'    => array( 'draft', 'publish' ),
472
			'posts_per_page' => -1,
473
			'include'        => $gallery_ids
474
		) );
475
	}
476
477
	return array();
478
}
479
480
/**
481
 * Clears all css load optimization post meta
482
 */
483
function foogallery_clear_all_css_load_optimizations() {
484
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
485
}
486
487
/**
488
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
489
 */
490
function foogallery_perform_version_check() {
491
	$checker = new FooGallery_Version_Check();
492
	$checker->perform_check();
493
}
494
495
/**
496
 * Returns the JPEG quality used when generating thumbnails
497
 *
498
 * @return int The quality value stored in settings
499
 */
500
function foogallery_thumbnail_jpeg_quality() {
501
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
502
503
	//check if we get an invalid value for whatever reason and if so return a default of 80
504
	if ( $quality <= 0 ) {
505
		$quality = 80;
506
	}
507
508
	return $quality;
509
}
510
511
/**
512
 * Returns the caption title source setting
513
 *
514
 * @return string
515
 */
516
function foogallery_caption_title_source() {
517
	$source = foogallery_get_setting( 'caption_title_source', 'caption' );
0 ignored issues
show
Documentation introduced by
'caption' is of type string, 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...
518
519
	if ( empty( $source ) ) {
520
		$source = 'caption';
521
	}
522
523
	return $source;
524
}
525
526
/**
527
 * Returns the attachment caption title based on the caption_title_source setting
528
 *
529
 * @param $attachment_post WP_Post
530
 *
531
 * @return string
532
 */
533
function foogallery_get_caption_title_for_attachment($attachment_post) {
534
	$source = foogallery_caption_title_source();
535
536
	if ( 'title' === $source ) {
537
		$caption = trim( $attachment_post->post_title );
538
	} else {
539
		$caption = trim( $attachment_post->post_excerpt );
540
	}
541
542
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
543
}