-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-gf-zero-spam-addon.php
More file actions
796 lines (688 loc) · 26.1 KB
/
Copy pathclass-gf-zero-spam-addon.php
File metadata and controls
796 lines (688 loc) · 26.1 KB
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<?php
if ( ! class_exists( 'GFForms' ) || ! is_callable( [ 'GFForms', 'include_addon_framework' ] ) ) {
return;
}
GFForms::include_addon_framework();
// Email rejection feature.
require_once GF_ZERO_SPAM_DIR . 'includes/class-email-rejection.php';
require_once GF_ZERO_SPAM_DIR . 'includes/class-email-rejection-settings.php';
require_once GF_ZERO_SPAM_DIR . 'includes/class-email-rejection-field-settings.php';
require_once GF_ZERO_SPAM_DIR . 'includes/class-gf-zero-spam-shield-silent-captcha.php';
/**
* @since 1.2
*/
class GF_Zero_Spam_AddOn extends GFAddOn {
/**
* @since 1.3
*
* @var string
*/
protected $_slug = 'gf-zero-spam';
/**
* @since 1.3
*
* @var string
*/
protected $_path = GF_ZERO_SPAM_BASENAME;
/**
* @since 1.3
*
* @var string
*/
protected $_full_path = GF_ZERO_SPAM_FILE;
/**
* @since 1.3
*
* @var string
*/
protected $_title = 'Gravity Forms Zero Spam';
/**
* @since 1.3
*
* @var string
*/
protected $_short_title = 'Zero Spam';
const REPORT_LAST_SENT_DATE_OPTION = 'gf_zero_spam_report_last_date';
const REPORT_CRON_HOOK_NAME = 'gf_zero_spam_send_report';
/**
* Email rejection settings instance.
*
* @since 1.5.0
*
* @var GF_Zero_Spam_Email_Rejection_Settings|null
*/
private $email_rejection_settings;
/**
* Shield silentCAPTCHA integration instance.
*/
private ?GF_Zero_Spam_Shield_Silent_Captcha $shield_silent_captcha = null;
/**
* Gets the singleton instance.
*
* @since 1.5.0
*
* @return GF_Zero_Spam_AddOn
*/
public static function get_instance() {
static $instance = null;
if ( null === $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Initializes the addon, registers hooks, and sets up features.
*
* @since 1.2
*
* @return void
*/
public function init() {
// Initialize email rejection feature before parent::init() because
// parent::init() calls plugin_settings_init() which reads plugin_settings_fields(),
// and that method needs $this->email_rejection_settings to be set.
$email_rejection = new GF_Zero_Spam_Email_Rejection();
$email_rejection->init();
$this->email_rejection_settings = new GF_Zero_Spam_Email_Rejection_Settings( $this );
$this->email_rejection_settings->init();
$email_rejection_field_settings = new GF_Zero_Spam_Email_Rejection_Field_Settings();
$email_rejection_field_settings->init();
$this->shield_silent_captcha = new GF_Zero_Spam_Shield_Silent_Captcha( $this );
$this->shield_silent_captcha->init();
parent::init();
add_filter( 'gform_form_settings_fields', [ $this, 'add_settings_field' ], 10, 2 );
add_filter( 'gform_tooltips', [ $this, 'add_tooltip' ] );
// Adding at 20 priority so anyone filtering the default priority (10) will define the default, but the
// per-form setting may still _override_ the default.
add_filter( 'gf_zero_spam_check_key_field', [ $this, 'filter_gf_zero_spam_check_key_field' ], 20, 2 );
add_filter( 'gf_zero_spam_add_key_field', [ $this, 'filter_gf_zero_spam_add_key_field' ], 20 );
add_filter( 'cron_schedules', [ $this, 'add_cron_schedules' ] );
add_action( self::REPORT_CRON_HOOK_NAME, [ $this, 'send_report' ] );
add_action( 'gform_after_submission', [ $this, 'after_submission' ] );
add_action( 'gform_update_status', [ $this, 'update_status' ], 10, 2 );
}
/**
* @inheritdoc
* @since 1.4
*/
public function get_menu_icon() {
return '<svg style="height: 28px; width: 37px; max-width: 37px" width="1358" height="1056" viewBox="0 0 1358 1056" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1058.52 262.19L767.31 478.75C729.344 507.251 683.153 522.66 635.68 522.66C588.207 522.66 542.016 507.251 504.05 478.75L212.84 262.19C204 255.619 198.133 245.806 196.529 234.909C194.924 224.012 197.714 212.925 204.285 204.085C210.856 195.245 220.669 189.378 231.566 187.774C242.463 186.169 253.55 188.959 262.39 195.53L553.6 412.13C577.276 429.899 606.078 439.505 635.68 439.505C665.282 439.505 694.084 429.899 717.76 412.13L1008.97 195.53C1013.34 192.276 1018.32 189.916 1023.6 188.585C1028.89 187.253 1034.39 186.975 1039.78 187.768C1045.17 188.56 1050.36 190.407 1055.04 193.204C1059.72 196 1063.8 199.691 1067.05 204.065C1070.31 208.439 1072.67 213.412 1074 218.698C1075.33 223.985 1075.61 229.481 1074.82 234.875C1074.02 240.269 1072.18 245.454 1069.38 250.133C1066.59 254.813 1062.89 258.896 1058.52 262.15V262.19ZM1276.96 974.8C1276.55 975.21 1276.07 975.48 1275.65 975.87C1226.01 1025.06 1159.49 1053.49 1089.62 1055.37C1019.76 1057.25 951.81 1032.45 899.59 986H189.38C84.95 986 0 901 0 796.62V189.45C0 84.99 85 0 189.38 0H1081.96C1186.43 0 1271.41 85 1271.41 189.45V580.16C1298.08 605.314 1319.45 635.55 1334.26 669.087C1349.06 702.625 1357.01 738.786 1357.63 775.442C1358.25 812.097 1351.53 848.507 1337.87 882.526C1324.21 916.546 1303.87 947.488 1278.07 973.53C1277.64 973.91 1277.37 974.39 1276.96 974.8ZM1274.6 780.04C1274.6 673.94 1188.29 587.63 1082.2 587.63C1045.54 587.597 1009.64 598.108 978.78 617.91L1244.32 883.45C1264.12 852.595 1274.63 816.7 1274.6 780.04ZM189.38 902.97H835.82C810.095 851.575 801.133 793.407 810.198 736.653C819.262 679.898 845.896 627.415 886.35 586.59C886.74 586.16 887.02 585.68 887.43 585.27C887.84 584.86 888.32 584.58 888.74 584.19C927.343 545.965 976.404 520.03 1029.73 509.658C1083.06 499.286 1138.26 504.941 1188.38 525.91V189.45C1188.35 161.235 1177.13 134.186 1157.18 114.235C1137.22 94.284 1110.17 83.0618 1081.96 83.03H189.38C161.182 83.0829 134.156 94.3155 114.227 114.265C94.2984 134.214 83.0938 161.252 83.07 189.45V796.62C83.1017 824.809 94.3111 851.835 114.24 871.772C134.169 891.708 161.191 902.928 189.38 902.97ZM1082.2 972.44C1118.86 972.467 1154.75 961.956 1185.61 942.16L920.07 676.62C900.272 707.478 889.762 743.377 889.79 780.04C889.79 886.13 976.07 972.44 1082.2 972.44Z" fill="black"/></svg>';
}
/**
* Uses global and per-form settings to determine whether to check for spam.
*
* @since 1.2
*
* @param bool $check_key_field Whether to check the key field.
* @param array $form The form object.
*
* @return array|mixed
*/
public function filter_gf_zero_spam_check_key_field( $check_key_field = true, $form = [] ) {
// The setting has been set, but it's not enabled.
if ( isset( $form['enableGFZeroSpam'] ) && empty( $form['enableGFZeroSpam'] ) ) {
return false;
}
$enabled = $this->get_plugin_setting( 'gf_zero_spam_blocking' );
if ( is_null( $enabled ) ) {
return $check_key_field;
}
return ! empty( $enabled );
}
/**
* Uses global setting to modify whether to add the spam key field to the form.
*
* @since 1.4
*
* @param bool $add_key_field Whether to add the spam key field to the form.
*
* @return bool Whether to add the spam key field to the form.
*/
public function filter_gf_zero_spam_add_key_field( $add_key_field = true ) {
$enabled = $this->get_plugin_setting( 'gf_zero_spam_blocking' );
// Not yet set.
if ( is_null( $enabled ) ) {
return $add_key_field;
}
return ! empty( $enabled );
}
/**
* Includes custom tooltip text for the Zero Spam setting in the Form Settings page.
*
* @since 1.2
*
* @param array $tooltips Key/Value pair of tooltip/tooltip text.
*
* @return array
*/
public function add_tooltip( $tooltips ) {
$tooltips['enableGFZeroSpam'] = esc_html__( 'Enable to fight spam using a simple, effective method that is more effective than the built-in anti-spam honeypot.', 'gravity-forms-zero-spam' );
return $tooltips;
}
/**
* Adds the Zero Spam field to form settings.
*
* Uses the "Spam" section (GF 2.9.21+) when available, falls back to "Form Options".
*
* @since 1.2
*
* @see https://docs.gravityforms.com/gform_form_settings_fields/
*
* @param array $fields Form Settings fields.
* @param array $form The current form.
*
* @return array
*/
public function add_settings_field( $fields, $form = [] ) {
$field = [
'name' => 'enableGFZeroSpam',
'type' => 'toggle',
'label' => esc_html__( 'Prevent spam using Gravity Forms Zero Spam', 'gravity-forms-zero-spam' ),
'tooltip' => gform_tooltip( 'enableGFZeroSpam', '', true ),
'default_value' => apply_filters( 'gf_zero_spam_check_key_field', true, $form ),
];
if ( isset( $fields['spam'] ) ) {
// Spam section added in GF 2.9.21.
$fields['spam']['fields'][] = $field;
} else {
$fields['form_options']['fields'][] = $field;
}
return $fields;
}
/**
* Registers addon global settings.
*
* @since 1.4
*
* @return array
*/
public function plugin_settings_fields() {
$spam_report_description = sprintf(
'<h3 style="margin-top: 0">%s</h3><p>%s</p><p><strong>%s</strong></p><p>%s</p><hr style="margin: 1em 0;">',
esc_html__( 'Know when entries are flagged as spam.', 'gravity-forms-zero-spam' ),
esc_html__( 'It can be hard to know when entries are being marked as spam. When enabled, this feature will send an automated spam report, giving you a summary of recent spam entries. If no spam entries have been submitted, no report will be sent.', 'gravity-forms-zero-spam' ),
esc_html__( 'This feature works with any spam filter, including the Gravity Forms spam honeypot, Gravity Forms Zero Spam, reCAPTCHA, or others.', 'gravity-forms-zero-spam' ),
strtr(
esc_html__( 'Note: Depending on site traffic, time-based reports may not always be sent at the scheduled frequency. {{link}}See how to set up a "cron" to make this more reliable{{/link}}.', 'gravity-forms-zero-spam' ),
[
'{{heading}}' => 'Spam Report',
'{{link}}' => '<a href="https://deliciousbrains.com/wp-offload-ses/doc/cron-setup/" rel="nofollow noopener noreferrer" target="_blank"><span class="screen-reader-text">' . esc_html__( 'Link opens in a new tab', 'gravity-forms-zero-spam' ) . '</span>',
'{{/link}}' => '</a>',
]
)
);
// translators: Do not translate the placeholders inside the curly brackets, like this {{placeholders}}.
$email_body = '<h2>' . esc_html_x( 'Gravity Forms Spam Report', 'The heading inside the email body.', 'gravity-forms-zero-spam' ) . '</h2>';
// translators: Do not translate the placeholders inside the curly brackets, like this {{placeholders}}.
$email_body .= wpautop( esc_html__( 'You have received {{total_spam_count}} spam entries from the following form(s):', 'gravity-forms-zero-spam' ) );
$email_body .= '{{spam_report_list}}';
// translators: Do not translate the placeholders inside the curly brackets, like this {{placeholders}}.
$email_body .= wpautop( '<em>' . esc_html__( 'To modify or disable this email, visit {{settings_link}}the Gravity Forms Zero Spam settings page{{/settings_link}}.', 'gravity-forms-zero-spam' ) . '</em>' );
$available_variables_message = wpautop( esc_html__( 'The following variables may be used:', 'gravity-forms-zero-spam' ) );
$available_variables_message .= '<ul class="ul-disc" style="margin-bottom: 1em;">';
$available_variables_message .= '<li style="list-style: disc;"><code>{{site_name}}</code> - ' . esc_html__( 'The name of this website', 'gravity-forms-zero-spam' ) . '</li>';
$available_variables_message .= '<li style="list-style: disc;"><code>{{admin_email}}</code> - ' . esc_html__( 'The email of the site administrator', 'gravity-forms-zero-spam' ) . '</li>';
$available_variables_message .= '<li style="list-style: disc;"><code>{{total_spam_count}}</code> - ' . esc_html__( 'The total number of spam emails received since the last report.', 'gravity-forms-zero-spam' ) . '</li>';
$available_variables_message .= '<li style="list-style: disc;"><code>{{spam_report_list}}</code> - ' . esc_html__( 'A list of forms and the number of spam entries since the last report.', 'gravity-forms-zero-spam' ) . '</li>';
$available_variables_message .= '<li style="list-style: disc;"><code>{{settings_link}}</code> and <code>{{/settings_link}}</code> - ' . esc_html__( 'A link to the plugin settings page. Text inside the variables will be the link text. Make sure to include both the opening and closing variables.', 'gravity-forms-zero-spam' ) . '</li>';
$available_variables_message .= '</ul>';
$sections = [
[
'title' => esc_html__( 'Spam Blocking', 'gravity-forms-zero-spam' ),
'description' => esc_html__( 'Enable to fight spam using a simple, effective method that is more effective than the built-in anti-spam honeypot.', 'gravity-forms-zero-spam' ) . ' ' . esc_html__( 'It is possible to enable or disable spam blocking on a per-form basis inside each form\'s settings.', 'gravity-forms-zero-spam' ),
'fields' => [
[
'label' => esc_html__( 'Enable Zero Spam by Default', 'gravity-forms-zero-spam' ),
'type' => 'radio',
'name' => 'gf_zero_spam_blocking',
'default_value' => '1',
'choices' => [
[
'label' => __( 'Enabled: Add Zero Spam to Gravity Forms forms', 'gravity-forms-zero-spam' ),
'value' => '1',
],
[
'label' => __( 'Disabled: Use Gravity Forms\' built-in spam prevention', 'gravity-forms-zero-spam' ),
'value' => '0',
],
],
'required' => false,
],
],
],
[
'title' => esc_html__( 'Spam Report Email', 'gravity-forms-zero-spam' ),
'description' => $spam_report_description,
'fields' => [
[
'label' => esc_html__( 'Spam Report Frequency', 'gravity-forms-zero-spam' ),
// translators: Do not translate the placeholders inside the curly brackets, like this {{placeholders}}.
'description' => wpautop( esc_html__( 'How frequently should spam report emails be sent?', 'gravity-forms-zero-spam' ) ),
'type' => 'radio',
'name' => 'gf_zero_spam_email_frequency',
'value' => '',
'choices' => [
[
'label' => __( 'Disabled', 'gravity-forms-zero-spam' ),
'value' => '',
],
[
'label' => __( 'Threshold-Based', 'gravity-forms-zero-spam' ),
'value' => 'entry_limit',
],
[
'label' => __( 'Twice Daily', 'gravity-forms-zero-spam' ),
'value' => 'twicedaily',
],
[
'label' => __( 'Daily', 'gravity-forms-zero-spam' ),
'value' => 'daily',
],
[
'label' => __( 'Weekly', 'gravity-forms-zero-spam' ),
'value' => 'weekly',
],
[
'label' => __( 'Monthly', 'gravity-forms-zero-spam' ),
'value' => 'monthly',
],
],
'required' => false,
'save_callback' => function ( $field, $value ) {
return $this->update_cron_job( $value );
},
],
[
'label' => esc_html__( 'Spam Entry Threshold', 'gravity-forms-zero-spam' ),
'description' => esc_html__( 'A spam report email will be sent when the specified number of spam entries is reached.', 'gravity-forms-zero-spam' ),
'type' => 'text',
'input_type' => 'number',
'min' => 1,
'value' => 10,
'step' => 1,
'name' => 'gf_zero_spam_entry_limit',
'dependency' => [
'live' => true,
'fields' => [
[
'field' => 'gf_zero_spam_email_frequency',
'values' => [ 'entry_limit' ],
],
],
],
'validation_callback' => function ( $field, $value ) {
if ( (int) $value < 1 ) {
$field->set_error( esc_html__( 'Entry limit has to be 1 or more.', 'gravity-forms-zero-spam' ) );
}
},
],
[
'label' => esc_html__( 'Email Address', 'gravity-forms-zero-spam' ),
'description' => esc_html__( 'Send spam report to this email address.', 'gravity-forms-zero-spam' ),
'type' => 'text',
'input_type' => 'email',
'value' => '{{admin_email}}',
'name' => 'gf_zero_spam_report_email',
'required' => true,
'validation_callback' => function ( $field, $value ) {
if ( is_email( $value ) || '{{admin_email}}' === $value ) {
return;
}
$field->set_error( esc_html__( 'The email entered is invalid.', 'gravity-forms-zero-spam' ) );
},
'dependency' => [
'live' => true,
'fields' => [
[
'field' => 'gf_zero_spam_email_frequency',
],
],
],
],
[
'name' => 'gf_zero_spam_subject',
'label' => esc_html__( 'Email Subject', 'gravity-forms-zero-spam' ),
'type' => 'text',
'value' => esc_html__( 'Your Gravity Forms spam report for {{site_name}}', 'gravity-forms-zero-spam' ),
'required' => true,
'dependency' => [
'live' => true,
'fields' => [
[
'field' => 'gf_zero_spam_email_frequency',
],
],
],
],
[
'name' => 'gf_zero_spam_message',
'label' => esc_html__( 'Email Message', 'gravity-forms-zero-spam' ),
'description' => $available_variables_message,
'type' => 'textarea',
'value' => trim( $email_body ),
'use_editor' => true,
'required' => true,
'dependency' => [
'live' => true,
'fields' => [
[
'field' => 'gf_zero_spam_email_frequency',
],
],
],
],
[
'name' => 'gf_zero_spam_test_email',
'type' => 'hidden',
'value' => '',
'save_callback' => function ( $field, $value ) {
if ( empty( $value ) ) {
return;
}
$this->send_report( [], true );
},
],
[
'name' => 'gf_zero_spam_test_email_button',
'type' => 'button',
'label' => esc_html__( 'Send Test Email & Save Settings', 'gravity-forms-zero-spam' ),
'value' => esc_html__( 'Send Email & Save Settings', 'gravity-forms-zero-spam' ),
'class' => 'button',
'onclick' => 'document.getElementById("gf_zero_spam_test_email").value = "1"; document.getElementById("gform-settings-save").click();',
],
],
],
];
// Append Email Rejection Rules section.
if ( $this->email_rejection_settings ) {
$sections = $this->email_rejection_settings->add_settings_section( $sections );
}
if ( $this->shield_silent_captcha ) {
$sections = $this->shield_silent_captcha->add_plugin_settings_fields( $sections );
}
return $sections;
}
/**
* Overrides parent to inject rules JSON from hidden input.
*
* @since 1.5.0
*
* @param array $settings The settings to save.
*
* @return void
*/
public function update_plugin_settings( $settings ) {
if ( $this->email_rejection_settings ) {
$settings = $this->email_rejection_settings->save_rules_from_post( $settings );
}
if ( $this->shield_silent_captcha ) {
$settings = $this->shield_silent_captcha->normalize_plugin_settings( $settings );
}
parent::update_plugin_settings( $settings );
}
/**
* Checks if entry limit has been reached after status update.
*
* @since 1.4
*
* @param int $entry_id The entry ID.
* @param string $property_value The new status.
*
* @return void
*/
public function update_status( $entry_id, $property_value ) {
if ( 'spam' !== $property_value ) {
return;
}
$this->check_entry_limit();
}
/**
* Checks if entry limit has been reached after submission.
*
* @since 1.4
*
* @param array $entry The entry object.
*
* @return void
*/
public function after_submission( $entry ) {
if ( 'spam' !== $entry['status'] ) {
return;
}
$this->check_entry_limit();
}
/**
* Checks if entry limit has been reached.
*
* @since 1.4
*
* @param bool $send_report Whether to send the report if the limit is reached.
*
* @return bool
*/
public function check_entry_limit( $send_report = true ) {
$frequency = $this->get_plugin_setting( 'gf_zero_spam_email_frequency' );
if ( 'entry_limit' !== $frequency ) {
return false;
}
$results = $this->get_latest_spam_entries();
$limit = $this->get_plugin_setting( 'gf_zero_spam_entry_limit' );
if ( $limit && count( $results ) < (int) $limit ) {
return false;
}
if ( $send_report ) {
$this->send_report( $results );
}
return true;
}
/**
* Adds monthly intervals to existing cron schedules.
*
* @since 1.4
*
* @param array $schedules Existing cron schedules.
*
* @return array
*/
public function add_cron_schedules( $schedules ) {
if ( isset( $schedules['monthly'] ) ) {
return $schedules;
}
$schedules['monthly'] = [
'interval' => MONTH_IN_SECONDS,
'display' => __( 'Once Monthly', 'gravity-forms-zero-spam' ),
];
return $schedules;
}
/**
* Sends spam report.
*
* @since 1.4
*
* @param array $results Spam entry results.
* @param bool $is_test Whether this is a test email.
*
* @return boolean
*/
public function send_report( $results = [], $is_test = false ) {
// When called from cron, $results will be empty.
if ( empty( $results ) ) {
$results = $this->get_latest_spam_entries();
}
if ( empty( $results ) && ! $is_test ) {
return false;
}
if ( $is_test ) {
$settings = $this->get_posted_settings();
} else {
$settings = $this->get_plugin_settings();
}
$email = rgar( $settings, 'gf_zero_spam_report_email' );
if ( empty( $email ) ) {
return false;
}
$email = $this->replace_tags( $email );
if ( ! is_email( $email ) ) {
return false;
}
$subject = rgar( $settings, 'gf_zero_spam_subject' );
$message = rgar( $settings, 'gf_zero_spam_message' );
if ( '' === $subject || '' === $message ) {
return false;
}
$subject = $this->replace_tags( $subject );
$message = $this->replace_tags( $message );
$message = wpautop( $message );
$headers = [ 'Content-type' => 'Content-type: text/html; charset=' . esc_attr( get_option( 'blog_charset' ) ) ];
$success = wp_mail( $email, $subject, $message, $headers );
// Don't log or update last sent date when sending test email.
if ( $is_test ) {
return $success;
}
if ( $success ) {
$this->log_debug( __METHOD__ . '(): Spam report email sent successfully.' );
update_option( self::REPORT_LAST_SENT_DATE_OPTION, time() );
} else {
$this->log_error( __METHOD__ . '(): Spam report email failed to send.' );
}
return false;
}
/**
* Replaces tags in email template.
*
* @since 1.4
*
* @param string $value The template string with tags.
*
* @return string
*/
private function replace_tags( $value = '' ) {
$replace = [
'{{site_name}}' => get_bloginfo( 'name' ),
'{{admin_email}}' => get_bloginfo( 'admin_email' ),
'{{total_spam_count}}' => (string) $this->get_spam_count(),
'{{spam_report_list}}' => $this->get_report_list(),
'{{settings_link}}' => '<a href="' . esc_url( admin_url( 'admin.php?page=gf_settings&subview=gf-zero-spam' ) ) . '">',
'{{/settings_link}}' => '</a>',
];
return strtr( $value, $replace );
}
/**
* Gets latest spam entries.
*
* @since 1.4
*
* @return array
*/
private function get_latest_spam_entries() {
global $wpdb;
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared on the line above.
return $wpdb->get_results(
$wpdb->prepare( "SELECT `id`, `form_id` FROM {$wpdb->prefix}gf_entry WHERE `status`=%s AND `date_created` >= %s ORDER BY `form_id`", 'spam', $this->get_last_report_date( 'Y-m-d H:i:s' ) ),
ARRAY_A
);
}
/**
* Returns the date the last report was set.
*
* @since 1.4
*
* @param string $date_format Date format to return.
*
* @return string Date in format passed by $date_format.
*/
private function get_last_report_date( $date_format = 'Y-m-d' ) {
$last_report_timestamp = get_option( self::REPORT_LAST_SENT_DATE_OPTION, time() );
return gmdate( $date_format, $last_report_timestamp );
}
/**
* Gets report list.
*
* @since 1.4
*
* @return string HTML list of spam entries.
*/
private function get_report_list() {
$results = $this->get_latest_spam_entries();
if ( empty( $results ) ) {
return '';
}
$counted_results = [];
foreach ( $results as $result ) {
if ( isset( $counted_results[ $result['form_id'] ] ) ) {
++$counted_results[ $result['form_id'] ];
} else {
$counted_results[ $result['form_id'] ] = 1;
}
}
$last_date = $this->get_last_report_date( 'Y-m-d' );
$results_output = [];
foreach ( $counted_results as $form_id => $count ) {
$form_info = GFFormsModel::get_form( $form_id );
// Don't include forms that are in the trash.
if ( ! $form_info ) {
continue;
}
$args = [
'id' => $form_id,
'filter' => 'spam',
];
// If last report date is set, the link will go to spam entries submitted after that date.
if ( $last_date ) {
$args['s'] = $last_date;
$args['field_id'] = 'date_created';
$args['orderby'] = '0';
$args['order'] = 'ASC';
$args['operator'] = '>';
}
$link = add_query_arg(
$args,
admin_url( 'admin.php?page=gf_entries&view=entries' )
);
$count = (int) $count;
$results_output[] = strtr(
'{{form_link}}: {{count}} {{new_entries}}',
[
'{{form_link}}' => '<a href="' . esc_url( $link ) . '">' . esc_html( $form_info->title ) . '</a>',
'{{count}}' => $count,
'{{new_entries}}' => _n( 'spam entry', 'spam entries', $count, 'gravity-forms-zero-spam' ),
]
);
}
$output = '<ul><li>' . implode( '</li><li>', $results_output ) . '</li></ul>';
return $output;
}
/**
* Gets spam count.
*
* @since 1.4
*
* @return int $count The number of spam entries since the last report was sent.
*/
private function get_spam_count() {
$results = $this->get_latest_spam_entries();
return count( $results );
}
/**
* Adds cron job for spam reporting.
*
* @since 1.4
*
* @param string $frequency The frequency of the cron job.
*
* @return string
*/
public function update_cron_job( $frequency ) {
// Always remove the existing cron job.
wp_clear_scheduled_hook( self::REPORT_CRON_HOOK_NAME );
if ( empty( $frequency ) ) {
return $frequency;
}
if ( 'entry_limit' === $frequency ) {
return $frequency;
}
wp_schedule_event( time(), $frequency, self::REPORT_CRON_HOOK_NAME );
return $frequency;
}
}
GF_Zero_Spam_AddOn::get_instance();