| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle installation and db table creation |
| 4 |
* |
| 5 |
* @package Email Log |
| 6 |
* @subpackage Install |
| 7 |
* @author Sudar |
| 8 |
* @since 1.7 |
| 9 |
*/ |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
| 12 |
|
| 13 |
/** |
| 14 |
* Helper class to create and maintain tables |
| 15 |
* |
| 16 |
* @author Sudar |
| 17 |
*/ |
| 18 |
class Email_Log_Init { |
| 19 |
|
| 20 |
/** |
| 21 |
* Perform activation based on multisite or not |
| 22 |
* |
| 23 |
* @since 1.7 |
| 24 |
* @static |
| 25 |
* @access public |
| 26 |
* |
| 27 |
* @global object $wpdb |
| 28 |
*/ |
| 29 |
public static function on_activate( $network_wide ) { |
| 30 |
global $wpdb; |
| 31 |
|
| 32 |
if ( is_multisite() && $network_wide ) { |
| 33 |
// store the current blog id |
| 34 |
$current_blog = $wpdb->blogid; |
| 35 |
|
| 36 |
// Get all blogs in the network and activate plugin on each one |
| 37 |
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); //phpcs:ignore |
| 38 |
foreach ( $blog_ids as $blog_id ) { |
| 39 |
switch_to_blog( $blog_id ); |
| 40 |
self::create_emaillog_table(); |
| 41 |
restore_current_blog(); |
| 42 |
} |
| 43 |
} else { |
| 44 |
self::create_emaillog_table(); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create email log table when a new blog is created |
| 50 |
* |
| 51 |
* @since 1.7 |
| 52 |
* @static |
| 53 |
* @access public |
| 54 |
*/ |
| 55 |
public static function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { |
| 56 |
if ( is_plugin_active_for_network( 'email-log/email-log.php' ) ) { |
| 57 |
switch_to_blog( $blog_id ); |
| 58 |
self::create_emaillog_table(); |
| 59 |
restore_current_blog(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Delete email log table when a blog is deleted |
| 65 |
* |
| 66 |
* @since 1.7 |
| 67 |
* @static |
| 68 |
* @access public |
| 69 |
* |
| 70 |
* @global object $wpdb |
| 71 |
* @param array $tables List of tables to be deleted |
| 72 |
* @return array $tables Modified list of tables to be deleted |
| 73 |
*/ |
| 74 |
public static function on_delete_blog( $tables ) { |
| 75 |
global $wpdb; |
| 76 |
$tables[] = $wpdb->prefix . EmailLog::TABLE_NAME; |
| 77 |
return $tables; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Create email log table |
| 82 |
* |
| 83 |
* @since 1.7 |
| 84 |
* @static |
| 85 |
* @access private |
| 86 |
* |
| 87 |
* @global object $wpdb |
| 88 |
*/ |
| 89 |
private static function create_emaillog_table() { |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
$table_name = $wpdb->prefix . EmailLog::TABLE_NAME; |
| 93 |
$charset_collate = $wpdb->get_charset_collate(); |
| 94 |
|
| 95 |
if ( $wpdb->get_var( "show tables like '{$table_name}'" ) != $table_name ) { //phpcs:ignore |
| 96 |
|
| 97 |
$sql = 'CREATE TABLE ' . $table_name . ' ( |
| 98 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 99 |
to_email VARCHAR(100) NOT NULL, |
| 100 |
subject VARCHAR(250) NOT NULL, |
| 101 |
message TEXT NOT NULL, |
| 102 |
headers TEXT NOT NULL, |
| 103 |
attachments TEXT NOT NULL, |
| 104 |
sent_date timestamp NOT NULL, |
| 105 |
PRIMARY KEY (id) |
| 106 |
) ' . $charset_collate . ' ;'; |
| 107 |
|
| 108 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 109 |
dbDelta( $sql ); |
| 110 |
|
| 111 |
add_option( EmailLog::DB_OPTION_NAME, EmailLog::DB_VERSION ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// When the Plugin installed |
| 117 |
register_activation_hook( EMAIL_LOG_PLUGIN_FILE, array( 'Email_Log_Init', 'on_activate' ) ); |
| 118 |
|
| 119 |
// when a new blog is created in multisite |
| 120 |
add_action( 'wpmu_new_blog', array( 'Email_Log_Init', 'on_create_blog' ), 10, 6 ); |
| 121 |
|
| 122 |
// when a blog is deleted in multisite |
| 123 |
add_filter( 'wpmu_drop_tables', array( 'Email_Log_Init', 'on_delete_blog' ) ); |
| 124 |
?> |
| 125 |
|