-
Notifications
You must be signed in to change notification settings - Fork 8
/
render.php
457 lines (359 loc) · 15 KB
/
render.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
* Render classes
*
* @package GEM
*/
/**
* GoDaddy Email Marketing form.
*
* @since 1.0
*/
class GEM_Form_Renderer {
/**
* Form instance number.
*
* @var int
*/
private static $loops = 0;
/**
* Generates the form.
*
* @param string $form_id Form ID.
* @param bool $echo Wether to echo the form field. Default false.
*
* @return string
*/
public function process( $form_id, $echo = false ) {
$form = GEM_Dispatcher::get_fields( (int) $form_id );
$forms = GEM_Dispatcher::get_forms();
$form_ids = array();
if ( ! empty( $forms->signups ) ) {
foreach ( $forms->signups as $signup_form ) {
$form_ids[] = $signup_form->id;
}
}
if ( ! empty( $form->fields ) && in_array( (int) $form_id, $form_ids, true ) ) :
self::$loops++;
ob_start();
?>
<div class="gem-form-wrapper" id="form-<?php echo absint( $form_id ); ?>">
<form action="<?php echo esc_url( $form->submit ); ?>" method="post" class="gem-form">
<?php do_action( 'gem_before_fields', $form_id, $form->fields ); ?>
<?php foreach ( $form->fields as $count => $field ) : ?>
<p><?php GEM_Form_Fields::dispatch_field( $field, self::$loops ); ?></p>
<?php endforeach; ?>
<?php do_action( 'gem_after_fields', $form_id, $form->fields ); ?>
<p>
<input type="hidden" name="form_id" value="<?php echo absint( $form->id ); ?>" />
<input type="submit" value="<?php echo esc_attr( $form->button_text ); ?>" class="button gem-submit" />
<span class="gem-spinner"></span>
</p>
<?php
$show_powered_by = GEM_Settings_Controls::get_option( 'display_powered_by' ) ? true : false;
if ( $show_powered_by ) :
?>
<p>
<a href="https://www.godaddy.com/online-marketing/email-marketing" rel="nofollow" target="_blank"><?php esc_html_e( 'Powered by GoDaddy', 'godaddy-email-marketing-sign-up-forms' ); ?></a>
</p>
<?php endif; ?>
</form>
</div>
<?php
$output = ob_get_clean();
if ( $echo ) {
echo $output; // xss ok
}
return $output;
endif;
}
}
/**
* GoDaddy Email Marketing form fields.
*
* @since 1.0
*/
class GEM_Form_Fields {
/**
* Form instance number.
*
* @var int
*/
private static $cycle = 0;
/**
* Dispatches the method used to display fields.
*
* @param object $field Form field.
* @param int $cycle Form instance number.
*/
public static function dispatch_field( $field, $cycle = 1 ) {
if ( ! is_object( $field ) || ! method_exists( __CLASS__, $field->type ) ) {
return;
}
self::$cycle = absint( $cycle );
if ( ! is_null( $field->field_type ) ) {
$field = self::adjust_field_type( $field );
if ( ! method_exists( __CLASS__, $field->field_type ) ) {
return;
}
call_user_func( array( __CLASS__, $field->field_type ), $field );
} else {
call_user_func( array( __CLASS__, $field->type ), $field );
}
}
/**
* Generates the ID form the form name.
*
* @param string $field_name Form name.
* @return string The form ID.
*/
public static function get_form_id( $field_name ) {
// Since HTML ID's can't exist in the same exact spelling more than once... make it special.
return sprintf( 'form_%s_%s', self::$cycle, $field_name );
}
/**
* Displays the string field.
*
* @todo How is this differnt from the GEM_Form_Fields::text_field method?
*
* @param array $args Settings field arguments.
*/
public static function string( $args ) {
$field_classes = array( 'gem-field' );
// Is this field required?
if ( $args->required ) {
$field_classes[] = 'gem-required';
}
$field_classes = (array) apply_filters( 'gem_required_field_class', $field_classes, $args );
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
<br/>
<input type="text" name="<?php echo esc_attr( $args->name ); ?>" id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" class="<?php echo esc_attr( join( ' ', $field_classes ) ); ?>" data-label="<?php echo esc_attr( $args->display ); ?>" />
<?php
}
/**
* Displays the checkbox field.
*
* @todo This appears to be deprecated.
*
* @param array $args Settings field arguments.
*/
public static function checkbox( $args ) {
$field_classes = array( 'gem-checkbox' );
// Is this field required?
if ( $args->required ) {
$field_classes[] = 'gem-required';
}
$field_classes = (array) apply_filters( 'gem_required_field_class', $field_classes, $args );
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) . $args->value ); ?>">
<input type="checkbox" value="<?php echo esc_attr( $args->value ); ?>" name="<?php echo esc_attr( $args->name ); ?>" id="<?php echo esc_attr( self::get_form_id( $args->name ) . $args->value ); ?>" class="<?php echo esc_attr( join( ' ', $field_classes ) ); ?>" data-label="<?php echo esc_attr( $args->display ); ?>" />
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
<br/>
<?php
}
/**
* Displays the checkboxes field.
*
* @param array $args Settings field arguments.
*/
public static function checkboxes( $args ) {
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
</br>
<?php
$trim_values = array( '[', ']' );
$options = $args->options;
foreach ( $trim_values as $trim ) {
$options = trim( $options, $trim );
}
$trimmed_options = array();
$options = str_replace( '"', '', $options );
$trimmed_options = explode( ',', $options );
foreach ( $trimmed_options as $key => $value ) :
?>
<input type="checkbox" data-id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" data-name="<?php echo esc_attr( $args->name ); ?>" value="<?php echo esc_attr( $value ); ?>" /> <?php echo esc_attr( $value ); ?><br>
<?php endforeach; ?>
<input type="hidden" id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" name="<?php echo esc_attr( $args->name ); ?>" value="" class="<?php echo $args->required ? 'gem-required' : ''; ?>" data-label="<?php echo esc_attr( $args->display ); ?>" />
<?php
}
/**
* Displays the select dropdown field.
*
* @param array $args Settings field arguments.
*/
public static function dropdown( $args ) {
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
</br>
<select id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" name="<?php echo esc_attr( $args->name ); ?>" class="<?php echo $args->required ? 'gem-required' : ''; ?>" data-label="<?php echo esc_attr( $args->display ); ?>">
<?php
$trim_values = array( '[', ']' );
$options = $args->options;
foreach ( $trim_values as $trim ) {
$options = trim( $options, $trim );
}
$trimmed_options = array();
$options = str_replace( '"', '', $options );
$trimmed_options = explode( ',', $options );
foreach ( $trimmed_options as $dropdown_options ) :
?>
<option value="<?php echo esc_attr( $dropdown_options ); ?>"> <?php echo esc_html( $dropdown_options ); ?><br>
<?php endforeach; ?>
</select>
<?php
}
/**
* Displays the radio buttons field.
*
* @param array $args Settings field arguments.
*/
public static function radio_buttons( $args ) {
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" class="<?php echo $args->required ? 'gem-required' : ''; ?>" data-label="<?php echo esc_attr( $args->display ); ?>" data-name="<?php echo esc_attr( $args->name ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
</br>
<?php
$trim_values = array( '[', ']' );
$options = $args->options;
foreach ( $trim_values as $trim ) {
$options = trim( $options, $trim );
}
$trimmed_options = array();
$options = str_replace( '"', '', $options );
$trimmed_options = explode( ',', $options );
foreach ( $trimmed_options as $key => $value ) :
?>
<input type="radio" id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" name="<?php echo esc_attr( $args->name ); ?>" value="<?php echo esc_attr( $value ); ?>" /> <?php echo esc_html( $value ); ?><br>
<?php
endforeach;
}
/**
* Displays the date field.
*
* @param array $args Settings field arguments.
*/
public static function date( $args ) {
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
</br>
<?php $current_year = date( 'Y' ); ?>
<span class="third">
<select fingerprint="date" data-id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" data-name="<?php echo esc_attr( $args->name ); ?>">
<option value=""> <?php esc_html_e( 'Month', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'January', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'January', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'Febuary', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'Febuary', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'March', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'March', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'April', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'April', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'May', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'May', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'June', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'June', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'July', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'July', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'August', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'August', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'September', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'September', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'October', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'October', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'November', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'November', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<option value="<?php esc_attr_e( 'December', 'godaddy-email-marketing-sign-up-forms' ); ?>"> <?php esc_html_e( 'December', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
</select>
</span>
<span class="third">
<select fingerprint="date" data-id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" data-name="<?php echo esc_attr( $args->name ); ?>">
<option value=""> <?php esc_html_e( 'Day', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<?php for ( $i = 1; $i < 32; $i++ ) : ?>
<option value="<?php echo strlen( $i ) < 2 ? '0' . esc_attr( $i ) : esc_attr( $i ); ?>"> <?php echo esc_attr( $i ); ?> </option>
<?php endfor; ?>
</select>
</span>
<span class="third">
<select fingerprint="date" data-id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" data-name="<?php echo esc_attr( $args->name ); ?>">
<option value=""> <?php esc_html_e( 'Year', 'godaddy-email-marketing-sign-up-forms' ); ?> </option>
<?php for ( $x = $current_year + 5; $x > $current_year - 81; $x-- ) : ?>
<option value="<?php echo absint( $x ); ?>"> <?php echo absint( $x ); ?> </option>
<?php endfor; ?>
</select>
</span>
<input type="hidden" id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" name="<?php echo esc_attr( $args->name ); ?>" value="" class="<?php echo $args->required ? 'gem-required' : ''; ?>" data-label="<?php echo esc_attr( $args->display ); ?>" />
<?php
}
/**
* Displays the text field.
*
* @param array $args Settings field arguments.
*/
public static function text_field( $args ) {
$field_classes = array( 'gem-field' );
// Is this field required?
if ( $args->required ) {
$field_classes[] = 'gem-required';
}
$field_classes = (array) apply_filters( 'gem_required_field_class', $field_classes, $args );
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<?php echo esc_html( $args->display ); ?>
<?php if ( $args->required && apply_filters( 'gem_required_field_indicator', true, $args ) ) : ?>
<span class="required">*</span>
<?php endif; ?>
</label>
<input type="text" name="<?php echo esc_attr( $args->name ); ?>" id="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>" class="<?php echo esc_attr( join( ' ', $field_classes ) ); ?>" data-label="<?php echo esc_attr( $args->display ); ?>" />
<?php
}
/**
* Displays the "Terms of Service" field.
*
* @param array $args Settings field arguments.
*/
public static function tos_link( $args ) {
$field_classes = array( 'gem-field' );
// Is this field required?
if ( $args->required ) {
$field_classes[] = 'gem-required';
}
$field_classes = (array) apply_filters( 'gem_required_field_class', $field_classes, $args );
$field_options = ! empty( $args->options ) ? json_decode( $args->options ) : false;
$tos_link = isset( $field_options->link ) ? $field_options->link : false;
?>
<label for="<?php echo esc_attr( self::get_form_id( $args->name ) ); ?>">
<a href="<?php echo esc_url( $tos_link ); ?>" target="_blank"><?php echo esc_html( $args->display ); ?></a>
</label>
<?php
}
/**
* Adjust a field type to reuse existing methods.
*
* @return string Field type
*/
private static function adjust_field_type( $field ) {
if ( in_array( $field->field_type, [ 'tracking_option', 'age_check' ], true ) ) {
$field->value = $field->display;
$field->field_type = 'checkbox';
}
return $field;
}
}