Completed
Push — master ( e85e3d...0703ae )
by Sudar
03:41
created

TableManager::create_table()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 25
ccs 0
cts 12
cp 0
crap 6
rs 8.8571
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 LOG_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
	public function on_activate( $network_wide ) {
39
		if ( is_multisite() && $network_wide ) {
40
			// Note: if there are more than 10,000 blogs or
41
			// if `wp_is_large_network` filter is set, then this may fail.
42
			$sites = wp_get_sites();
43
44
			foreach ( $sites as $site ) {
45
				switch_to_blog( $site['blog_id'] );
46
				$this->create_table();
47
				restore_current_blog();
48
			}
49
		} else {
50
			$this->create_table();
51
		}
52
	}
53
54
	/**
55
	 * Create email log table when a new blog is created.
56
	 */
57
	public function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
58
		if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) {
59
			switch_to_blog( $blog_id );
60
			$this->create_table();
61
			restore_current_blog();
62
		}
63
	}
64
65
	/**
66
	 * Add email log table to the list of tables deleted when a blog is deleted.
67
	 *
68
	 * @param  array $tables List of tables to be deleted.
69
	 *
70
	 * @return string[]  $tables Modified list of tables to be deleted.
71
	 */
72 2
	public function on_delete_blog( $tables ) {
73 2
		$tables[] = $this->get_log_table_name();
74
75 2
		return $tables;
76
	}
77
78
	/**
79
	 * Get email log table name.
80
	 *
81
	 * @return string Email Log Table name.
82
	 */
83 4
	public function get_log_table_name() {
84 4
		global $wpdb;
85
86 4
		return $wpdb->prefix . self::LOG_TABLE_NAME;
87
	}
88
89
	/**
90
	 * Insert log data into DB.
91
	 *
92
	 * @param array $data Data to be inserted.
93
	 */
94
	public function insert_log( $data ) {
95
		global $wpdb;
96
97
		$table_name = $this->get_log_table_name();
98
		$wpdb->insert( $table_name, $data );
99
	}
100
101
	/**
102
	 * Create email log table.
103
	 *
104
	 * @access private
105
	 *
106
	 * @global object $wpdb
107
	 */
108
	private function create_table() {
109
		global $wpdb;
110
111
		$table_name      = $this->get_log_table_name();
112
		$charset_collate = $wpdb->get_charset_collate();
113
114
		if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) {
115
116
			$sql = 'CREATE TABLE ' . $table_name . ' (
117
				id mediumint(9) NOT NULL AUTO_INCREMENT,
118
				to_email VARCHAR(100) NOT NULL,
119
				subject VARCHAR(250) NOT NULL,
120
				message TEXT NOT NULL,
121
				headers TEXT NOT NULL,
122
				attachments TEXT NOT NULL,
123
				sent_date timestamp NOT NULL,
124
				PRIMARY KEY  (id)
125
			) ' . $charset_collate . ' ;';
126
127
			require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
128
			dbDelta( $sql );
129
130
			add_option( self::DB_OPTION_NAME, self::DB_VERSION );
131
		}
132
	}
133
}
134