Completed
Pull Request — master (#859)
by Devin
19:40
created

template-functions.php ➔ give_get_template()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 23
rs 8.7972
ccs 8
cts 8
cp 1
crap 4
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Template Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Templates
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php 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
 * @access public
42
 *
43
 * @param string $template_name
44
 * @param array $args (default: array())
45
 * @param string $template_path (default: '')
46
 * @param string $default_path (default: '')
47
 */
48
function give_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
49
    if ( ! empty( $args ) && is_array( $args ) ) {
50
        extract( $args );
51
    }
52
53
    $template_names = array( $template_name . '.php' );
54
55
    $located = give_locate_template( $template_names, $template_path, $default_path );
56
57 44
    if ( ! file_exists( $located ) ) {
58
        give_output_error( sprintf( __( 'Error: %s template does not find.', '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
    do_action( 'give_before_template_part', $template_name, $template_path, $located, $args );
66
67 44
    include( $located );
68
69
    do_action( 'give_after_template_part', $template_name, $template_path, $located, $args );
70 44
}
71
72
/**
73
 * Retrieves a template part
74
 *
75
 * @since v1.0
76
 *
77
 * Taken from bbPress
78
 *
79
 * @param string $slug
80
 * @param string $name Optional. Default null
81
 * @param bool $load
82
 *
83
 * @return string
84
 *
85
 * @uses  give_locate_template()
86
 * @uses  load_template()
87
 * @uses  get_template_part()
88
 */
89
function give_get_template_part( $slug, $name = null, $load = true ) {
90
91
	// Execute code for this part
92
	do_action( 'get_template_part_' . $slug, $slug, $name );
93 45
94
	// Setup possible parts
95
	$templates = array();
96 45
	if ( isset( $name ) ) {
97
		$templates[] = $slug . '-' . $name . '.php';
98
	}
99 45
	$templates[] = $slug . '.php';
100
101
	// Allow template parts to be filtered
102
	$templates = apply_filters( 'give_get_template_part', $templates, $slug, $name );
103
104 45
	// Return the part that is found
105
	return give_locate_template( $templates, $load, false );
106
}
107 45
108
/**
109 45
 * Retrieve the name of the highest priority template file that exists.
110 45
 *
111 45
 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
112
 * inherit from a parent theme can just overload one file. If the template is
113 45
 * not found in either of those, it looks in the theme-compat folder last.
114
 *
115 45
 * Forked from bbPress
116 45
 *
117
 * @since 1.0
118 45
 *
119
 * @param string|array $template_names Template file(s) to search for, in order.
120 45
 * @param bool $load If true the template file will be loaded if it is found.
121 44
 * @param bool $require_once Whether to require_once or require. Default true.
122 44
 *                                     Has no effect if $load is false.
123
 *
124 45
 * @return string The template filename if one is located.
125
 */
126
function give_locate_template( $template_names, $load = false, $require_once = true ) {
127
	// No file found yet
128
	$located = false;
129
130
	// Try to find a template file
131
	foreach ( (array) $template_names as $template_name ) {
132
133
		// Continue if template is empty
134
		if ( empty( $template_name ) ) {
135 46
			continue;
136
		}
137
138 46
		// Trim off any slashes from the template name
139 46
		$template_name = ltrim( $template_name, '/' );
140 46
141 46
		// try locating this template file by looping through the template paths
142
		foreach ( give_get_theme_template_paths() as $template_path ) {
143 46
144
			if ( file_exists( $template_path . $template_name ) ) {
145
				$located = $template_path . $template_name;
146 46
				break;
147
			}
148 46
		}
149
150
		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...
151
			break;
152
		}
153
	}
154
155
	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...
156
		load_template( $located, $require_once );
157
	}
158
159
	return $located;
160 48
}
161
162
/**
163
 * Returns a list of paths to check for template locations
164
 *
165
 * @since 1.0
166
 * @return mixed|void
167
 */
168
function give_get_theme_template_paths() {
169
170
	$template_dir = give_get_theme_template_dir_name();
171
172
	$file_paths = array(
173
		1   => trailingslashit( get_stylesheet_directory() ) . $template_dir,
174
		10  => trailingslashit( get_template_directory() ) . $template_dir,
175
		100 => give_get_templates_dir()
176
	);
177
178
	$file_paths = apply_filters( 'give_template_paths', $file_paths );
179
180
	// sort the file paths based on priority
181
	ksort( $file_paths, SORT_NUMERIC );
182
183
	return array_map( 'trailingslashit', $file_paths );
184
}
185
186
/**
187
 * Returns the template directory name.
188
 *
189
 * Themes can filter this by using the give_templates_dir filter.
190
 *
191
 * @since 1.0
192
 * @return string
193
 */
194
function give_get_theme_template_dir_name() {
195
	return trailingslashit( apply_filters( 'give_templates_dir', 'give' ) );
196
}
197
198
/**
199
 * Adds Give Version to the <head> tag
200
 *
201
 * @since 1.0
202
 * @return void
203
 */
204
function give_version_in_header() {
205
	echo '<meta name="generator" content="Give v' . GIVE_VERSION . '" />' . "\n";
206
}
207
208
add_action( 'wp_head', 'give_version_in_header' );
209
210
/**
211
 * Determines if we're currently on the Donations History page.
212
 *
213
 * @since 1.0
214
 * @return bool True if on the Donations History page, false otherwise.
215
 */
216
function give_is_donation_history_page() {
217
218
	$ret = is_page( give_get_option( 'history_page' ) );
219
220
	return apply_filters( 'give_is_donation_history_page', $ret );
221
}
222
223
/**
224
 * Adds body classes for Give pages
225
 *
226
 * @since 1.0
227
 *
228
 * @param array $class current classes
229
 *
230
 * @return array Modified array of classes
231
 */
232
function give_add_body_classes( $class ) {
233
	$classes = (array) $class;
234
235
	if ( give_is_success_page() ) {
236
		$classes[] = 'give-success';
237
		$classes[] = 'give-page';
238
	}
239
240
	if ( give_is_failed_transaction_page() ) {
241
		$classes[] = 'give-failed-transaction';
242
		$classes[] = 'give-page';
243
	}
244
245
	if ( give_is_donation_history_page() ) {
246
		$classes[] = 'give-donation-history';
247
		$classes[] = 'give-page';
248
	}
249
250
	if ( give_is_test_mode() ) {
251
		$classes[] = 'give-test-mode';
252
		$classes[] = 'give-page';
253
	}
254
255
	//Theme-specific Classes used to prevent conflicts via CSS
256
	$current_theme = wp_get_theme();
257
258
	switch ( $current_theme->template ) {
259
260
		case 'Divi':
261
			$classes[] = 'give-divi';
262
			break;
263
		case 'Avada':
264
			$classes[] = 'give-avada';
265
			break;
266
		case 'twentysixteen':
267
			$classes[] = 'give-twentysixteen';
268
			break;
269
270
	}
271
272
	return array_unique( $classes );
273
}
274
275
add_filter( 'body_class', 'give_add_body_classes' );
276
277
278
/**
279
 * Add Post Class Filter
280
 *
281
 * Adds extra post classes for forms
282
 *
283
 * @since       1.0
284
 *
285
 * @param array        $classes
286
 * @param string|array $class
287
 * @param int|string   $post_id
288
 *
289
 * @return array
290
 */
291
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...
292
	if ( ! $post_id || 'give_forms' !== get_post_type( $post_id ) ) {
293
		return $classes;
294
	}
295
296
	//@TODO: Add classes for custom taxonomy and form configurations (multi vs single donations, etc).
297
298
	if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) {
299
		unset( $classes[ $key ] );
300
	}
301
302
	return $classes;
303
}
304
305
306
add_filter( 'post_class', 'give_add_post_class', 20, 3 );
307
308
/**
309
 * Get the placeholder image URL for forms etc
310
 *
311
 * @access public
312
 * @return string
313
 */
314
function give_get_placeholder_img_src() {
315
316
	$placeholder_url = '//placehold.it/600x600&text=' . urlencode( esc_attr__( 'Give Placeholder Image', 'give' ) );
317
318
	return apply_filters( 'give_placeholder_img_src', $placeholder_url );
319
}
320
321
322
/**
323
 * Global
324
 */
325
if ( ! function_exists( 'give_output_content_wrapper' ) ) {
326
327
	/**
328
	 * Output the start of the page wrapper.
329
	 */
330
	function give_output_content_wrapper() {
331
		give_get_template_part( 'global/wrapper-start' );
332
	}
333
}
334
if ( ! function_exists( 'give_output_content_wrapper_end' ) ) {
335
336
	/**
337
	 * Output the end of the page wrapper.
338
	 */
339
	function give_output_content_wrapper_end() {
340
		give_get_template_part( 'global/wrapper-end' );
341
	}
342
}
343
344
/**
345
 * Single Give Form
346
 */
347
if ( ! function_exists( 'give_left_sidebar_pre_wrap' ) ) {
348
	function give_left_sidebar_pre_wrap() {
349
		echo apply_filters( 'give_left_sidebar_pre_wrap', '<div id="give-sidebar-left" class="give-sidebar give-single-form-sidebar-left">' );
350
	}
351
}
352
353
if ( ! function_exists( 'give_left_sidebar_post_wrap' ) ) {
354
	function give_left_sidebar_post_wrap() {
355
		echo apply_filters( 'give_left_sidebar_post_wrap', '</div>' );
356
	}
357
}
358
359
if ( ! function_exists( 'give_get_forms_sidebar' ) ) {
360
	function give_get_forms_sidebar() {
361
		give_get_template_part( 'single-give-form/sidebar' );
362
	}
363
}
364
365
if ( ! function_exists( 'give_show_form_images' ) ) {
366
367
	/**
368
	 * Output the product image before the single product summary.
369
	 */
370
	function give_show_form_images() {
371
		$featured_image_option = give_get_option( 'disable_form_featured_img' );
372
		if ( $featured_image_option !== 'on' ) {
373
			give_get_template_part( 'single-give-form/featured-image' );
374
		}
375
	}
376
}
377
378
if ( ! function_exists( 'give_template_single_title' ) ) {
379
380
	/**
381
	 * Output the product title.
382
	 */
383
	function give_template_single_title() {
384
		give_get_template_part( 'single-give-form/title' );
385
	}
386
}
387
388
if ( ! function_exists( 'give_show_avatars' ) ) {
389
390
	/**
391
	 * Output the product title.
392
	 */
393
	function give_show_avatars() {
394
		echo do_shortcode( '[give_donators_gravatars]' );
395
	}
396
}
397
398
/**
399
 * Conditional Functions
400
 */
401
402
if ( ! function_exists( 'is_give_form' ) ) {
403
404
	/**
405
	 * is_give_form
406
	 *
407
	 * Returns true when viewing a single form.
408
	 *
409
	 * @since 1.6
410
	 *
411
	 * @return bool
412
	 */
413
	function is_give_form() {
414
		return is_singular( array( 'give_form' ) );
415
	}
416
}
417
418
if ( ! function_exists( 'is_give_category' ) ) {
419
420
	/**
421
	 * is_give_category
422
	 *
423
	 * Returns true when viewing give form category archive.
424
	 *
425
	 * @since 1.6
426
	 *
427
	 * @param string $term The term slug your checking for.
428
	 *                     Leave blank to return true on any.
429
	 *                     Default is blank.
430
	 *
431
	 * @return bool
432
	 */
433
	function is_give_category( $term = '' ) {
434
		return is_tax( 'give_forms_category', $term );
435
	}
436
}
437
438
if ( ! function_exists( 'is_give_tag' ) ) {
439
440
	/**
441
	 * is_give_tag
442
	 *
443
	 * Returns true when viewing give form tag archive.
444
	 *
445
	 * @since 1.6
446
	 *
447
	 * @param string $term The term slug your checking for.
448
	 *                     Leave blank to return true on any.
449
	 *                     Default is blank.
450
	 *
451
	 * @return bool
452
	 */
453
	function is_give_tag( $term = '' ) {
454
		return is_tax( 'give_forms_tag', $term );
455
	}
456
}
457
458
if ( ! function_exists( 'is_give_taxonomy' ) ) {
459
460
	/**
461
	 * is_give_taxonomy
462
	 *
463
	 * Returns true when viewing a give form taxonomy archive.
464
	 *
465
	 * @since 1.6
466
	 *
467
	 * @return bool
468
	 */
469
	function is_give_taxonomy() {
470
		return is_tax( get_object_taxonomies( 'give_form' ) );
471
	}
472
}
473