| 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'] = get_current_blog_id(); |
| 76 |
} |
| 77 |
|
| 78 |
$result = $this->database->insert( |
| 79 |
$this->log_table, |
| 80 |
$args |
| 81 |
); |
| 82 |
|
| 83 |
if ( ! $result ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->database->insert_id; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get an email from the log. |
| 92 |
* |
| 93 |
* @param int $id The ID of the email to get. |
| 94 |
* |
| 95 |
* @return array|bool |
| 96 |
*/ |
| 97 |
public function get_email( $id ) { |
| 98 |
$sql = $this->database->prepare( "SELECT * FROM {$this->log_table} WHERE email_id = %d", $id ); |
| 99 |
$row = $this->database->get_row( $sql, ARRAY_A ); |
| 100 |
|
| 101 |
if ( is_null( $row ) ) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
$row = array_map( 'maybe_unserialize', $row ); |
| 106 |
|
| 107 |
return $row; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Update an email in the database. |
| 112 |
* |
| 113 |
* @param int $email_id The ID of the email to update. |
| 114 |
* @param string $key The field to update. |
| 115 |
* @param mixed $value The value of the update. |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public function update_email( $email_id, $key, $value ) { |
| 120 |
$result = $this->database->update( |
| 121 |
$this->log_table, |
| 122 |
array( $key => maybe_serialize( $value ) ), |
| 123 |
array( 'email_id' => $email_id ), |
| 124 |
array( '%s' ), |
| 125 |
array( '%d' ) |
| 126 |
); |
| 127 |
|
| 128 |
if ( ! $result ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Delete an email from the database. |
| 137 |
* |
| 138 |
* @param int $email_id The ID of the email to delete. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public function delete_email( $email_id ) { |
| 143 |
$result = $this->database->delete( |
| 144 |
$this->log_table, |
| 145 |
array( 'email_id' => $email_id ), |
| 146 |
array( '%d' ) |
| 147 |
); |
| 148 |
|
| 149 |
if ( ! $result ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Schedule the cron to clean up the log tables. |
| 158 |
*/ |
| 159 |
private function schedule_cron() { |
| 160 |
if ( ! wp_next_scheduled( 'deliciousbrains_wp_offload_ses_log_cleanup' ) ) { |
| 161 |
wp_schedule_event( strtotime( '+1 day' ), 'daily', 'deliciousbrains_wp_offload_ses_log_cleanup' ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Return the currently selected log duration. |
| 167 |
* |
| 168 |
* @return int |
| 169 |
*/ |
| 170 |
public function get_log_duration() { |
| 171 |
/** @var WP_Offload_SES $wp_offload_ses */ |
| 172 |
global $wp_offload_ses; |
| 173 |
|
| 174 |
$duration = (int) $wp_offload_ses->settings->get_setting( 'log-duration' ); |
| 175 |
|
| 176 |
if ( ! array_key_exists( $duration, self::get_log_durations() ) ) { |
| 177 |
$duration = 90; |
| 178 |
$wp_offload_ses->settings->set_setting( 'log-duration', $duration ); |
| 179 |
$wp_offload_ses->settings->save_settings(); |
| 180 |
} |
| 181 |
|
| 182 |
return self::validate_duration( $duration ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Validates the provided log duration. |
| 187 |
* |
| 188 |
* @param int $duration The duration to validate. |
| 189 |
* |
| 190 |
* @return int |
| 191 |
*/ |
| 192 |
private static function validate_duration( $duration ) { |
| 193 |
$options = array( |
| 194 |
'options' => array( |
| 195 |
'default' => 90, |
| 196 |
'min_range' => 0, |
| 197 |
), |
| 198 |
); |
| 199 |
|
| 200 |
return filter_var( $duration, FILTER_VALIDATE_INT, $options ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Return the available log durations. |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public static function get_log_durations() { |
| 209 |
$default_durations = array( |
| 210 |
'30' => __( 'After 30 days', 'wp-offload-ses' ), |
| 211 |
'60' => __( 'After 60 days', 'wp-offload-ses' ), |
| 212 |
'90' => __( 'After 90 days', 'wp-offload-ses' ), |
| 213 |
'180' => __( 'After 6 months', 'wp-offload-ses' ), |
| 214 |
'365' => __( 'After 1 year', 'wp-offload-ses' ), |
| 215 |
'730' => __( 'After 2 years', 'wp-offload-ses' ), |
| 216 |
); |
| 217 |
|
| 218 |
$log_durations = apply_filters( 'wposes_log_durations', $default_durations ); |
| 219 |
|
| 220 |
if ( ! is_array( $log_durations ) ) { |
| 221 |
return $default_durations; |
| 222 |
} |
| 223 |
|
| 224 |
foreach ( $log_durations as $duration => $text ) { |
| 225 |
if ( self::validate_duration( $duration ) !== (int) $duration ) { |
| 226 |
unset( $log_durations[ $duration ] ); |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
if ( ! is_string( $text ) || empty( $text ) ) { |
| 231 |
unset( $log_durations[ $duration ] ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
ksort( $log_durations ); |
| 236 |
|
| 237 |
return $log_durations; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Remove outdated logs. |
| 242 |
*/ |
| 243 |
public function log_cleanup() { |
| 244 |
if ( ! wp_doing_cron() ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$duration = $this->get_log_duration(); |
| 249 |
|
| 250 |
if ( 0 === $duration ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$subsite_sql = ''; |
| 255 |
|
| 256 |
if ( is_multisite() ) { |
| 257 |
$subsite_id = get_current_blog_id(); |
| 258 |
$subsite_sql = "AND $this->log_table.subsite_id = $subsite_id"; |
| 259 |
} |
| 260 |
|
| 261 |
$query = "DELETE $this->log_table, $this->clicks_table FROM $this->log_table |
| 262 |
LEFT JOIN $this->clicks_table ON $this->log_table.email_id = $this->clicks_table.email_id |
| 263 |
WHERE $this->log_table.email_created < NOW() - INTERVAL $duration DAY |
| 264 |
$subsite_sql"; |
| 265 |
$this->database->query( $query ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Install/update the log table(s) if necessary. |
| 270 |
*/ |
| 271 |
public function install_tables() { |
| 272 |
global $wpdb; |
| 273 |
|
| 274 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 275 |
|
| 276 |
$wpdb->hide_errors(); |
| 277 |
$charset_collate = $wpdb->get_charset_collate(); |
| 278 |
|
| 279 |
$sql = "CREATE TABLE {$this->log_table} ( |
| 280 |
`email_id` BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 281 |
`subsite_id` BIGINT(20), |
| 282 |
`email_to` TEXT NOT NULL, |
| 283 |
`email_subject` VARCHAR(255) NOT NULL, |
| 284 |
`email_message` LONGTEXT NOT NULL, |
| 285 |
`email_headers` LONGTEXT NOT NULL, |
| 286 |
`email_attachments` LONGTEXT NOT NULL, |
| 287 |
`email_status` VARCHAR(20) NOT NULL, |
| 288 |
`email_open_count` INT DEFAULT '0', |
| 289 |
`email_first_open_date` DATETIME, |
| 290 |
`email_last_open_date` DATETIME, |
| 291 |
`email_created` DATETIME NOT NULL, |
| 292 |
`email_sent` DATETIME, |
| 293 |
PRIMARY KEY (email_id) |
| 294 |
) $charset_collate;"; |
| 295 |
dbDelta( $sql ); |
| 296 |
} |
| 297 |
|
| 298 |
} |
| 299 |
|