PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.17.0
GiveWP – Donation Plugin and Fundraising Platform v2.17.0
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Form / LegacyConsumer / FieldView.php

FieldView.php in GiveWP – Donation Plugin and Fundraising Platform 2.17.0, at src/Form/LegacyConsumer/FieldView.php

123 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Form\LegacyConsumer;
4
5 use Give\Framework\FieldsAPI\Contracts\Node;
6 use Give\Framework\FieldsAPI\Types;
7
8 /**
9 * @since 2.10.2
10 * @since 2.14.0 Add field classes hook for setting custom class names on the wrapper.
11 */
12 class FieldView {
13 const INPUT_TYPE_ATTRIBUTES = [
14 Types::PHONE => 'tel',
15 Types::EMAIL => 'email',
16 Types::URL => 'url',
17 ];
18
19 /**
20 * @since 2.10.2
21 * @since 2.14.0 add $formId as a param
22 * @since 2.14.0 Add filter to allow rendering logic for custom fields
23 * @since 2.16.0 Add visibility conditions to field container
24 *
25 * @param Node $field
26 * @param int $formId
27 *
28 * @return void
29 */
30 public static function render( Node $field, $formId ) {
31 $type = $field->getType();
32 $fieldIdAttribute = give( UniqueIdAttributeGenerator::class )->getId( $formId, $field->getName() );
33
34 if ( $type === Types::HIDDEN ) {
35 include static::getTemplatePath( 'hidden' );
36
37 return;
38 }
39
40 $classList = apply_filters( "give_form_{$formId}_field_classes_{$field->getName()}", [
41 'form-row',
42 'form-row-wide'
43 ] );
44 $className = implode( ' ', array_unique( $classList ) );
45
46 printf(
47 '<div class="%1$s" data-field-type="%2$s" data-field-name="%3$s" %4$s>',
48 $className,
49 $field->getType(),
50 $field->getName(),
51 self::getVisibilityConditionAttribute( $field )
52 );
53
54 // By default, new fields will use templates/label.html.php and templates/base.html.php
55 switch ( $type ) {
56 case Types::HTML:
57 case Types::CHECKBOX:
58 case Types::RADIO: // Radio provides its own label
59 include static::getTemplatePath( $type );
60 break;
61 // These fields need a label and have their own template.
62 case Types::FILE:
63 case Types::SELECT:
64 case Types::TEXTAREA:
65 include static::getTemplatePath( 'label' );
66 include static::getTemplatePath( $type );
67 break;
68 // By default, include a template and use the base input template.
69 case Types::DATE:
70 case Types::EMAIL:
71 case Types::PHONE:
72 case Types::TEXT:
73 case Types::URL:
74 // Used in the template
75 $typeAttribute = array_key_exists( $type, static::INPUT_TYPE_ATTRIBUTES ) ? static::INPUT_TYPE_ATTRIBUTES[ $type ] : 'text';
76 include static::getTemplatePath( 'label' );
77 include static::getTemplatePath( 'base' );
78 break;
79 default:
80 /**
81 * Provide a custom function to render for a custom node type.
82 *
83 * @since 2.14.0
84 *
85 * @param Node $field The node to render.
86 * @param int $formId The form ID that the node is a part of.
87 *
88 * @void
89 */
90 do_action( "give_fields_api_render_{$field->getType()}", $field, $formId );
91 }
92 echo '</div>';
93 }
94
95 /**
96 * @since 2.12.0
97 *
98 * @param string $templateName
99 *
100 * @return string
101 */
102 protected static function getTemplatePath( $templateName ) {
103 return plugin_dir_path( __FILE__ ) . "/templates/{$templateName}.html.php";
104 }
105
106 /**
107 * @param Node $field
108 *
109 * @return string
110 */
111 private static function getVisibilityConditionAttribute( Node $field ) {
112 $visibilityConditions = method_exists( $field, 'getVisibilityConditions' ) ? $field->getVisibilityConditions() : null;
113
114 if ( $visibilityConditions ) {
115 $visibilityConditionsJson = esc_attr( json_encode( $visibilityConditions ) );
116
117 return "data-field-visibility-conditions=\"$visibilityConditionsJson\"";
118 }
119
120 return '';
121 }
122 }
123