← All changes
|
includes/forms/views/class-charitable-public-form-view.php
+126
-66
1.6.7
→
1.8.13
View file →
| @@ -4,17 +4,19 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * This is responsible for rendering the output of forms. |
| 6 | 6 | * |
| 7 | 7 | * @package Charitable/Forms/Charitable_Public_Form_View |
| 8 | - * @author Eric Daams | |
| 9 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 8 | + * @author David Bisset | |
| 9 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 10 | 10 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 | 11 | * @since 1.5.0 |
| 12 | - * @version 1.5.2 | |
| 12 | + * @version 1.6.44 | |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | // Exit if accessed directly. |
| 16 | -if ( ! defined( 'ABSPATH' ) ) { exit; } | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit; | |
| 18 | +} | |
| 17 | 19 | |
| 18 | 20 | if ( ! class_exists( 'Charitable_Public_Form_View' ) ) : |
| 19 | 21 | |
| 20 | 22 | /** |
| @@ -109,10 +111,13 @@ | ||
| 109 | 111 | * @param string $class The template class name. |
| 110 | 112 | * @param string $path The path to the template. |
| 111 | 113 | * @return void |
| 112 | 114 | */ |
| 113 | - public function register_custom_field_template( $field_type, $class, $path ) { | |
| 114 | - $this->custom_field_templates[ $field_type ] = array( 'class' => $class, 'path' => $path ); | |
| 115 | + public function register_custom_field_template( $field_type, $class, $path ) { // phpcs:ignore | |
| 116 | + $this->custom_field_templates[ $field_type ] = array( | |
| 117 | + 'class' => $class, | |
| 118 | + 'path' => $path, | |
| 119 | + ); | |
| 115 | 120 | } |
| 116 | 121 | |
| 117 | 122 | /** |
| 118 | 123 | * Render a form. |
| @@ -121,15 +126,36 @@ | ||
| 121 | 126 | * |
| 122 | 127 | * @return void |
| 123 | 128 | */ |
| 124 | 129 | public function render() { |
| 125 | - $this->render_notices(); | |
| 130 | + | |
| 131 | + $form_fields = $this->form->get_fields(); | |
| 132 | + $this->enqueue_script(); | |
| 133 | + if ( ( isset( $form_fields['donation_fields'] ) && 'bottom' !== charitable_get_option( 'donation_form_notices_display', 'top' ) ) || ! isset( $form_fields['donation_fields'] ) ) { | |
| 134 | + $this->render_notices(); | |
| 135 | + } | |
| 126 | 136 | $this->render_honeypot(); |
| 127 | 137 | $this->render_hidden_fields(); |
| 128 | 138 | $this->render_fields(); |
| 139 | + if ( isset( $form_fields['donation_fields'] ) && 'bottom' === charitable_get_option( 'donation_form_notices_display', 'top' ) ) { | |
| 140 | + $this->render_notices(); | |
| 141 | + } | |
| 129 | 142 | } |
| 130 | 143 | |
| 131 | 144 | /** |
| 145 | + * Enqueue the forms script. | |
| 146 | + * | |
| 147 | + * @since 1.6.44 | |
| 148 | + * | |
| 149 | + * @return void | |
| 150 | + */ | |
| 151 | + public function enqueue_script() { | |
| 152 | + if ( ! wp_script_is( 'charitable-forms', 'enqueued' ) ) { | |
| 153 | + wp_enqueue_script( 'charitable-forms' ); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 132 | 158 | * Render notices before the form. |
| 133 | 159 | * |
| 134 | 160 | * @since 1.5.0 |
| 135 | 161 | * |
| @@ -135,13 +161,20 @@ | ||
| 135 | 161 | * |
| 136 | 162 | * @return void |
| 137 | 163 | */ |
| 138 | 164 | public function render_notices() { |
| 139 | - charitable_template_from_session( 'form-fields/notices.php', array( | |
| 140 | - 'notices' => charitable_get_notices()->get_notices(), | |
| 141 | - ), 'notices' ); | |
| 165 | + charitable_template_from_session( | |
| 166 | + 'form-fields/notices.php', | |
| 167 | + array( | |
| 168 | + 'notices' => charitable_get_notices()->get_notices(), | |
| 169 | + ), | |
| 170 | + 'notices' | |
| 171 | + ); | |
| 142 | 172 | |
| 143 | 173 | $this->rendered_notices = true; |
| 174 | + | |
| 175 | + /* Clear all notices. */ | |
| 176 | + charitable_get_notices()->clear(); | |
| 144 | 177 | } |
| 145 | 178 | |
| 146 | 179 | /** |
| 147 | 180 | * Render the honeypot fields. |
| @@ -177,9 +210,10 @@ | ||
| 177 | 210 | |
| 178 | 211 | /** |
| 179 | 212 | * Render all of a form's fields. |
| 180 | 213 | * |
| 181 | - * @since 1.5.0 | |
| 214 | + * @since 1.8.3.4 | |
| 215 | + * @since 1.8.3.5 | |
| 182 | 216 | * |
| 183 | 217 | * @param array $fields Optional. A set of fields to display. If not set, |
| 184 | 218 | * we will look for fields in the form's `get_fields()` |
| 185 | 219 | * method (if it has one). |
| @@ -201,12 +235,30 @@ | ||
| 201 | 235 | } |
| 202 | 236 | |
| 203 | 237 | $i = 1; |
| 204 | 238 | |
| 239 | + $form_template = charitable_get_option( 'donation_form_template', false ); | |
| 240 | + | |
| 241 | + $opening_div = false; | |
| 242 | + | |
| 205 | 243 | foreach ( $fields as $key => $field ) { |
| 206 | - $this->render_field( $field, $key, array( | |
| 207 | - 'index' => $i, | |
| 208 | - ) ); | |
| 244 | + // insert div group for certain fields. | |
| 245 | + if ( $i === 1 && ( 'minimal' === $form_template && ( 'title' === strtolower( $key ) || 'first_name' === strtolower( $key ) ) ) ) { | |
| 246 | + echo '<div class="charitable-form-group charitable-form-group-number-' . esc_attr( $i ) . '" charitable-form-group-' . esc_attr( $key ) . '">'; | |
| 247 | + } elseif ( 'minimal' === $form_template && 'first_name' === strtolower( $key ) ) { | |
| 248 | + echo '<div>'; | |
| 249 | + $opening_div = true; | |
| 250 | + } | |
| 251 | + $this->render_field( | |
| 252 | + $field, | |
| 253 | + $key, | |
| 254 | + array( | |
| 255 | + 'index' => $i, | |
| 256 | + ) | |
| 257 | + ); | |
| 258 | + if ( 'minimal' === $form_template && 'last_name' === strtolower( $key ) && $opening_div ) { | |
| 259 | + echo '</div>'; | |
| 260 | + } | |
| 209 | 261 | |
| 210 | 262 | $i += $this->increment_index( $field, $key, $i ); |
| 211 | 263 | } |
| 212 | 264 | } |
| @@ -235,9 +287,9 @@ | ||
| 235 | 287 | |
| 236 | 288 | $attrs = charitable_get_arbitrary_attributes( array( 'attrs' => $args ) ); |
| 237 | 289 | } |
| 238 | 290 | |
| 239 | - printf( '<input type="hidden" name="%s" value="%s" %s />', esc_attr( $key ), esc_attr( $value ), $attrs ); | |
| 291 | + printf( '<input type="hidden" name="%s" value="%s" %s />', esc_attr( $key ), esc_attr( $value ), $attrs ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 240 | 292 | } |
| 241 | 293 | |
| 242 | 294 | /** |
| 243 | 295 | * Render a specific form field. |
| @@ -283,13 +335,15 @@ | ||
| 283 | 335 | if ( ! $template ) { |
| 284 | 336 | return false; |
| 285 | 337 | } |
| 286 | 338 | |
| 287 | - $template->set_view_args( array( | |
| 288 | - 'form' => $this->form, | |
| 289 | - 'field' => $field, | |
| 290 | - 'classes' => $this->get_field_classes( $field, $index ), | |
| 291 | - ) ); | |
| 339 | + $template->set_view_args( | |
| 340 | + array( | |
| 341 | + 'form' => $this->form, | |
| 342 | + 'field' => $field, | |
| 343 | + 'classes' => $this->get_field_classes( $field, $index ), | |
| 344 | + ) | |
| 345 | + ); | |
| 292 | 346 | |
| 293 | 347 | $template->render(); |
| 294 | 348 | |
| 295 | 349 | return true; |
| @@ -304,9 +358,9 @@ | ||
| 304 | 358 | * @param string|null $namespace Namespace for the form field's name attribute. |
| 305 | 359 | * @param int $index The current index. |
| 306 | 360 | * @return string |
| 307 | 361 | */ |
| 308 | - protected function get_field_name( $key, $namespace = null, $index = 0 ) { | |
| 362 | + protected function get_field_name( $key, $namespace = null, $index = 0 ) { // phpcs:ignore | |
| 309 | 363 | $name = $key; |
| 310 | 364 | |
| 311 | 365 | if ( ! is_null( $namespace ) ) { |
| 312 | 366 | $name = $namespace . '[' . $name . ']'; |
| @@ -340,9 +394,9 @@ | ||
| 340 | 394 | |
| 341 | 395 | /** |
| 342 | 396 | * Filter the template to be used for the form. |
| 343 | 397 | * |
| 344 | - * Any callback hooking into this filter should return a `Charitable_Template` | |
| 398 | + * Any callback hooking into this filter should return a `Charitable_Template` | |
| 345 | 399 | * instance. Anything else will be ignored. |
| 346 | 400 | * |
| 347 | 401 | * @since 1.0.0 |
| 348 | 402 | * |
| @@ -349,9 +403,9 @@ | ||
| 349 | 403 | * @param false|Charitable_Template $template False by default. |
| 350 | 404 | * @param array $field Field definition. |
| 351 | 405 | * @param Charitable_Form $form The Charitable_Form object. |
| 352 | 406 | * @param int $index The current index. |
| 353 | - */ | |
| 407 | + */ | |
| 354 | 408 | $template = apply_filters( 'charitable_form_field_template', $template, $field, $this->form, $index ); |
| 355 | 409 | |
| 356 | 410 | /* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */ |
| 357 | 411 | if ( ! $this->is_valid_template( $template ) ) { |
| @@ -441,14 +495,17 @@ | ||
| 441 | 495 | * @since 1.0.0 |
| 442 | 496 | * |
| 443 | 497 | * @param string[] $types Field types. |
| 444 | 498 | */ |
| 445 | - $default_field_types = apply_filters( 'charitable_default_template_field_types', array( | |
| 446 | - 'text', | |
| 447 | - 'email', | |
| 448 | - 'password', | |
| 449 | - 'date', | |
| 450 | - ) ); | |
| 499 | + $default_field_types = apply_filters( | |
| 500 | + 'charitable_default_template_field_types', | |
| 501 | + array( | |
| 502 | + 'text', | |
| 503 | + 'email', | |
| 504 | + 'password', | |
| 505 | + 'date', | |
| 506 | + ) | |
| 507 | + ); | |
| 451 | 508 | |
| 452 | 509 | return in_array( $field_type, $default_field_types ); |
| 453 | 510 | } |
| 454 | 511 | |
| @@ -507,18 +564,18 @@ | ||
| 507 | 564 | public function get_field_type_classes( $type ) { |
| 508 | 565 | $classes = array(); |
| 509 | 566 | |
| 510 | 567 | switch ( $type ) { |
| 511 | - case 'paragraph' : | |
| 568 | + case 'paragraph': | |
| 512 | 569 | $classes[] = 'charitable-form-content'; |
| 513 | 570 | break; |
| 514 | 571 | |
| 515 | - case 'fieldset' : | |
| 516 | - case 'donation-amount-wrapper' : | |
| 572 | + case 'fieldset': | |
| 573 | + case 'donation-amount-wrapper': | |
| 517 | 574 | $classes[] = 'charitable-fieldset'; |
| 518 | 575 | break; |
| 519 | 576 | |
| 520 | - default : | |
| 577 | + default: | |
| 521 | 578 | $classes[] = 'charitable-form-field'; |
| 522 | 579 | $classes[] = 'charitable-form-field-' . $type; |
| 523 | 580 | } |
| 524 | 581 | |
| @@ -534,9 +591,9 @@ | ||
| 534 | 591 | * @param string $key The key of the current field. May be empty. |
| 535 | 592 | * @param int|null $index The current index. May be null. |
| 536 | 593 | * @return int |
| 537 | 594 | */ |
| 538 | - public function increment_index( $field, $key = '', $index = null) { | |
| 595 | + public function increment_index( $field, $key = '', $index = null ) { | |
| 539 | 596 | if ( ! $this->should_increment( $field ) ) { |
| 540 | 597 | return 0; |
| 541 | 598 | } |
| 542 | 599 | |
| @@ -550,9 +607,9 @@ | ||
| 550 | 607 | * @param string $key The key of the current field. |
| 551 | 608 | * @param Charitable_Form $form The form we are displaying. |
| 552 | 609 | * @param int $index The current index. |
| 553 | 610 | */ |
| 554 | - return apply_filters( 'charitable_form_field_increment', 1, $field, $key, $this->form, $index ); | |
| 611 | + return apply_filters( 'charitable_form_field_increment', 1, $field, $key, $this->form, $index ); | |
| 555 | 612 | } |
| 556 | 613 | |
| 557 | 614 | /** |
| 558 | 615 | * Whether the index should be incremented. |
| @@ -586,38 +643,41 @@ | ||
| 586 | 643 | * Filter the custom field templates. |
| 587 | 644 | * |
| 588 | 645 | * @since 1.5.0 |
| 589 | 646 | */ |
| 590 | - $templates = apply_filters( 'charitable_public_form_view_custom_field_templates', array( | |
| 591 | - 'donation-amount-wrapper' => array( | |
| 592 | - 'class' => 'Charitable_Template', | |
| 593 | - 'path' => 'donation-form/donation-amount-wrapper.php', | |
| 594 | - ), | |
| 595 | - 'donation-amount' => array( | |
| 596 | - 'class' => 'Charitable_Template', | |
| 597 | - 'path' => 'donation-form/donation-amount.php', | |
| 598 | - ), | |
| 599 | - 'donor-fields' => array( | |
| 600 | - 'class' => 'Charitable_Template', | |
| 601 | - 'path' => 'donation-form/donor-fields.php', | |
| 602 | - ), | |
| 603 | - 'details-fields' => array( | |
| 604 | - 'class' => 'Charitable_Template', | |
| 605 | - 'path' => 'donation-form/details-fields.php', | |
| 606 | - ), | |
| 607 | - 'meta-fields' => array( | |
| 608 | - 'class' => 'Charitable_Template', | |
| 609 | - 'path' => 'donation-form/meta-fields.php', | |
| 610 | - ), | |
| 611 | - 'gateway-fields' => array( | |
| 612 | - 'class' => 'Charitable_Template', | |
| 613 | - 'path' => 'donation-form/gateway-fields.php', | |
| 614 | - ), | |
| 615 | - 'cc-expiration' => array( | |
| 616 | - 'class' => 'Charitable_Template', | |
| 617 | - 'path' => 'donation-form/cc-expiration.php', | |
| 618 | - ), | |
| 619 | - ) ); | |
| 647 | + $templates = apply_filters( | |
| 648 | + 'charitable_public_form_view_custom_field_templates', | |
| 649 | + array( | |
| 650 | + 'donation-amount-wrapper' => array( | |
| 651 | + 'class' => 'Charitable_Template', | |
| 652 | + 'path' => 'donation-form/donation-amount-wrapper.php', | |
| 653 | + ), | |
| 654 | + 'donation-amount' => array( | |
| 655 | + 'class' => 'Charitable_Template', | |
| 656 | + 'path' => 'donation-form/donation-amount.php', | |
| 657 | + ), | |
| 658 | + 'donor-fields' => array( | |
| 659 | + 'class' => 'Charitable_Template', | |
| 660 | + 'path' => 'donation-form/donor-fields.php', | |
| 661 | + ), | |
| 662 | + 'details-fields' => array( | |
| 663 | + 'class' => 'Charitable_Template', | |
| 664 | + 'path' => 'donation-form/details-fields.php', | |
| 665 | + ), | |
| 666 | + 'meta-fields' => array( | |
| 667 | + 'class' => 'Charitable_Template', | |
| 668 | + 'path' => 'donation-form/meta-fields.php', | |
| 669 | + ), | |
| 670 | + 'gateway-fields' => array( | |
| 671 | + 'class' => 'Charitable_Template', | |
| 672 | + 'path' => 'donation-form/gateway-fields.php', | |
| 673 | + ), | |
| 674 | + 'cc-expiration' => array( | |
| 675 | + 'class' => 'Charitable_Template', | |
| 676 | + 'path' => 'donation-form/cc-expiration.php', | |
| 677 | + ), | |
| 678 | + ) | |
| 679 | + ); | |
| 620 | 680 | |
| 621 | 681 | return array_filter( $templates, array( $this, 'sanitize_custom_field_template' ) ); |
| 622 | 682 | } |
| 623 | 683 | |
| @@ -633,5 +693,5 @@ | ||
| 633 | 693 | return is_array( $template ) && array_key_exists( 'path', $template ) && array_key_exists( 'class', $template ); |
| 634 | 694 | } |
| 635 | 695 | } |
| 636 | 696 | |
| 637 | -endif; // End interface_exists check. | |
| 697 | +endif; | |