PluginProbe ʕ •ᴥ•ʔ
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More / 1.9.1.2
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More v1.9.1.2
1.10.1.1 1.10.1 1.10.0.5 trunk 1.1.4 1.1.4.2 1.1.5 1.1.5.1 1.1.6 1.1.6.1 1.1.7 1.1.7.1 1.1.7.2 1.1.8 1.1.8.1 1.1.8.2 1.1.8.3 1.1.8.4 1.10.0.1 1.10.0.2 1.10.0.3 1.10.0.4 1.2.0 1.2.0.1 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 1.2.3.1 1.2.3.2 1.2.4 1.2.4.1 1.2.5 1.2.5.1 1.2.6 1.2.7 1.2.8 1.2.8.1 1.2.9 1.3.0 1.3.1 1.3.1.1 1.3.1.2 1.3.2 1.3.3 1.3.5 1.3.6 1.3.6.1 1.3.6.2 1.3.7.2 1.3.7.3 1.3.7.4 1.3.8 1.3.9.1 1.4.0.1 1.4.1.1 1.4.2 1.4.2.1 1.4.2.2 1.4.3 1.4.4 1.4.4.1 1.4.5 1.4.5.1 1.4.5.2 1.4.5.3 1.4.6 1.4.7.1 1.4.7.2 1.4.8.1 1.4.9 1.5.0.1 1.5.0.3 1.5.0.4 1.5.1 1.5.1.1 1.5.1.3 1.5.2.1 1.5.2.2 1.5.2.3 1.5.3 1.5.3.1 1.5.4.1 1.5.4.2 1.5.5 1.5.5.1 1.5.6 1.5.6.2 1.5.7 1.5.8.2 1.5.9.1 1.5.9.4 1.5.9.5 1.6.0.1 1.6.0.2 1.6.1 1.6.2.2 1.6.2.3 1.6.3.1 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.7 1.6.7.1 1.6.7.2 1.6.7.3 1.6.8 1.6.8.1 1.6.9 1.7.0 1.7.1.1 1.7.1.2 1.7.2 1.7.2.1 1.7.3 1.7.4 1.7.4.1 1.7.4.2 1.7.5.1 1.7.5.2 1.7.5.3 1.7.5.5 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.7.9.1 1.8.0.1 1.8.0.2 1.8.1.1 1.8.1.2 1.8.1.3 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.3.1 1.8.4 1.8.4.1 1.8.5.2 1.8.5.3 1.8.5.4 1.8.6.2 1.8.6.3 1.8.6.4 1.8.7.2 1.8.8.2 1.8.8.3 1.8.9.1 1.8.9.2 1.8.9.4 1.8.9.5 1.8.9.6 1.9.0.1 1.9.0.2 1.9.0.3 1.9.0.4 1.9.1.1 1.9.1.2 1.9.1.3 1.9.1.4 1.9.1.5 1.9.1.6 1.9.2.1 1.9.2.2 1.9.2.3 1.9.3.1 1.9.3.2 1.9.4.1 1.9.4.2 1.9.5 1.9.5.1 1.9.5.2 1.9.6 1.9.6.1 1.9.6.2 1.9.7.1 1.9.7.2 1.9.7.3 1.9.8.1 1.9.8.2 1.9.8.4 1.9.8.7 1.9.9.2 1.9.9.3 1.9.9.4
wpforms-lite / includes / templates / class-base.php
wpforms-lite / includes / templates Last commit date
class-base.php 1 year ago class-blank.php 1 year ago class-simple-contact-form.php 1 year ago
class-base.php
343 lines
1 <?php
2
3 /**
4 * Base form template.
5 *
6 * @since 1.0.0
7 */
8 abstract class WPForms_Template {
9
10 /**
11 * Full name of the template, eg "Contact Form".
12 *
13 * @since 1.0.0
14 *
15 * @var string
16 */
17 public $name;
18
19 /**
20 * Slug of the template, eg "contact-form" - no spaces.
21 *
22 * @since 1.0.0
23 *
24 * @var string
25 */
26 public $slug;
27
28 /**
29 * Source of the template.
30 *
31 * @since 1.6.8
32 *
33 * @var array
34 */
35 public $source;
36
37 /**
38 * Categories array.
39 *
40 * @since 1.6.8
41 *
42 * @var array
43 */
44 public $categories;
45
46 /**
47 * Short description the template.
48 *
49 * @since 1.0.0
50 *
51 * @var string
52 */
53 public $description = '';
54
55 /**
56 * Short description of the fields included with the template.
57 *
58 * @since 1.0.0
59 *
60 * @var string
61 */
62 public $includes = '';
63
64 /**
65 * URL of the icon to display in the admin area.
66 *
67 * @since 1.0.0
68 *
69 * @var string
70 */
71 public $icon = '';
72
73 /**
74 * Form template preview URL.
75 *
76 * @since 1.7.5.3
77 *
78 * @var string
79 */
80 public $url = '';
81
82 /**
83 * Form template thumbnail url.
84 *
85 * @since 1.8.2
86 *
87 * @var string
88 */
89 public $thumbnail = '';
90
91 /**
92 * Array of data that is assigned to the post_content on form creation.
93 *
94 * @since 1.0.0
95 *
96 * @var array
97 */
98 public $data;
99
100 /**
101 * Priority to show in the list of available templates.
102 *
103 * @since 1.0.0
104 *
105 * @var int
106 */
107 public $priority = 20;
108
109 /**
110 * Core or additional template.
111 *
112 * @since 1.4.0
113 *
114 * @var bool
115 */
116 public $core = false;
117
118 /**
119 * Modal message to display when the template is applied.
120 *
121 * @since 1.0.0
122 *
123 * @var array
124 */
125 public $modal = '';
126
127 /**
128 * Primary class constructor.
129 *
130 * @since 1.0.0
131 */
132 public function __construct() {
133
134 // Bootstrap.
135 $this->init();
136
137 $type = $this->core ? '_core' : '';
138
139 add_filter( "wpforms_form_templates{$type}", [ $this, 'template_details' ], $this->priority );
140 add_filter( 'wpforms_create_form_args', [ $this, 'template_data' ], 10, 2 );
141 add_filter( 'wpforms_save_form_args', [ $this, 'template_replace' ], 10, 3 );
142 add_filter( 'wpforms_builder_template_active', [ $this, 'template_active' ], 10, 2 );
143 }
144
145 /**
146 * Let's get started.
147 *
148 * @since 1.0.0
149 */
150 public function init() {}
151
152 /**
153 * Add basic template details to the Add New Form admin screen.
154 *
155 * @since 1.0.0
156 *
157 * @param array $templates Templates array.
158 *
159 * @return array
160 */
161 public function template_details( $templates ) {
162
163 $templates[] = [
164 'name' => $this->name,
165 'slug' => $this->slug,
166 'source' => $this->source,
167 'categories' => $this->categories,
168 'description' => $this->description,
169 'includes' => $this->includes,
170 'icon' => $this->icon,
171 'url' => ! empty( $this->url ) ? $this->url : '',
172 'plugin_dir' => $this->get_plugin_dir(),
173 'thumbnail' => ! empty( $this->thumbnail ) ? $this->thumbnail : '',
174 ];
175
176 return $templates;
177 }
178
179 /**
180 * Get the directory name of the plugin in which current template resides.
181 *
182 * @since 1.6.9
183 *
184 * @return string
185 */
186 private function get_plugin_dir(): string {
187
188 $reflection = new ReflectionClass( $this );
189 $template_file_path = wp_normalize_path( $reflection->getFileName() );
190
191 // Cutting out the WP_PLUGIN_DIR from the beginning of the template file path.
192 $template_file_path = preg_replace( '{^' . wp_slash( wp_normalize_path( WP_PLUGIN_DIR ) ) . '}', '', $template_file_path );
193
194 $template_file_chunks = explode( '/', $template_file_path );
195
196 return $template_file_chunks[1];
197 }
198
199 /**
200 * Add template data when form is created.
201 *
202 * @since 1.0.0
203 *
204 * @param array $args Create form arguments.
205 * @param array $data Template data.
206 *
207 * @return array
208 */
209 public function template_data( $args, $data ): array {
210
211 if ( empty( $data['template'] ) || $data['template'] !== $this->slug ) {
212 return $args;
213 }
214
215 // Enable Notifications by default.
216 $this->data['settings']['notification_enable'] = $this->data['settings']['notification_enable'] ?? '1';
217
218 /**
219 * Allow modifying form data when a template is applied to the new form.
220 *
221 * @since 1.9.0
222 *
223 * @param array $form_data New form data.
224 */
225 $this->data = (array) apply_filters( 'wpforms_templates_class_base_template_modify_data', $this->data );
226
227 $args['post_content'] = wpforms_encode( $this->data );
228
229 return $args;
230 }
231
232 /**
233 * Replace template on post update if triggered.
234 *
235 * @since 1.0.0
236 *
237 * @param array $form Form post data.
238 * @param array $data Form data.
239 * @param array $args Update form arguments.
240 *
241 * @return array
242 */
243 public function template_replace( $form, $data, $args ): array { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
244
245 // We should proceed only if the template slug passed via $args['template'] is equal to the current template slug.
246 // This will work only for offline templates: Blank Form, all the Addons Templates, and all the custom templates.
247 // All the online (modern) templates use the hash as the identifier,
248 // and they are handled by `\WPForms\Admin\Builder\Templates::apply_to_existing_form()`.
249 if ( empty( $args['template'] ) || $args['template'] !== $this->slug ) {
250 return $form;
251 }
252
253 $form_data = wpforms_decode( wp_unslash( $form['post_content'] ) );
254
255 // Something is wrong with the form data.
256 if ( empty( $form_data ) ) {
257 return $form;
258 }
259
260 // Compile the new form data preserving needed data from the existing form.
261 $new = $this->data;
262 $new['id'] = $form_data['id'] ?? 0;
263 $new['settings'] = $form_data['settings'] ?? [];
264 $new['payments'] = $form_data['payments'] ?? [];
265 $new['meta'] = $form_data['meta'] ?? [];
266
267 $template_id = $this->data['meta']['template'] ?? '';
268
269 // Preserve template ID `wpforms-user-template-{$form_id}` when overwriting it with core template.
270 if ( wpforms_is_form_template( $form['ID'] ) ) {
271 $template_id = $form_data['meta']['template'] ?? '';
272 }
273
274 $new['meta']['template'] = $template_id;
275
276 /**
277 * Allow modifying form data when a template is replaced.
278 *
279 * @since 1.7.9
280 *
281 * @param array $new Updated form data.
282 * @param array $form_data Current form data.
283 * @param array $template Template data.
284 */
285 $new = (array) apply_filters( 'wpforms_templates_class_base_template_replace_modify_data', $new, $form_data, $this );
286
287 // Update the form with new data.
288 $form['post_content'] = wpforms_encode( $new );
289
290 return $form;
291 }
292
293 /**
294 * Pass information about the active template back to the builder.
295 *
296 * @since 1.0.0
297 *
298 * @param array $details Details.
299 * @param object $form Form data.
300 *
301 * @return array|void
302 */
303 public function template_active( $details, $form ) {
304
305 if ( empty( $form ) ) {
306 return;
307 }
308
309 $form_data = wpforms_decode( $form->post_content );
310
311 if ( empty( $this->modal ) || empty( $form_data['meta']['template'] ) || $this->slug !== $form_data['meta']['template'] ) {
312 return $details;
313 } else {
314 $display = $this->template_modal_conditional( $form_data );
315 }
316
317 return [
318 'name' => $this->name,
319 'slug' => $this->slug,
320 'description' => $this->description,
321 'includes' => $this->includes,
322 'icon' => $this->icon,
323 'modal' => $this->modal,
324 'modal_display' => $display,
325 ];
326 }
327
328 /**
329 * Conditional to determine if the template informational modal screens
330 * should display.
331 *
332 * @since 1.0.0
333 *
334 * @param array $form_data Form data and settings.
335 *
336 * @return bool
337 */
338 public function template_modal_conditional( $form_data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
339
340 return false;
341 }
342 }
343