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... ( f41da1...dd7887 )
by Brad
02:58
created

functions.php ➔ foogallery_get_gallery_template()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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 a specific gallery template based on the slug
31
 * @param $slug
32
 *
33
 * @return bool|array
34
 */
35
function foogallery_get_gallery_template( $slug ) {
36
	foreach ( foogallery_gallery_templates() as $template ) {
37
		if ( $slug == $template['slug'] ) {
38
			return $template;
39
		}
40
	}
41
42
	return false;
43
}
44
45
/**
46
 * Return the FooGallery extension API class
47
 *
48
 * @return FooGallery_Extensions_API
49
 */
50
function foogallery_extensions_api() {
51
	return new FooGallery_Extensions_API();
52
}
53
54
/**
55
 * Returns the default gallery template
56
 *
57
 * @return string
58
 */
59
function foogallery_default_gallery_template() {
60
	return foogallery_get_setting( 'gallery_template' );
61
}
62
63
/**
64
 * Returns if gallery permalinks are enabled
65
 *
66
 * @return bool
67
 */
68
function foogallery_permalinks_enabled() {
69
	return foogallery_get_setting( 'gallery_permalinks_enabled' );
70
}
71
72
/**
73
 * Returns the gallery permalink
74
 *
75
 * @return string
76
 */
77
function foogallery_permalink() {
78
	return foogallery_get_setting( 'gallery_permalink' );
79
}
80
81
/**
82
 * Return the FooGallery saved setting, or a default value
83
 *
84
 * @param string $key The key for the setting
85
 *
86
 * @param bool $default The default if no value is saved or found
87
 *
88
 * @return mixed
89
 */
90
function foogallery_get_setting( $key, $default = false ) {
91
	$foogallery = FooGallery_Plugin::get_instance();
92
93
	return $foogallery->options()->get( $key, foogallery_get_default( $key, $default ) );
94
}
95
96
/**
97
 * Builds up a FooGallery gallery shortcode
98
 *
99
 * @param $gallery_id
100
 *
101
 * @return string
102
 */
103
function foogallery_build_gallery_shortcode( $gallery_id ) {
104
	return '[' . foogallery_gallery_shortcode_tag() . ' id="' . $gallery_id . '"]';
105
}
106
107
/**
108
 * Returns the gallery shortcode tag
109
 *
110
 * @return string
111
 */
112
function foogallery_gallery_shortcode_tag() {
113
	return apply_filters( 'foogallery_gallery_shortcode_tag', FOOGALLERY_CPT_GALLERY );
114
}
115
116
/**
117
 * Helper method for getting default settings
118
 *
119
 * @param string $key The default config key to retrieve.
120
 *
121
 * @param bool $default The default if no default is set or found
122
 *
123
 * @return string Key value on success, false on failure.
124
 */
125
function foogallery_get_default( $key, $default = false ) {
126
127
	$defaults = array(
128
		'gallery_template'           => 'default',
129
		'gallery_permalinks_enabled' => false,
130
		'gallery_permalink'          => 'gallery',
131
		'lightbox'                   => 'none',
132
		'thumb_jpeg_quality'         => '80',
133
		'thumb_resize_animations'    => true,
134
		'gallery_sorting'            => '',
135
		'datasource'				 => 'media_library'
136
	);
137
138
	// A handy filter to override the defaults
139
	$defaults = apply_filters( 'foogallery_defaults', $defaults );
140
141
	// Return the key specified.
142
	return isset($defaults[ $key ]) ? $defaults[ $key ] : $default;
143
}
144
145
/**
146
 * Returns the FooGallery Add Gallery Url within the admin
147
 *
148
 * @return string The Url to the FooGallery Add Gallery page in admin
149
 */
150
function foogallery_admin_add_gallery_url() {
151
	return admin_url( 'post-new.php?post_type=' . FOOGALLERY_CPT_GALLERY );
152
}
153
154
/**
155
 * Returns the FooGallery help page Url within the admin
156
 *
157
 * @return string The Url to the FooGallery help page in admin
158
 */
159
function foogallery_admin_help_url() {
160
	return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_HELP_SLUG ), foogallery_admin_menu_parent_slug() ) );
161
}
162
163
/**
164
 * Returns the FooGallery settings page Url within the admin
165
 *
166
 * @return string The Url to the FooGallery settings page in admin
167
 */
168
function foogallery_admin_settings_url() {
169
	return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_SETTINGS_SLUG ), foogallery_admin_menu_parent_slug() ) );
170
}
171
172
/**
173
 * Returns the FooGallery extensions page Url within the admin
174
 *
175
 * @return string The Url to the FooGallery extensions page in admin
176
 */
177
function foogallery_admin_extensions_url() {
178
	return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_EXTENSIONS_SLUG ), foogallery_admin_menu_parent_slug() ) );
179
}
180
181
/**
182
 * Returns the FooGallery system info page Url within the admin
183
 *
184
 * @return string The Url to the FooGallery system info page in admin
185
 */
186
function foogallery_admin_systeminfo_url() {
187
	return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_SYSTEMINFO_SLUG ), foogallery_admin_menu_parent_slug() ) );
188
}
189
190
/**
191
 * Get a foogallery template setting for the current foogallery that is being output to the frontend
192
 * @param string	$key
193
 * @param string	$default
194
 *
195
 * @return bool
196
 */
197
function foogallery_gallery_template_setting( $key, $default = '' ) {
198
	global $current_foogallery;
199
	global $current_foogallery_arguments;
200
	global $current_foogallery_template;
201
202
	$settings_key = "{$current_foogallery_template}_{$key}";
203
204
	if ( $current_foogallery_arguments && array_key_exists( $key, $current_foogallery_arguments ) ) {
205
		//try to get the value from the arguments
206
		$value = $current_foogallery_arguments[ $key ];
207
208
	} else if ( !empty( $current_foogallery ) && $current_foogallery->settings && array_key_exists( $settings_key, $current_foogallery->settings ) ) {
209
		//then get the value out of the saved gallery settings
210
		$value = $current_foogallery->settings[ $settings_key ];
211
	} else {
212
		//otherwise set it to the default
213
		$value = $default;
214
	}
215
216
	$value = apply_filters( 'foogallery_gallery_template_setting-' . $key, $value );
217
218
	return $value;
219
}
220
221
/**
222
 * Get the admin menu parent slug
223
 * @return string
224
 */
225
function foogallery_admin_menu_parent_slug() {
226
	return apply_filters( 'foogallery_admin_menu_parent_slug', FOOGALLERY_ADMIN_MENU_PARENT_SLUG );
227
}
228
229
/**
230
 * Helper function to build up the admin menu Url
231
 * @param array $extra_args
232
 *
233
 * @return string|void
234
 */
235
function foogallery_build_admin_menu_url( $extra_args = array() ) {
236
	$url = admin_url( foogallery_admin_menu_parent_slug() );
237
	if ( ! empty( $extra_args ) ) {
238
		$url = add_query_arg( $extra_args, $url );
239
	}
240
	return $url;
241
}
242
243
/**
244
 * Helper function for adding a foogallery sub menu
245
 *
246
 * @param $menu_title
247
 * @param string $capability
248
 * @param string $menu_slug
249
 * @param $function
250
 */
251
function foogallery_add_submenu_page( $menu_title, $capability, $menu_slug, $function ) {
252
	add_submenu_page(
253
		foogallery_admin_menu_parent_slug(),
254
		$menu_title,
255
		$menu_title,
256
		$capability,
257
		$menu_slug,
258
		$function
259
	);
260
}
261
262
/**
263
 * Returns all FooGallery galleries
264
 *
265
 * @return FooGallery[] array of FooGallery galleries
266
 */
267
function foogallery_get_all_galleries( $excludes = false ) {
268
	$args = array(
269
		'post_type'     => FOOGALLERY_CPT_GALLERY,
270
		'post_status'	=> array( 'publish', 'draft' ),
271
		'cache_results' => false,
272
		'nopaging'      => true,
273
	);
274
275
	if ( is_array( $excludes ) ) {
276
		$args['post__not_in'] = $excludes;
277
	}
278
279
	$gallery_posts = get_posts( $args );
280
281
	if ( empty( $gallery_posts ) ) {
282
		return array();
283
	}
284
285
	$galleries = array();
286
287
	foreach ( $gallery_posts as $post ) {
288
		$galleries[] = FooGallery::get( $post );
289
	}
290
291
	return $galleries;
292
}
293
294
/**
295
 * Parse some content and return an array of all gallery shortcodes that are used inside it
296
 *
297
 * @param $content The content to search for gallery shortcodes
298
 *
299
 * @return array An array of all the foogallery shortcodes found in the content
300
 */
301
function foogallery_extract_gallery_shortcodes( $content ) {
302
	$shortcodes = array();
303
304
	$regex_pattern = foogallery_gallery_shortcode_regex();
305
	if ( preg_match_all( '/' . $regex_pattern . '/s', $content, $matches ) ) {
306
		for ( $i = 0; $i < count( $matches[0] ); ++$i ) {
307
			$shortcode = $matches[0][$i];
308
			$args = $matches[3][$i];
309
			$attribure_string = str_replace( ' ', '&', trim( $args ) );
310
			$attribure_string = str_replace( '"', '', $attribure_string );
311
			$attributes = wp_parse_args( $attribure_string );
312
			if ( array_key_exists( 'id', $attributes ) ) {
313
				$id = intval( $attributes['id'] );
314
				$shortcodes[ $id ] = $shortcode;
315
			}
316
		}
317
	}
318
319
	return $shortcodes;
320
}
321
322
/**
323
 * Build up the FooGallery shortcode regex
324
 *
325
 * @return string
326
 */
327
function foogallery_gallery_shortcode_regex() {
328
	$tag = foogallery_gallery_shortcode_tag();
329
330
	return
331
		'\\['                              	 // Opening bracket
332
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
333
		. "($tag)"                     		 // 2: Shortcode name
334
		. '(?![\\w-])'                       // Not followed by word character or hyphen
335
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
336
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
337
		.     '(?:'
338
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
339
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
340
		.     ')*?'
341
		. ')'
342
		. '(?:'
343
		.     '(\\/)'                        // 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...
344
		.     '\\]'                          // ... and closing bracket
345
		. '|'
346
		.     '\\]'                          // Closing bracket
347
		.     '(?:'
348
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
349
		.             '[^\\[]*+'             // Not an opening bracket
350
		.             '(?:'
351
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
352
		.                 '[^\\[]*+'         // Not an opening bracket
353
		.             ')*+'
354
		.         ')'
355
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
356
		.     ')?'
357
		. ')'
358
		. '(\\]?)';                          // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
359
}
360
361
/**
362
 * Builds up a class attribute that can be used in a gallery template
363
 * @param $gallery FooGallery
364
 *
365
 * @return string the classname based on the gallery and any extra attributes
366
 */
367
function foogallery_build_class_attribute( $gallery ) {
368
369
	$classes[] = 'foogallery';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$classes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $classes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
370
	$classes[] = 'foogallery-container';
371
	$classes[] = "fg-{$gallery->gallery_template}";
372
373
	//get some default classes from common gallery settings
374
	$classes[] = $gallery->get_setting( 'theme', 'fg-light' );
375
	$classes[] = $gallery->get_setting( 'border_size', 'fg-border-thin' );
376
	$classes[] = $gallery->get_setting( 'rounded_corners', '' );
377
	$classes[] = $gallery->get_setting( 'drop_shadow', 'fg-shadow-outline' );
378
	$classes[] = $gallery->get_setting( 'inner_shadow', '' );
379
	$classes[] = $gallery->get_setting( 'loading_icon', 'fg-loading-default' );
380
	$classes[] = $gallery->get_setting( 'loaded_effect', 'fg-loaded-fade-in' );
381
382
383
	$caption_preset = $gallery->get_setting( 'hover_effect_preset', 'fg-custom' );
384
385
	$classes[] = $caption_preset;
386
387
	//only set these caption classes if custom preset is selected
388
	if ( 'fg-custom' === $caption_preset ) {
389
		$classes[] = $gallery->get_setting( 'hover_effect_color' , '' );
390
		$classes[] = $gallery->get_setting( 'hover_effect_scale' , '' );
391
		$classes[] = $gallery->get_setting( 'hover_effect_caption_visibility', 'fg-caption-hover' );
392
		$classes[] = $gallery->get_setting( 'hover_effect_transition', 'fg-hover-fade' );
393
		$classes[] = $gallery->get_setting( 'hover_effect_icon', 'fg-hover-zoom' );
394
	}
395
396
	$num_args = func_num_args();
397
398
	if ( $num_args > 1 ) {
399
		$arg_list = func_get_args();
400
		for ( $i = 1; $i < $num_args; $i++ ) {
401
			$classes[] = $arg_list[$i];
402
		}
403
	}
404
405
	$classes = apply_filters( 'foogallery_build_class_attribute', $classes, $gallery );
406
407
	return implode( ' ', $classes );
408
}
409
410
/**
411
 * Builds up a SAFE class attribute that can be used in a gallery template
412
 * @param $gallery FooGallery
413
 *
414
 * @return string the classname based on the gallery and any extra attributes
415
 */
416
function foogallery_build_class_attribute_safe( $gallery ) {
0 ignored issues
show
Unused Code introduced by
The parameter $gallery 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...
417
	$args = func_get_args();
418
	$result = call_user_func_array("foogallery_build_class_attribute", $args);
419
	return esc_attr( $result );
420
}
421
422
/**
423
 * Renders an escaped class attribute that can be used directly by gallery templates
424
 *
425
 * @param $gallery FooGallery
426
 */
427
function foogallery_build_class_attribute_render_safe( $gallery ) {
0 ignored issues
show
Unused Code introduced by
The parameter $gallery 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...
428
	$args = func_get_args();
429
	$result = call_user_func_array("foogallery_build_class_attribute_safe", $args);
430
	echo $result;
431
}
432
433
/**
434
 * Builds up the attributes that are appended to a gallery template container
435
 *
436
 * @param $gallery    FooGallery
437
 * @param $attributes array
438
 *
439
 * @return string
440
 */
441
function foogallery_build_container_attributes_safe( $gallery, $attributes ) {
442
443
	//add the default gallery id
444
	$attributes['id'] = 'foogallery-gallery-' . $gallery->ID;
445
446
	//add the standard data-foogallery attribute so that the JS initializes correctly
447
    $attributes['data-foogallery'] = foogallery_build_container_data_options( $gallery, $attributes );
448
449
	//allow others to add their own attributes globally
450
	$attributes = apply_filters( 'foogallery_build_container_attributes', $attributes, $gallery );
451
452
	//allow others to add their own attributes for a specific gallery template
453
	$attributes = apply_filters( 'foogallery_build_container_attributes-' . $gallery->gallery_template, $attributes, $gallery );
454
455
	//clean up the attributes to make them safe for output
456
	$html = '';
457
	foreach( $attributes as $key=>$value) {
458
		$safe_value = esc_attr( $value );
459
		$html .= "{$key}=\"{$safe_value}\" ";
460
	}
461
462
	return $html;
463
}
464
465
/**
466
 * Builds up the data-foogallery attribute options that is used by the core javascript
467
 *
468
 * @param $gallery
469
 * @param $attributes
470
 *
471
 * @return string
472
 */
473
function foogallery_build_container_data_options( $gallery, $attributes ) {
474
	$options = apply_filters( 'foogallery_build_container_data_options', array(), $gallery, $attributes );
475
476
	$options = apply_filters( 'foogallery_build_container_data_options-'. $gallery->gallery_template, $options, $gallery, $attributes );
477
478
	return json_encode( $options );
479
}
480
481
/**
482
 * Render a foogallery
483
 *
484
 * @param       $gallery_id int The id of the foogallery you want to render
485
 * @param array $args
486
 */
487
function foogallery_render_gallery( $gallery_id, $args = array()) {
488
	//create new instance of template engine
489
	$engine = new FooGallery_Template_Loader();
490
491
	$shortcode_args = wp_parse_args( $args, array(
492
		'id' => $gallery_id
493
	) );
494
495
	$engine->render_template( $shortcode_args );
496
}
497
498
/**
499
 * Returns the available sorting options that can be chosen for galleries and albums
500
 */
501
function foogallery_sorting_options() {
502
	return apply_filters( 'foogallery_sorting_options', array(
503
		'' => __('Default', 'foogallery'),
504
		'date_desc' => __('Date created - newest first', 'foogallery'),
505
		'date_asc' => __('Date created - oldest first', 'foogallery'),
506
		'modified_desc' => __('Date modified - most recent first', 'foogallery'),
507
		'modified_asc' => __('Date modified - most recent last', 'foogallery'),
508
		'title_asc' => __('Title - alphabetically', 'foogallery'),
509
		'title_desc' => __('Title - reverse', 'foogallery'),
510
		'rand' => __('Random', 'foogallery')
511
	) );
512
}
513
514
function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
515
	$orderby_arg = 'post__in';
516
517
	switch ( $sorting_option ) {
518
		case 'date_desc':
519
		case 'date_asc':
520
			$orderby_arg = 'date';
521
			break;
522
		case 'modified_desc':
523
		case 'modified_asc':
524
			$orderby_arg = 'modified';
525
			break;
526
		case 'title_asc':
527
		case 'title_desc':
528
			$orderby_arg = 'title';
529
			break;
530
		case 'rand':
531
			$orderby_arg = 'rand';
532
			break;
533
	}
534
535
	return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
536
}
537
538
function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
539
	$order_arg = 'DESC';
540
541
	switch ( $sorting_option ) {
542
		case 'date_asc':
543
		case 'modified_asc':
544
		case 'title_asc':
545
		$order_arg = 'ASC';
546
			break;
547
	}
548
549
	return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
550
}
551
552
/**
553
 * Activate the default templates extension when there are no gallery templates loaded
554
 */
555
function foogallery_activate_default_templates_extension() {
556
	$api = foogallery_extensions_api();
557
	$api->activate( 'default_templates' );
558
}
559
560
/**
561
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
562
 *
563
 * @param $handle string
564
 * @param $src string
565
 * @param array $deps
566
 * @param bool $ver
567
 * @param string $media
568
 */
569
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
570
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
571
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
572
}
573
574
575
/**
576
 * Returns all foogallery post objects that are attached to the post
577
 *
578
 * @param $post_id int The ID of the post
579
 *
580
 * @return array List of foogallery posts.
581
 */
582
function foogallery_get_galleries_attached_to_post( $post_id ) {
583
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
584
585
	if ( !empty( $gallery_ids ) ) {
586
		return get_posts( array(
587
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
588
			'post_status'    => array( 'draft', 'publish' ),
589
			'posts_per_page' => -1,
590
			'include'        => $gallery_ids
591
		) );
592
	}
593
594
	return array();
595
}
596
597
/**
598
 * Clears all css load optimization post meta
599
 */
600
function foogallery_clear_all_css_load_optimizations() {
601
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
602
}
603
604
/**
605
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
606
 */
607
function foogallery_perform_version_check() {
608
	$checker = new FooGallery_Version_Check();
609
	$checker->perform_check();
610
}
611
612
/**
613
 * Returns the JPEG quality used when generating thumbnails
614
 *
615
 * @return int The quality value stored in settings
616
 */
617
function foogallery_thumbnail_jpeg_quality() {
618
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
619
620
	//check if we get an invalid value for whatever reason and if so return a default of 80
621
	if ( $quality <= 0 ) {
622
		$quality = 80;
623
	}
624
625
	return $quality;
626
}
627
628
/**
629
 * Returns the caption title source setting
630
 *
631
 * @return string
632
 */
633
function foogallery_caption_title_source() {
634
	$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...
635
636
	if ( empty( $source ) ) {
637
		$source = 'caption';
638
	}
639
640
	return $source;
641
}
642
643
/**
644
 * Returns the attachment caption title based on the caption_title_source setting
645
 *
646
 * @param $attachment_post WP_Post
647
 *
648
 * @return string
649
 */
650
function foogallery_get_caption_title_for_attachment($attachment_post) {
651
	$source = foogallery_caption_title_source();
652
653
	if ( 'title' === $source ) {
654
		$caption = trim( $attachment_post->post_title );
655
	} else {
656
		$caption = trim( $attachment_post->post_excerpt );
657
	}
658
659
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
660
}
661
662
/**
663
 * Returns the caption description source setting
664
 *
665
 * @return string
666
 */
667
function foogallery_caption_desc_source() {
668
	$source = foogallery_get_setting( 'caption_desc_source', 'desc' );
0 ignored issues
show
Documentation introduced by
'desc' 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...
669
670
	if ( empty( $source ) ) {
671
		$source = 'desc';
672
	}
673
674
	return $source;
675
}
676
677
/**
678
 * Returns the attachment caption description based on the caption_desc_source setting
679
 *
680
 * @param $attachment_post WP_Post
681
 *
682
 * @return string
683
 */
684
function foogallery_get_caption_desc_for_attachment($attachment_post) {
685
	$source = foogallery_caption_desc_source();
686
687
	switch ( $source ) {
688
		case 'title':
689
			$caption = trim( $attachment_post->post_title );
690
			break;
691
		case 'caption':
692
			$caption = trim( $attachment_post->post_excerpt );
693
			break;
694
		case 'alt':
695
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
696
			break;
697
		default:
698
			$caption = trim( $attachment_post->post_content );
699
	}
700
701
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
702
}
703
704
/**
705
 * Runs thumbnail tests and outputs results in a table format
706
 */
707
function foogallery_output_thumbnail_generation_results() {
708
	$thumbs = new FooGallery_Thumbnails();
709
	try {
710
		$results = $thumbs->run_thumbnail_generation_tests();
711
        if ( $results['success'] ) {
712
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
713
        } else {
714
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
715
            var_dump( $results['error'] );
716
			var_dump( $results['file_info'] );
717
        }
718
	}
719
	catch (Exception $e) {
720
		echo 'Exception: ' . $e->getMessage();
721
	}
722
}
723
724
/**
725
 * Returns the URL to the test image
726
 *
727
 * @return string
728
 */
729
function foogallery_test_thumb_url() {
730
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/test_thumb_1.jpg' );
731
}
732
733
/**
734
 * Return all the gallery datasources used within FooGallery
735
 *
736
 * @return array
737
 */
738
function foogallery_gallery_datasources() {
739
	$default_datasource = foogallery_default_datasource();
740
741
	$datasources[$default_datasource] = 'FooGalleryDatasource_MediaLibrary';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$datasources was never initialized. Although not strictly required by PHP, it is generally a good practice to add $datasources = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
742
743
	return apply_filters( 'foogallery_gallery_datasources', $datasources );
744
}
745
746
/**
747
 * Returns the default gallery datasource
748
 *
749
 * @return string
750
 */
751
function foogallery_default_datasource() {
752
	return foogallery_get_default( 'datasource', 'media_library' );
0 ignored issues
show
Documentation introduced by
'media_library' 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...
753
}
754
755
/**
756
 * Instantiates a FooGallery datasource based on a datasource name
757
 *
758
 * @param $datasource_name string
759
 *
760
 * @return IFooGalleryDatasource
761
 */
762
function foogallery_instantiate_datasource( $datasource_name ) {
763
	$datasources = foogallery_gallery_datasources();
764
	if ( array_key_exists( $datasource_name, $datasources ) ) {
765
		return new $datasources[$datasource_name];
766
	}
767
768
	return new FooGalleryDatasource_MediaLibrary();
769
}
770
771
/**
772
 * Returns the src to the built-in image placeholder
773
 * @return string
774
 */
775
function foogallery_image_placeholder_src() {
776
	return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
777
}
778
779
/**
780
 * Returns the image html for the built-in image placeholder
781
 *
782
 * @param array $args
783
 *
784
 * @return string
785
 */
786
function foogallery_image_placeholder_html( $args ) {
787
	if ( !isset( $args ) ) {
788
		$args = array(
789
			'width' => 150,
790
			'height' => 150
791
		);
792
	}
793
794
	$args['src'] = foogallery_image_placeholder_src();
795
	$args = array_map( 'esc_attr', $args );
796
	$html = '<img ';
797
	foreach ( $args as $name => $value ) {
798
		$html .= " $name=" . '"' . $value . '"';
799
	}
800
	$html .= ' />';
801
	return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
802
}
803
804
/**
805
 * Returns the thumbnail html for the featured attachment for a gallery.
806
 * If no featured attachment can be found, then a placeholder image src is returned instead
807
 *
808
 * @param FooGallery $gallery
809
 * @param array $args
810
 *
811
 * @return string
812
 */
813
function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
814
	if ( !isset( $gallery ) ) return '';
815
816
	if ( !isset( $args ) ) {
817
		$args = array(
818
			'width' => 150,
819
			'height' => 150
820
		);
821
	}
822
823
	$featuredAttachment = $gallery->featured_attachment();
824
	if ( $featuredAttachment ) {
825
		return $featuredAttachment->html_img( $args );
826
	} else {
827
		//if we have no featured attachment, then use the built-in image placeholder
828
		return foogallery_image_placeholder_html( $args );
829
	}
830
}
831
832
/**
833
 * Returns the thumbnail src for the featured attachment for a gallery.
834
 * If no featured attachment can be found, then a placeholder image src is returned instead
835
 *
836
 * @param FooGallery $gallery
837
 * @param array $args
838
 *
839
 * @return string
840
 */
841
function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
842
	if ( !isset( $gallery ) ) return '';
843
844
	if ( !isset( $args ) ) {
845
		$args = array(
846
			'width' => 150,
847
			'height' => 150
848
		);
849
	}
850
851
	$featuredAttachment = $gallery->featured_attachment();
852
	if ( $featuredAttachment ) {
853
		return $featuredAttachment->html_img_src( $args );
854
	} else {
855
		//if we have no featured attachment, then use the built-in image placeholder
856
		return foogallery_image_placeholder_src();
857
	}
858
}
859
860
/**
861
 * Returns the available retina options that can be chosen
862
 */
863
function foogallery_retina_options() {
864
    return apply_filters( 'foogallery_retina_options', array(
865
        '2x' => __('2x', 'foogallery'),
866
        '3x' => __('3x', 'foogallery'),
867
        '4x' => __('4x', 'foogallery')
868
    ) );
869
}
870
871
/**
872
 * Does a full uninstall of the plugin including all data and settings!
873
 */
874
function foogallery_uninstall() {
875
876
	if ( !current_user_can( 'install_plugins' ) ) exit;
877
878
	//delete all gallery posts first
879
	global $wpdb;
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...
880
	$query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
881
	$gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
882
883
	if ( !empty( $gallery_post_ids ) ) {
884
		$deleted = 0;
885
		foreach ( $gallery_post_ids as $post_id ) {
886
			$del = wp_delete_post( $post_id );
887
			if ( false !== $del ) {
888
				++$deleted;
889
			}
890
		}
891
	}
892
893
	//delete all options
894
	if ( is_network_admin() ) {
895
		delete_site_option( FOOGALLERY_SLUG );
896
	} else {
897
		delete_option( FOOGALLERY_SLUG );
898
	}
899
	delete_option( FOOGALLERY_OPTION_VERSION );
900
	delete_option( FOOGALLERY_OPTION_THUMB_TEST );
901
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
902
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
903
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
904
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
905
	delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
906
	delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
907
908
	//let any extensions clean up after themselves
909
	do_action( 'foogallery_uninstall' );
910
}
911
912
/**
913
 * Returns an attachment field friendly name, based on a field name that is passed in
914
 *
915
 * @param $field
916
 *
917
 * @return string
918
 */
919
function foogallery_get_attachment_field_friendly_name( $field ) {
920
	switch ( $field ) {
921
		case 'title':
922
			return __( 'Attachment Title', 'foogallery' );
923
		case 'caption':
924
			return __( 'Attachment Caption', 'foogallery' );
925
		case 'desc':
926
			return __( 'Attachment Description', 'foogallery' );
927
		case 'alt':
928
			return __( 'Attachment Alt', 'foogallery' );
929
	}
930
}