PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.13
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.13
2.12.7 2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 All 97 releases
sureforms / inc / global-settings / email-summary.php

email-summary.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.13, at inc/global-settings/email-summary.php

243 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email Summaries.
4 *
5 * @package sureforms.
6 * @since 0.0.2
7 */
8
9 namespace SRFM\Inc\Global_Settings;
10
11 use WP_REST_Response;
12 use WP_REST_Request;
13 use WP_Query;
14 use SRFM\Inc\Traits\Get_Instance;
15 use SRFM\Inc\Helper;
16
17 /**
18 * Email Summary Class.
19 *
20 * @since 0.0.2
21 */
22 class Email_Summary {
23 use Get_Instance;
24
25 /**
26 * Constructor
27 *
28 * @since 0.0.1
29 */
30 public function __construct() {
31 add_action( 'srfm_weekly_scheduled_events', [ $this, 'send_entries_to_admin' ] );
32 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
33 }
34
35 /**
36 * API endpoint to send test email.
37 *
38 * @return void
39 * @since 0.0.2
40 */
41 public function register_custom_endpoint() {
42 $sureforms_helper = new Helper();
43 register_rest_route(
44 'sureforms/v1',
45 '/send-test-email-summary',
46 [
47 'methods' => 'POST',
48 'callback' => [ $this, 'send_test_email' ],
49 'permission_callback' => [ $sureforms_helper, 'get_items_permissions_check' ],
50 ]
51 );
52 }
53
54 /**
55 * Send test email.
56 *
57 * @param WP_REST_Request $request Request object.
58 * @return WP_REST_Response
59 * @since 0.0.2
60 */
61 public function send_test_email( $request ) {
62 $data = $request->get_body();
63 $data = json_decode( $data, true );
64
65 $email_send_to = '';
66
67 if ( is_array( $data ) && isset( $data['srfm_email_sent_to'] ) && is_string( $data['srfm_email_sent_to'] ) ) {
68 $email_send_to = $data['srfm_email_sent_to'];
69 }
70
71 $get_email_summary_options = [
72 'srfm_email_sent_to' => $email_send_to,
73 ];
74
75 self::send_entries_to_admin( $get_email_summary_options );
76
77 return new WP_REST_Response(
78 [
79 'data' => __( 'Test Email Sent Successfully.', 'sureforms' ),
80 ]
81 );
82 }
83
84 /**
85 * Function to get the total number of entries for the last week.
86 *
87 * @since 0.0.2
88 * @return string HTML table with entries count.
89 */
90 public static function get_total_entries_for_week() {
91 $args = [
92 'post_type' => SRFM_FORMS_POST_TYPE,
93 'posts_per_page' => -1,
94 ];
95
96 $query = new WP_Query( $args );
97
98 $admin_user_name = get_user_by( 'id', 1 ) ? get_user_by( 'id', 1 )->display_name : 'Admin';
99
100 $table_html = '<b>' . __( 'Hello', 'sureforms' ) . ' ' . $admin_user_name . ',</b><br><br>';
101 $table_html .= '<span>' . __( 'Let\'s see how your forms performed in the last week', 'sureforms' ) . '</span><br><br>';
102 $table_html .= '<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">';
103 $table_html .= '<thead>';
104 $table_html .= '<tr style="background-color: #333; color: #fff; text-align: left;">';
105 $table_html .= '<th style="padding: 10px;">' . __( 'Form Name', 'sureforms' ) . '</th>';
106 $table_html .= '<th style="padding: 10px;">' . __( 'Entries', 'sureforms' ) . '</th>';
107 $table_html .= '</tr>';
108 $table_html .= '</thead>';
109 $table_html .= '<tbody>';
110
111 if ( $query->have_posts() ) {
112 $row_index = 0;
113 while ( $query->have_posts() ) {
114 $query->the_post();
115 global $post;
116
117 $post_id_formatted = strval( $post->ID );
118
119 $previous_week_start = gmdate( 'Y-m-d', strtotime( '-1 week last monday' ) );
120 $previous_week_end = gmdate( 'Y-m-d', strtotime( '-1 week next sunday' ) );
121
122 $taxonomy = 'sureforms_tax';
123 $entries_args = [
124 'post_type' => SRFM_ENTRIES_POST_TYPE,
125 'tax_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query. --We require tax_query for this function to work.
126 [
127 'taxonomy' => $taxonomy,
128 'field' => 'slug',
129 'terms' => $post_id_formatted,
130 ],
131 ],
132 'date_query' => [
133 [
134 'after' => $previous_week_start,
135 'before' => $previous_week_end,
136 'inclusive' => true,
137 ],
138 ],
139 ];
140 $entries_query = new WP_Query( $entries_args );
141 $entry_count = $entries_query->post_count;
142
143 $bg_color = 0 === $row_index % 2 ? '#ffffff' : '#f2f2f2;';
144
145 $table_html .= '<tr style="background-color: ' . $bg_color . ';">';
146 $table_html .= '<td style="padding: 10px;">' . esc_html( get_the_title() ) . '</td>';
147 $table_html .= '<td style="padding: 10px;">' . esc_html( Helper::get_string_value( $entry_count ) ) . '</td>';
148 $table_html .= '</tr>';
149
150 $row_index++;
151 }
152 } else {
153 $table_html .= '<tr>';
154 $table_html .= '<td colspan="2" style="padding: 10px;">' . __( 'No forms found.', 'sureforms' ) . '</td>';
155 $table_html .= '</tr>';
156 }
157
158 $table_html .= '</tbody>';
159 $table_html .= '</table>';
160
161 wp_reset_postdata();
162
163 return $table_html;
164 }
165
166 /**
167 * Function to send the entries to admin mail.
168 *
169 * @param array<mixed>|bool $email_summary_options Email Summary Options.
170 * @since 0.0.2
171 * @return void
172 */
173 public static function send_entries_to_admin( $email_summary_options ) {
174 $entries_count_table = self::get_total_entries_for_week();
175
176 $recipients_string = '';
177
178 if ( is_array( $email_summary_options ) && isset( $email_summary_options['srfm_email_sent_to'] ) && is_string( $email_summary_options['srfm_email_sent_to'] ) ) {
179 $recipients_string = $email_summary_options['srfm_email_sent_to'];
180 }
181
182 $recipients = $recipients_string ? explode( ',', $recipients_string ) : [];
183
184 $site_title = get_bloginfo( 'name' );
185
186 $subject = __( 'SureForms Email Summary - ', 'sureforms' ) . $site_title;
187 $message = $entries_count_table;
188 $headers = [
189 'Content-Type: text/html; charset=UTF-8',
190 'From: ' . get_option( 'admin_email' ),
191 ];
192
193 wp_mail( $recipients, $subject, $message, $headers );
194 }
195
196 /**
197 * Schedule the event action to run weekly.
198 *
199 * @return void
200 * @since 0.0.2
201 */
202 public static function schedule_weekly_entries_email() {
203 $email_summary_options = get_option( 'srfm_email_summary_settings_options' );
204
205 $time = apply_filters( 'srfm_weekly_email_summary_time', '09:00:00' );
206
207 if ( wp_next_scheduled( 'srfm_weekly_scheduled_events' ) ) {
208 wp_clear_scheduled_hook( 'srfm_weekly_scheduled_events' );
209 }
210
211 $day = __( 'Monday', 'sureforms' );
212
213 if ( is_array( $email_summary_options ) && isset( $email_summary_options['srfm_schedule_report'] ) && is_string( $email_summary_options['srfm_schedule_report'] ) ) {
214 $day = Helper::get_string_value( $email_summary_options['srfm_schedule_report'] );
215 }
216
217 $current_time = time();
218 $current_time_user_timezone = Helper::get_integer_value( strtotime( gmdate( 'Y-m-d H:i:s', $current_time ) ) );
219
220 if ( ! preg_match( '/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/', $time ) ) {
221 $time = '09:00:00';
222 }
223
224 $next_day_user_timezone = Helper::get_integer_value( strtotime( "next $day $time", $current_time_user_timezone ) );
225
226 $scheduled_time = Helper::get_integer_value( strtotime( gmdate( 'Y-m-d H:i:s', $next_day_user_timezone ) ) );
227
228 if ( false === as_has_scheduled_action( 'srfm_weekly_scheduled_events' ) ) {
229 as_schedule_recurring_action(
230 $scheduled_time,
231 WEEK_IN_SECONDS,
232 'srfm_weekly_scheduled_events',
233 [
234 'email_summary_options' => $email_summary_options,
235 ],
236 'sureforms',
237 true
238 );
239 }
240 }
241
242 }
243