PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Document / Models / Elements / Form.php

Form.php in Depicter — Popup & Slider Builder trunk, at app/src/Document/Models/Elements/Form.php

209 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document\Models\Elements;
3
4 use Averta\Core\Utility\Arr;
5 use Depicter\Document\CSS\Selector;
6 use Depicter\Document\Models;
7 use Depicter\Html\Html;
8
9 class Form extends Models\Element
10 {
11 /**
12 * @throws \JsonMapper_Exception
13 */
14 public function render() {
15 $args = $this->getDefaultAttributes();
16 $output = '';
17
18 // If it was parent form element.
19 if ( ! $this->componentType ) {
20 $output = Html::form( $this->getSubmitPath(), 'post', $args, "\n" . $this->getFormContent() );
21
22 // Otherwise, it's input and buttons inside the form
23 } else {
24 $args['data-type'] = $this->componentType;
25 switch ( $this->componentType ) {
26 case 'form:input':
27 $output = Html::div( $args, $this->getInputContent() );
28 break;
29 case 'form:submit':
30 $output = Html::button( $args, $this->getContent() );
31
32 break;
33 case 'form:message':
34 $output = Html::div( $args, $this->getMessageContent() );
35 break;
36 case "hiddenInput":
37 $args['data-source'] = $this->options->source;
38 $args['data-source-key'] = $this->options->sourceKey;
39 $output = Html::div( $args, $this->getHiddenInputContent() );
40 default:
41 break;
42 }
43 }
44
45 return $output . "\n";
46 }
47
48 /**
49 * Get CSS class names of this element
50 *
51 * @return array
52 */
53 public function getClassNamesList(): array{
54 $classes = parent::getClassNamesList();
55 if ( ! $this->componentType ) {
56 $classes[] = ( !empty( $this->options->labelsPlacement ) && 'left' === $this->options->labelsPlacement ) ? Selector::prefixify( 'label-left' ) : Selector::prefixify( 'label-top' );
57 } else if ( $this->componentType == 'form:input' ) {
58 $classes[] = Selector::prefixify( 'form-field' );
59 }
60 return $classes;
61 }
62
63 /**
64 * Retrieves the content of element
65 *
66 * @return string
67 * @throws \JsonMapper_Exception
68 */
69 protected function getFormContent(): string{
70 $output = Html::input( 'hidden', 'action', 'depicter-lead-submit') . "\n";
71 $output .= Html::input( 'hidden', '_sourceId', $this->getDocumentID() ) . "\n";
72
73 if ( !empty( $this->options->captcha ) ) {
74 $clientKey = \Depicter::options()->get('google_recaptcha_client_key', false);
75 $secretKey = \Depicter::options()->get('google_recaptcha_secret_key', false);
76
77 if ( $clientKey && $secretKey ) {
78 $output .= Html::input( 'hidden', '_g_recaptcha_key' , $clientKey ) . "\n";
79 $output .= Html::input( 'hidden', '_g_recaptcha_token', '' ) . "\n";
80 }
81 }
82
83 if ( $this->childrenObjects ) {
84 $output .= Html::input( 'hidden', '_contentId', $this->getID() ) . "\n";
85 $output .= Html::input( 'hidden', '_contentName', $this->getName() ) . "\n";
86 }
87
88 $output .= Html::input( 'hidden', '_csrfToken', wp_create_nonce( 'depicter-csrf-lead-' . $this->getDocumentID() ) ) . "\n\n";
89
90 if ( ! empty( $this->childrenObjects ) ) {
91 foreach ( $this->childrenObjects as $element ) {
92 $output .= $element->prepare()->render();
93 }
94 }
95
96 return $output;
97 }
98
99 protected function getInputContent(): string{
100 $output = '';
101
102 if( ! empty( $this->options->attributes->name ) ){
103 $this->options->attributes->name = sanitize_title( $this->options->attributes->name );
104 }
105 if ( ! empty( $this->options->label ) && ! empty( $this->options->showLabel ) ) {
106 $output .= Html::label([
107 'class' => $this->options->type === "checkbox" ? Selector::prefixify( 'choice-label' ) : Selector::prefixify( 'field-label' ),
108 'for' => $this->options->attributes->name
109 ], $this->options->label ) . "\n";
110 }
111
112 $args = [
113 'id' => $this->options->attributes->name,
114 'name' => $this->options->attributes->name,
115 'placeholder' => $this->options->placeholder ?? '',
116 ];
117
118 if ( ! empty( $this->options->attributes->autoComplete ) ) {
119 $args['autocomplete'] = $this->options->attributes->autoComplete;
120 }
121
122 if ( ! empty( $this->options->attributes->required ) ) {
123 $args['required'] = '';
124 }
125
126 switch ( $this->options->type ) {
127 case 'textarea':
128 $args['class'] = Selector::prefixify( 'input' ) . ' ' . Selector::prefixify( 'textarea' );
129 if ( ! empty( $this->options->inputHeight ) ) {
130 $args['style'] = 'height: ' . $this->options->inputHeight . 'px;';
131 }
132 $output .= Html::textarea( $args ) . "\n";
133 break;
134 case 'text':
135 case 'email':
136 case 'number':
137 case 'tel':
138 case 'url':
139 $args['class'] = Selector::prefixify( 'input' ) . ' ' . Selector::prefixify( 'text-input' );
140 $output .= Html::input( $this->options->type, $this->options->attributes->name, '', $args ) . "\n";
141 break;
142 case 'checkbox':
143 $args['class'] = Selector::prefixify( 'input' ) . ' ' . Selector::prefixify( 'checkbox-input' );
144 $output .= Html::input( $this->options->type, $this->options->attributes->name, '', $args ) . "\n";
145 $output = Html::div([
146 'class' => Selector::prefixify( 'choice-container' )
147 ], $output ) . "\n";
148 break;
149 case 'submit':
150 default:
151 break;
152 }
153
154 if ( ! empty( $this->options->description ) && ! empty( $this->options->showDescription ) ) {
155 $output .= Html::p([
156 'class' => Selector::prefixify( 'field-description' )
157 ], $this->options->description ) . "\n";
158 }
159
160 return $output;
161 }
162
163 protected function getMessageContent(): string{
164 $output = '';
165 if ( ! empty( $this->options->showSuccessMessage ) && ! empty( $this->options->successMessage ) ) {
166 $output .= "\n" . Html::p([
167 'class' => Selector::prefixify( 'message' ) . ' ' . Selector::prefixify( 'message-success' )
168 ], $this->options->successMessage );
169 }
170
171 if ( ! empty( $this->options->errorMessage ) ) {
172 $output .= "\n" . Html::p([
173 'class' => Selector::prefixify( 'message' ) . ' ' . Selector::prefixify( 'message-error' ),
174 'style' => ! empty( $this->options->errorTextColor ) ? 'color: ' . $this->options->errorTextColor . ';' : '',
175 ], $this->options->errorMessage );
176 }
177
178 return $output;
179 }
180
181 protected function getHiddenInputContent(): string {
182 return Html::input( 'hidden', $this->options->name, '', [] );
183 }
184
185 /**
186 * Get list of selector and CSS for element and belonging child elements
187 *
188 * @return array
189 * @throws \JsonMapper_Exception
190 */
191 public function getSelectorAndCssList(){
192 parent::getSelectorAndCssList();
193 foreach ( $this->childrenObjects as $element ) {
194 $this->selectorCssList = Arr::merge( $this->selectorCssList, $element->prepare()->getSelectorAndCssList() );
195 }
196
197 return $this->selectorCssList;
198 }
199
200 /**
201 * Retrieves form submit path
202 *
203 * @return string
204 */
205 public function getSubmitPath(){
206 return trim( wp_parse_url( self_admin_url( 'admin-ajax.php' ), PHP_URL_PATH ) );
207 }
208 }
209