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
|
<?php namespace WPForms\Emails;
/** * Email Summaries main class. * * @since 1.5.4 */ class Summaries {
/** * Constructor. * * @since 1.5.4 */ public function __construct() {
$this->hooks();
$summaries_disabled = $this->is_disabled();
if ( $summaries_disabled && \wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) { \wp_clear_scheduled_hook( 'wpforms_email_summaries_cron' ); }
if ( ! $summaries_disabled && ! \wp_next_scheduled( 'wpforms_email_summaries_cron' ) ) { \wp_schedule_event( $this->get_first_cron_date_gmt(), 'wpforms_email_summaries_weekly', 'wpforms_email_summaries_cron' ); } }
/** * Get the instance of a class and store it in itself. * * @since 1.5.4 */ public static function get_instance() {
static $instance;
if ( ! $instance ) { $instance = new self(); }
return $instance; }
/** * Email Summaries hooks. * * @since 1.5.4 */ public function hooks() {
\add_filter( 'wpforms_settings_defaults', array( $this, 'disable_summaries_setting' ) ); \add_action( 'wpforms_settings_updated', array( $this, 'deregister_fetch_info_blocks_task' ) );
if ( ! $this->is_disabled() ) { \add_action( 'init', array( $this, 'preview' ) ); \add_filter( 'cron_schedules', array( $this, 'add_weekly_cron_schedule' ) ); \add_action( 'wpforms_email_summaries_cron', array( $this, 'cron' ) ); \add_filter( 'wpforms_tasks_get_tasks', array( $this, 'register_fetch_info_blocks_task' ) ); } }
/** * Check if Email Summaries are disabled in settings. * * @since 1.5.4 * * @return bool */ protected function is_disabled() {
return (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', (bool) \wpforms_setting( 'email-summaries-disable' ) ); }
/** * Add "Disable Email Summaries" to WPForms settings. * * @since 1.5.4 * * @param array $settings WPForms settings. * * @return mixed */ public function disable_summaries_setting( $settings ) {
if ( (bool) apply_filters( 'wpforms_emails_summaries_is_disabled', false ) ) { return $settings; }
$url = \add_query_arg( array( 'wpforms_email_template' => 'summary', 'wpforms_email_preview' => '1', ), \admin_url() );
$desc = \esc_html__( 'Disable Email Summaries weekly delivery.', 'wpforms-lite' );
if ( ! $this->is_disabled() ) { $desc .= '<br><a href="' . $url . '" target="_blank">' . \esc_html__( 'View Email Summary Example', 'wpforms-lite' ) . '</a>'; }
$settings['misc']['email-summaries-disable'] = array( 'id' => 'email-summaries-disable', 'name' => \esc_html__( 'Disable Email Summaries', 'wpforms-lite' ), 'desc' => $desc, 'type' => 'checkbox', );
return $settings; }
/** * Preview Email Summary. * * @since 1.5.4 */ public function preview() {
if ( ! \wpforms_current_user_can() ) { return; }
if ( ! isset( $_GET['wpforms_email_preview'], $_GET['wpforms_email_template'] ) ) { // phpcs:ignore return; }
if ( 'summary' !== $_GET['wpforms_email_template'] ) { // phpcs:ignore return; }
$args = array( 'body' => array( 'entries' => $this->get_entries(), 'info_block' => ( new InfoBlocks() )->get_next(), ), );
$template = ( new Templates\Summary() )->set_args( $args ); $template = \apply_filters( 'wpforms_emails_summaries_template', $template );
$content = $template->get();
if ( 'default' !== \wpforms_setting( 'email-template', 'default' ) ) { $content = \wpautop( $content ); }
echo $content; // phpcs:ignore
exit; }
/** * Get next cron occurrence date. * * @since 1.5.4 * * @return int */ protected function get_first_cron_date_gmt() {
$date = \absint( \strtotime( 'next monday 2pm' ) - ( \get_option( 'gmt_offset' ) * \HOUR_IN_SECONDS ) );
return $date ? $date : \time(); }
/** * Add custom Email Summaries cron schedule. * * @since 1.5.4 * * @param array $schedules WP cron schedules. * * @return array */ public function add_weekly_cron_schedule( $schedules ) {
$schedules['wpforms_email_summaries_weekly'] = array( 'interval' => \WEEK_IN_SECONDS, 'display' => \esc_html__( 'Weekly WPForms Email Summaries', 'wpforms-lite' ), );
return $schedules; }
/** * Email Summaries cron callback. * * @since 1.5.4 */ public function cron() {
$entries = $this->get_entries();
// Email won't be sent if there are no form entries. if ( empty( $entries ) ) { return; }
$info_blocks = new InfoBlocks();
$next_block = $info_blocks->get_next();
$args = array( 'body' => array( 'entries' => $entries, 'info_block' => $next_block, ), );
$template = ( new Templates\Summary() )->set_args( $args ); $template = \apply_filters( 'wpforms_emails_summaries_template', $template );
$content = $template->get();
if ( ! $content ) { return; }
$to_email = \apply_filters( 'wpforms_emails_summaries_cron_to_email', \get_option( 'admin_email' ) ); $subject = \apply_filters( 'wpforms_emails_summaries_cron_subject', \esc_html__( 'WPForms Summary', 'wpforms-lite' ) );
$sent = ( new Mailer() ) ->template( $template ) ->subject( $subject ) ->to_email( $to_email ) ->send();
if ( true === $sent ) { $info_blocks->register_sent( $next_block ); } }
/** * Get form entries. * * @since 1.5.4 * * @return array */ protected function get_entries() {
if ( \wpforms()->pro ) { $entries_count = new \WPForms\Pro\Reports\EntriesCount(); $results = $entries_count->get_by( 'form', 0, 7, 'previous sunday' ); } else { $entries_count = new \WPForms\Lite\Reports\EntriesCount(); $results = $entries_count->get_by_form(); }
return $results; }
/** * Register Action Scheduler task to fetch and cache Info Blocks. * * @since 1.6.4 * * @param \WPForms\Tasks\Task[] $tasks List of task classes. * * @return array */ public static function register_fetch_info_blocks_task( $tasks ) {
$tasks[] = FetchInfoBlocksTask::class;
return $tasks; }
/** * Deregister Action Scheduler task to fetch and cache Info Blocks. * * @since 1.6.4 */ public function deregister_fetch_info_blocks_task() {
if ( ! $this->is_disabled() ) { return; }
// Deregister the task. ( new FetchInfoBlocksTask() )->cancel();
// Delete last run time record. delete_option( FetchInfoBlocksTask::LAST_RUN );
// Remove the cache file if it exists. $file_name = ( new InfoBlocks() )->get_cache_file_path(); if ( file_exists( $file_name ) ) { @unlink( $file_name ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged } } }
|