prefix . 'brai_' . $which; } /** Creates or updates the tables. */ function brai_install_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $c = $wpdb->get_charset_collate(); $visitors = brai_table( 'visitors' ); $events = brai_table( 'events' ); $messages = brai_table( 'messages' ); dbDelta( "CREATE TABLE $visitors ( vid varchar(40) NOT NULL, user_id bigint(20) unsigned NOT NULL DEFAULT 0, email varchar(190) NOT NULL DEFAULT '', name varchar(190) NOT NULL DEFAULT '', phone varchar(50) NOT NULL DEFAULT '', status varchar(20) NOT NULL DEFAULT 'browsing', cart longtext NULL, cart_value decimal(12,2) NOT NULL DEFAULT 0, order_id bigint(20) unsigned NOT NULL DEFAULT 0, views int(10) unsigned NOT NULL DEFAULT 0, product_views int(10) unsigned NOT NULL DEFAULT 0, landing varchar(255) NOT NULL DEFAULT '', referrer varchar(255) NOT NULL DEFAULT '', first_seen datetime NOT NULL, last_seen datetime NOT NULL, basket_at datetime NULL, ordered_at datetime NULL, reminders tinyint(3) unsigned NOT NULL DEFAULT 0, reminded_at datetime NULL, device varchar(10) NOT NULL DEFAULT '', browser varchar(30) NOT NULL DEFAULT '', os varchar(20) NOT NULL DEFAULT '', screen varchar(15) NOT NULL DEFAULT '', country varchar(60) NOT NULL DEFAULT '', region varchar(80) NOT NULL DEFAULT '', city varchar(80) NOT NULL DEFAULT '', src text NULL, visits int(10) unsigned NOT NULL DEFAULT 0, secs int(10) unsigned NOT NULL DEFAULT 0, contacted_at datetime NULL, contacted_by bigint(20) unsigned NOT NULL DEFAULT 0, note text NULL, PRIMARY KEY (vid), KEY last_seen (last_seen), KEY status (status), KEY email (email) ) $c;" ); dbDelta( "CREATE TABLE $events ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, vid varchar(40) NOT NULL, ts datetime NOT NULL, type varchar(20) NOT NULL, object_id bigint(20) unsigned NOT NULL DEFAULT 0, qty int(11) NOT NULL DEFAULT 0, value decimal(12,2) NOT NULL DEFAULT 0, url varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY vid (vid), KEY ts (ts), KEY type_obj (type, object_id) ) $c;" ); /* Emails sent to customers: the ones staff write, and the automatic reminders. */ dbDelta( "CREATE TABLE $messages ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, vid varchar(40) NOT NULL DEFAULT '', ts datetime NOT NULL, kind varchar(12) NOT NULL DEFAULT 'manual', step tinyint(3) unsigned NOT NULL DEFAULT 0, user_id bigint(20) unsigned NOT NULL DEFAULT 0, from_name varchar(190) NOT NULL DEFAULT '', from_email varchar(190) NOT NULL DEFAULT '', to_email varchar(190) NOT NULL DEFAULT '', subject varchar(255) NOT NULL DEFAULT '', body text NULL, PRIMARY KEY (id), KEY vid (vid), KEY ts (ts) ) $c;" ); update_option( 'brai_db', BRAI_DB, false ); brai_backfill_mail_log(); } /** Schedules the daily clean-up, hourly reminders and the morning summary. */ function brai_schedule() { if ( ! wp_next_scheduled( 'brai_cleanup' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'brai_cleanup' ); } if ( ! wp_next_scheduled( 'brai_recovery' ) ) { wp_schedule_event( time() + 300, 'hourly', 'brai_recovery' ); } if ( ! wp_next_scheduled( 'brai_digest' ) ) { wp_schedule_single_event( brai_digest_next(), 'brai_digest' ); } } add_action( 'init', 'brai_schedule' ); /** Runs when the plugin is switched on. */ function brai_activate() { brai_install_tables(); brai_migrate_snippet(); } /** Runs when the plugin is switched off: stops the scheduled tasks (data is kept). */ function brai_deactivate() { foreach ( array( 'brai_cleanup', 'brai_recovery', 'brai_digest', 'brai_weekly' ) as $hook ) { wp_clear_scheduled_hook( $hook ); } } add_action( 'plugins_loaded', function () { if ( get_option( 'brai_db' ) !== BRAI_DB ) { brai_install_tables(); } } ); /* Daily: delete records older than the keep period. */ add_action( 'brai_cleanup', function () { global $wpdb; $cut = gmdate( 'Y-m-d H:i:s', time() - brai_keep_days() * DAY_IN_SECONDS ); // phpcs:disable WordPress.DB.DirectDatabaseQuery -- the plugin's own tables. $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE ts < %s', brai_table( 'events' ), $cut ) ); $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE last_seen < %s', brai_table( 'visitors' ), $cut ) ); $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE ts < %s', brai_table( 'messages' ), $cut ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery } ); /** * Copies data and settings from the earlier code-snippet version of this tool (tables prefixed wi_ba_), once, * if they exist and this plugin's tables are still empty. */ function brai_migrate_snippet() { global $wpdb; if ( get_option( 'brai_migrated' ) ) { return; } $done = array(); // phpcs:disable WordPress.DB.DirectDatabaseQuery -- a one-off copy between the plugin's own tables. foreach ( array( 'visitors', 'events' ) as $w ) { $old = $wpdb->prefix . 'wi_ba_' . $w; $new = brai_table( $w ); if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $old ) ) ) !== $old ) { continue; } if ( (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $new ) ) ) { continue; } $cols = array_values( array_intersect( $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $old ) ), $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $new ) ) ) ); if ( ! $cols ) { continue; } /* Every table and column name is passed as an identifier (%i): one placeholder for each column the two tables share. */ $list = implode( ', ', array_fill( 0, count( $cols ), '%i' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- $list is only a list of %i placeholders, and the values are passed as one array. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO %i ($list) SELECT $list FROM %i", array_merge( array( $new ), $cols, $cols, array( $old ) ) ) ); $done[ $w ] = (int) $wpdb->rows_affected; } // phpcs:enable WordPress.DB.DirectDatabaseQuery foreach ( array( 'mail', 'recovery_live', 'unsub_list', 'digest_last' ) as $o ) { $v = get_option( 'wi_ba_' . $o ); if ( false !== $v && false === get_option( 'brai_' . $o ) ) { update_option( 'brai_' . $o, $v, false ); } } update_option( 'brai_migrated', array( 'time' => time(), 'rows' => $done, ), false ); }