| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace forge12\contactform7\CF7DoubleOptIn; |
| 4 | 4 | |
| 5 | +use Forge12\Shared\Logger; | |
| 6 | + | |
| 5 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | 8 | exit; |
| 7 | 9 | } |
| 8 | 10 | |
| @@ -13,11 +15,20 @@ | ||
| 13 | 15 | */ |
| 14 | 16 | function createTableOptIn() { |
| 15 | 17 | global $wpdb; |
| 16 | 18 | |
| 19 | + $logger = Logger::getInstance(); | |
| 20 | + | |
| 17 | 21 | $tableName = 'f12_cf7_doubleoptin'; |
| 18 | 22 | $wpTableName = $wpdb->prefix . $tableName; |
| 19 | 23 | |
| 24 | + $logger->info( 'Creating database table', [ | |
| 25 | + 'plugin' => 'double-opt-in', | |
| 26 | + 'table_name' => $wpTableName, | |
| 27 | + 'class' => __CLASS__ ?? null, | |
| 28 | + 'method' => __FUNCTION__, | |
| 29 | + ] ); | |
| 30 | + | |
| 20 | 31 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 21 | 32 | |
| 22 | 33 | $sql = "CREATE TABLE " . $wpTableName . " ( |
| 23 | 34 | id int(11) NOT NULL auto_increment, |
| @@ -35,13 +46,26 @@ | ||
| 35 | 46 | category int(11), |
| 36 | 47 | email varchar(255), |
| 37 | 48 | form LONGTEXT, |
| 38 | 49 | mail_optin LONGTEXT, |
| 50 | + consent_text TEXT, | |
| 51 | + consent_field varchar(64) DEFAULT '', | |
| 52 | + reminder_sent_at varchar(255) DEFAULT '', | |
| 53 | + mail_reminder LONGTEXT, | |
| 39 | 54 | PRIMARY KEY (id) |
| 40 | - )"; | |
| 55 | + )"; | |
| 56 | + | |
| 41 | 57 | dbDelta( $sql ); |
| 58 | + | |
| 59 | + $logger->info( 'Database table created or updated', [ | |
| 60 | + 'plugin' => 'double-opt-in', | |
| 61 | + 'table_name' => $wpTableName, | |
| 62 | + 'class' => __CLASS__ ?? null, | |
| 63 | + 'method' => __FUNCTION__, | |
| 64 | + ] ); | |
| 42 | 65 | } |
| 43 | 66 | |
| 67 | + | |
| 44 | 68 | /** |
| 45 | 69 | * Create the table for the categories |
| 46 | 70 | * |
| 47 | 71 | * @return void |
| @@ -48,11 +72,20 @@ | ||
| 48 | 72 | */ |
| 49 | 73 | function createTableOptinCategories() { |
| 50 | 74 | global $wpdb; |
| 51 | 75 | |
| 76 | + $logger = Logger::getInstance(); | |
| 77 | + | |
| 52 | 78 | $tableName = 'f12_cf7_doubleoptin_categories'; |
| 53 | 79 | $wpTableName = $wpdb->prefix . $tableName; |
| 54 | 80 | |
| 81 | + $logger->info( 'Creating database table', [ | |
| 82 | + 'plugin' => 'double-opt-in', | |
| 83 | + 'table_name' => $wpTableName, | |
| 84 | + 'class' => __CLASS__ ?? null, | |
| 85 | + 'method' => __FUNCTION__, | |
| 86 | + ] ); | |
| 87 | + | |
| 55 | 88 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 56 | 89 | |
| 57 | 90 | $sql = "CREATE TABLE " . $wpTableName . " ( |
| 58 | 91 | id int(11) NOT NULL auto_increment, |
| @@ -59,23 +92,113 @@ | ||
| 59 | 92 | name varchar(255), |
| 60 | 93 | createtime varchar(255) DEFAULT '', |
| 61 | 94 | updatetime varchar(255) DEFAULT '', |
| 62 | 95 | PRIMARY KEY (id) |
| 63 | - )"; | |
| 96 | + )"; | |
| 97 | + | |
| 64 | 98 | dbDelta( $sql ); |
| 99 | + | |
| 100 | + $logger->info( 'Database table created or updated', [ | |
| 101 | + 'plugin' => 'double-opt-in', | |
| 102 | + 'table_name' => $wpTableName, | |
| 103 | + 'class' => __CLASS__ ?? null, | |
| 104 | + 'method' => __FUNCTION__, | |
| 105 | + ] ); | |
| 65 | 106 | } |
| 66 | 107 | |
| 108 | + | |
| 67 | 109 | /** |
| 68 | 110 | * On Activation create the custom table to store the required information |
| 69 | 111 | * for the double opt in system. |
| 70 | 112 | */ |
| 71 | -function onActivation() { | |
| 72 | - createTableOptin(); | |
| 73 | - createTableOptinCategories(); | |
| 113 | +function onActivation( $network_wide = false ) { | |
| 114 | + $logger = Logger::getInstance(); | |
| 74 | 115 | |
| 75 | - // Add cron | |
| 76 | - if ( ! wp_next_scheduled( 'dailyOptinClear' ) ) { | |
| 77 | - wp_schedule_event( time(), 'daily', 'dailyOptinClear' ); | |
| 116 | + $logger->info( 'Plugin activation started', [ | |
| 117 | + 'plugin' => 'double-opt-in', | |
| 118 | + 'network_wide' => $network_wide, | |
| 119 | + 'class' => __CLASS__ ?? null, | |
| 120 | + 'method' => __FUNCTION__, | |
| 121 | + ] ); | |
| 122 | + | |
| 123 | + if ( is_multisite() && $network_wide ) { | |
| 124 | + $sites = get_sites( [ 'fields' => 'ids', 'number' => 0 ] ); | |
| 125 | + foreach ( $sites as $site_id ) { | |
| 126 | + switch_to_blog( $site_id ); | |
| 127 | + createTableOptin(); | |
| 128 | + createTableOptinCategories(); | |
| 129 | + createTableAuditLog(); | |
| 130 | + restore_current_blog(); | |
| 131 | + } | |
| 132 | + } else { | |
| 133 | + createTableOptin(); | |
| 134 | + createTableOptinCategories(); | |
| 135 | + createTableAuditLog(); | |
| 78 | 136 | } |
| 137 | + | |
| 138 | + $logger->info( 'Plugin activation completed', [ | |
| 139 | + 'plugin' => 'double-opt-in', | |
| 140 | + 'network_wide' => $network_wide, | |
| 141 | + 'class' => __CLASS__ ?? null, | |
| 142 | + 'method' => __FUNCTION__, | |
| 143 | + ] ); | |
| 79 | 144 | } |
| 80 | 145 | |
| 81 | -register_activation_hook( plugin_dir_path(__FILE__).'CF7DoubleOptIn.class.php', '\forge12\contactform7\CF7DoubleOptIn\onActivation' ); | |
| 146 | +/** | |
| 147 | + * Create the table for the audit log. | |
| 148 | + * | |
| 149 | + * @since 4.2.0 | |
| 150 | + * @return void | |
| 151 | + */ | |
| 152 | +function createTableAuditLog() { | |
| 153 | + global $wpdb; | |
| 154 | + | |
| 155 | + $logger = Logger::getInstance(); | |
| 156 | + | |
| 157 | + $tableName = 'f12_cf7_doubleoptin_audit_log'; | |
| 158 | + $wpTableName = $wpdb->prefix . $tableName; | |
| 159 | + | |
| 160 | + $logger->info( 'Creating audit log table', [ | |
| 161 | + 'plugin' => 'double-opt-in', | |
| 162 | + 'table_name' => $wpTableName, | |
| 163 | + 'method' => __FUNCTION__, | |
| 164 | + ] ); | |
| 165 | + | |
| 166 | + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 167 | + | |
| 168 | + $sql = "CREATE TABLE " . $wpTableName . " ( | |
| 169 | + id bigint(20) unsigned NOT NULL auto_increment, | |
| 170 | + event_type varchar(50) NOT NULL, | |
| 171 | + severity varchar(20) NOT NULL DEFAULT 'info', | |
| 172 | + message text NOT NULL, | |
| 173 | + user_id bigint(20) unsigned DEFAULT NULL, | |
| 174 | + details longtext DEFAULT NULL, | |
| 175 | + created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 176 | + PRIMARY KEY (id), | |
| 177 | + KEY idx_type (event_type), | |
| 178 | + KEY idx_severity (severity), | |
| 179 | + KEY idx_created (created_at) | |
| 180 | + )"; | |
| 181 | + | |
| 182 | + dbDelta( $sql ); | |
| 183 | + | |
| 184 | + $logger->info( 'Audit log table created or updated', [ | |
| 185 | + 'plugin' => 'double-opt-in', | |
| 186 | + 'table_name' => $wpTableName, | |
| 187 | + 'method' => __FUNCTION__, | |
| 188 | + ] ); | |
| 189 | +} | |
| 190 | + | |
| 191 | +register_activation_hook( plugin_dir_path(__FILE__).'CF7DoubleOptIn.class.php', '\forge12\contactform7\CF7DoubleOptIn\onActivation' ); | |
| 192 | + | |
| 193 | +add_action( 'wp_initialize_site', function ( $new_site ) { | |
| 194 | + if ( ! is_plugin_active_for_network( | |
| 195 | + plugin_basename( __DIR__ . '/CF7DoubleOptIn.class.php' ) | |
| 196 | + ) ) { | |
| 197 | + return; | |
| 198 | + } | |
| 199 | + switch_to_blog( $new_site->blog_id ); | |
| 200 | + createTableOptin(); | |
| 201 | + createTableOptinCategories(); | |
| 202 | + createTableAuditLog(); | |
| 203 | + restore_current_blog(); | |
| 204 | +}, 900 ); | |