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... ( 6eed57 )
by Brad
02:17
created

functions.php ➔ foogallery_build_class_attribute_safe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
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 the FooGallery extension API class
31
 *
32
 * @return FooGallery_Extensions_API
33
 */
34
function foogallery_extensions_api() {
35
	return new FooGallery_Extensions_API();
36
}
37
38
/**
39
 * Returns the default gallery template
40
 *
41
 * @return string
42
 */
43
function foogallery_default_gallery_template() {
44
	return foogallery_get_setting( 'gallery_template' );
45
}
46
47
/**
48
 * Returns if gallery permalinks are enabled
49
 *
50
 * @return bool
51
 */
52
function foogallery_permalinks_enabled() {
53
	return foogallery_get_setting( 'gallery_permalinks_enabled' );
54
}
55
56
/**
57
 * Returns the gallery permalink
58
 *
59
 * @return string
60
 */
61
function foogallery_permalink() {
62
	return foogallery_get_setting( 'gallery_permalink' );
63
}
64
65
/**
66
 * Return the FooGallery saved setting, or a default value
67
 *
68
 * @param string $key The key for the setting
69
 *
70
 * @param bool $default The default if no value is saved or found
71
 *
72
 * @return mixed
73
 */
74
function foogallery_get_setting( $key, $default = false ) {
75
	$foogallery = FooGallery_Plugin::get_instance();
76
77
	return $foogallery->options()->get( $key, foogallery_get_default( $key, $default ) );
78
}
79
80
/**
81
 * Builds up a FooGallery gallery shortcode
82
 *
83
 * @param $gallery_id
84
 *
85
 * @return string
86
 */
87
function foogallery_build_gallery_shortcode( $gallery_id ) {
88
	return '[' . foogallery_gallery_shortcode_tag() . ' id="' . $gallery_id . '"]';
89
}
90
91
/**
92
 * Returns the gallery shortcode tag
93
 *
94
 * @return string
95
 */
96
function foogallery_gallery_shortcode_tag() {
97
	return apply_filters( 'foogallery_gallery_shortcode_tag', FOOGALLERY_CPT_GALLERY );
98
}
99
100
/**
101
 * Helper method for getting default settings
102
 *
103
 * @param string $key The default config key to retrieve.
104
 *
105
 * @param bool $default The default if no default is set or found
106
 *
107
 * @return string Key value on success, false on failure.
108
 */
109
function foogallery_get_default( $key, $default = false ) {
110
111
	$defaults = array(
112
		'gallery_template'           => 'default',
113
		'gallery_permalinks_enabled' => false,
114
		'gallery_permalink'          => 'gallery',
115
		'lightbox'                   => 'none',
116
		'thumb_jpeg_quality'         => '80',
117
		'thumb_resize_animations'    => true,
118
		'gallery_sorting'            => '',
119
		'datasource'				 => 'media_library'
120
	);
121
122
	// A handy filter to override the defaults
123
	$defaults = apply_filters( 'foogallery_defaults', $defaults );
124
125
	// Return the key specified.
126
	return isset($defaults[ $key ]) ? $defaults[ $key ] : $default;
127
}
128
129
/**
130
 * Returns the FooGallery Add Gallery Url within the admin
131
 *
132
 * @return string The Url to the FooGallery Add Gallery page in admin
133
 */
134
function foogallery_admin_add_gallery_url() {
135
	return admin_url( 'post-new.php?post_type=' . FOOGALLERY_CPT_GALLERY );
136
}
137
138
/**
139
 * Returns the FooGallery help page Url within the admin
140
 *
141
 * @return string The Url to the FooGallery help page in admin
142
 */
143
function foogallery_admin_help_url() {
144
	return admin_url( add_query_arg( array( 'page' => 'foogallery-help' ), foogallery_admin_menu_parent_slug() ) );
145
}
146
147
/**
148
 * Returns the FooGallery settings page Url within the admin
149
 *
150
 * @return string The Url to the FooGallery settings page in admin
151
 */
152
function foogallery_admin_settings_url() {
153
	return admin_url( add_query_arg( array( 'page' => 'foogallery-settings' ), foogallery_admin_menu_parent_slug() ) );
154
}
155
156
/**
157
 * Returns the FooGallery extensions page Url within the admin
158
 *
159
 * @return string The Url to the FooGallery extensions page in admin
160
 */
161
function foogallery_admin_extensions_url() {
162
	return admin_url( add_query_arg( array( 'page' => 'foogallery-extensions' ), foogallery_admin_menu_parent_slug() ) );
163
}
164
165
/**
166
 * Returns the FooGallery system info page Url within the admin
167
 *
168
 * @return string The Url to the FooGallery system info page in admin
169
 */
170
function foogallery_admin_systeminfo_url() {
171
	return admin_url( add_query_arg( array( 'page' => 'foogallery-systeminfo' ), foogallery_admin_menu_parent_slug() ) );
172
}
173
174
/**
175
 * Get a foogallery template setting for the current foogallery that is being output to the frontend
176
 * @param string	$key
177
 * @param string	$default
178
 *
179
 * @return bool
180
 */
181
function foogallery_gallery_template_setting( $key, $default = '' ) {
182
	global $current_foogallery;
183
	global $current_foogallery_arguments;
184
	global $current_foogallery_template;
185
186
	$settings_key = "{$current_foogallery_template}_{$key}";
187
188
	if ( $current_foogallery_arguments && array_key_exists( $key, $current_foogallery_arguments ) ) {
189
		//try to get the value from the arguments
190
		$value = $current_foogallery_arguments[ $key ];
191
192
	} else if ( !empty( $current_foogallery ) && $current_foogallery->settings && array_key_exists( $settings_key, $current_foogallery->settings ) ) {
193
		//then get the value out of the saved gallery settings
194
		$value = $current_foogallery->settings[ $settings_key ];
195
	} else {
196
		//otherwise set it to the default
197
		$value = $default;
198
	}
199
200
	$value = apply_filters( 'foogallery_gallery_template_setting-' . $key, $value );
201
202
	return $value;
203
}
204
205
/**
206
 * Get the admin menu parent slug
207
 * @return string
208
 */
209
function foogallery_admin_menu_parent_slug() {
210
	return apply_filters( 'foogallery_admin_menu_parent_slug', FOOGALLERY_ADMIN_MENU_PARENT_SLUG );
211
}
212
213
/**
214
 * Helper function to build up the admin menu Url
215
 * @param array $extra_args
216
 *
217
 * @return string|void
218
 */
219
function foogallery_build_admin_menu_url( $extra_args = array() ) {
220
	$url = admin_url( foogallery_admin_menu_parent_slug() );
221
	if ( ! empty( $extra_args ) ) {
222
		$url = add_query_arg( $extra_args, $url );
223
	}
224
	return $url;
225
}
226
227
/**
228
 * Helper function for adding a foogallery sub menu
229
 *
230
 * @param $menu_title
231
 * @param string $capability
232
 * @param string $menu_slug
233
 * @param $function
234
 */
235
function foogallery_add_submenu_page( $menu_title, $capability, $menu_slug, $function ) {
236
	add_submenu_page(
237
		foogallery_admin_menu_parent_slug(),
238
		$menu_title,
239
		$menu_title,
240
		$capability,
241
		$menu_slug,
242
		$function
243
	);
244
}
245
246
/**
247
 * Returns all FooGallery galleries
248
 *
249
 * @return FooGallery[] array of FooGallery galleries
250
 */
251
function foogallery_get_all_galleries( $excludes = false ) {
252
	$args = array(
253
		'post_type'     => FOOGALLERY_CPT_GALLERY,
254
		'post_status'	=> array( 'publish', 'draft' ),
255
		'cache_results' => false,
256
		'nopaging'      => true,
257
	);
258
259
	if ( is_array( $excludes ) ) {
260
		$args['post__not_in'] = $excludes;
261
	}
262
263
	$gallery_posts = get_posts( $args );
264
265
	if ( empty( $gallery_posts ) ) {
266
		return array();
267
	}
268
269
	$galleries = array();
270
271
	foreach ( $gallery_posts as $post ) {
272
		$galleries[] = FooGallery::get( $post );
273
	}
274
275
	return $galleries;
276
}
277
278
/**
279
 * Parse some content and return an array of all gallery shortcodes that are used inside it
280
 *
281
 * @param $content The content to search for gallery shortcodes
282
 *
283
 * @return array An array of all the foogallery shortcodes found in the content
284
 */
285
function foogallery_extract_gallery_shortcodes( $content ) {
286
	$shortcodes = array();
287
288
	$regex_pattern = foogallery_gallery_shortcode_regex();
289
	if ( preg_match_all( '/' . $regex_pattern . '/s', $content, $matches ) ) {
290
		for ( $i = 0; $i < count( $matches[0] ); ++$i ) {
291
			$shortcode = $matches[0][$i];
292
			$args = $matches[3][$i];
293
			$attribure_string = str_replace( ' ', '&', trim( $args ) );
294
			$attribure_string = str_replace( '"', '', $attribure_string );
295
			$attributes = wp_parse_args( $attribure_string );
296
			if ( array_key_exists( 'id', $attributes ) ) {
297
				$id = intval( $attributes['id'] );
298
				$shortcodes[ $id ] = $shortcode;
299
			}
300
		}
301
	}
302
303
	return $shortcodes;
304
}
305
306
/**
307
 * Build up the FooGallery shortcode regex
308
 *
309
 * @return string
310
 */
311
function foogallery_gallery_shortcode_regex() {
312
	$tag = foogallery_gallery_shortcode_tag();
313
314
	return
315
		'\\['                              	 // Opening bracket
316
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
317
		. "($tag)"                     		 // 2: Shortcode name
318
		. '(?![\\w-])'                       // Not followed by word character or hyphen
319
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
320
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
321
		.     '(?:'
322
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
323
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
324
		.     ')*?'
325
		. ')'
326
		. '(?:'
327
		.     '(\\/)'                        // 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...
328
		.     '\\]'                          // ... and closing bracket
329
		. '|'
330
		.     '\\]'                          // Closing bracket
331
		.     '(?:'
332
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
333
		.             '[^\\[]*+'             // Not an opening bracket
334
		.             '(?:'
335
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
336
		.                 '[^\\[]*+'         // Not an opening bracket
337
		.             ')*+'
338
		.         ')'
339
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
340
		.     ')?'
341
		. ')'
342
		. '(\\]?)';                          // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
343
}
344
345
/**
346
 * Builds up a class attribute that can be used in a gallery template
347
 * @param $gallery FooGallery
348
 *
349
 * @return string the classname based on the gallery and any extra attributes
350
 */
351
function foogallery_build_class_attribute( $gallery ) {
352
	$classes[] = 'foogallery-container';
353
	$classes[] = "foogallery-{$gallery->gallery_template}";
354
355
	//get some default classes from common gallery settings
356
	$border_style = $gallery->get_meta( "{$gallery->gallery_template}_border-style", '' );
357
	$hover_effect_type = $gallery->get_meta( "{$gallery->gallery_template}_hover-effect-type", '' );
358
	$hover_icon = $gallery->get_meta( "{$gallery->gallery_template}_hover-effect", '' );
359
	$caption_hover_effect = $gallery->get_meta( "{$gallery->gallery_template}_caption-hover-effect", 'hover-caption-simple' );
360
	//$caption_content = $gallery->get_meta( "{$gallery->gallery_template}_caption-content", 'title' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
361
	if ( 'hover-effect-caption' === $hover_effect_type || 'hover-effect-none' === $hover_effect_type ) {
362
		$hover_icon = '';
363
	}
364
	$classes[] = $border_style;
365
	$classes[] = $hover_effect_type;
366
	$classes[] = $hover_icon;
367
	$classes[] = $caption_hover_effect;
368
	//$classes[] = $caption_content;
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
369
370
	$num_args = func_num_args();
371
372
	if ( $num_args > 1 ) {
373
		$arg_list = func_get_args();
374
		for ( $i = 1; $i < $num_args; $i++ ) {
375
			$classes[] = $arg_list[$i];
376
		}
377
	}
378
379
	$classes = apply_filters( 'foogallery_build_class_attribute', $classes, $gallery );
380
381
	return implode( ' ', $classes );
382
}
383
384
/**
385
 * Builds up a SAFE class attribute that can be used in a gallery template
386
 * @param $gallery FooGallery
387
 *
388
 * @return string the classname based on the gallery and any extra attributes
389
 */
390
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...
391
	$args = func_get_args();
392
	$result = call_user_func_array("foogallery_build_class_attribute", $args);
393
	return esc_attr( $result );
394
}
395
396
/**
397
 * Renders an escaped class attribute that can be used directly by gallery templates
398
 *
399
 * @param $gallery FooGallery
400
 */
401
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...
402
	$args = func_get_args();
403
	$result = call_user_func_array("foogallery_build_class_attribute_safe", $args);
404
	echo $result;
405
}
406
407
/**
408
 * Builds up the attributes that are appended to a gallery template container
409
 *
410
 * @param $gallery    FooGallery
411
 * @param $attributes array
412
 *
413
 * @return string
414
 */
415
function foogallery_build_container_attributes_safe( $gallery, $attributes ) {
416
417
	//add the default gallery id
418
	$attributes['id'] = 'foogallery-gallery-' . $gallery->ID;
419
420
	//allow others to add their own attributes globally
421
	$attributes = apply_filters( 'foogallery_build_container_attributes', $attributes, $gallery );
422
423
	//allow others to add their own attributes for a specific gallery template
424
	$attributes = apply_filters( 'foogallery_build_container_attributes-' . $gallery->gallery_template, $attributes, $gallery );
425
426
	//clean up the attributes to make them safe for output
427
	$html = '';
428
	foreach( $attributes as $key=>$value) {
429
		$safe_value = esc_attr( $value );
430
		$html .= "{$key}=\"{$safe_value}\" ";
431
	}
432
433
	return $html;
434
}
435
436
/**
437
 * Render a foogallery
438
 *
439
 * @param $gallery_id int The id of the foogallery you want to render
440
 */
441
function foogallery_render_gallery( $gallery_id ) {
442
	//create new instance of template engine
443
	$engine = new FooGallery_Template_Loader();
444
445
	$engine->render_template( array(
446
		'id' => $gallery_id
447
	) );
448
}
449
450
/**
451
 * Returns the available sorting options that can be chosen for galleries and albums
452
 */
453
function foogallery_sorting_options() {
454
	return apply_filters( 'foogallery_sorting_options', array(
455
		'' => __('Default', 'foogallery'),
456
		'date_desc' => __('Date created - newest first', 'foogallery'),
457
		'date_asc' => __('Date created - oldest first', 'foogallery'),
458
		'modified_desc' => __('Date modified - most recent first', 'foogallery'),
459
		'modified_asc' => __('Date modified - most recent last', 'foogallery'),
460
		'title_asc' => __('Title - alphabetically', 'foogallery'),
461
		'title_desc' => __('Title - reverse', 'foogallery'),
462
		'rand' => __('Random', 'foogallery')
463
	) );
464
}
465
466
function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
467
	$orderby_arg = 'post__in';
468
469
	switch ( $sorting_option ) {
470
		case 'date_desc':
471
		case 'date_asc':
472
			$orderby_arg = 'date';
473
			break;
474
		case 'modified_desc':
475
		case 'modified_asc':
476
			$orderby_arg = 'modified';
477
			break;
478
		case 'title_asc':
479
		case 'title_desc':
480
			$orderby_arg = 'title';
481
			break;
482
		case 'rand':
483
			$orderby_arg = 'rand';
484
			break;
485
	}
486
487
	return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
488
}
489
490
function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
491
	$order_arg = 'DESC';
492
493
	switch ( $sorting_option ) {
494
		case 'date_asc':
495
		case 'modified_asc':
496
		case 'title_asc':
497
		$order_arg = 'ASC';
498
			break;
499
	}
500
501
	return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
502
}
503
504
/**
505
 * Activate the default templates extension when there are no gallery templates loaded
506
 */
507
function foogallery_activate_default_templates_extension() {
508
	$api = foogallery_extensions_api();
509
	$api->activate( 'default_templates' );
510
}
511
512
/**
513
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
514
 *
515
 * @param $handle string
516
 * @param $src string
517
 * @param array $deps
518
 * @param bool $ver
519
 * @param string $media
520
 */
521
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
522
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
523
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
524
}
525
526
527
/**
528
 * Returns all foogallery post objects that are attached to the post
529
 *
530
 * @param $post_id int The ID of the post
531
 *
532
 * @return array List of foogallery posts.
533
 */
534
function foogallery_get_galleries_attached_to_post( $post_id ) {
535
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
536
537
	if ( !empty( $gallery_ids ) ) {
538
		return get_posts( array(
539
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
540
			'post_status'    => array( 'draft', 'publish' ),
541
			'posts_per_page' => -1,
542
			'include'        => $gallery_ids
543
		) );
544
	}
545
546
	return array();
547
}
548
549
/**
550
 * Clears all css load optimization post meta
551
 */
552
function foogallery_clear_all_css_load_optimizations() {
553
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
554
}
555
556
/**
557
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
558
 */
559
function foogallery_perform_version_check() {
560
	$checker = new FooGallery_Version_Check();
561
	$checker->perform_check();
562
}
563
564
/**
565
 * Returns the JPEG quality used when generating thumbnails
566
 *
567
 * @return int The quality value stored in settings
568
 */
569
function foogallery_thumbnail_jpeg_quality() {
570
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
571
572
	//check if we get an invalid value for whatever reason and if so return a default of 80
573
	if ( $quality <= 0 ) {
574
		$quality = 80;
575
	}
576
577
	return $quality;
578
}
579
580
/**
581
 * Returns the caption title source setting
582
 *
583
 * @return string
584
 */
585
function foogallery_caption_title_source() {
586
	$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...
587
588
	if ( empty( $source ) ) {
589
		$source = 'caption';
590
	}
591
592
	return $source;
593
}
594
595
/**
596
 * Returns the attachment caption title based on the caption_title_source setting
597
 *
598
 * @param $attachment_post WP_Post
599
 *
600
 * @return string
601
 */
602
function foogallery_get_caption_title_for_attachment($attachment_post) {
603
	$source = foogallery_caption_title_source();
604
605
	if ( 'title' === $source ) {
606
		$caption = trim( $attachment_post->post_title );
607
	} else {
608
		$caption = trim( $attachment_post->post_excerpt );
609
	}
610
611
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
612
}
613
614
/**
615
 * Returns the caption description source setting
616
 *
617
 * @return string
618
 */
619
function foogallery_caption_desc_source() {
620
	$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...
621
622
	if ( empty( $source ) ) {
623
		$source = 'desc';
624
	}
625
626
	return $source;
627
}
628
629
/**
630
 * Returns the attachment caption description based on the caption_desc_source setting
631
 *
632
 * @param $attachment_post WP_Post
633
 *
634
 * @return string
635
 */
636
function foogallery_get_caption_desc_for_attachment($attachment_post) {
637
	$source = foogallery_caption_desc_source();
638
639
	switch ( $source ) {
640
		case 'title':
641
			$caption = trim( $attachment_post->post_title );
642
			break;
643
		case 'caption':
644
			$caption = trim( $attachment_post->post_excerpt );
645
			break;
646
		case 'alt':
647
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
648
			break;
649
		default:
650
			$caption = trim( $attachment_post->post_content );
651
	}
652
653
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
654
}
655
656
/**
657
 * Runs thumbnail tests and outputs results in a table format
658
 */
659
function foogallery_output_thumbnail_generation_results() {
660
	$thumbs = new FooGallery_Thumbnails();
661
	try {
662
		$results = $thumbs->run_thumbnail_generation_tests();
663
        if ( $results['success'] ) {
664
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
665
        } else {
666
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
667
            var_dump( $results['error'] );
668
			var_dump( $results['file_info'] );
669
        }
670
	}
671
	catch (Exception $e) {
672
		echo 'Exception: ' . $e->getMessage();
673
	}
674
}
675
676
/**
677
 * Returns the URL to the test image
678
 *
679
 * @return string
680
 */
681
function foogallery_test_thumb_url() {
682
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/test_thumb_1.jpg' );
683
}
684
685
/**
686
 * Return all the gallery datasources used within FooGallery
687
 *
688
 * @return array
689
 */
690
function foogallery_gallery_datasources() {
691
	$default_datasource = foogallery_default_datasource();
692
693
	$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...
694
695
	return apply_filters( 'foogallery_gallery_datasources', $datasources );
696
}
697
698
/**
699
 * Returns the default gallery datasource
700
 *
701
 * @return string
702
 */
703
function foogallery_default_datasource() {
704
	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...
705
}
706
707
/**
708
 * Instantiates a FooGallery datasource based on a datasource name
709
 *
710
 * @param $datasource_name string
711
 *
712
 * @return IFooGalleryDatasource
713
 */
714
function foogallery_instantiate_datasource( $datasource_name ) {
715
	$datasources = foogallery_gallery_datasources();
716
	if ( array_key_exists( $datasource_name, $datasources ) ) {
717
		return new $datasources[$datasource_name];
718
	}
719
720
	return new FooGalleryDatasource_MediaLibrary();
721
}
722
723
/**
724
 * Returns the src to the built-in image placeholder
725
 * @return string
726
 */
727
function foogallery_image_placeholder_src() {
728
	return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
729
}
730
731
/**
732
 * Returns the image html for the built-in image placeholder
733
 *
734
 * @param array $args
735
 *
736
 * @return string
737
 */
738
function foogallery_image_placeholder_html( $args ) {
739
	if ( !isset( $args ) ) {
740
		$args = array(
741
			'width' => 150,
742
			'height' => 150
743
		);
744
	}
745
746
	$args['src'] = foogallery_image_placeholder_src();
747
	$args = array_map( 'esc_attr', $args );
748
	$html = '<img ';
749
	foreach ( $args as $name => $value ) {
750
		$html .= " $name=" . '"' . $value . '"';
751
	}
752
	$html .= ' />';
753
	return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
754
}
755
756
/**
757
 * Returns the thumbnail html for the featured attachment for a gallery.
758
 * If no featured attachment can be found, then a placeholder image src is returned instead
759
 *
760
 * @param FooGallery $gallery
761
 * @param array $args
762
 *
763
 * @return string
764
 */
765
function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
766
	if ( !isset( $gallery ) ) return '';
767
768
	if ( !isset( $args ) ) {
769
		$args = array(
770
			'width' => 150,
771
			'height' => 150
772
		);
773
	}
774
775
	$featuredAttachment = $gallery->featured_attachment();
776
	if ( $featuredAttachment ) {
777
		return $featuredAttachment->html_img( $args );
778
	} else {
779
		//if we have no featured attachment, then use the built-in image placeholder
780
		return foogallery_image_placeholder_html( $args );
781
	}
782
}
783
784
/**
785
 * Returns the thumbnail src for the featured attachment for a gallery.
786
 * If no featured attachment can be found, then a placeholder image src is returned instead
787
 *
788
 * @param FooGallery $gallery
789
 * @param array $args
790
 *
791
 * @return string
792
 */
793
function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
794
	if ( !isset( $gallery ) ) return '';
795
796
	if ( !isset( $args ) ) {
797
		$args = array(
798
			'width' => 150,
799
			'height' => 150
800
		);
801
	}
802
803
	$featuredAttachment = $gallery->featured_attachment();
804
	if ( $featuredAttachment ) {
805
		return $featuredAttachment->html_img_src( $args );
806
	} else {
807
		//if we have no featured attachment, then use the built-in image placeholder
808
		return foogallery_image_placeholder_src();
809
	}
810
}
811
812
/**
813
 * Returns the available retina options that can be chosen
814
 */
815
function foogallery_retina_options() {
816
    return apply_filters( 'foogallery_retina_options', array(
817
        '2x' => __('2x', 'foogallery'),
818
        '3x' => __('3x', 'foogallery'),
819
        '4x' => __('4x', 'foogallery')
820
    ) );
821
}
822
823
/**
824
 * Does a full uninstall of the plugin including all data and settings!
825
 */
826
function foogallery_uninstall() {
827
828
	if ( !current_user_can( 'install_plugins' ) ) exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function foogallery_uninstall() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
829
830
	//delete all gallery posts first
831
	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...
832
	$query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
833
	$gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
834
835
	if ( !empty( $gallery_post_ids ) ) {
836
		$deleted = 0;
837
		foreach ( $gallery_post_ids as $post_id ) {
838
			$del = wp_delete_post( $post_id );
839
			if ( false !== $del ) {
840
				++$deleted;
841
			}
842
		}
843
	}
844
845
	//delete all options
846
	if ( is_network_admin() ) {
847
		delete_site_option( FOOGALLERY_SLUG );
848
	} else {
849
		delete_option( FOOGALLERY_SLUG );
850
	}
851
	delete_option( FOOGALLERY_OPTION_VERSION );
852
	delete_option( FOOGALLERY_OPTION_THUMB_TEST );
853
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
854
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
855
	delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
856
	delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
857
	delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
858
	delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
859
860
	//let any extensions clean up after themselves
861
	do_action( 'foogallery_uninstall' );
862
}