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/justified-gallery-imag... ( 5f22c3...a63c02 )
by Brad
11s
created

functions.php ➔ foogallery_output_thumbnail_generation_results()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 5
nop 0
dl 0
loc 15
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
	);
120
121
	// A handy filter to override the defaults
122
	$defaults = apply_filters( 'foogallery_defaults', $defaults );
123
124
	// Return the key specified.
125
	return isset($defaults[ $key ]) ? $defaults[ $key ] : $default;
126
}
127
128
/**
129
 * Returns the FooGallery Add Gallery Url within the admin
130
 *
131
 * @return string The Url to the FooGallery Add Gallery page in admin
132
 */
133
function foogallery_admin_add_gallery_url() {
134
	return admin_url( 'post-new.php?post_type=' . FOOGALLERY_CPT_GALLERY );
135
}
136
137
/**
138
 * Returns the FooGallery help page Url within the admin
139
 *
140
 * @return string The Url to the FooGallery help page in admin
141
 */
142
function foogallery_admin_help_url() {
143
	return admin_url( add_query_arg( array( 'page' => 'foogallery-help' ), foogallery_admin_menu_parent_slug() ) );
144
}
145
146
/**
147
 * Returns the FooGallery settings page Url within the admin
148
 *
149
 * @return string The Url to the FooGallery settings page in admin
150
 */
151
function foogallery_admin_settings_url() {
152
	return admin_url( add_query_arg( array( 'page' => 'foogallery-settings' ), foogallery_admin_menu_parent_slug() ) );
153
}
154
155
/**
156
 * Returns the FooGallery extensions page Url within the admin
157
 *
158
 * @return string The Url to the FooGallery extensions page in admin
159
 */
160
function foogallery_admin_extensions_url() {
161
	return admin_url( add_query_arg( array( 'page' => 'foogallery-extensions' ), foogallery_admin_menu_parent_slug() ) );
162
}
163
164
/**
165
 * Returns the FooGallery system info page Url within the admin
166
 *
167
 * @return string The Url to the FooGallery system info page in admin
168
 */
169
function foogallery_admin_systeminfo_url() {
170
	return admin_url( add_query_arg( array( 'page' => 'foogallery-systeminfo' ), foogallery_admin_menu_parent_slug() ) );
171
}
172
173
/**
174
 * Get a foogallery template setting for the current foogallery that is being output to the frontend
175
 * @param string	$key
176
 * @param string	$default
177
 *
178
 * @return bool
179
 */
180
function foogallery_gallery_template_setting( $key, $default = '' ) {
181
	global $current_foogallery;
182
	global $current_foogallery_arguments;
183
	global $current_foogallery_template;
184
185
	$settings_key = "{$current_foogallery_template}_{$key}";
186
187
	if ( $current_foogallery_arguments && array_key_exists( $key, $current_foogallery_arguments ) ) {
188
		//try to get the value from the arguments
189
		$value = $current_foogallery_arguments[ $key ];
190
191
	} else if ( !empty( $current_foogallery ) && $current_foogallery->settings && array_key_exists( $settings_key, $current_foogallery->settings ) ) {
192
		//then get the value out of the saved gallery settings
193
		$value = $current_foogallery->settings[ $settings_key ];
194
	} else {
195
		//otherwise set it to the default
196
		$value = $default;
197
	}
198
199
	$value = apply_filters( 'foogallery_gallery_template_setting-' . $key, $value );
200
201
	return $value;
202
}
203
204
/**
205
 * Get the admin menu parent slug
206
 * @return string
207
 */
208
function foogallery_admin_menu_parent_slug() {
209
	return apply_filters( 'foogallery_admin_menu_parent_slug', FOOGALLERY_ADMIN_MENU_PARENT_SLUG );
210
}
211
212
/**
213
 * Helper function to build up the admin menu Url
214
 * @param array $extra_args
215
 *
216
 * @return string|void
217
 */
218
function foogallery_build_admin_menu_url( $extra_args = array() ) {
219
	$url = admin_url( foogallery_admin_menu_parent_slug() );
220
	if ( ! empty( $extra_args ) ) {
221
		$url = add_query_arg( $extra_args, $url );
222
	}
223
	return $url;
224
}
225
226
/**
227
 * Helper function for adding a foogallery sub menu
228
 *
229
 * @param $menu_title
230
 * @param string $capability
231
 * @param string $menu_slug
232
 * @param $function
233
 */
234
function foogallery_add_submenu_page( $menu_title, $capability, $menu_slug, $function ) {
235
	add_submenu_page(
236
		foogallery_admin_menu_parent_slug(),
237
		$menu_title,
238
		$menu_title,
239
		$capability,
240
		$menu_slug,
241
		$function
242
	);
243
}
244
245
/**
246
 * Returns all FooGallery galleries
247
 *
248
 * @return FooGallery[] array of FooGallery galleries
249
 */
250
function foogallery_get_all_galleries( $excludes = false ) {
251
	$args = array(
252
		'post_type'     => FOOGALLERY_CPT_GALLERY,
253
		'post_status'	=> array( 'publish', 'draft' ),
254
		'cache_results' => false,
255
		'nopaging'      => true,
256
	);
257
258
	if ( is_array( $excludes ) ) {
259
		$args['post__not_in'] = $excludes;
260
	}
261
262
	$gallery_posts = get_posts( $args );
263
264
	if ( empty( $gallery_posts ) ) {
265
		return array();
266
	}
267
268
	$galleries = array();
269
270
	foreach ( $gallery_posts as $post ) {
271
		$galleries[] = FooGallery::get( $post );
272
	}
273
274
	return $galleries;
275
}
276
277
/**
278
 * Parse some content and return an array of all gallery shortcodes that are used inside it
279
 *
280
 * @param $content The content to search for gallery shortcodes
281
 *
282
 * @return array An array of all the foogallery shortcodes found in the content
283
 */
284
function foogallery_extract_gallery_shortcodes( $content ) {
285
	$shortcodes = array();
286
287
	$regex_pattern = foogallery_gallery_shortcode_regex();
288
	if ( preg_match_all( '/' . $regex_pattern . '/s', $content, $matches ) ) {
289
		for ( $i = 0; $i < count( $matches[0] ); ++$i ) {
290
			$shortcode = $matches[0][$i];
291
			$args = $matches[3][$i];
292
			$attribure_string = str_replace( ' ', '&', trim( $args ) );
293
			$attribure_string = str_replace( '"', '', $attribure_string );
294
			$attributes = wp_parse_args( $attribure_string );
295
			if ( array_key_exists( 'id', $attributes ) ) {
296
				$id = intval( $attributes['id'] );
297
				$shortcodes[ $id ] = $shortcode;
298
			}
299
		}
300
	}
301
302
	return $shortcodes;
303
}
304
305
/**
306
 * Build up the FooGallery shortcode regex
307
 *
308
 * @return string
309
 */
310
function foogallery_gallery_shortcode_regex() {
311
	$tag = foogallery_gallery_shortcode_tag();
312
313
	return
314
		'\\['                              	 // Opening bracket
315
		. '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
316
		. "($tag)"                     		 // 2: Shortcode name
317
		. '(?![\\w-])'                       // Not followed by word character or hyphen
318
		. '('                                // 3: Unroll the loop: Inside the opening shortcode tag
319
		.     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
320
		.     '(?:'
321
		.         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
322
		.         '[^\\]\\/]*'               // Not a closing bracket or forward slash
323
		.     ')*?'
324
		. ')'
325
		. '(?:'
326
		.     '(\\/)'                        // 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...
327
		.     '\\]'                          // ... and closing bracket
328
		. '|'
329
		.     '\\]'                          // Closing bracket
330
		.     '(?:'
331
		.         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
332
		.             '[^\\[]*+'             // Not an opening bracket
333
		.             '(?:'
334
		.                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
335
		.                 '[^\\[]*+'         // Not an opening bracket
336
		.             ')*+'
337
		.         ')'
338
		.         '\\[\\/\\2\\]'             // Closing shortcode tag
339
		.     ')?'
340
		. ')'
341
		. '(\\]?)';                          // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
342
}
343
344
/**
345
 * Builds up a class attribute that can be used in a gallery template
346
 * @param $gallery FooGallery
347
 *
348
 * @return string the classname based on the gallery and any extra attributes
349
 */
350
function foogallery_build_class_attribute( $gallery ) {
351
	$classes[] = 'foogallery-container';
352
	$classes[] = "foogallery-{$gallery->gallery_template}";
353
	$num_args = func_num_args();
354
355
	if ( $num_args > 1 ) {
356
		$arg_list = func_get_args();
357
		for ( $i = 1; $i < $num_args; $i++ ) {
358
			$classes[] = $arg_list[$i];
359
		}
360
	}
361
362
	$classes = apply_filters( 'foogallery_build_class_attribute', $classes );
363
364
	return implode( ' ', $classes );
365
}
366
367
/**
368
 * Renders an escaped class attribute that can be used directly by gallery templates
369
 *
370
 * @param $gallery FooGallery
371
 */
372
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...
373
	$args = func_get_args();
374
	$result = call_user_func_array("foogallery_build_class_attribute", $args);
375
	echo esc_attr( $result );
376
}
377
378
/**
379
 * Render a foogallery
380
 *
381
 * @param $gallery_id int The id of the foogallery you want to render
382
 */
383
function foogallery_render_gallery( $gallery_id ) {
384
	//create new instance of template engine
385
	$engine = new FooGallery_Template_Loader();
386
387
	$engine->render_template( array(
388
		'id' => $gallery_id
389
	) );
390
}
391
392
/**
393
 * Returns the available sorting options that can be chosen for galleries and albums
394
 */
395
function foogallery_sorting_options() {
396
	return apply_filters( 'foogallery_sorting_options', array(
397
		'' => __('Default', 'foogallery'),
398
		'date_desc' => __('Date created - newest first', 'foogallery'),
399
		'date_asc' => __('Date created - oldest first', 'foogallery'),
400
		'modified_desc' => __('Date modified - most recent first', 'foogallery'),
401
		'modified_asc' => __('Date modified - most recent last', 'foogallery'),
402
		'title_asc' => __('Title - alphabetically', 'foogallery'),
403
		'title_desc' => __('Title - reverse', 'foogallery'),
404
		'rand' => __('Random', 'foogallery')
405
	) );
406
}
407
408
function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
409
	$orderby_arg = 'post__in';
410
411
	switch ( $sorting_option ) {
412
		case 'date_desc':
413
		case 'date_asc':
414
			$orderby_arg = 'date';
415
			break;
416
		case 'modified_desc':
417
		case 'modified_asc':
418
			$orderby_arg = 'modified';
419
			break;
420
		case 'title_asc':
421
		case 'title_desc':
422
			$orderby_arg = 'title';
423
			break;
424
		case 'rand':
425
			$orderby_arg = 'rand';
426
			break;
427
	}
428
429
	return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
430
}
431
432
function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
433
	$order_arg = 'DESC';
434
435
	switch ( $sorting_option ) {
436
		case 'date_asc':
437
		case 'modified_asc':
438
		case 'title_asc':
439
		$order_arg = 'ASC';
440
			break;
441
	}
442
443
	return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
444
}
445
446
/**
447
 * Activate the default templates extension when there are no gallery templates loaded
448
 */
449
function foogallery_activate_default_templates_extension() {
450
	$api = foogallery_extensions_api();
451
	$api->activate( 'default_templates' );
452
}
453
454
/**
455
 * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
456
 *
457
 * @param $handle string
458
 * @param $src string
459
 * @param array $deps
460
 * @param bool $ver
461
 * @param string $media
462
 */
463
function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
464
	wp_enqueue_style( $handle, $src, $deps, $ver, $media );
465
	do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
466
}
467
468
469
/**
470
 * Returns all foogallery post objects that are attached to the post
471
 *
472
 * @param $post_id int The ID of the post
473
 *
474
 * @return array List of foogallery posts.
475
 */
476
function foogallery_get_galleries_attached_to_post( $post_id ) {
477
	$gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
478
479
	if ( !empty( $gallery_ids ) ) {
480
		return get_posts( array(
481
			'post_type'      => array( FOOGALLERY_CPT_GALLERY, ),
482
			'post_status'    => array( 'draft', 'publish' ),
483
			'posts_per_page' => -1,
484
			'include'        => $gallery_ids
485
		) );
486
	}
487
488
	return array();
489
}
490
491
/**
492
 * Clears all css load optimization post meta
493
 */
494
function foogallery_clear_all_css_load_optimizations() {
495
	delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
496
}
497
498
/**
499
 * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
500
 */
501
function foogallery_perform_version_check() {
502
	$checker = new FooGallery_Version_Check();
503
	$checker->perform_check();
504
}
505
506
/**
507
 * Returns the JPEG quality used when generating thumbnails
508
 *
509
 * @return int The quality value stored in settings
510
 */
511
function foogallery_thumbnail_jpeg_quality() {
512
	$quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
513
514
	//check if we get an invalid value for whatever reason and if so return a default of 80
515
	if ( $quality <= 0 ) {
516
		$quality = 80;
517
	}
518
519
	return $quality;
520
}
521
522
/**
523
 * Returns the caption title source setting
524
 *
525
 * @return string
526
 */
527
function foogallery_caption_title_source() {
528
	$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...
529
530
	if ( empty( $source ) ) {
531
		$source = 'caption';
532
	}
533
534
	return $source;
535
}
536
537
/**
538
 * Returns the attachment caption title based on the caption_title_source setting
539
 *
540
 * @param $attachment_post WP_Post
541
 *
542
 * @return string
543
 */
544
function foogallery_get_caption_title_for_attachment($attachment_post) {
545
	$source = foogallery_caption_title_source();
546
547
	if ( 'title' === $source ) {
548
		$caption = trim( $attachment_post->post_title );
549
	} else {
550
		$caption = trim( $attachment_post->post_excerpt );
551
	}
552
553
	return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
554
}
555
556
/**
557
 * Returns the caption description source setting
558
 *
559
 * @return string
560
 */
561
function foogallery_caption_desc_source() {
562
	$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...
563
564
	if ( empty( $source ) ) {
565
		$source = 'desc';
566
	}
567
568
	return $source;
569
}
570
571
/**
572
 * Returns the attachment caption description based on the caption_desc_source setting
573
 *
574
 * @param $attachment_post WP_Post
575
 *
576
 * @return string
577
 */
578
function foogallery_get_caption_desc_for_attachment($attachment_post) {
579
	$source = foogallery_caption_desc_source();
580
581
	switch ( $source ) {
582
		case 'title':
583
			$caption = trim( $attachment_post->post_title );
584
			break;
585
		case 'caption':
586
			$caption = trim( $attachment_post->post_excerpt );
587
			break;
588
		case 'alt':
589
			$caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
590
			break;
591
		default:
592
			$caption = trim( $attachment_post->post_content );
593
	}
594
595
	return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
596
}
597
598
/**
599
 * Runs thumbnail tests and outputs results in a table format
600
 */
601
function foogallery_output_thumbnail_generation_results() {
602
	$thumbs = new FooGallery_Thumbnails();
603
	try {
604
		$results = $thumbs->run_thumbnail_generation_tests();
605
        if ( $results['success'] ) {
606
            echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
607
        } else {
608
            echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
609
            var_dump( $results['error'] );
610
        }
611
	}
612
	catch (Exception $e) {
613
		echo 'Exception: ' . $e->getMessage();
614
	}
615
}
616
617
/**
618
 * Returns the URL to the test image
619
 *
620
 * @return string
621
 */
622
function foogallery_test_thumb_url() {
623
    return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/test_thumb_1.jpg' );
624
}