|
1
|
|
|
<?php namespace EmailLog\Core\DB; |
|
2
|
|
|
/** |
|
3
|
|
|
* Handle installation and db table creation |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helper class to create table. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 2.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class TableManager { |
|
14
|
|
|
|
|
15
|
|
|
/* Database table name */ |
|
16
|
|
|
const TABLE_NAME = 'email_log'; |
|
17
|
|
|
|
|
18
|
|
|
/* Database option name */ |
|
19
|
|
|
const DB_OPTION_NAME = 'email-log-db'; |
|
20
|
|
|
|
|
21
|
|
|
/* Database version */ |
|
22
|
|
|
const DB_VERSION = '0.1'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Setup hooks. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function load() { |
|
28
|
|
|
// when a new blog is created in multisite |
|
29
|
|
|
add_action( 'wpmu_new_blog', array( $this, 'on_create_blog' ), 10, 6 ); |
|
30
|
|
|
|
|
31
|
|
|
// when a blog is deleted in multisite |
|
32
|
|
|
add_filter( 'wpmu_drop_tables', array( $this, 'on_delete_blog' ) ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* On plugin activation, create table if needed. |
|
37
|
|
|
* |
|
38
|
|
|
* @global object $wpdb |
|
39
|
|
|
*/ |
|
40
|
|
|
public function on_activate( $network_wide ) { |
|
41
|
|
|
global $wpdb; |
|
42
|
|
|
|
|
43
|
|
|
if ( is_multisite() && $network_wide ) { |
|
44
|
|
|
// Note: if there are more than 10,000 blogs or |
|
45
|
|
|
// if `wp_is_large_network` filter is set, then this may fail. |
|
46
|
|
|
$sites = wp_get_sites(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ( $sites as $site ) { |
|
49
|
|
|
switch_to_blog( $site['blog_id'] ); |
|
50
|
|
|
$this->create_table(); |
|
51
|
|
|
restore_current_blog(); |
|
52
|
|
|
} |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->create_table(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create email log table when a new blog is created. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { |
|
|
|
|
|
|
62
|
|
|
if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) { |
|
63
|
|
|
switch_to_blog( $blog_id ); |
|
64
|
|
|
$this->create_table(); |
|
65
|
|
|
restore_current_blog(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add email log table to the list of tables deleted when a blog is deleted. |
|
71
|
|
|
* |
|
72
|
|
|
* @global object $wpdb |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $tables List of tables to be deleted. |
|
75
|
|
|
* @return string[] $tables Modified list of tables to be deleted. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function on_delete_blog( $tables ) { |
|
78
|
|
|
global $wpdb; |
|
79
|
|
|
|
|
80
|
|
|
$tables[] = $wpdb->prefix . self::TABLE_NAME; |
|
81
|
|
|
return $tables; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create email log table. |
|
86
|
|
|
* |
|
87
|
|
|
* @access private |
|
88
|
|
|
* |
|
89
|
|
|
* @global object $wpdb |
|
90
|
|
|
*/ |
|
91
|
|
|
private function create_table() { |
|
92
|
|
|
global $wpdb; |
|
93
|
|
|
|
|
94
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME; |
|
95
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
|
96
|
|
|
|
|
97
|
|
|
if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$sql = 'CREATE TABLE ' . $table_name . ' ( |
|
100
|
|
|
id mediumint(9) NOT NULL AUTO_INCREMENT, |
|
101
|
|
|
to_email VARCHAR(100) NOT NULL, |
|
102
|
|
|
subject VARCHAR(250) NOT NULL, |
|
103
|
|
|
message TEXT NOT NULL, |
|
104
|
|
|
headers TEXT NOT NULL, |
|
105
|
|
|
attachments TEXT NOT NULL, |
|
106
|
|
|
sent_date timestamp NOT NULL, |
|
107
|
|
|
PRIMARY KEY (id) |
|
108
|
|
|
) ' . $charset_collate . ' ;'; |
|
109
|
|
|
|
|
110
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
111
|
|
|
dbDelta( $sql ); |
|
112
|
|
|
|
|
113
|
|
|
add_option( self::DB_OPTION_NAME, self::DB_VERSION ); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.