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... ( 6aa6f0...0fed14 )
by Brad
02:45
created

functions.php ➔ foogallery_build_class_attribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.4285
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
	$num_args = func_num_args();
374
375
	if ( $num_args > 1 ) {
376
		$arg_list = func_get_args();
377
		for ( $i = 1; $i < $num_args; $i++ ) {
378
			$classes[] = $arg_list[$i];
379
		}
380
	}
381
382
	$classes = apply_filters( 'foogallery_build_class_attribute', $classes, $gallery );
383
384
	return implode( ' ', $classes );
385
}
386
387
/**
388
 * Builds up a SAFE class attribute that can be used in a gallery template
389
 * @param $gallery FooGallery
390
 *
391
 * @return string the classname based on the gallery and any extra attributes
392
 */
393
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...
394
	$args = func_get_args();
395
	$result = call_user_func_array("foogallery_build_class_attribute", $args);
396
	return esc_attr( $result );
397
}
398
399
/**
400
 * Renders an escaped class attribute that can be used directly by gallery templates
401
 *
402
 * @param $gallery FooGallery
403
 */
404
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...
405
	$args = func_get_args();
406
	$result = call_user_func_array("foogallery_build_class_attribute_safe", $args);
407
	echo $result;
408
}
409
410
/**
411
 * Builds up the attributes that are appended to a gallery template container
412
 *
413
 * @param $gallery    FooGallery
414
 * @param $attributes array
415
 *
416
 * @return string
417
 */
418
function foogallery_build_container_attributes_safe( $gallery, $attributes ) {
419
420
	//add the default gallery id
421
	$attributes['id'] = 'foogallery-gallery-' . $gallery->ID;
422
423
	//add the standard data-foogallery attribute so that the JS initializes correctly
424
    $attributes['data-foogallery'] = foogallery_build_container_data_options( $gallery, $attributes );
425
426
	//allow others to add their own attributes globally
427
	$attributes = apply_filters( 'foogallery_build_container_attributes', $attributes, $gallery );
428
429
	//allow others to add their own attributes for a specific gallery template
430
	$attributes = apply_filters( 'foogallery_build_container_attributes-' . $gallery->gallery_template, $attributes, $gallery );
431
432
	//clean up the attributes to make them safe for output
433
	$html = '';
434
	foreach( $attributes as $key=>$value) {
435
		$safe_value = esc_attr( $value );
436
		$html .= "{$key}=\"{$safe_value}\" ";
437
	}
438
439
	return $html;
440
}
441
442
/**
443
 * Builds up the data-foogallery attribute options that is used by the core javascript
444
 *
445
 * @param $gallery
446
 * @param $attributes
447
 *
448
 * @return string
449
 */
450
function foogallery_build_container_data_options( $gallery, $attributes ) {
451
	$options = apply_filters( 'foogallery_build_container_data_options', array(), $gallery, $attributes );
452
453
	$options = apply_filters( 'foogallery_build_container_data_options-'. $gallery->gallery_template, $options, $gallery, $attributes );
454
455
	return json_encode( $options );
456
}
457
458
/**
459
 * Render a foogallery
460
 *
461
 * @param       $gallery_id int The id of the foogallery you want to render
462
 * @param array $args
463
 */
464
function foogallery_render_gallery( $gallery_id, $args = array()) {
465
	//create new instance of template engine
466
	$engine = new FooGallery_Template_Loader();
467
468
	$shortcode_args = wp_parse_args( $args, array(
469
		'id' => $gallery_id
470
	) );
471
472
	$engine->render_template( $shortcode_args );
473
}
474
475
/**
476
 * Returns the available sorting options that can be chosen for galleries and albums
477
 */
478
function foogallery_sorting_options() {
479
	return apply_filters( 'foogallery_sorting_options', array(
480
		'' => __('Default', 'foogallery'),
481
		'date_desc' => __('Date created - newest first', 'foogallery'),
482
		'date_asc' => __('Date created - oldest first', 'foogallery'),
483
		'modified_desc' => __('Date modified - most recent first', 'foogallery'),
484
		'modified_asc' => __('Date modified - most recent last', 'foogallery'),
485
		'title_asc' => __('Title - alphabetically', 'foogallery'),
486
		'title_desc' => __('Title - reverse', 'foogallery'),
487
		'rand' => __('Random', 'foogallery')
488
	) );
489
}
490
491
function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
492
	$orderby_arg = 'post__in';
493
494
	switch ( $sorting_option ) {
495
		case 'date_desc':
496
		case 'date_asc':
497
			$orderby_arg = 'date';
498
			break;
499
		case 'modified_desc':
500
		case 'modified_asc':
501
			$orderby_arg = 'modified';
502
			break;
503
		case 'title_asc':
504
		case 'title_desc':
505
			$orderby_arg = 'title';
506
			break;
507
		case 'rand':
508
			$orderby_arg = 'rand';
509
			break;
510
	}
511
512
	return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
513
}
514
515
function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
516
	$order_arg = 'DESC';
517
518
	switch ( $sorting_option ) {
519
		case 'date_asc':
520
		case 'modified_asc':
521
		case 'title_asc':
522
		$order_arg = 'ASC';
523
			break;
524
	}
525
526
	return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
527
}
528
529
/**
530
 * Activate the default templates extension when there are no gallery templates loaded
531
 */
532
function foogallery_activate_default_templates_extension() {
533
	$api = foogallery_extensions_api();
534
	$api->activate( 'default_templates' );
535
}
536
537
/**
538
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
539
 *
540
 * @param $handle string
541
 * @param $src string
542
 * @param array $deps
543
 * @param bool $ver
544
 * @param string $media
545
 */
546
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
547
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
548
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
549
}
550
551
552
/**
553
 * Returns all foogallery post objects that are attached to the post
554
 *
555
 * @param $post_id int The ID of the post
556
 *
557
 * @return array List of foogallery posts.
558
 */
559
function foogallery_get_galleries_attached_to_post( $post_id ) {
560
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
561
562
	if ( !empty( $gallery_ids ) ) {
563
		return get_posts( array(
564
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
565
			'post_status'    => array( 'draft', 'publish' ),
566
			'posts_per_page' => -1,
567
			'include'        => $gallery_ids
568
		) );
569
	}
570
571
	return array();
572
}
573
574
/**
575
 * Clears all css load optimization post meta
576
 */
577
function foogallery_clear_all_css_load_optimizations() {
578
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
579
}
580
581
/**
582
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
583
 */
584
function foogallery_perform_version_check() {
585
	$checker = new FooGallery_Version_Check();
586
	$checker->perform_check();
587
}
588
589
/**
590
 * Returns the JPEG quality used when generating thumbnails
591
 *
592
 * @return int The quality value stored in settings
593
 */
594
function foogallery_thumbnail_jpeg_quality() {
595
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
596
597
	//check if we get an invalid value for whatever reason and if so return a default of 80
598
	if ( $quality <= 0 ) {
599
		$quality = 80;
600
	}
601
602
	return $quality;
603
}
604
605
/**
606
 * Returns the caption title source setting
607
 *
608
 * @return string
609
 */
610
function foogallery_caption_title_source() {
611
	$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...
612
613
	if ( empty( $source ) ) {
614
		$source = 'caption';
615
	}
616
617
	return $source;
618
}
619
620
/**
621
 * Returns the attachment caption title based on the caption_title_source setting
622
 *
623
 * @param $attachment_post WP_Post
624
 *
625
 * @return string
626
 */
627
function foogallery_get_caption_title_for_attachment($attachment_post) {
628
	$source = foogallery_caption_title_source();
629
630
	if ( 'title' === $source ) {
631
		$caption = trim( $attachment_post->post_title );
632
	} else {
633
		$caption = trim( $attachment_post->post_excerpt );
634
	}
635
636
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
637
}
638
639
/**
640
 * Returns the caption description source setting
641
 *
642
 * @return string
643
 */
644
function foogallery_caption_desc_source() {
645
	$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...
646
647
	if ( empty( $source ) ) {
648
		$source = 'desc';
649
	}
650
651
	return $source;
652
}
653
654
/**
655
 * Returns the attachment caption description based on the caption_desc_source setting
656
 *
657
 * @param $attachment_post WP_Post
658
 *
659
 * @return string
660
 */
661
function foogallery_get_caption_desc_for_attachment($attachment_post) {
662
	$source = foogallery_caption_desc_source();
663
664
	switch ( $source ) {
665
		case 'title':
666
			$caption = trim( $attachment_post->post_title );
667
			break;
668
		case 'caption':
669
			$caption = trim( $attachment_post->post_excerpt );
670
			break;
671
		case 'alt':
672
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
673
			break;
674
		default:
675
			$caption = trim( $attachment_post->post_content );
676
	}
677
678
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
679
}
680
681
/**
682
 * Runs thumbnail tests and outputs results in a table format
683
 */
684
function foogallery_output_thumbnail_generation_results() {
685
	$thumbs = new FooGallery_Thumbnails();
686
	try {
687
		$results = $thumbs->run_thumbnail_generation_tests();
688
        if ( $results['success'] ) {
689
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
690
        } else {
691
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
692
            var_dump( $results['error'] );
693
			var_dump( $results['file_info'] );
694
        }
695
	}
696
	catch (Exception $e) {
697
		echo 'Exception: ' . $e->getMessage();
698
	}
699
}
700
701
/**
702
 * Returns the URL to the test image
703
 *
704
 * @return string
705
 */
706
function foogallery_test_thumb_url() {
707
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/test_thumb_1.jpg' );
708
}
709
710
/**
711
 * Return all the gallery datasources used within FooGallery
712
 *
713
 * @return array
714
 */
715
function foogallery_gallery_datasources() {
716
	$default_datasource = foogallery_default_datasource();
717
718
	$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...
719
720
	return apply_filters( 'foogallery_gallery_datasources', $datasources );
721
}
722
723
/**
724
 * Returns the default gallery datasource
725
 *
726
 * @return string
727
 */
728
function foogallery_default_datasource() {
729
	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...
730
}
731
732
/**
733
 * Instantiates a FooGallery datasource based on a datasource name
734
 *
735
 * @param $datasource_name string
736
 *
737
 * @return IFooGalleryDatasource
738
 */
739
function foogallery_instantiate_datasource( $datasource_name ) {
740
	$datasources = foogallery_gallery_datasources();
741
	if ( array_key_exists( $datasource_name, $datasources ) ) {
742
		return new $datasources[$datasource_name];
743
	}
744
745
	return new FooGalleryDatasource_MediaLibrary();
746
}
747
748
/**
749
 * Returns the src to the built-in image placeholder
750
 * @return string
751
 */
752
function foogallery_image_placeholder_src() {
753
	return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
754
}
755
756
/**
757
 * Returns the image html for the built-in image placeholder
758
 *
759
 * @param array $args
760
 *
761
 * @return string
762
 */
763
function foogallery_image_placeholder_html( $args ) {
764
	if ( !isset( $args ) ) {
765
		$args = array(
766
			'width' => 150,
767
			'height' => 150
768
		);
769
	}
770
771
	$args['src'] = foogallery_image_placeholder_src();
772
	$args = array_map( 'esc_attr', $args );
773
	$html = '<img ';
774
	foreach ( $args as $name => $value ) {
775
		$html .= " $name=" . '"' . $value . '"';
776
	}
777
	$html .= ' />';
778
	return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
779
}
780
781
/**
782
 * Returns the thumbnail html for the featured attachment for a gallery.
783
 * If no featured attachment can be found, then a placeholder image src is returned instead
784
 *
785
 * @param FooGallery $gallery
786
 * @param array $args
787
 *
788
 * @return string
789
 */
790
function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
791
	if ( !isset( $gallery ) ) return '';
792
793
	if ( !isset( $args ) ) {
794
		$args = array(
795
			'width' => 150,
796
			'height' => 150
797
		);
798
	}
799
800
	$featuredAttachment = $gallery->featured_attachment();
801
	if ( $featuredAttachment ) {
802
		return $featuredAttachment->html_img( $args );
803
	} else {
804
		//if we have no featured attachment, then use the built-in image placeholder
805
		return foogallery_image_placeholder_html( $args );
806
	}
807
}
808
809
/**
810
 * Returns the thumbnail src for the featured attachment for a gallery.
811
 * If no featured attachment can be found, then a placeholder image src is returned instead
812
 *
813
 * @param FooGallery $gallery
814
 * @param array $args
815
 *
816
 * @return string
817
 */
818
function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
819
	if ( !isset( $gallery ) ) return '';
820
821
	if ( !isset( $args ) ) {
822
		$args = array(
823
			'width' => 150,
824
			'height' => 150
825
		);
826
	}
827
828
	$featuredAttachment = $gallery->featured_attachment();
829
	if ( $featuredAttachment ) {
830
		return $featuredAttachment->html_img_src( $args );
831
	} else {
832
		//if we have no featured attachment, then use the built-in image placeholder
833
		return foogallery_image_placeholder_src();
834
	}
835
}
836
837
/**
838
 * Returns the available retina options that can be chosen
839
 */
840
function foogallery_retina_options() {
841
    return apply_filters( 'foogallery_retina_options', array(
842
        '2x' => __('2x', 'foogallery'),
843
        '3x' => __('3x', 'foogallery'),
844
        '4x' => __('4x', 'foogallery')
845
    ) );
846
}
847
848
/**
849
 * Does a full uninstall of the plugin including all data and settings!
850
 */
851
function foogallery_uninstall() {
852
853
	if ( !current_user_can( 'install_plugins' ) ) exit;
854
855
	//delete all gallery posts first
856
	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...
857
	$query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
858
	$gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
859
860
	if ( !empty( $gallery_post_ids ) ) {
861
		$deleted = 0;
862
		foreach ( $gallery_post_ids as $post_id ) {
863
			$del = wp_delete_post( $post_id );
864
			if ( false !== $del ) {
865
				++$deleted;
866
			}
867
		}
868
	}
869
870
	//delete all options
871
	if ( is_network_admin() ) {
872
		delete_site_option( FOOGALLERY_SLUG );
873
	} else {
874
		delete_option( FOOGALLERY_SLUG );
875
	}
876
	delete_option( FOOGALLERY_OPTION_VERSION );
877
	delete_option( FOOGALLERY_OPTION_THUMB_TEST );
878
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
879
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
880
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
881
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
882
	delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
883
	delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
884
885
	//let any extensions clean up after themselves
886
	do_action( 'foogallery_uninstall' );
887
}
888
889
/**
890
 * Returns an attachment field friendly name, based on a field name that is passed in
891
 *
892
 * @param $field
893
 *
894
 * @return string
895
 */
896
function foogallery_get_attachment_field_friendly_name( $field ) {
897
	switch ( $field ) {
898
		case 'title':
899
			return __( 'Attachment Title', 'foogallery' );
900
		case 'caption':
901
			return __( 'Attachment Caption', 'foogallery' );
902
		case 'desc':
903
			return __( 'Attachment Description', 'foogallery' );
904
		case 'alt':
905
			return __( 'Attachment Alt', 'foogallery' );
906
	}
907
}