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... ( a56405...b693b9 )
by Brad
02:39
created

functions.php ➔ foogallery_build_container_data_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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