Test Failed
Pull Request — master (#1963)
by Matt
05:09
created

template-functions.php ➔ give_add_body_classes()   D

Complexity

Conditions 9
Paths 80

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 80
nop 1
dl 0
loc 45
ccs 0
cts 9
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Template Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Templates
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Returns the path to the Give templates directory
19
 *
20
 * @since 1.0
21
 * @return string
22
 */
23
function give_get_templates_dir() {
24 47
	return GIVE_PLUGIN_DIR . 'templates';
25
}
26
27
/**
28
 * Returns the URL to the Give templates directory
29
 *
30
 * @since 1.0
31
 * @return string
32
 */
33
function give_get_templates_url() {
34 1
	return GIVE_PLUGIN_URL . 'templates';
35
}
36
37
/**
38
 * Get other templates, passing attributes and including the file.
39
 *
40
 * @since 1.6
41
 *
42
 * @param string $template_name Template file name.
43
 * @param array  $args          Passed arguments. Default is empty array().
44
 * @param string $template_path Template file path. Default is empty.
45
 * @param string $default_path  Default path. Default is empty.
46
 */
47
function give_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
48
    if ( ! empty( $args ) && is_array( $args ) ) {
49
        extract( $args );
0 ignored issues
show
introduced by
extract() usage is highly discouraged, due to the complexity and unintended issues it might cause.
Loading history...
50
    }
51
52
    $template_names = array( $template_name . '.php' );
53
54
    $located = give_locate_template( $template_names, $template_path, $default_path );
0 ignored issues
show
Documentation introduced by
$template_path 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...
Documentation introduced by
$default_path 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...
55
56
    if ( ! file_exists( $located ) ) {
57 44
		/* translators: %s: the template */
58
        Give()->notices->print_frontend_notice( sprintf( __( 'The %s template was not found.', 'give' ), $located ), true );
59
        return;
60 44
    }
61 44
62 44
    // Allow 3rd party plugin filter template file from their plugin.
63 44
    $located = apply_filters( 'give_get_template', $located, $template_name, $args, $template_path, $default_path );
64 44
65
	/**
66
	 * Fires in give template, before the file is included.
67 44
	 *
68
	 * Allows you to execute code before the file is included.
69
	 *
70 44
	 * @since 1.6
71
	 *
72
	 * @param string $template_name Template file name.
73
	 * @param string $template_path Template file path.
74
	 * @param string $located       Template file filter by 3rd party plugin.
75
	 * @param array  $args          Passed arguments.
76
	 */
77
    do_action( 'give_before_template_part', $template_name, $template_path, $located, $args );
78
79
    include( $located );
80
81
	/**
82
	 * Fires in give template, after the file is included.
83
	 *
84
	 * Allows you to execute code after the file is included.
85
	 *
86
	 * @since 1.6
87
	 *
88
	 * @param string $template_name Template file name.
89
	 * @param string $template_path Template file path.
90
	 * @param string $located       Template file filter by 3rd party plugin.
91
	 * @param array  $args          Passed arguments.
92
	 */
93 45
    do_action( 'give_after_template_part', $template_name, $template_path, $located, $args );
94
}
95
96 45
/**
97
 * Retrieves a template part
98
 *
99 45
 * Taken from bbPress.
100
 *
101
 * @since 1.0
102
 *
103
 * @param string $slug Template part file slug {slug}.php.
104 45
 * @param string $name Optional. Template part file name {slug}-{name}.php. Default is null.
105
 * @param bool   $load If true the template file will be loaded, if it is found.
106
 *
107 45
 * @return string 
108
 */
109 45
function give_get_template_part( $slug, $name = null, $load = true ) {
110 45
111 45
	/**
112
	 * Fires in give template part, before the template part is retrieved.
113 45
	 *
114
	 * Allows you to execute code before retrieving the template part.
115 45
	 *
116 45
	 * @since 1.0
117
	 *
118 45
	 * @param string $slug Template part file slug {slug}.php.
119
	 * @param string $name Template part file name {slug}-{name}.php.
120 45
	 */
121 44
	do_action( "get_template_part_{$slug}", $slug, $name );
122 44
123
	// Setup possible parts
124 45
	$templates = array();
125
	if ( isset( $name ) ) {
126
		$templates[] = $slug . '-' . $name . '.php';
127
	}
128
	$templates[] = $slug . '.php';
129
130
	// Allow template parts to be filtered
131
	$templates = apply_filters( 'give_get_template_part', $templates, $slug, $name );
132
133
	// Return the part that is found
134
	return give_locate_template( $templates, $load, false );
135 46
}
136
137
/**
138 46
 * Retrieve the name of the highest priority template file that exists.
139 46
 *
140 46
 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
141 46
 * inherit from a parent theme can just overload one file. If the template is
142
 * not found in either of those, it looks in the theme-compat folder last.
143 46
 *
144
 * Forked from bbPress
145
 *
146 46
 * @since 1.0
147
 *
148 46
 * @param string|array $template_names Template file(s) to search for, in order.
149
 * @param bool $load If true the template file will be loaded if it is found.
150
 * @param bool $require_once Whether to require_once or require. Default true.
151
 *                                     Has no effect if $load is false.
152
 *
153
 * @return string The template filename if one is located.
154
 */
155
function give_locate_template( $template_names, $load = false, $require_once = true ) {
156
	// No file found yet
157
	$located = false;
158
159
	// Try to find a template file
160 48
	foreach ( (array) $template_names as $template_name ) {
161
162
		// Continue if template is empty
163
		if ( empty( $template_name ) ) {
164
			continue;
165
		}
166
167
		// Trim off any slashes from the template name
168
		$template_name = ltrim( $template_name, '/' );
169
170
		// try locating this template file by looping through the template paths
171
		foreach ( give_get_theme_template_paths() as $template_path ) {
172
173
			if ( file_exists( $template_path . $template_name ) ) {
174
				$located = $template_path . $template_name;
175
				break;
176
			}
177
		}
178
179
		if ( $located ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $located of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
180
			break;
181
		}
182
	}
183
184
	if ( ( true == $load ) && ! empty( $located ) ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
185
		load_template( $located, $require_once );
186
	}
187
188
	return $located;
189
}
190
191
/**
192
 * Returns a list of paths to check for template locations
193
 *
194
 * @since 1.0
195
 * @return mixed|void
196
 */
197
function give_get_theme_template_paths() {
198
199
	$template_dir = give_get_theme_template_dir_name();
200
201
	$file_paths = array(
202
		1   => trailingslashit( get_stylesheet_directory() ) . $template_dir,
203
		10  => trailingslashit( get_template_directory() ) . $template_dir,
204
		100 => give_get_templates_dir()
205
	);
206
207
	$file_paths = apply_filters( 'give_template_paths', $file_paths );
208
209
	// sort the file paths based on priority
210
	ksort( $file_paths, SORT_NUMERIC );
211
212
	return array_map( 'trailingslashit', $file_paths );
213
}
214
215
/**
216
 * Returns the template directory name.
217
 *
218
 * Themes can filter this by using the give_templates_dir filter.
219
 *
220
 * @since 1.0
221
 * @return string
222
 */
223
function give_get_theme_template_dir_name() {
224
	return trailingslashit( apply_filters( 'give_templates_dir', 'give' ) );
225
}
226
227
/**
228
 * Adds Give Version to the <head> tag
229
 *
230
 * @since 1.0
231
 * @return void
232
 */
233
function give_version_in_header() {
234
	echo '<meta name="generator" content="Give v' . GIVE_VERSION . '" />' . "\n";
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_VERSION'
Loading history...
235
}
236
237
add_action( 'wp_head', 'give_version_in_header' );
238
239
/**
240
 * Determines if we're currently on the Donations History page.
241
 *
242
 * @since 1.0
243
 * @return bool True if on the Donations History page, false otherwise.
244
 */
245
function give_is_donation_history_page() {
246
247
	$ret = is_page( give_get_option( 'history_page' ) );
248
249
	return apply_filters( 'give_is_donation_history_page', $ret );
250
}
251
252
/**
253
 * Adds body classes for Give pages
254
 *
255
 * @since 1.0
256
 *
257
 * @param array $class current classes
258
 *
259
 * @return array Modified array of classes
260
 */
261
function give_add_body_classes( $class ) {
262
	$classes = (array) $class;
263
264
	if ( give_is_success_page() ) {
265
		$classes[] = 'give-success';
266
		$classes[] = 'give-page';
267
	}
268
269
	if ( give_is_failed_transaction_page() ) {
270
		$classes[] = 'give-failed-transaction';
271
		$classes[] = 'give-page';
272
	}
273
274
	if ( give_is_donation_history_page() ) {
275
		$classes[] = 'give-donation-history';
276
		$classes[] = 'give-page';
277
	}
278
279
	if ( give_is_test_mode() ) {
280
		$classes[] = 'give-test-mode';
281
		$classes[] = 'give-page';
282
	}
283
284
	//Theme-specific Classes used to prevent conflicts via CSS
285
	$current_theme = wp_get_theme();
286
287
	switch ( $current_theme->template ) {
288
289
		case 'Divi':
290
			$classes[] = 'give-divi';
291
			break;
292
		case 'Avada':
293
			$classes[] = 'give-avada';
294
			break;
295
		case 'twentysixteen':
296
			$classes[] = 'give-twentysixteen';
297
			break;
298
		case 'twentyseventeen':
299
			$classes[] = 'give-twentyseventeen';
300
			break;
301
302
	}
303
304
	return array_unique( $classes );
305
}
306
307
add_filter( 'body_class', 'give_add_body_classes' );
308
309
310
/**
311
 * Add Post Class Filter
312
 *
313
 * Adds extra post classes for forms
314
 *
315
 * @since       1.0
316
 *
317
 * @param array        $classes
318
 * @param string|array $class
319
 * @param int|string   $post_id
320
 *
321
 * @return array
322
 */
323
function give_add_post_class( $classes, $class = '', $post_id = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
324
	if ( ! $post_id || 'give_forms' !== get_post_type( $post_id ) ) {
325
		return $classes;
326
	}
327
328
	//@TODO: Add classes for custom taxonomy and form configurations (multi vs single donations, etc).
329
330
	if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) {
331
		unset( $classes[ $key ] );
332
	}
333
334
	return $classes;
335
}
336
337
338
add_filter( 'post_class', 'give_add_post_class', 20, 3 );
339
340
/**
341
 * Get the placeholder image URL for forms etc
342
 *
343
 * @access public
344
 * @return string
345
 */
346
function give_get_placeholder_img_src() {
347
348
	$placeholder_url = '//placehold.it/600x600&text=' . urlencode( esc_attr__( 'Give Placeholder Image', 'give' ) );
349
350
	return apply_filters( 'give_placeholder_img_src', $placeholder_url );
351
}
352
353
354
/**
355
 * Global
356
 */
357
if ( ! function_exists( 'give_output_content_wrapper' ) ) {
358
359
	/**
360
	 * Output the start of the page wrapper.
361
	 */
362
	function give_output_content_wrapper() {
363
		give_get_template_part( 'global/wrapper-start' );
364
	}
365
}
366
if ( ! function_exists( 'give_output_content_wrapper_end' ) ) {
367
368
	/**
369
	 * Output the end of the page wrapper.
370
	 */
371
	function give_output_content_wrapper_end() {
372
		give_get_template_part( 'global/wrapper-end' );
373
	}
374
}
375
376
/**
377
 * Single Give Form
378
 */
379
if ( ! function_exists( 'give_left_sidebar_pre_wrap' ) ) {
380
	function give_left_sidebar_pre_wrap() {
381
		echo apply_filters( 'give_left_sidebar_pre_wrap', '<div id="give-sidebar-left" class="give-sidebar give-single-form-sidebar-left">' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
382
	}
383
}
384
385
if ( ! function_exists( 'give_left_sidebar_post_wrap' ) ) {
386
	function give_left_sidebar_post_wrap() {
387
		echo apply_filters( 'give_left_sidebar_post_wrap', '</div>' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
388
	}
389
}
390
391
if ( ! function_exists( 'give_get_forms_sidebar' ) ) {
392
	function give_get_forms_sidebar() {
393
		give_get_template_part( 'single-give-form/sidebar' );
394
	}
395
}
396
397
if ( ! function_exists( 'give_show_form_images' ) ) {
398
399
	/**
400
	 * Output the donation form featured image.
401
	 */
402
	function give_show_form_images() {
403
		if ( give_is_setting_enabled( give_get_option( 'form_featured_img' ) ) ) {
404
			give_get_template_part( 'single-give-form/featured-image' );
405
		}
406
	}
407
}
408
409
if ( ! function_exists( 'give_template_single_title' ) ) {
410
411
	/**
412
	 * Output the form title.
413
	 */
414
	function give_template_single_title() {
415
		give_get_template_part( 'single-give-form/title' );
416
	}
417
}
418
419
/**
420
 * Conditional Functions
421
 */
422
423
if ( ! function_exists( 'is_give_form' ) ) {
424
425
	/**
426
	 * is_give_form
427
	 *
428
	 * Returns true when viewing a single form.
429
	 *
430
	 * @since 1.6
431
	 *
432
	 * @return bool
433
	 */
434
	function is_give_form() {
435
		return is_singular( array( 'give_form' ) );
436
	}
437
}
438
439
if ( ! function_exists( 'is_give_category' ) ) {
440
441
	/**
442
	 * is_give_category
443
	 *
444
	 * Returns true when viewing give form category archive.
445
	 *
446
	 * @since 1.6
447
	 *
448
	 * @param string $term The term slug your checking for.
449
	 *                     Leave blank to return true on any.
450
	 *                     Default is blank.
451
	 *
452
	 * @return bool
453
	 */
454
	function is_give_category( $term = '' ) {
455
		return is_tax( 'give_forms_category', $term );
456
	}
457
}
458
459
if ( ! function_exists( 'is_give_tag' ) ) {
460
461
	/**
462
	 * is_give_tag
463
	 *
464
	 * Returns true when viewing give form tag archive.
465
	 *
466
	 * @since 1.6
467
	 *
468
	 * @param string $term The term slug your checking for.
469
	 *                     Leave blank to return true on any.
470
	 *                     Default is blank.
471
	 *
472
	 * @return bool
473
	 */
474
	function is_give_tag( $term = '' ) {
475
		return is_tax( 'give_forms_tag', $term );
476
	}
477
}
478
479
if ( ! function_exists( 'is_give_taxonomy' ) ) {
480
481
	/**
482
	 * is_give_taxonomy
483
	 *
484
	 * Returns true when viewing a give form taxonomy archive.
485
	 *
486
	 * @since 1.6
487
	 *
488
	 * @return bool
489
	 */
490
	function is_give_taxonomy() {
491
		return is_tax( get_object_taxonomies( 'give_form' ) );
492
	}
493
}
494