1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
<?php
/** * Base form template. * * @since 1.0.0 */ abstract class WPForms_Template {
/** * Full name of the template, eg "Contact Form". * * @since 1.0.0 * @var string */ public $name;
/** * Slug of the template, eg "contact-form" - no spaces. * * @since 1.0.0 * @var string */ public $slug;
/** * Short description the template. * * @since 1.0.0 * @var string */ public $description = '';
/** * Short description of the fields included with the template. * * @since 1.0.0 * @var string */ public $includes = '';
/** * URL of the icon to display in the admin area. * * @since 1.0.0 * @var string */ public $icon = '';
/** * Array of data that is assigned to the post_content on form creation. * * @since 1.0.0 * @var array */ public $data;
/** * Priority to show in the list of available templates. * * @since 1.0.0 * @var int */ public $priority = 20;
/** * Core or additional template. * * @since 1.4.0 * @var bool */ public $core = false;
/** * Modal message to display when the template is applied. * * @since 1.0.0 * @var array */ public $modal = '';
/** * Primary class constructor. * * @since 1.0.0 */ public function __construct() {
// Bootstrap. $this->init();
$type = $this->core ? '_core' : '';
add_filter( "wpforms_form_templates{$type}", array( $this, 'template_details' ), $this->priority ); add_filter( 'wpforms_create_form_args', array( $this, 'template_data' ), 10, 2 ); add_filter( 'wpforms_save_form_args', array( $this, 'template_replace' ), 10, 3 ); add_filter( 'wpforms_builder_template_active', array( $this, 'template_active' ), 10, 2 ); }
/** * Let's get started. * * @since 1.0.0 */ public function init() { }
/** * Add basic template details to the Add New Form admin screen. * * @since 1.0.0 * * @param array $templates * * @return array */ public function template_details( $templates ) {
$templates[] = array( 'name' => $this->name, 'slug' => $this->slug, 'description' => $this->description, 'includes' => $this->includes, 'icon' => $this->icon, );
return $templates; }
/** * Add template data when form is created. * * @since 1.0.0 * * @param array $args * @param array $data * * @return array */ public function template_data( $args, $data ) {
if ( ! empty( $data ) && ! empty( $data['template'] ) ) { if ( $data['template'] === $this->slug ) { $args['post_content'] = wpforms_encode( $this->data ); } }
return $args; }
/** * Replace template on post update if triggered. * * @since 1.0.0 * * @param array $form * @param array $data * @param array $args * * @return array */ public function template_replace( $form, $data, $args ) {
if ( ! empty( $args['template'] ) ) { if ( $args['template'] === $this->slug ) { $new = $this->data; $new['settings'] = ! empty( $form['post_content']['settings'] ) ? $form['post_content']['settings'] : array(); $form['post_content'] = wpforms_encode( $new ); } }
return $form; }
/** * Pass information about the active template back to the builder. * * @since 1.0.0 * * @param array $details * @param object $form * * @return array */ public function template_active( $details, $form ) {
if ( empty( $form ) ) { return; }
$form_data = wpforms_decode( $form->post_content );
if ( empty( $this->modal ) || empty( $form_data['meta']['template'] ) || $this->slug !== $form_data['meta']['template'] ) { return $details; } else { $display = $this->template_modal_conditional( $form_data ); }
$template = array( 'name' => $this->name, 'slug' => $this->slug, 'description' => $this->description, 'includes' => $this->includes, 'icon' => $this->icon, 'modal' => $this->modal, 'modal_display' => $display, );
return $template; }
/** * Conditional to determine if the template informational modal screens * should display. * * @since 1.0.0 * * @param array $form_data Form data and settings. * * @return bool */ public function template_modal_conditional( $form_data ) {
return false; } }
|