1 | <?php namespace EmailLog\Core\DB; |
||
15 | class TableManager implements Loadie { |
||
16 | |||
17 | /* Database table name */ |
||
18 | const LOG_TABLE_NAME = 'email_log'; |
||
19 | |||
20 | /* Database option name */ |
||
21 | const DB_OPTION_NAME = 'email-log-db'; |
||
22 | |||
23 | /* Database version */ |
||
24 | const DB_VERSION = '0.2'; |
||
25 | |||
26 | /** |
||
27 | * Setup hooks. |
||
28 | */ |
||
29 | public function load() { |
||
37 | |||
38 | /** |
||
39 | * On plugin activation, create table if needed. |
||
40 | * |
||
41 | * @param bool $network_wide True if the plugin was network activated. |
||
42 | */ |
||
43 | public function on_activate( $network_wide ) { |
||
58 | |||
59 | /** |
||
60 | * Create email log table when a new blog is created. |
||
61 | * |
||
62 | * @param int $blog_id Blog Id. |
||
63 | */ |
||
64 | public function create_table_for_new_blog( $blog_id ) { |
||
71 | |||
72 | /** |
||
73 | * Add email log table to the list of tables deleted when a blog is deleted. |
||
74 | * |
||
75 | * @param array $tables List of tables to be deleted. |
||
76 | * |
||
77 | * @return string[] $tables Modified list of tables to be deleted. |
||
78 | */ |
||
79 | 1 | public function delete_table_from_deleted_blog( $tables ) { |
|
84 | |||
85 | /** |
||
86 | * Get email log table name. |
||
87 | * |
||
88 | * @return string Email Log Table name. |
||
89 | */ |
||
90 | 2 | public function get_log_table_name() { |
|
95 | |||
96 | /** |
||
97 | * Insert log data into DB. |
||
98 | * |
||
99 | * @param array $data Data to be inserted. |
||
100 | */ |
||
101 | public function insert_log( $data ) { |
||
107 | |||
108 | /** |
||
109 | * Delete log entries by ids. |
||
110 | * |
||
111 | * @param string $ids Comma separated list of log ids. |
||
112 | * |
||
113 | * @return false|int Number of log entries that got deleted. False on failure. |
||
114 | */ |
||
115 | public function delete_logs( $ids ) { |
||
125 | |||
126 | /** |
||
127 | * Delete all log entries. |
||
128 | * |
||
129 | * @return false|int Number of log entries that got deleted. False on failure. |
||
130 | */ |
||
131 | public function delete_all_logs() { |
||
138 | |||
139 | /** |
||
140 | * Deletes Email Logs older than the specified interval. |
||
141 | * |
||
142 | * @param int $interval_in_days No. of days beyond which logs are to be deleted. |
||
143 | * |
||
144 | * @return int $deleted_rows_count Count of rows deleted. |
||
145 | */ |
||
146 | public function delete_logs_older_than( $interval_in_days ) { |
||
155 | |||
156 | /** |
||
157 | * Fetch log item by ID. |
||
158 | * |
||
159 | * @param array $ids Optional. Array of IDs of the log items to be retrieved. |
||
160 | * |
||
161 | * @return array Log item(s). |
||
162 | */ |
||
163 | public function fetch_log_items_by_id( $ids = array() ) { |
||
180 | |||
181 | /** |
||
182 | * Fetch log items. |
||
183 | * |
||
184 | * @param array $request Request object. |
||
185 | * @param int $per_page Entries per page. |
||
186 | * @param int $current_page_no Current page no. |
||
187 | * |
||
188 | * @return array Log entries and total items count. |
||
189 | */ |
||
190 | public function fetch_log_items( $request, $per_page, $current_page_no ) { |
||
236 | |||
237 | /** |
||
238 | * Create email log table. |
||
239 | * |
||
240 | * @access private |
||
241 | * |
||
242 | * @global object $wpdb |
||
243 | */ |
||
244 | private function create_table_if_needed() { |
||
245 | global $wpdb; |
||
246 | |||
247 | $table_name = $this->get_log_table_name(); |
||
248 | $charset_collate = $wpdb->get_charset_collate(); |
||
249 | |||
250 | if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { |
||
251 | |||
252 | $sql = 'CREATE TABLE ' . $table_name . ' ( |
||
253 | id mediumint(9) NOT NULL AUTO_INCREMENT, |
||
254 | to_email VARCHAR(100) NOT NULL, |
||
255 | subject VARCHAR(250) NOT NULL, |
||
256 | message TEXT NOT NULL, |
||
257 | headers TEXT NOT NULL, |
||
258 | attachments TEXT NOT NULL, |
||
259 | sent_date timestamp NOT NULL, |
||
260 | attachment_name VARCHAR(1000), |
||
261 | ip_address VARCHAR(15), |
||
262 | result TINYINT(1) |
||
263 | PRIMARY KEY (id) |
||
264 | ) ' . $charset_collate . ' ;'; |
||
265 | |||
266 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
||
267 | dbDelta( $sql ); |
||
268 | |||
269 | add_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get the total number of email logs. |
||
275 | * |
||
276 | * @return int Total email log count |
||
277 | */ |
||
278 | public function get_logs_count() { |
||
285 | |||
286 | /** |
||
287 | * Updates the DB schema. |
||
288 | * |
||
289 | * Adds new columns to the Database as of v0.2. |
||
290 | * |
||
291 | * @since 2.3.0 |
||
292 | */ |
||
293 | private function update_table_if_needed() { |
||
325 | } |
||
326 |