Passed
Pull Request — master (#418)
by Brian
03:59
created

render_email_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
    exit; // Exit if accessed directly
5
}
6
7
class WPInv_Payment_Form_Elements {
8
9
    /**
10
     * @param array payment form elements
11
     */
12
    protected $elements;
13
14
    public function __construct() {
15
16
        foreach( $this->get_elements() as $element ) {
17
            $element = $element['type'];
18
19
            if ( method_exists( $this, "render_{$element}_template" ) ) {
20
                add_action( 'wpinv_payment_form_render_element_template', array( $this, "render_{$element}_template" ), 10, 2 );
21
            }
22
23
        }
24
25
    }
26
27
    /**
28
     * Returns all the elements that can be added to a form.
29
     */
30
    public function get_elements() {
31
32
        if ( ! empty( $this->elements ) ) {
33
            return $this->elements;
34
        }
35
36
        $this->elements = wpinv_get_data( 'payment-form-elements' );
37
38
        $this->elements = apply_filters( 'wpinv_filter_core_payment_form_elements', $this->elements );
39
        return $this->elements;
40
    }
41
42
    /**
43
     * Returns the restrict markup.
44
     */
45
    public function get_restrict_markup( $field, $field_type ) {
46
        $restrict = "$field.type=='$field_type'";
47
        return "v-if=\"$restrict\"";
48
    }
49
50
    /**
51
     * Renders the gateway select element template.
52
     */
53
    public function render_gateway_select_template( $field ) {
54
        $restrict = $this->get_restrict_markup( $field, 'gateway_select' );
55
        $text     = __( 'The gateway select box will appear here', 'invoicing' );
56
        echo "
57
            <div $restrict class='alert alert-info' role='alert'>
58
                <span>$text</span>
59
            </div>
60
        ";
61
    }
62
63
    /**
64
     * Renders the ip address element template.
65
     */
66
    public function render_ip_address_template( $field ) {
67
        $restrict   = $this->get_restrict_markup( $field, 'ip_address' );
68
        $ip_address = sanitize_text_field( wpinv_get_ip() );
69
        $url        = esc_url( getpaid_ip_location_url( $ip_address ) );
70
71
        echo "
72
            <div $restrict class='getpaid-ip-info'>
73
                <span>{{{$field}.text}}</span>
74
                <a target='_blank' href='$url'>$ip_address&nbsp;&nbsp;<i class='fa fa-external-link-square' aria-hidden='true'></i></a>
75
            </div>
76
        ";
77
    }
78
79
    /**
80
     * Renders the total payable element template.
81
     */
82
    public function render_total_payable_template( $field ) {
83
        $restrict = $this->get_restrict_markup( $field, 'total_payable' );
84
        $text     = __( 'The total payable amount will appear here', 'invoicing' );
85
        echo "
86
            <div $restrict class='alert alert-info' role='alert'>
87
                <span>$text</span>
88
            </div>
89
        ";
90
    }
91
92
    /**
93
     * Renders the title element template.
94
     */
95
    public function render_heading_template( $field ) {
96
        $restrict = $this->get_restrict_markup( $field, 'heading' );
97
        echo "<component :is='$field.level' $restrict v-html='$field.text'></component>";
98
    }
99
100
    /**
101
     * Renders a paragraph element template.
102
     */
103
    public function render_paragraph_template( $field ) {
104
        $restrict = $this->get_restrict_markup( $field, 'paragraph' );
105
        $label    = "$field.text";
106
        echo "<p $restrict v-html='$label' style='font-size: 16px;'></p>";
107
    }
108
109
    /**
110
     * Renders the text element template.
111
     */
112
    public function render_text_template( $field ) {
113
        $restrict = $this->get_restrict_markup( $field, 'text' );
114
        $label    = "$field.label";
115
        echo "
116
            <div $restrict class='wpinv-payment-form-field-preview'>
117
                <div class='wpinv-payment-form-field-preview-overlay'></div>
118
                <label :for='$field.id'>{{" . $label . "}}</label>
119
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='text'>
120
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
121
            </div>    
122
        ";
123
    }
124
125
    /**
126
     * Renders the price select element template.
127
     */
128
    public function render_price_select_template( $field ) {
129
        $restrict = $this->get_restrict_markup( $field, 'price_select' );
130
        ?>
131
            <div <?php echo $restrict; ?> class='wpinv-payment-form-field-preview'>
132
                <div class='wpinv-payment-form-field-preview-overlay'></div>
133
134
                <label>{{<?php echo $field; ?>.label}}</label>
135
136
                <!-- Buttons -->
137
                <div v-if='<?php echo esc_attr( $field ); ?>.select_type=="buttons"' class="getpaid-price-buttons">
138
                    <span v-for="(option, index) in <?php echo esc_attr( $field ); ?>.options.split(',')" :key="index">
139
                        <input type="radio" :id="<?php echo esc_attr( $field ); ?>.id + index" :checked="index==0" />
140
                        <label :for="<?php echo esc_attr( $field ); ?>.id + index" class="rounded">{{option | optionize}}</label>
141
                    </span>
142
                </div>
143
144
                <!-- Circles -->
145
                <div v-if='<?php echo esc_attr( $field ); ?>.select_type=="circles"' class="getpaid-price-buttons getpaid-price-circles">
146
                    <span v-for="(option, index) in <?php echo esc_attr( $field ); ?>.options.split(',')" :key="index">
147
                        <input type="radio" :id="<?php echo esc_attr( $field ); ?>.id + index" :checked="index==0" />
148
                        <label :for="<?php echo esc_attr( $field ); ?>.id + index"><span>{{option | optionize}}</span></label>
149
                    </span>
150
                </div>
151
152
                <!-- Radios -->
153
                <div v-if='<?php echo esc_attr( $field ); ?>.select_type=="radios"'>
154
                    <div v-for="(option, index) in <?php echo esc_attr( $field ); ?>.options.split(',')" :key="index">
155
                        <label>
156
                            <input type="radio" :checked="index==0" />
157
                            <span>{{option | optionize}}</span>
158
                        </label>
159
                    </div>
160
                </div>
161
162
                <!-- Checkboxes -->
163
                <div v-if='<?php echo esc_attr( $field ); ?>.select_type=="checkboxes"'>
164
                    <div v-for="(option, index) in <?php echo esc_attr( $field ); ?>.options.split(',')" :key="index">
165
                        <label>
166
                            <input type="checkbox" :checked="index==0" />
167
                            <span>{{option | optionize}}</span>
168
                        </label>
169
                    </div>
170
                </div>
171
172
                <!-- Select -->
173
                <select v-if='<?php echo esc_attr( $field ); ?>.select_type=="select"' class='form-control custom-select'>
174
                    <option v-if="<?php echo esc_attr( $field ); ?>.placeholder" selected="selected">
175
                        {{<?php echo esc_attr( $field ); ?>.placeholder}}
176
                    </option>
177
                    <option v-for="(option, index) in <?php echo esc_attr( $field ); ?>.options.split(',')" :key="index">
178
                        {{option | optionize}}
179
                    </option>
180
                </select>
181
                <small v-if='<?php echo esc_attr( $field ); ?>.description' class='form-text text-muted' v-html='<?php echo esc_attr( $field ); ?>.description'></small>
182
            </div>
183
184
        <?php
185
    }
186
187
    /**
188
     * Renders the textarea element template.
189
     */
190
    public function render_textarea_template( $field ) {
191
        $restrict = $this->get_restrict_markup( $field, 'textarea' );
192
        $label    = "$field.label";
193
        echo "
194
            <div $restrict class='wpinv-payment-form-field-preview'>
195
                <div class='wpinv-payment-form-field-preview-overlay'></div>
196
                <label :for='$field.id'>{{" . $label . "}}</label>
197
                <textarea  :placeholder='$field.placeholder' :id='$field.id' class='form-control' rows='3'></textarea>
198
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
199
            </div>    
200
        ";
201
    }
202
203
    /**
204
     * Renders the select element template.
205
     */
206
    public function render_select_template( $field ) {
207
        $restrict    = $this->get_restrict_markup( $field, 'select' );
208
        $label       = "$field.label";
209
        $placeholder = "$field.placeholder";
210
        $id          = $field . '.id';
211
        echo "
212
            <div $restrict class='wpinv-payment-form-field-preview'>
213
                <div class='wpinv-payment-form-field-preview-overlay'></div>
214
                <label :for='$id'>{{" . $label . "}}</label>
215
                <select id='$id' class='form-control custom-select'  v-model='$field.value'>
216
                    <option v-if='$placeholder' value='' disabled>{{" . $placeholder . "}}</option>
217
                    <option v-for='option in $field.options' value='option'>{{option}}</option>
218
                </select>
219
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
220
            </div>
221
        ";
222
    }
223
224
    /**
225
     * Renders the checkbox element template.
226
     */
227
    public function render_checkbox_template( $field ) {
228
        $restrict = $this->get_restrict_markup( $field, 'checkbox' );
229
        echo "
230
            <div class='form-check' $restrict>
231
                <div class='wpinv-payment-form-field-preview-overlay'></div>
232
                <input  :id='$field.id' class='form-check-input' type='checkbox' />
233
                <label class='form-check-label' :for='$field.id' v-html='$field.label'></label>
234
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
235
            </div>    
236
        ";
237
    }
238
239
    /**
240
     * Renders the radio element template.
241
     */
242
    public function render_radio_template( $field ) {
243
        $restrict    = $this->get_restrict_markup( $field, 'radio' );
244
        $label       = "$field.label";
245
        $id          = $field . '.id';
246
        echo "
247
            <div $restrict class='wpinv-payment-form-field-preview'>
248
                <div class='wpinv-payment-form-field-preview-overlay'></div>
249
                <legend class='col-form-label' v-if='$label'>{{" . $label . "}}</legend>
250
                <div class='form-check' v-for='(option, index) in $field.options'>
251
                    <input class='form-check-input' type='radio' :id='$id + index'>
252
                    <label class='form-check-label' :for='$id + index'>{{option}}</label>
253
                </div>
254
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
255
            </div>
256
        ";
257
    }
258
259
    /**
260
     * Renders the address element template.
261
     */
262
    public function render_address_template( $field ) {
263
        $restrict    = $this->get_restrict_markup( $field, 'address' );
264
265
        echo "
266
            <div class='wpinv-address-wrapper' $restrict>
267
                <draggable v-model='$field.fields' group='address_fields_preview'>
268
                    <div class='form-group address-field-preview wpinv-payment-form-field-preview' v-for='(field, index) in $field.fields' :key='field.name' v-show='field.visible'>
269
                        <div class='wpinv-payment-form-field-preview-overlay'></div>
270
                        <label :for='field.name'>{{field.label}}<span class='text-danger' v-if='field.required'> *</span></label>
271
                        <input v-if='field.name !== \"wpinv_country\" && field.name !== \"wpinv_state\"' class='form-control' type='text' :id='field.name' :placeholder='field.placeholder'>
272
                        <select v-else class='form-control' :id='field.name'>
273
                            <option v-if='field.placeholder'>{{field.placeholder}}</option>
274
                        </select>
275
                        <small v-if='field.description' class='form-text text-muted' v-html='field.description'></small>
276
                    </div>
277
                </draggable>
278
            </div>
279
        ";
280
    }
281
282
    /**
283
     * Renders the email element template.
284
     */
285
    public function render_email_template( $field ) {
286
        $restrict = $this->get_restrict_markup( $field, 'email' );
287
        $label    = "$field.label";
288
        echo "
289
            <div $restrict class='wpinv-payment-form-field-preview'>
290
                <div class='wpinv-payment-form-field-preview-overlay'></div>
291
                <label :for='$field.id'>{{" . $label . "}}</label>
292
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='email'>
293
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
294
            </div>    
295
        ";
296
    }
297
298
    /**
299
     * Renders the billing_email element template.
300
     */
301
    public function render_billing_email_template( $field ) {
302
        $restrict = $this->get_restrict_markup( $field, 'billing_email' );
303
        $label    = "$field.label";
304
        echo "
305
            <div $restrict>
306
                <label :for='$field.id'>{{" . $label . "}}</label>
307
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='email'>
308
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
309
            </div>    
310
        ";
311
    }
312
313
    /**
314
     * Renders the website element template.
315
     */
316
    public function render_website_template( $field ) {
317
        $restrict = $this->get_restrict_markup( $field, 'website' );
318
        $label    = "$field.label";
319
        echo "
320
            <div $restrict class='wpinv-payment-form-field-preview'>
321
                <div class='wpinv-payment-form-field-preview-overlay'></div>
322
                <label :for='$field.id'>{{" . $label . "}}</label>
323
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='url'>
324
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
325
            </div>    
326
        ";
327
    }
328
329
    /**
330
     * Renders the date element template.
331
     */
332
    public function render_date_template( $field ) {
333
        $restrict = $this->get_restrict_markup( $field, 'date' );
334
        $label    = "$field.label";
335
        echo "
336
            <div $restrict class='wpinv-payment-form-field-preview'>
337
                <div class='wpinv-payment-form-field-preview-overlay'></div>
338
                <label :for='$field.id'>{{" . $label . "}}</label>
339
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='date'>
340
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
341
            </div>    
342
        ";
343
    }
344
345
    /**
346
     * Renders the time element template.
347
     */
348
    public function render_time_template( $field ) {
349
        $restrict = $this->get_restrict_markup( $field, 'time' );
350
        $label    = "$field.label";
351
        echo "
352
            <div $restrict class='wpinv-payment-form-field-preview'>
353
                <div class='wpinv-payment-form-field-preview-overlay'></div>
354
                <label :for='$field.id'>{{" . $label . "}}</label>
355
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='time'>
356
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
357
            </div>    
358
        ";
359
    }
360
361
    /**
362
     * Renders the number element template.
363
     */
364
    public function render_number_template( $field ) {
365
        $restrict = $this->get_restrict_markup( $field, 'number' );
366
        $label    = "$field.label";
367
        echo "
368
            <div $restrict class='wpinv-payment-form-field-preview'>
369
                <div class='wpinv-payment-form-field-preview-overlay'></div>
370
                <label :for='$field.id'>{{" . $label . "}}</label>
371
                <input  :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='number'>
372
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
373
            </div>    
374
        ";
375
    }
376
377
    /**
378
     * Renders the separator element template.
379
     */
380
    public function render_separator_template( $field ) {
381
        $restrict = $this->get_restrict_markup( $field, 'separator' );
382
        echo "<hr class='featurette-divider' $restrict>";
383
    }
384
385
    /**
386
     * Renders the pay button element template.
387
     */
388
    public function render_pay_button_template( $field ) {
389
        $restrict = $this->get_restrict_markup( $field, 'pay_button' );
390
        $label    = "$field.label";
391
        echo "
392
            <div $restrict>
393
                <button class='form-control btn submit-button' :class='$field.class' type='submit' @click.prevent=''>{{" . $label . "}}</button>
394
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
395
            </div>    
396
        ";
397
    }
398
399
    /**
400
     * Renders the alert element template.
401
     */
402
    public function render_alert_template( $field ) {
403
        $restrict = $this->get_restrict_markup( $field, 'alert' );
404
        $text     = "$field.text";
405
        echo "
406
            <div $restrict class='alert' :class='$field.class' role='alert'>
407
                <span v-html='$text'></span>
408
                <button v-if='$field.dismissible' type='button' class='close' @click.prevent=''>
409
                    <span aria-hidden='true'>&times;</span>
410
                </button>
411
            </div>    
412
        ";
413
    }
414
415
    /**
416
     * Renders the discount element template.
417
     */
418
    public function render_discount_template( $field ) {
419
        $restrict  = $this->get_restrict_markup( $field, 'discount' );
420
        ?>
421
422
            <div <?php echo $restrict; ?> class="discount_field border rounded p-3 wpinv-payment-form-field-preview">
423
                <div class='wpinv-payment-form-field-preview-overlay'></div>
424
                <div class="discount_field_inner d-flex flex-column flex-md-row">
425
                    <input  :placeholder="<?php echo $field ?>.input_label" class="form-control mr-2 mb-2" style="flex: 1;" type="text">
426
                    <button class="btn btn-secondary submit-button mb-2" type="submit" @click.prevent="">{{<?php echo $field; ?>.button_label}}</button>
427
                </div>
428
                <small v-if='<?php echo $field ?>.description' class='form-text text-muted' v-html='<?php echo $field ?>.description'></small>
429
            </div>
430
431
        <?php
432
    }
433
434
    /**
435
     * Renders the items element template.
436
     */
437
    public function render_items_template( $field ) {
438
        $restrict  = $this->get_restrict_markup( $field, 'items' );
439
        ?>
440
441
        <div <?php echo $restrict; ?> class='item_totals'>
442
            <div v-if='!is_default'>
443
                <div v-if='! canCheckoutSeveralSubscriptions(<?php echo $field; ?>)' class='alert alert-info' role='alert'><?php _e( 'Item totals will appear here. Click to set items.', 'invoicing' ) ?></div>
444
                <div v-if='canCheckoutSeveralSubscriptions(<?php echo $field; ?>)' class='alert alert-danger' role='alert'><?php _e( 'Your form allows customers to buy several recurring items. This is not supported and might lead to unexpected behaviour.', 'invoicing' ); ?></div>
445
            </div>
446
            <div v-if='is_default'>
447
                <div class='alert alert-info' role='alert'><?php _e( 'Item totals will appear here.', 'invoicing' ) ?></div>
448
            </div>
449
        </div>
450
451
        <?php
452
    }
453
454
    /**
455
     * Returns an array of items for the currently being edited form.
456
     */
457
    public function get_form_items( $id = false ) {
458
        $form = new GetPaid_Payment_Form( $id );
459
460
        // Is this a default form?
461
        if ( $form->is_default() ) {
462
            return array();
463
        }
464
465
        return $form->get_items( 'view', 'arrays' );
466
    }
467
468
    /**
469
     * Converts form items for use.
470
     */
471
    public function convert_checkout_items( $items, $invoice ) {
472
473
        $converted = array();
474
        foreach ( $items as $item ) {
475
476
            $item_id = $item['id'];
477
            $_item   = new WPInv_Item( $item_id );
478
479
            if( ! $_item ) {
480
                continue;
481
            }
482
483
            $converted[] = array(
484
                'title'            => esc_html( wpinv_get_cart_item_name( $item ) ) . wpinv_get_item_suffix( $_item ),
485
                'id'               => $item['id'],
486
                'price'            => $item['subtotal'],
487
                'custom_price'     => $_item->get_is_dynamic_pricing(),
488
                'recurring'        => $_item->is_recurring(),
489
                'description'      => apply_filters( 'wpinv_checkout_cart_line_item_summary', '', $item, $_item, $invoice ),
490
                'minimum_price'    => $_item->get_minimum_price(),
491
                'allow_quantities' => false,
492
                'quantity'         => $item['quantity'],
493
                'required'         => true,
494
            );
495
        }
496
        return $converted;
497
498
    }
499
500
    /**
501
     * Converts an array of id => quantity for use.
502
     */
503
    public function convert_normal_items( $items ) {
504
505
        $converted = array();
506
        foreach ( $items as $item_id => $quantity ) {
507
508
            $item   = new WPInv_Item( $item_id );
509
510
            if( ! $item ) {
511
                continue;
512
            }
513
514
            $converted[] = array(
515
                'title'            => esc_html( $item->get_name() ) . wpinv_get_item_suffix( $item ),
516
                'id'               => $item_id,
517
                'price'            => $item->get_price(),
518
                'custom_price'     => $item->get_is_dynamic_pricing(),
519
                'recurring'        => $item->is_recurring(),
520
                'description'      => $item->get_summary(),
521
                'minimum_price'    => $item->get_minimum_price(),
522
                'allow_quantities' => ! empty( $quantity ),
523
                'quantity'         => empty( $quantity ) ? 1 : $quantity,
524
                'required'         => true,
525
            );
526
527
        }
528
529
        return $converted;
530
531
    }
532
533
    /**
534
     * Returns an array of elements for the currently being edited form.
535
     */
536
    public function get_form_elements( $id = false ) {
537
538
        if ( empty( $id ) ) {
539
            return wpinv_get_data( 'sample-payment-form' );
540
        }
541
        
542
        $form_elements = get_post_meta( $id, 'wpinv_form_elements', true );
543
544
        if ( is_array( $form_elements ) ) {
545
            return $form_elements;
546
        }
547
548
        return wpinv_get_data( 'sample-payment-form' );
549
    }
550
551
}
552