Completed
Pull Request — master (#893)
by Rami
20:06
created

template-functions.php ➔ give_get_template_part()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 8
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 27
rs 8.8571
ccs 12
cts 12
cp 1
crap 2
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
 *
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 );
50
    }
51
52
    $template_names = array( $template_name . '.php' );
53
54
    $located = give_locate_template( $template_names, $template_path, $default_path );
55
56
    if ( ! file_exists( $located ) ) {
57 44
		/* translators: %s: the template */
58
        give_output_error( 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";
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
299
	}
300
301
	return array_unique( $classes );
302
}
303
304
add_filter( 'body_class', 'give_add_body_classes' );
305
306
307
/**
308
 * Add Post Class Filter
309
 *
310
 * Adds extra post classes for forms
311
 *
312
 * @since       1.0
313
 *
314
 * @param array        $classes
315
 * @param string|array $class
316
 * @param int|string   $post_id
317
 *
318
 * @return array
319
 */
320
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...
321
	if ( ! $post_id || 'give_forms' !== get_post_type( $post_id ) ) {
322
		return $classes;
323
	}
324
325
	//@TODO: Add classes for custom taxonomy and form configurations (multi vs single donations, etc).
326
327
	if ( false !== ( $key = array_search( 'hentry', $classes ) ) ) {
328
		unset( $classes[ $key ] );
329
	}
330
331
	return $classes;
332
}
333
334
335
add_filter( 'post_class', 'give_add_post_class', 20, 3 );
336
337
/**
338
 * Get the placeholder image URL for forms etc
339
 *
340
 * @access public
341
 * @return string
342
 */
343
function give_get_placeholder_img_src() {
344
345
	$placeholder_url = '//placehold.it/600x600&text=' . urlencode( esc_attr__( 'Give Placeholder Image', 'give' ) );
346
347
	return apply_filters( 'give_placeholder_img_src', $placeholder_url );
348
}
349
350
351
/**
352
 * Global
353
 */
354
if ( ! function_exists( 'give_output_content_wrapper' ) ) {
355
356
	/**
357
	 * Output the start of the page wrapper.
358
	 */
359
	function give_output_content_wrapper() {
360
		give_get_template_part( 'global/wrapper-start' );
361
	}
362
}
363
if ( ! function_exists( 'give_output_content_wrapper_end' ) ) {
364
365
	/**
366
	 * Output the end of the page wrapper.
367
	 */
368
	function give_output_content_wrapper_end() {
369
		give_get_template_part( 'global/wrapper-end' );
370
	}
371
}
372
373
/**
374
 * Single Give Form
375
 */
376
if ( ! function_exists( 'give_left_sidebar_pre_wrap' ) ) {
377
	function give_left_sidebar_pre_wrap() {
378
		echo apply_filters( 'give_left_sidebar_pre_wrap', '<div id="give-sidebar-left" class="give-sidebar give-single-form-sidebar-left">' );
379
	}
380
}
381
382
if ( ! function_exists( 'give_left_sidebar_post_wrap' ) ) {
383
	function give_left_sidebar_post_wrap() {
384
		echo apply_filters( 'give_left_sidebar_post_wrap', '</div>' );
385
	}
386
}
387
388
if ( ! function_exists( 'give_get_forms_sidebar' ) ) {
389
	function give_get_forms_sidebar() {
390
		give_get_template_part( 'single-give-form/sidebar' );
391
	}
392
}
393
394
if ( ! function_exists( 'give_show_form_images' ) ) {
395
396
	/**
397
	 * Output the product image before the single product summary.
398
	 */
399
	function give_show_form_images() {
400
		$featured_image_option = give_get_option( 'disable_form_featured_img' );
401
		if ( $featured_image_option !== 'on' ) {
402
			give_get_template_part( 'single-give-form/featured-image' );
403
		}
404
	}
405
}
406
407
if ( ! function_exists( 'give_template_single_title' ) ) {
408
409
	/**
410
	 * Output the product title.
411
	 */
412
	function give_template_single_title() {
413
		give_get_template_part( 'single-give-form/title' );
414
	}
415
}
416
417
if ( ! function_exists( 'give_show_avatars' ) ) {
418
419
	/**
420
	 * Output the product title.
421
	 */
422
	function give_show_avatars() {
423
		echo do_shortcode( '[give_donators_gravatars]' );
424
	}
425
}
426
427
/**
428
 * Conditional Functions
429
 */
430
431
if ( ! function_exists( 'is_give_form' ) ) {
432
433
	/**
434
	 * is_give_form
435
	 *
436
	 * Returns true when viewing a single form.
437
	 *
438
	 * @since 1.6
439
	 *
440
	 * @return bool
441
	 */
442
	function is_give_form() {
443
		return is_singular( array( 'give_form' ) );
444
	}
445
}
446
447
if ( ! function_exists( 'is_give_category' ) ) {
448
449
	/**
450
	 * is_give_category
451
	 *
452
	 * Returns true when viewing give form category archive.
453
	 *
454
	 * @since 1.6
455
	 *
456
	 * @param string $term The term slug your checking for.
457
	 *                     Leave blank to return true on any.
458
	 *                     Default is blank.
459
	 *
460
	 * @return bool
461
	 */
462
	function is_give_category( $term = '' ) {
463
		return is_tax( 'give_forms_category', $term );
464
	}
465
}
466
467
if ( ! function_exists( 'is_give_tag' ) ) {
468
469
	/**
470
	 * is_give_tag
471
	 *
472
	 * Returns true when viewing give form tag archive.
473
	 *
474
	 * @since 1.6
475
	 *
476
	 * @param string $term The term slug your checking for.
477
	 *                     Leave blank to return true on any.
478
	 *                     Default is blank.
479
	 *
480
	 * @return bool
481
	 */
482
	function is_give_tag( $term = '' ) {
483
		return is_tax( 'give_forms_tag', $term );
484
	}
485
}
486
487
if ( ! function_exists( 'is_give_taxonomy' ) ) {
488
489
	/**
490
	 * is_give_taxonomy
491
	 *
492
	 * Returns true when viewing a give form taxonomy archive.
493
	 *
494
	 * @since 1.6
495
	 *
496
	 * @return bool
497
	 */
498
	function is_give_taxonomy() {
499
		return is_tax( get_object_taxonomies( 'give_form' ) );
500
	}
501
}
502