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 — master ( 6e44b1...c642a0 )
by Brad
03:14
created

functions.php ➔ foogallery_get_caption_title_for_attachment()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 12
nop 2
dl 0
loc 24
rs 6.7272
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;
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...
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
        apply_filters( 'foogallery_admin_menu_capability', $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[] = "foogallery-{$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
 * @deprecated 1.4.7 Default templates loaded by default and no longer activated via extension
531
 *
532
 * Activate the default templates extension when there are no gallery templates loaded
533
 */
534
function foogallery_activate_default_templates_extension() {
535
    //no longer needed but left in case any 3rd party extensions call this function
536
    _deprecated_function( __FUNCTION__, '1.4.7' );
537
}
538
539
/**
540
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
541
 *
542
 * @param $handle string
543
 * @param $src string
544
 * @param array $deps
545
 * @param bool $ver
546
 * @param string $media
547
 */
548
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
549
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
550
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
551
}
552
553
554
/**
555
 * Returns all foogallery post objects that are attached to the post
556
 *
557
 * @param $post_id int The ID of the post
558
 *
559
 * @return array List of foogallery posts.
560
 */
561
function foogallery_get_galleries_attached_to_post( $post_id ) {
562
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
563
564
	if ( !empty( $gallery_ids ) ) {
565
		return get_posts( array(
566
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
567
			'post_status'    => array( 'draft', 'publish' ),
568
			'posts_per_page' => -1,
569
			'include'        => $gallery_ids
570
		) );
571
	}
572
573
	return array();
574
}
575
576
/**
577
 * Clears all css load optimization post meta
578
 */
579
function foogallery_clear_all_css_load_optimizations() {
580
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
581
}
582
583
/**
584
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
585
 */
586
function foogallery_perform_version_check() {
587
	$checker = new FooGallery_Version_Check();
588
	$checker->perform_check();
589
}
590
591
/**
592
 * Returns the JPEG quality used when generating thumbnails
593
 *
594
 * @return int The quality value stored in settings
595
 */
596
function foogallery_thumbnail_jpeg_quality() {
597
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
598
599
	//check if we get an invalid value for whatever reason and if so return a default of 80
600
	if ( $quality <= 0 ) {
601
		$quality = 80;
602
	}
603
604
	return $quality;
605
}
606
607
/**
608
 * Returns the caption title source setting
609
 *
610
 * @return string
611
 */
612
function foogallery_caption_title_source() {
613
	$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...
614
615
	if ( empty( $source ) ) {
616
		$source = 'caption';
617
	}
618
619
	return $source;
620
}
621
622
/**
623
 * Returns the attachment caption title based on the caption_title_source setting
624
 *
625
 * @param WP_Post $attachment_post
626
 * @param bool $source
627
 *
628
 * @return string
629
 */
630
function foogallery_get_caption_title_for_attachment($attachment_post, $source = false) {
631
	if ( false === $source ) {
632
		$source = foogallery_gallery_template_setting( 'caption_title_source', false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

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...
633
		if ( false === $source || "none" === $source ) {
634
			$source = foogallery_caption_title_source();
635
		}
636
	}
637
638
	switch ( $source ) {
639
		case 'title':
640
			$caption = trim( $attachment_post->post_title );
641
			break;
642
		case 'desc':
643
			$caption = trim( $attachment_post->post_content );
644
			break;
645
		case 'alt':
646
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
647
			break;
648
		default:
649
			$caption = trim( $attachment_post->post_excerpt );
650
	}
651
652
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
653
}
654
655
/**
656
 * Returns the caption description source setting
657
 *
658
 * @return string
659
 */
660
function foogallery_caption_desc_source() {
661
	$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...
662
663
	if ( empty( $source ) ) {
664
		$source = 'desc';
665
	}
666
667
	return $source;
668
}
669
670
/**
671
 * Returns the attachment caption description based on the caption_desc_source setting
672
 *
673
 * @param WP_Post $attachment_post
674
 * @param bool $source
675
 *
676
 * @return string
677
 */
678
function foogallery_get_caption_desc_for_attachment($attachment_post, $source = false) {
679
	if ( false === $source ) {
680
		$source = foogallery_gallery_template_setting( 'caption_desc_source', false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

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...
681
		if ( false === $source || "none" === $source ) {
682
			$source = foogallery_caption_desc_source();
683
		}
684
	}
685
686
	switch ( $source ) {
687
		case 'title':
688
			$caption = trim( $attachment_post->post_title );
689
			break;
690
		case 'caption':
691
			$caption = trim( $attachment_post->post_excerpt );
692
			break;
693
		case 'alt':
694
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
695
			break;
696
		default:
697
			$caption = trim( $attachment_post->post_content );
698
	}
699
700
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
701
}
702
703
/**
704
 * Runs thumbnail tests and outputs results in a table format
705
 */
706
function foogallery_output_thumbnail_generation_results() {
707
	$thumbs = new FooGallery_Thumbnails();
708
	try {
709
		$results = $thumbs->run_thumbnail_generation_tests();
710
        if ( $results['success'] ) {
711
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
712
        } else {
713
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
714
            var_dump( $results['error'] );
715
			var_dump( $results['file_info'] );
716
        }
717
	}
718
	catch (Exception $e) {
719
		echo 'Exception: ' . $e->getMessage();
720
	}
721
}
722
723
/**
724
 * Returns the URL to the test image
725
 *
726
 * @return string
727
 */
728
function foogallery_test_thumb_url() {
729
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/logo.png' );
730
}
731
732
/**
733
 * Return all the gallery datasources used within FooGallery
734
 *
735
 * @return array
736
 */
737
function foogallery_gallery_datasources() {
738
	$default_datasource = foogallery_default_datasource();
739
740
	$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...
741
742
	return apply_filters( 'foogallery_gallery_datasources', $datasources );
743
}
744
745
/**
746
 * Returns the default gallery datasource
747
 *
748
 * @return string
749
 */
750
function foogallery_default_datasource() {
751
	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...
752
}
753
754
/**
755
 * Instantiates a FooGallery datasource based on a datasource name
756
 *
757
 * @param $datasource_name string
758
 *
759
 * @return IFooGalleryDatasource
760
 */
761
function foogallery_instantiate_datasource( $datasource_name ) {
762
	$datasources = foogallery_gallery_datasources();
763
	if ( array_key_exists( $datasource_name, $datasources ) ) {
764
		return new $datasources[$datasource_name];
765
	}
766
767
	return new FooGalleryDatasource_MediaLibrary();
768
}
769
770
/**
771
 * Returns the src to the built-in image placeholder
772
 * @return string
773
 */
774
function foogallery_image_placeholder_src() {
775
	return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
776
}
777
778
/**
779
 * Returns the image html for the built-in image placeholder
780
 *
781
 * @param array $args
782
 *
783
 * @return string
784
 */
785
function foogallery_image_placeholder_html( $args ) {
786
	if ( !isset( $args ) ) {
787
		$args = array(
788
			'width' => 150,
789
			'height' => 150
790
		);
791
	}
792
793
	$args['src'] = foogallery_image_placeholder_src();
794
	$args = array_map( 'esc_attr', $args );
795
	$html = '<img ';
796
	foreach ( $args as $name => $value ) {
797
		$html .= " $name=" . '"' . $value . '"';
798
	}
799
	$html .= ' />';
800
	return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
801
}
802
803
/**
804
 * Returns the thumbnail html for the featured attachment for a gallery.
805
 * If no featured attachment can be found, then a placeholder image src is returned instead
806
 *
807
 * @param FooGallery $gallery
808
 * @param array $args
809
 *
810
 * @return string
811
 */
812
function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
813
    if ( !isset( $gallery ) || false === $gallery ) return '';
814
815
	if ( !isset( $args ) ) {
816
		$args = array(
817
			'width' => 150,
818
			'height' => 150
819
		);
820
	}
821
822
	$featuredAttachment = $gallery->featured_attachment();
823
	if ( $featuredAttachment ) {
824
		return $featuredAttachment->html_img( $args );
825
	} else {
826
		//if we have no featured attachment, then use the built-in image placeholder
827
		return foogallery_image_placeholder_html( $args );
828
	}
829
}
830
831
/**
832
 * Returns the thumbnail src for the featured attachment for a gallery.
833
 * If no featured attachment can be found, then a placeholder image src is returned instead
834
 *
835
 * @param FooGallery $gallery
836
 * @param array $args
837
 *
838
 * @return string
839
 */
840
function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
841
	if ( !isset( $gallery ) || false === $gallery ) return '';
842
843
	if ( !isset( $args ) ) {
844
		$args = array(
845
			'width' => 150,
846
			'height' => 150
847
		);
848
	}
849
850
	$featuredAttachment = $gallery->featured_attachment();
851
	if ( $featuredAttachment ) {
852
		return $featuredAttachment->html_img_src( $args );
853
	} else {
854
		//if we have no featured attachment, then use the built-in image placeholder
855
		return foogallery_image_placeholder_src();
856
	}
857
}
858
859
/**
860
 * Returns the available retina options that can be chosen
861
 */
862
function foogallery_retina_options() {
863
    return apply_filters( 'foogallery_retina_options', array(
864
        '2x' => __('2x', 'foogallery'),
865
        '3x' => __('3x', 'foogallery'),
866
        '4x' => __('4x', 'foogallery')
867
    ) );
868
}
869
870
/**
871
 * Does a full uninstall of the plugin including all data and settings!
872
 */
873
function foogallery_uninstall() {
874
875
	if ( !current_user_can( 'install_plugins' ) ) exit;
876
877
	//delete all gallery posts first
878
	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...
879
	$query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
880
	$gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
881
882
	if ( !empty( $gallery_post_ids ) ) {
883
		$deleted = 0;
884
		foreach ( $gallery_post_ids as $post_id ) {
885
			$del = wp_delete_post( $post_id );
886
			if ( false !== $del ) {
887
				++$deleted;
888
			}
889
		}
890
	}
891
892
	//delete all options
893
	if ( is_network_admin() ) {
894
		delete_site_option( FOOGALLERY_SLUG );
895
	} else {
896
		delete_option( FOOGALLERY_SLUG );
897
	}
898
	delete_option( FOOGALLERY_OPTION_VERSION );
899
	delete_option( FOOGALLERY_OPTION_THUMB_TEST );
900
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
901
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
902
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
903
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
904
	delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
905
	delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
906
907
	//let any extensions clean up after themselves
908
	do_action( 'foogallery_uninstall' );
909
}
910
911
/**
912
 * Returns an attachment field friendly name, based on a field name that is passed in
913
 *
914
 * @param $field
915
 *
916
 * @return string
917
 */
918
function foogallery_get_attachment_field_friendly_name( $field ) {
919
	switch ( $field ) {
920
		case 'title':
921
			return __( 'Attachment Title', 'foogallery' );
922
		case 'caption':
923
			return __( 'Attachment Caption', 'foogallery' );
924
		case 'desc':
925
			return __( 'Attachment Description', 'foogallery' );
926
		case 'alt':
927
			return __( 'Attachment Alt', 'foogallery' );
928
	}
929
}
930
931
/**
932
 * Returns the fields for a specific gallery template
933
 *
934
 * @param $template mixed
935
 * @return mixed
936
 */
937
function foogallery_get_fields_for_template( $template ) {
938
939
    if ( is_string( $template ) ) {
940
        $template = foogallery_get_gallery_template( $template );
941
    }
942
943
    $fields = $template['fields'];
944
945
    // Allow for extensions to override fields for every gallery template.
946
    // Also passes the $template along so you can inspect and conditionally alter fields based on the template properties
947
    $fields = apply_filters( 'foogallery_override_gallery_template_fields', $fields, $template );
948
949
    // Allow for extensions to override fields for a specific gallery template.
950
    // Also passes the $template along so you can inspect and conditionally alter fields based on the template properties
951
    $fields = apply_filters( "foogallery_override_gallery_template_fields-{$template['slug']}", $fields, $template );
952
953
    foreach ( $fields as &$field ) {
954
        //allow for the field to be altered by extensions. Also used by the build-in fields, e.g. lightbox
955
        $field = apply_filters( 'foogallery_alter_gallery_template_field', $field, $template['slug'] );
956
    }
957
958
    return $fields;
959
}
960
961
/**
962
 * Builds default settings for the supplied gallery template
963
 *
964
 * @param $template_name
965
 * @return array
966
 */
967
function foogallery_build_default_settings_for_gallery_template( $template_name ) {
968
    $fields = foogallery_get_fields_for_template( $template_name );
969
    $settings = array();
970
971
    //loop through the fields and build up an array of keys and default values
972
    foreach( $fields as $field ) {
973
        $default = array_key_exists( 'default', $field ) ? $field['default'] : false;
974
        if ( !empty( $default ) ) {
975
            $settings["{$template_name}_{$field['id']}"] = $default;
976
        }
977
    }
978
979
    return $settings;
980
}
981
982
/**
983
 * Returns the choices used for the thumb link field type
984
 * @return array
985
 */
986
function foogallery_gallery_template_field_thumb_link_choices() {
987
    return apply_filters( 'foogallery_gallery_template_field_thumb_links', array(
988
        'image'  => __( 'Full Size Image (Lightbox)', 'foogallery' ),
989
        'page'   => __( 'Image Attachment Page', 'foogallery' ),
990
        'custom' => __( 'Custom URL', 'foogallery' ),
991
        'none'   => __( 'Not linked', 'foogallery' ),
992
    ) );
993
}
994
995
/**
996
 * Returns the choices used for the lightbox field type
997
 * @return array
998
 */
999
function foogallery_gallery_template_field_lightbox_choices() {
1000
    $lightboxes = apply_filters( 'foogallery_gallery_template_field_lightboxes', array() );
1001
    $lightboxes['none'] = __( 'None', 'foogallery' );
1002
    return $lightboxes;
1003
}
1004
1005
1006
if ( !function_exists('wp_get_raw_referer') ) {
1007
	/**
1008
	 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
1009
	 *
1010
	 * Do not use for redirects, use {@see wp_get_referer()} instead.
1011
	 *
1012
	 * @since 1.4.9
1013
	 * @return string|false Referer URL on success, false on failure.
1014
	 */
1015
	function wp_get_raw_referer() {
0 ignored issues
show
Coding Style introduced by
wp_get_raw_referer uses the super-global variable $_REQUEST 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...
Coding Style introduced by
wp_get_raw_referer uses the super-global variable $_SERVER 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...
1016
		if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
1017
			return wp_unslash( $_REQUEST['_wp_http_referer'] );
1018
		} else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
1019
			return wp_unslash( $_SERVER['HTTP_REFERER'] );
1020
		}
1021
1022
		return false;
1023
	}
1024
}
1025
1026
/**
1027
 * Return the attachments for the currently displayed gallery
1028
 *
1029
 * @return array
1030
 */
1031
function foogallery_current_gallery_attachments_for_rendering() {
1032
    global $current_foogallery;
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...
1033
1034
    $attachments = apply_filters( 'foogallery_gallery_attachments_override_for_rendering', false, $current_foogallery );
1035
1036
    if ( $attachments !== false) {
1037
        return $attachments;
1038
    }
1039
1040
    //by default, return all attachments
1041
    return $current_foogallery->attachments();
1042
}