Completed
Push — feature/45-drop-php-52 ( d61672...167909 )
by Sudar
11:21
created

TableManager::on_delete_blog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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 ) {
5 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $site_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 array  $tables Modified list of tables to be deleted.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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 ) {
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $table_name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
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