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... ( 8f3dfe...76b14a )
by Brad
07:12
created

functions.php ➔ foogallery_get_caption_desc_for_attachment()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 8
nop 2
dl 0
loc 21
rs 8.7624
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[] = "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
 * 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 WP_Post $attachment_post
624
 * @param bool $source
625
 *
626
 * @return string
627
 */
628
function foogallery_get_caption_title_for_attachment($attachment_post, $source = false) {
629
	if ( false === $source ) {
630
		$source = foogallery_caption_title_source();
631
	}
632
633
	switch ( $source ) {
634
		case 'title':
635
			$caption = trim( $attachment_post->post_title );
636
			break;
637
		case 'desc':
638
			$caption = trim( $attachment_post->post_content );
639
			break;
640
		case 'alt':
641
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
642
			break;
643
		default:
644
			$caption = trim( $attachment_post->post_excerpt );
645
	}
646
647
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
648
}
649
650
/**
651
 * Returns the caption description source setting
652
 *
653
 * @return string
654
 */
655
function foogallery_caption_desc_source() {
656
	$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...
657
658
	if ( empty( $source ) ) {
659
		$source = 'desc';
660
	}
661
662
	return $source;
663
}
664
665
/**
666
 * Returns the attachment caption description based on the caption_desc_source setting
667
 *
668
 * @param WP_Post $attachment_post
669
 * @param bool $source
670
 *
671
 * @return string
672
 */
673
function foogallery_get_caption_desc_for_attachment($attachment_post, $source = false) {
674
	if ( false === $source ) {
675
		$source = foogallery_caption_desc_source();
676
	}
677
678
	switch ( $source ) {
679
		case 'title':
680
			$caption = trim( $attachment_post->post_title );
681
			break;
682
		case 'caption':
683
			$caption = trim( $attachment_post->post_excerpt );
684
			break;
685
		case 'alt':
686
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
687
			break;
688
		default:
689
			$caption = trim( $attachment_post->post_content );
690
	}
691
692
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
693
}
694
695
/**
696
 * Runs thumbnail tests and outputs results in a table format
697
 */
698
function foogallery_output_thumbnail_generation_results() {
699
	$thumbs = new FooGallery_Thumbnails();
700
	try {
701
		$results = $thumbs->run_thumbnail_generation_tests();
702
        if ( $results['success'] ) {
703
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
704
        } else {
705
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
706
            var_dump( $results['error'] );
707
			var_dump( $results['file_info'] );
708
        }
709
	}
710
	catch (Exception $e) {
711
		echo 'Exception: ' . $e->getMessage();
712
	}
713
}
714
715
/**
716
 * Returns the URL to the test image
717
 *
718
 * @return string
719
 */
720
function foogallery_test_thumb_url() {
721
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/test_thumb_1.jpg' );
722
}
723
724
/**
725
 * Return all the gallery datasources used within FooGallery
726
 *
727
 * @return array
728
 */
729
function foogallery_gallery_datasources() {
730
	$default_datasource = foogallery_default_datasource();
731
732
	$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...
733
734
	return apply_filters( 'foogallery_gallery_datasources', $datasources );
735
}
736
737
/**
738
 * Returns the default gallery datasource
739
 *
740
 * @return string
741
 */
742
function foogallery_default_datasource() {
743
	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...
744
}
745
746
/**
747
 * Instantiates a FooGallery datasource based on a datasource name
748
 *
749
 * @param $datasource_name string
750
 *
751
 * @return IFooGalleryDatasource
752
 */
753
function foogallery_instantiate_datasource( $datasource_name ) {
754
	$datasources = foogallery_gallery_datasources();
755
	if ( array_key_exists( $datasource_name, $datasources ) ) {
756
		return new $datasources[$datasource_name];
757
	}
758
759
	return new FooGalleryDatasource_MediaLibrary();
760
}
761
762
/**
763
 * Returns the src to the built-in image placeholder
764
 * @return string
765
 */
766
function foogallery_image_placeholder_src() {
767
	return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
768
}
769
770
/**
771
 * Returns the image html for the built-in image placeholder
772
 *
773
 * @param array $args
774
 *
775
 * @return string
776
 */
777
function foogallery_image_placeholder_html( $args ) {
778
	if ( !isset( $args ) ) {
779
		$args = array(
780
			'width' => 150,
781
			'height' => 150
782
		);
783
	}
784
785
	$args['src'] = foogallery_image_placeholder_src();
786
	$args = array_map( 'esc_attr', $args );
787
	$html = '<img ';
788
	foreach ( $args as $name => $value ) {
789
		$html .= " $name=" . '"' . $value . '"';
790
	}
791
	$html .= ' />';
792
	return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
793
}
794
795
/**
796
 * Returns the thumbnail html for the featured attachment for a gallery.
797
 * If no featured attachment can be found, then a placeholder image src is returned instead
798
 *
799
 * @param FooGallery $gallery
800
 * @param array $args
801
 *
802
 * @return string
803
 */
804
function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
805
	if ( !isset( $gallery ) ) return '';
806
807
	if ( !isset( $args ) ) {
808
		$args = array(
809
			'width' => 150,
810
			'height' => 150
811
		);
812
	}
813
814
	$featuredAttachment = $gallery->featured_attachment();
815
	if ( $featuredAttachment ) {
816
		return $featuredAttachment->html_img( $args );
817
	} else {
818
		//if we have no featured attachment, then use the built-in image placeholder
819
		return foogallery_image_placeholder_html( $args );
820
	}
821
}
822
823
/**
824
 * Returns the thumbnail src for the featured attachment for a gallery.
825
 * If no featured attachment can be found, then a placeholder image src is returned instead
826
 *
827
 * @param FooGallery $gallery
828
 * @param array $args
829
 *
830
 * @return string
831
 */
832
function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
833
	if ( !isset( $gallery ) ) return '';
834
835
	if ( !isset( $args ) ) {
836
		$args = array(
837
			'width' => 150,
838
			'height' => 150
839
		);
840
	}
841
842
	$featuredAttachment = $gallery->featured_attachment();
843
	if ( $featuredAttachment ) {
844
		return $featuredAttachment->html_img_src( $args );
845
	} else {
846
		//if we have no featured attachment, then use the built-in image placeholder
847
		return foogallery_image_placeholder_src();
848
	}
849
}
850
851
/**
852
 * Returns the available retina options that can be chosen
853
 */
854
function foogallery_retina_options() {
855
    return apply_filters( 'foogallery_retina_options', array(
856
        '2x' => __('2x', 'foogallery'),
857
        '3x' => __('3x', 'foogallery'),
858
        '4x' => __('4x', 'foogallery')
859
    ) );
860
}
861
862
/**
863
 * Does a full uninstall of the plugin including all data and settings!
864
 */
865
function foogallery_uninstall() {
866
867
	if ( !current_user_can( 'install_plugins' ) ) exit;
868
869
	//delete all gallery posts first
870
	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...
871
	$query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
872
	$gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
873
874
	if ( !empty( $gallery_post_ids ) ) {
875
		$deleted = 0;
876
		foreach ( $gallery_post_ids as $post_id ) {
877
			$del = wp_delete_post( $post_id );
878
			if ( false !== $del ) {
879
				++$deleted;
880
			}
881
		}
882
	}
883
884
	//delete all options
885
	if ( is_network_admin() ) {
886
		delete_site_option( FOOGALLERY_SLUG );
887
	} else {
888
		delete_option( FOOGALLERY_SLUG );
889
	}
890
	delete_option( FOOGALLERY_OPTION_VERSION );
891
	delete_option( FOOGALLERY_OPTION_THUMB_TEST );
892
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
893
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
894
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
895
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
896
	delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
897
	delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
898
899
	//let any extensions clean up after themselves
900
	do_action( 'foogallery_uninstall' );
901
}
902
903
/**
904
 * Returns an attachment field friendly name, based on a field name that is passed in
905
 *
906
 * @param $field
907
 *
908
 * @return string
909
 */
910
function foogallery_get_attachment_field_friendly_name( $field ) {
911
	switch ( $field ) {
912
		case 'title':
913
			return __( 'Attachment Title', 'foogallery' );
914
		case 'caption':
915
			return __( 'Attachment Caption', 'foogallery' );
916
		case 'desc':
917
			return __( 'Attachment Description', 'foogallery' );
918
		case 'alt':
919
			return __( 'Attachment Alt', 'foogallery' );
920
	}
921
}