C:\xampp\htdocs\landing\wp-content\plugins\wpforms-lite\includes\emails\class-emails.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php

use WPForms\Tasks\Actions\EntryEmailsTask;

/**
 * Emails.
 *
 * This class handles all (notification) emails sent by WPForms.
 *
 * Heavily influenced by the great AffiliateWP plugin by Pippin Williamson.
 * https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/emails/class-affwp-emails.php
 *
 * @since 1.1.3
 */
class WPForms_WP_Emails {

    
/**
     * Store the from address.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $from_address;

    
/**
     * Store the from name.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $from_name;

    
/**
     * Store the reply-to address.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $reply_to false;

    
/**
     * Store the carbon copy addresses.
     *
     * @since 1.3.1
     *
     * @var string
     */
    
private $cc false;

    
/**
     * Store the email content type.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $content_type;

    
/**
     * Store the email headers.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $headers;

    
/**
     * Whether to send email in HTML.
     *
     * @since 1.1.3
     *
     * @var bool
     */
    
private $html true;

    
/**
     * The email template to use.
     *
     * @since 1.1.3
     *
     * @var string
     */
    
private $template;

    
/**
     * Form data and settings.
     *
     * @since 1.1.3
     *
     * @var array
     */
    
public $form_data = array();

    
/**
     * Fields, formatted, and sanitized.
     *
     * @since 1.1.3
     *
     * @var array
     */
    
public $fields = array();

    
/**
     * Entry ID.
     *
     * @since 1.2.3
     *
     * @var int
     */
    
public $entry_id '';

    
/**
     * Notification ID that is currently being processed.
     *
     * @since 1.5.7
     *
     * @var int
     */
    
public $notification_id '';

    
/**
     * Get things going.
     *
     * @since 1.1.3
     */
    
public function __construct() {

        if ( 
'none' === $this->get_template() ) {
            
$this->html false;
        }

        
add_action'wpforms_email_send_before', array( $this'send_before' ) );
        
add_action'wpforms_email_send_after', array( $this'send_after' ) );
    }

    
/**
     * Set a property.
     *
     * @since 1.1.3
     *
     * @param string $key   Object property key.
     * @param mixed  $value Object property value.
     */
    
public function __set$key$value ) {

        
$this->$key $value;
    }

    
/**
     * Get the email from name.
     *
     * @since 1.1.3
     *
     * @return string The email from name
     */
    
public function get_from_name() {

        if ( ! empty( 
$this->from_name ) ) {
            
$this->from_name $this->process_tag$this->from_name );
        } else {
            
$this->from_name get_bloginfo'name' );
        }

        return 
apply_filters'wpforms_email_from_name'wpforms_decode_string$this->from_name ), $this );
    }

    
/**
     * Get the email from address.
     *
     * @since 1.1.3
     *
     * @return string The email from address.
     */
    
public function get_from_address() {

        if ( ! empty( 
$this->from_address ) ) {
            
$this->from_address $this->process_tag$this->from_address );
        } else {
            
$this->from_address get_option'admin_email' );
        }

        return 
apply_filters'wpforms_email_from_address'wpforms_decode_string$this->from_address ), $this );
    }

    
/**
     * Get the email reply-to.
     *
     * @since 1.1.3
     *
     * @return string The email reply-to address.
     */
    
public function get_reply_to() {

        if ( ! empty( 
$this->reply_to ) ) {

            
$this->reply_to $this->process_tag$this->reply_to );

            if ( ! 
is_email$this->reply_to ) ) {
                
$this->reply_to false;
            }
        }

        return 
apply_filters'wpforms_email_reply_to'wpforms_decode_string$this->reply_to ), $this );
    }

    
/**
     * Get the email carbon copy addresses.
     *
     * @since 1.3.1
     *
     * @return string The email reply-to address.
     */
    
public function get_cc() {

        if ( ! empty( 
$this->cc ) ) {

            
$this->cc $this->process_tag$this->cc );

            
$addresses array_map'trim'explode','$this->cc ) );

            foreach ( 
$addresses as $key => $address ) {
                if ( ! 
is_email$address ) ) {
                    unset( 
$addresses$key ] );
                }
            }

            
$this->cc implode','$addresses );
        }

        return 
apply_filters'wpforms_email_cc'wpforms_decode_string$this->cc ), $this );
    }

    
/**
     * Get the email content type.
     *
     * @since 1.1.3
     *
     * @return string The email content type.
     */
    
public function get_content_type() {

        if ( ! 
$this->content_type && $this->html ) {
            
$this->content_type apply_filters'wpforms_email_default_content_type''text/html'$this );
        } elseif ( ! 
$this->html ) {
            
$this->content_type 'text/plain';
        }

        return 
apply_filters'wpforms_email_content_type'$this->content_type$this );
    }

    
/**
     * Get the email headers.
     *
     * @since 1.1.3
     *
     * @return string The email headers.
     */
    
public function get_headers() {

        if ( ! 
$this->headers ) {
            
$this->headers "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
            if ( 
$this->get_reply_to() ) {
                
$this->headers .= "Reply-To: {$this->get_reply_to()}\r\n";
            }
            if ( 
$this->get_cc() ) {
                
$this->headers .= "Cc: {$this->get_cc()}\r\n";
            }
            
$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
        }

        return 
apply_filters'wpforms_email_headers'$this->headers$this );
    }

    
/**
     * Build the email.
     *
     * @since 1.1.3
     *
     * @param string $message The email message.
     *
     * @return string
     */
    
public function build_email$message ) {

        
// Plain text email shortcut.
        
if ( false === $this->html ) {
            
$message $this->process_tag$message );
            
$message str_replace'{all_fields}'$this->wpforms_html_field_valuefalse ), $message );

            return 
apply_filters'wpforms_email_message'wpforms_decode_string$message ), $this );
        }

        
/*
         * Generate an HTML email.
         */

        
ob_start();

        
$this->get_template_part'header'$this->get_template(), true );

        
// Hooks into the email header.
        
do_action'wpforms_email_header'$this );

        
$this->get_template_part'body'$this->get_template(), true );

        
// Hooks into the email body.
        
do_action'wpforms_email_body'$this );

        
$this->get_template_part'footer'$this->get_template(), true );

        
// Hooks into the email footer.
        
do_action'wpforms_email_footer'$this );

        
$message $this->process_tag$message );
        
$message nl2br$message );

        
$body ob_get_clean();

        
$message str_replace'{email}'$message$body );
        
$message str_replace'{all_fields}'$this->wpforms_html_field_valuetrue ), $message );
        
$message make_clickable$message );

        return 
apply_filters'wpforms_email_message'$message$this );
    }

    
/**
     * Send the email.
     *
     * @since 1.1.3
     *
     * @param string $to          The To address.
     * @param string $subject     The subject line of the email.
     * @param string $message     The body of the email.
     * @param array  $attachments Attachments to the email.
     *
     * @return bool
     */
    
public function send$to$subject$message$attachments = array() ) {

        if ( ! 
did_action'init' ) && ! did_action'admin_init' ) ) {
            
_doing_it_wrong__FUNCTION__esc_html__'You cannot send emails with WPForms_WP_Emails() until init/admin_init has been reached.''wpforms-lite' ), null );

            return 
false;
        }

        
// Don't send anything if emails have been disabled.
        
if ( $this->is_email_disabled() ) {
            return 
false;
        }

        
// Don't send if email address is invalid.
        
if ( ! is_email$to ) ) {
            return 
false;
        }

        
// Hooks before email is sent.
        
do_action'wpforms_email_send_before'$this );

        
// Deprecated filter for $attachments.
        
$attachments apply_filters_deprecated(
            
'wpforms_email_attachments',
            array( 
$attachments$this ),
            
'1.5.7 of the WPForms plugin',
            
'wpforms_emails_send_email_data'
        
);

        
/*
         * Allow to filter data on per-email basis,
         * useful for localizations based on recipient email address, form settings,
         * or for specific notifications - whatever available in WPForms_WP_Emails class.
         */
        
$data apply_filters(
            
'wpforms_emails_send_email_data',
            array(
                
'to'          => $to,
                
'subject'     => $subject,
                
'message'     => $message,
                
'headers'     => $this->get_headers(),
                
'attachments' => $attachments,
            ),
            
$this
        
);

        
$send_same_process apply_filters(
            
'wpforms_tasks_entry_emails_trigger_send_same_process',
            
false,
            
$this->fields,
            ! empty( 
wpforms()->entry ) ? wpforms()->entry->get$this->entry_id ) : [],
            
$this->form_data,
            
$this->entry_id,
            
'entry'
        
);

        if (
            
$send_same_process ||
            ! empty( 
$this->form_data['settings']['disable_entries'] )
        ) {
            
// Let's do this NOW.
            
$result wp_mail(
                
$data['to'],
                
$this->get_prepared_subject$data['subject'] ),
                
$this->build_email$data['message'] ),
                
$data['headers'],
                
$data['attachments']
            );
        } else {
            
// Schedule the email.
            
$result = (bool) ( new EntryEmailsTask() )
                ->
params(
                    
$data['to'],
                    
$this->get_prepared_subject$data['subject'] ),
                    
$this->build_email$data['message'] ),
                    
$data['headers'],
                    
$data['attachments']
                )
                ->
register();
        }

        
// Hooks after the email is sent.
        
do_action'wpforms_email_send_after'$this );

        return 
$result;
    }

    
/**
     * Add filters/actions before the email is sent.
     *
     * @since 1.1.3
     */
    
public function send_before() {

        
add_filter'wp_mail_from', array( $this'get_from_address' ) );
        
add_filter'wp_mail_from_name', array( $this'get_from_name' ) );
        
add_filter'wp_mail_content_type', array( $this'get_content_type' ) );
    }

    
/**
     * Remove filters/actions after the email is sent.
     *
     * @since 1.1.3
     */
    
public function send_after() {

        
remove_filter'wp_mail_from', array( $this'get_from_address' ) );
        
remove_filter'wp_mail_from_name', array( $this'get_from_name' ) );
        
remove_filter'wp_mail_content_type', array( $this'get_content_type' ) );
    }

    
/**
     * Convert text formatted HTML. This is primarily for turning line breaks
     * into <p> and <br/> tags.
     *
     * @since 1.1.3
     *
     * @param string $message Text to convert.
     *
     * @return string
     */
    
public function text_to_html$message ) {

        if ( 
'text/html' === $this->content_type || true === $this->html ) {
            
$message wpautop$message );
        }

        return 
$message;
    }

    
/**
     * Process a smart tag.
     * Decodes entities and sanitized (keeping line breaks) by default.
     *
     * @uses wpforms_decode_string()
     *
     * @since 1.1.3
     * @since 1.6.0 Deprecated 2 params: $sanitize, $linebreaks.
     *
     * @param string $string String that may contain tags.
     *
     * @return string
     */
    
public function process_tag$string '' ) {

        return 
apply_filters'wpforms_process_smart_tags'$string$this->form_data$this->fields$this->entry_id );
    }

    
/**
     * Process the all fields smart tag if present.
     *
     * @since 1.1.3
     *
     * @param bool $is_html_email Toggle to use HTML or plaintext.
     *
     * @return string
     */
    
public function wpforms_html_field_value$is_html_email true ) { // phpcs:ignore

        
if ( empty( $this->fields ) ) {
            return 
'';
        }

        if ( empty( 
$this->form_data['fields'] ) ) {
            
$is_html_email false;
        }

        
$message '';

        if ( 
$is_html_email ) {
            
/*
             * HTML emails.
             */
            
ob_start();

            
// Hooks into the email field.
            
do_action'wpforms_email_field'$this );

            
$this->get_template_part'field'$this->get_template(), true );

            
$field_template ob_get_clean();

            
// Check to see if user has added support for field type.
            
$other_fields apply_filters'wpforms_email_display_other_fields', array(), $this );

            
$x 1;

            foreach ( 
$this->form_data['fields'] as $field_id => $field ) {

                
$field_name '';
                
$field_val  '';

                
// If the field exists in the form_data but not in the final
                // field data, then it's a non-input based field, "other fields".
                
if ( empty( $this->fields$field_id ] ) ) {

                    if ( empty( 
$other_fields ) || ! in_array$field['type'], $other_fieldstrue ) ) {
                        continue;
                    }

                    if ( 
'divider' === $field['type'] ) {
                        
$field_name = ! empty( $field['label'] ) ? str_repeat'&mdash;') . ' ' $field['label'] . ' ' str_repeat'&mdash;') : null;
                        
$field_val  = ! empty( $field['description'] ) ? $field['description'] : '';
                    } elseif ( 
'pagebreak' === $field['type'] ) {
                        if ( ! empty( 
$field['position'] ) && 'bottom' === $field['position'] ) {
                            continue;
                        }
                        
$title      = ! empty( $field['title'] ) ? $field['title'] : esc_html__'Page Break''wpforms-lite' );
                        
$field_name str_repeat'&mdash;') . ' ' $title ' ' str_repeat'&mdash;');
                    } elseif ( 
'html' === $field['type'] ) {
                        
$field_name null;
                        
$field_val  $field['code'];
                    }
                } else {

                    if (
                        ! 
apply_filters'wpforms_email_display_empty_fields'false ) &&
                        ( ! isset( 
$this->fields$field_id ]['value'] ) || (string) $this->fields$field_id ]['value'] === '' )
                    ) {
                        continue;
                    }

                    
$field_name $this->fields$field_id ]['name'];
                    
$field_val  = empty( $this->fields$field_id ]['value'] ) && ! is_numeric$this->fields$field_id ]['value'] ) ? '<em>' esc_html__'(empty)''wpforms-lite' ) . '</em>' $this->fields$field_id ]['value'];
                }

                if ( empty( 
$field_name ) && null !== $field_name ) {
                    
$field_name sprintf/* translators: %d - field ID. */
                        
esc_html__'Field ID #%d''wpforms-lite' ),
                        
absint$field['id'] )
                    );
                }

                
$field_item $field_template;

                if ( 
=== $x ) {
                    
$field_item str_replace'border-top:1px solid #dddddd;'''$field_item );
                }
                
$field_item str_replace'{field_name}'$field_name$field_item );
                
$field_item str_replace(
                    
'{field_value}',
                    
apply_filters(
                        
'wpforms_html_field_value',
                        
$field_val,
                        isset( 
$this->fields$field_id ] ) ? $this->fields$field_id ] : $field,
                        
$this->form_data,
                        
'email-html'
                    
),
                    
$field_item
                
);

                
$message .= wpautop$field_item );

                
$x ++;
            }
        } else {
            
/*
             * Plain Text emails.
             */
            
foreach ( $this->fields as $field ) {

                if (
                    ! 
apply_filters'wpforms_email_display_empty_fields'false ) &&
                    ( ! isset( 
$field['value'] ) || (string) $field['value'] === '' )
                ) {
                    continue;
                }

                
$field_val  = empty( $field['value'] ) && ! is_numeric$field['value'] ) ? esc_html__'(empty)''wpforms-lite' ) : $field['value'];
                
$field_name $field['name'];

                if ( empty( 
$field_name ) ) {
                    
$field_name sprintf/* translators: %d - field ID. */
                        
esc_html__'Field ID #%d''wpforms-lite' ),
                        
absint$field['id'] )
                    );
                }

                
$message    .= '--- ' $field_name " ---\r\n\r\n";
                
$field_value $field_val "\r\n\r\n";
                
$message    .= apply_filters'wpforms_plaintext_field_value'$field_value$field$this->form_data );
            }
        }

        if ( empty( 
$message ) ) {
            
$empty_message esc_html__'An empty form was submitted.''wpforms-lite' );
            
$message       $is_html_email wpautop$empty_message ) : $empty_message;
        }

        return 
$message;
    }

    
/**
     * Email kill switch if needed.
     *
     * @since 1.1.3
     *
     * @return bool
     */
    
public function is_email_disabled() {

        return (bool) 
apply_filters'wpforms_disable_all_emails'false$this );
    }

    
/**
     * Get the enabled email template.
     *
     * @since 1.1.3
     *
     * @return string When filtering return 'none' to switch to text/plain email.
     */
    
public function get_template() {

        if ( ! 
$this->template ) {
            
$this->template wpforms_setting'email-template''default' );
        }

        return 
apply_filters'wpforms_email_template'$this->template );
    }

    
/**
     * Retrieve a template part. Taken from bbPress.
     *
     * @since 1.1.3
     *
     * @param string $slug Template file slug.
     * @param string $name Optional. Default null.
     * @param bool   $load Maybe load.
     *
     * @return string
     */
    
public function get_template_part$slug$name null$load true ) {

        
// Setup possible parts.
        
$templates = array();
        if ( isset( 
$name ) ) {
            
$templates[] = $slug '-' $name '.php';
        }
        
$templates[] = $slug '.php';

        
// Return the part that is found.
        
return $this->locate_template$templates$loadfalse );
    }

    
/**
     * Retrieve the name of the highest priority template file that exists.
     *
     * Search in the STYLESHEETPATH before TEMPLATEPATH so that themes which
     * inherit from a parent theme can just overload one file. If the template is
     * not found in either of those, it looks in the theme-compat folder last.
     *
     * Taken from bbPress.
     *
     * @since 1.1.3
     *
     * @param string|array $template_names Template file(s) to search for, in order.
     * @param bool         $load           If true the template file will be loaded if it is found.
     * @param bool         $require_once   Whether to require_once or require. Default true.
     *                                     Has no effect if $load is false.
     *
     * @return string The template filename if one is located.
     */
    
public function locate_template$template_names$load false$require_once true ) {

        
// No file found yet.
        
$located false;

        
// Try to find a template file.
        
foreach ( (array) $template_names as $template_name ) {

            
// Continue if template is empty.
            
if ( empty( $template_name ) ) {
                continue;
            }

            
// Trim off any slashes from the template name.
            
$template_name ltrim$template_name'/' );

            
// Try locating this template file by looping through the template paths.
            
foreach ( $this->get_theme_template_paths() as $template_path ) {
                if ( 
file_exists$template_path $template_name ) ) {
                    
$located $template_path $template_name;
                    break;
                }
            }
        }

        if ( ( 
true === $load ) && ! empty( $located ) ) {
            
load_template$located$require_once );
        }

        return 
$located;
    }

    
/**
     * Return a list of paths to check for template locations
     *
     * @since 1.1.3
     *
     * @return array
     */
    
public function get_theme_template_paths() {

        
$template_dir 'wpforms-email';

        
$file_paths = array(
            
1   => trailingslashitget_stylesheet_directory() ) . $template_dir,
            
10  => trailingslashitget_template_directory() ) . $template_dir,
            
100 => WPFORMS_PLUGIN_DIR 'includes/emails/templates',
        );

        
$file_paths apply_filters'wpforms_email_template_paths'$file_paths );

        
// Sort the file paths based on priority.
        
ksort$file_pathsSORT_NUMERIC );

        return 
array_map'trailingslashit'$file_paths );
    }

    
/**
     * Perform email subject preparation: process tags, remove new lines, etc.
     *
     * @since 1.6.1
     *
     * @param string $subject Email subject to post-process.
     *
     * @return string
     */
    
private function get_prepared_subject$subject ) {

        
$subject $this->process_tag$subject );

        
$subject trimstr_replace( [ "\r\n""\r""\n" ], ' '$subject ) );

        return 
wpforms_decode_string$subject );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586