PluginProbe
WP Offload SES Lite / 1.3
WP Offload SES Lite v1.3
1.8.2 trunk 0.1.2 0.2.1 0.7.2.1 0.8.2 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
wp-ses / classes / Email-Log.php

Email-Log.php in WP Offload SES Lite 1.3, at classes/Email-Log.php

305 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 * Log emails for WP Offload SES.
4 *
5 * @author Delicious Brains
6 * @package WP Offload SES
7 */
8
9 namespace DeliciousBrains\WP_Offload_SES;
10
11 use DeliciousBrains\WP_Offload_SES\WP_Offload_SES;
12
13 /**
14 * Class Email_Log
15 *
16 * @since 1.0.0
17 */
18 class Email_Log {
19
20 /**
21 * The WordPress database class.
22 *
23 * @var \WPDB
24 */
25 private $database;
26
27 /**
28 * The table to log emails to.
29 *
30 * @var string
31 */
32 private $log_table;
33
34 /**
35 * The table to log email clicks.
36 *
37 * @var string
38 */
39 private $clicks_table;
40
41 /**
42 * Construct the log class.
43 */
44 public function __construct() {
45 global $wpdb;
46
47 $this->database = $wpdb;
48 $this->log_table = $this->database->base_prefix . 'oses_emails';
49 $this->clicks_table = $this->database->base_prefix . 'oses_clicks';
50
51 $this->schedule_cron();
52 add_action( 'deliciousbrains_wp_offload_ses_log_cleanup', array( $this, 'log_cleanup' ) );
53 }
54
55 /**
56 * Add an email to the log.
57 *
58 * @param array $atts The attributes of the email to log.
59 *
60 * @return int|bool
61 */
62 public function log_email( $atts ) {
63 $atts = array_map( 'maybe_serialize', $atts );
64 $args = array(
65 'email_to' => isset( $atts['to'] ) ? $atts['to'] : '',
66 'email_subject' => isset( $atts['subject'] ) ? $atts['subject'] : '',
67 'email_message' => isset( $atts['message'] ) ? $atts['message'] : '',
68 'email_headers' => isset( $atts['headers'] ) ? $atts['headers'] : '',
69 'email_attachments' => isset( $atts['attachments'] ) ? $atts['attachments'] : '',
70 'email_status' => 'queued',
71 'email_created' => current_time( 'mysql' ),
72 );
73
74 if ( is_multisite() ) {
75 $args['subsite_id'] = isset( $atts['subsite_id'] ) ? $atts['subsite_id'] : get_current_blog_id();
76 }
77
78 if ( isset( $atts['parent'] ) ) {
79 $args['email_parent'] = $atts['parent'];
80 }
81
82 $result = $this->database->insert(
83 $this->log_table,
84 $args
85 );
86
87 if ( ! $result ) {
88 return false;
89 }
90
91 return $this->database->insert_id;
92 }
93
94 /**
95 * Get an email from the log.
96 *
97 * @param int $id The ID of the email to get.
98 *
99 * @return array|bool
100 */
101 public function get_email( $id ) {
102 $sql = $this->database->prepare( "SELECT * FROM {$this->log_table} WHERE email_id = %d", $id );
103 $row = $this->database->get_row( $sql, ARRAY_A );
104
105 if ( is_null( $row ) ) {
106 return false;
107 }
108
109 $row = array_map( 'maybe_unserialize', $row );
110
111 return $row;
112 }
113
114 /**
115 * Update an email in the database.
116 *
117 * @param int $email_id The ID of the email to update.
118 * @param string $key The field to update.
119 * @param mixed $value The value of the update.
120 *
121 * @return bool
122 */
123 public function update_email( $email_id, $key, $value ) {
124 $result = $this->database->update(
125 $this->log_table,
126 array( $key => maybe_serialize( $value ) ),
127 array( 'email_id' => $email_id ),
128 array( '%s' ),
129 array( '%d' )
130 );
131
132 if ( ! $result ) {
133 return false;
134 }
135
136 return true;
137 }
138
139 /**
140 * Delete an email from the database.
141 *
142 * @param int $email_id The ID of the email to delete.
143 *
144 * @return bool
145 */
146 public function delete_email( $email_id ) {
147 $result = $this->database->delete(
148 $this->log_table,
149 array( 'email_id' => $email_id ),
150 array( '%d' )
151 );
152
153 if ( ! $result ) {
154 return false;
155 }
156
157 return true;
158 }
159
160 /**
161 * Schedule the cron to clean up the log tables.
162 */
163 private function schedule_cron() {
164 if ( ! wp_next_scheduled( 'deliciousbrains_wp_offload_ses_log_cleanup' ) ) {
165 wp_schedule_event( strtotime( '+1 day' ), 'daily', 'deliciousbrains_wp_offload_ses_log_cleanup' );
166 }
167 }
168
169 /**
170 * Return the currently selected log duration.
171 *
172 * @return int
173 */
174 public function get_log_duration() {
175 /** @var WP_Offload_SES $wp_offload_ses */
176 global $wp_offload_ses;
177
178 $duration = (int) $wp_offload_ses->settings->get_setting( 'log-duration' );
179
180 if ( ! array_key_exists( $duration, self::get_log_durations() ) ) {
181 $duration = 90;
182 }
183
184 return self::validate_duration( $duration );
185 }
186
187 /**
188 * Validates the provided log duration.
189 *
190 * @param int $duration The duration to validate.
191 *
192 * @return int
193 */
194 private static function validate_duration( $duration ) {
195 $options = array(
196 'options' => array(
197 'default' => 90,
198 'min_range' => 0,
199 ),
200 );
201
202 return filter_var( $duration, FILTER_VALIDATE_INT, $options );
203 }
204
205 /**
206 * Return the available log durations.
207 *
208 * @return array
209 */
210 public static function get_log_durations() {
211 $default_durations = array(
212 '30' => __( 'After 30 days', 'wp-offload-ses' ),
213 '60' => __( 'After 60 days', 'wp-offload-ses' ),
214 '90' => __( 'After 90 days', 'wp-offload-ses' ),
215 '180' => __( 'After 6 months', 'wp-offload-ses' ),
216 '365' => __( 'After 1 year', 'wp-offload-ses' ),
217 '730' => __( 'After 2 years', 'wp-offload-ses' ),
218 );
219
220 $log_durations = apply_filters( 'wposes_log_durations', $default_durations );
221
222 if ( ! is_array( $log_durations ) ) {
223 return $default_durations;
224 }
225
226 foreach ( $log_durations as $duration => $text ) {
227 if ( self::validate_duration( $duration ) !== (int) $duration ) {
228 unset( $log_durations[ $duration ] );
229 continue;
230 }
231
232 if ( ! is_string( $text ) || empty( $text ) ) {
233 unset( $log_durations[ $duration ] );
234 }
235 }
236
237 ksort( $log_durations );
238
239 return $log_durations;
240 }
241
242 /**
243 * Remove outdated logs.
244 */
245 public function log_cleanup() {
246 if ( ! wp_doing_cron() ) {
247 return;
248 }
249
250 $duration = $this->get_log_duration();
251
252 if ( 0 === $duration ) {
253 return;
254 }
255
256 $subsite_sql = '';
257
258 if ( is_multisite() ) {
259 $subsite_id = get_current_blog_id();
260 $subsite_sql = "AND $this->log_table.subsite_id = $subsite_id";
261 }
262
263 $query = "DELETE $this->log_table, $this->clicks_table FROM $this->log_table
264 LEFT JOIN $this->clicks_table ON $this->log_table.email_id = $this->clicks_table.email_id
265 WHERE $this->log_table.email_created < NOW() - INTERVAL $duration DAY
266 $subsite_sql";
267 $this->database->query( $query );
268 }
269
270 /**
271 * Install/update the log table(s) if necessary.
272 */
273 public function install_tables() {
274 global $wpdb;
275
276 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
277
278 $wpdb->hide_errors();
279 $charset_collate = $wpdb->get_charset_collate();
280
281 $sql = "CREATE TABLE {$this->log_table} (
282 `email_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
283 `subsite_id` BIGINT(20),
284 `email_to` TEXT NOT NULL,
285 `email_subject` VARCHAR(255) NOT NULL,
286 `email_message` LONGTEXT NOT NULL,
287 `email_headers` LONGTEXT NOT NULL,
288 `email_attachments` LONGTEXT NOT NULL,
289 `email_status` VARCHAR(20) NOT NULL,
290 `email_open_count` INT DEFAULT '0',
291 `email_first_open_date` DATETIME,
292 `email_last_open_date` DATETIME,
293 `email_created` DATETIME NOT NULL,
294 `email_sent` DATETIME,
295 `email_parent` BIGINT(20),
296 `auto_retries` INT DEFAULT '0',
297 `manual_retries` INT DEFAULT '0',
298 PRIMARY KEY (email_id),
299 INDEX email_subject (email_subject)
300 ) $charset_collate;";
301 dbDelta( $sql );
302 }
303
304 }
305