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