$wpdb->prefix . 'nodebook_contacts', 'companies' => $wpdb->prefix . 'nodebook_companies', 'activities' => $wpdb->prefix . 'nodebook_activities', 'visitors' => $wpdb->prefix . 'nodebook_visitors', 'segments' => $wpdb->prefix . 'nodebook_segments', 'segment_contacts' => $wpdb->prefix . 'nodebook_segment_contacts', 'aliases' => $wpdb->prefix . 'nodebook_aliases', 'values' => $wpdb->prefix . 'nodebook_values', 'tasks' => $wpdb->prefix . 'nodebook_tasks', 'deals' => $wpdb->prefix . 'nodebook_deals', ); } public static function table( $name ) { $t = self::tables(); return $t[ $name ]; } public static function activate() { self::create_tables(); add_option( Nodebook_Settings::OPTION, Nodebook_Settings::defaults() ); Nodebook_Cron::schedule(); Nodebook_Presets::on_activate(); } public static function deactivate() { Nodebook_Cron::unschedule(); Nodebook_Node::unschedule(); // Take the phone app's address out of the rewrite rules; they come back (and are flushed) on reactivation. global $wp_rewrite; if ( $wp_rewrite instanceof WP_Rewrite ) { foreach ( array_keys( (array) $wp_rewrite->extra_rules_top ) as $rule ) { if ( false !== strpos( (string) $wp_rewrite->extra_rules_top[ $rule ], 'nodebook_app=' ) ) { unset( $wp_rewrite->extra_rules_top[ $rule ] ); } } } delete_option( 'nodebook_app_rules' ); flush_rewrite_rules( false ); Nodebook_Modules::deactivate_all(); } public static function maybe_upgrade() { if ( get_option( 'nodebook_db_version' ) !== NODEBOOK_DB_VERSION ) { self::create_tables(); } if ( get_option( 'nodebook_version' ) !== NODEBOOK_VERSION ) { self::autoload_boot_options(); update_option( 'nodebook_version', NODEBOOK_VERSION, true ); } } /** * Options read on every request are kept in the autoloaded set, so a page view costs no extra * queries. Older versions stored them outside it; missing ones are created with their default. */ public static function autoload_boot_options() { foreach ( array( 'nodebook_outbox' => 'on', 'nodebook_module_versions' => array(), 'nodebook_app_rules' => '', ) as $name => $default ) { $value = get_option( $name, $default ); delete_option( $name ); add_option( $name, $value, '', true ); } } public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $c = $wpdb->get_charset_collate(); $t = self::tables(); $schemas = array(); $schemas[] = "CREATE TABLE {$t['contacts']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, email varchar(190) DEFAULT NULL, first_name varchar(100) NOT NULL DEFAULT '', last_name varchar(100) NOT NULL DEFAULT '', phone varchar(50) NOT NULL DEFAULT '', job_title varchar(150) NOT NULL DEFAULT '', company_id bigint(20) unsigned NOT NULL DEFAULT 0, status varchar(20) NOT NULL DEFAULT 'lead', source varchar(150) NOT NULL DEFAULT '', website varchar(255) NOT NULL DEFAULT '', address varchar(255) NOT NULL DEFAULT '', city varchar(100) NOT NULL DEFAULT '', state varchar(100) NOT NULL DEFAULT '', postcode varchar(20) NOT NULL DEFAULT '', country varchar(100) NOT NULL DEFAULT '', description text NULL, meta longtext NULL, wp_user_id bigint(20) unsigned NOT NULL DEFAULT 0, orders_count int(10) unsigned NOT NULL DEFAULT 0, total_spent decimal(18,4) NOT NULL DEFAULT 0, visits_count int(10) unsigned NOT NULL DEFAULT 0, first_seen datetime DEFAULT NULL, last_seen datetime DEFAULT NULL, first_source varchar(100) NOT NULL DEFAULT '', first_medium varchar(100) NOT NULL DEFAULT '', first_campaign varchar(150) NOT NULL DEFAULT '', first_referrer varchar(255) NOT NULL DEFAULT '', landing_page varchar(255) NOT NULL DEFAULT '', first_touch_at datetime DEFAULT NULL, watched tinyint(1) NOT NULL DEFAULT 0, last_intent_at datetime DEFAULT NULL, last_alert_at datetime DEFAULT NULL, followed_up_at datetime DEFAULT NULL, owner_id bigint(20) unsigned NOT NULL DEFAULT 0, remind_at datetime DEFAULT NULL, status_at datetime DEFAULT NULL, pinned text NULL, email_status varchar(20) NOT NULL DEFAULT '', email_status_at datetime DEFAULT NULL, consent_source varchar(190) NOT NULL DEFAULT '', last_inquiry_at datetime DEFAULT NULL, other_spent varchar(255) NOT NULL DEFAULT '', score tinyint(3) unsigned DEFAULT NULL, score_at datetime DEFAULT NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY email (email), KEY company_id (company_id), KEY status (status), KEY wp_user_id (wp_user_id), KEY last_seen (last_seen), KEY created_at (created_at), KEY first_source (first_source), KEY last_intent_at (last_intent_at), KEY owner_id (owner_id), KEY remind_at (remind_at), KEY status_at (status_at), KEY email_status (email_status), KEY last_inquiry_at (last_inquiry_at), KEY score (score) ) $c;"; $schemas[] = "CREATE TABLE {$t['companies']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(190) NOT NULL DEFAULT '', domain varchar(190) DEFAULT NULL, website varchar(255) NOT NULL DEFAULT '', phone varchar(50) NOT NULL DEFAULT '', industry varchar(100) NOT NULL DEFAULT '', size varchar(50) NOT NULL DEFAULT '', address varchar(255) NOT NULL DEFAULT '', city varchar(100) NOT NULL DEFAULT '', state varchar(100) NOT NULL DEFAULT '', postcode varchar(20) NOT NULL DEFAULT '', country varchar(100) NOT NULL DEFAULT '', description text NULL, meta longtext NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY domain (domain), KEY name (name) ) $c;"; $schemas[] = "CREATE TABLE {$t['activities']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, contact_id bigint(20) unsigned NOT NULL DEFAULT 0, visitor_id varchar(36) NOT NULL DEFAULT '', type varchar(30) NOT NULL DEFAULT '', object_id bigint(20) unsigned NOT NULL DEFAULT 0, title varchar(255) NOT NULL DEFAULT '', url text NULL, data longtext NULL, user_id bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, PRIMARY KEY (id), KEY contact_created (contact_id,created_at), KEY contact_type (contact_id,type), KEY type_created (type,created_at), KEY visitor_id (visitor_id), KEY type_object (type,object_id), KEY created_at (created_at) ) $c;"; $schemas[] = "CREATE TABLE {$t['visitors']} ( visitor_id varchar(36) NOT NULL, contact_id bigint(20) unsigned NOT NULL DEFAULT 0, last_url text NULL, created_at datetime NOT NULL, last_seen datetime NOT NULL, PRIMARY KEY (visitor_id), KEY contact_id (contact_id), KEY last_seen (last_seen) ) $c;"; $schemas[] = "CREATE TABLE {$t['segments']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(190) NOT NULL DEFAULT '', type varchar(10) NOT NULL DEFAULT 'static', rules longtext NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id) ) $c;"; $schemas[] = "CREATE TABLE {$t['segment_contacts']} ( segment_id bigint(20) unsigned NOT NULL, contact_id bigint(20) unsigned NOT NULL, added_at datetime NOT NULL, PRIMARY KEY (segment_id,contact_id), KEY contact_id (contact_id) ) $c;"; $schemas[] = "CREATE TABLE {$t['aliases']} ( email varchar(190) NOT NULL, contact_id bigint(20) unsigned NOT NULL, created_at datetime NOT NULL, PRIMARY KEY (email), KEY contact_id (contact_id) ) $c;"; $schemas[] = "CREATE TABLE {$t['values']} ( object_type varchar(10) NOT NULL, object_id bigint(20) unsigned NOT NULL, field_key varchar(64) NOT NULL, value_text varchar(1000) NOT NULL DEFAULT '', value_num decimal(20,4) DEFAULT NULL, value_date date DEFAULT NULL, PRIMARY KEY (object_type,object_id,field_key), KEY field_text (object_type,field_key,value_text(100)), KEY field_num (object_type,field_key,value_num), KEY field_date (object_type,field_key,value_date) ) $c;"; $schemas[] = "CREATE TABLE {$t['tasks']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, contact_id bigint(20) unsigned NOT NULL DEFAULT 0, deal_id bigint(20) unsigned NOT NULL DEFAULT 0, title varchar(190) NOT NULL DEFAULT '', due_at datetime DEFAULT NULL, assignee_id bigint(20) unsigned NOT NULL DEFAULT 0, created_by bigint(20) unsigned NOT NULL DEFAULT 0, done_at datetime DEFAULT NULL, done_by bigint(20) unsigned NOT NULL DEFAULT 0, outcome varchar(190) NOT NULL DEFAULT '', created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY contact_id (contact_id), KEY open_due (done_at,due_at), KEY assignee (assignee_id,done_at,due_at) ) $c;"; $schemas[] = "CREATE TABLE {$t['deals']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, pipeline_id bigint(20) unsigned NOT NULL DEFAULT 1, title varchar(190) NOT NULL DEFAULT '', contact_id bigint(20) unsigned NOT NULL DEFAULT 0, company_id bigint(20) unsigned NOT NULL DEFAULT 0, stage varchar(40) NOT NULL DEFAULT '', value decimal(18,4) NOT NULL DEFAULT 0, currency char(3) NOT NULL DEFAULT '', owner_id bigint(20) unsigned NOT NULL DEFAULT 0, expected_close date DEFAULT NULL, outcome varchar(10) NOT NULL DEFAULT '', lost_reason varchar(190) NOT NULL DEFAULT '', source varchar(100) NOT NULL DEFAULT '', activity_id bigint(20) unsigned NOT NULL DEFAULT 0, notes text NULL, stage_at datetime DEFAULT NULL, closed_at datetime DEFAULT NULL, created_by bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY board (pipeline_id,outcome,stage), KEY contact_id (contact_id), KEY company_id (company_id), KEY owner_id (owner_id), KEY expected_close (outcome,expected_close), KEY closed_at (outcome,closed_at) ) $c;"; foreach ( $schemas as $sql ) { dbDelta( $sql ); } $was = get_option( 'nodebook_db_version' ); update_option( 'nodebook_db_version', NODEBOOK_DB_VERSION ); if ( $was && $was !== NODEBOOK_DB_VERSION ) { // Before 1.3 the generic high-intent list was saved as a value, which hides the per-business defaults. $settings = get_option( 'nodebook_settings' ); $old = array( '/pricing*', '/prices*', '/contact*', '/book*', '/quote*', '/demo*', '/cart*', '/checkout*' ); if ( is_array( $settings ) && isset( $settings['intent_pages'] ) && array_values( array_filter( array_map( 'trim', preg_split( '/\R/', (string) $settings['intent_pages'] ) ) ) ) === $old ) { $settings['intent_pages'] = ''; update_option( 'nodebook_settings', $settings ); } } self::migrate(); Nodebook_Team::add_role(); // Scores for everyone shortly after installing or updating, without waiting for the night. if ( ( ! $was || $was !== NODEBOOK_DB_VERSION ) && function_exists( 'wp_schedule_single_event' ) && ! wp_next_scheduled( 'nodebook_insights_run' ) ) { update_option( 'nodebook_insights_cursor', 0, false ); wp_schedule_single_event( time() + 30, 'nodebook_insights_run' ); } if ( $was && $was !== NODEBOOK_DB_VERSION && class_exists( 'Nodebook_Repair' ) ) { Nodebook_Repair::schedule(); // Repairs data written by older versions, in the background. } } const MIGRATIONS = 'nodebook_migrations'; /** * One-time data repairs. Each is recorded once it ran, so it never repeats and is never skipped * (also called from the background repair job, in case an update request was cut short). */ public static function migrate() { global $wpdb; $done = (array) get_option( self::MIGRATIONS, array() ); $steps = array( // 1.9 measures "no movement" from the last status change; older contacts start from when they were created. 'status_at' => static function () use ( $wpdb ) { return false !== $wpdb->query( $wpdb->prepare( 'UPDATE %i SET status_at = created_at WHERE status_at IS NULL', Nodebook_Contacts::table() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time repair of the plugin's own table. }, 'unflag_confirm' => static function () { self::unflag_confirmation_views(); return true; }, // 1.13: inquiries of the last week that nobody marked as followed up go on the list // (older ones are assumed handled outside Nodebook, so an upgrade does not flood the list). 'last_inquiry' => static function () use ( $wpdb ) { $c = Nodebook_Contacts::table(); $a = Nodebook_Activities::table(); return false !== $wpdb->query( $wpdb->prepare( "UPDATE %i SET last_inquiry_at = ( SELECT MAX(ax.created_at) FROM %i ax WHERE ax.contact_id = %i.id AND ax.type = 'form' AND ax.user_id = 0 ) WHERE last_inquiry_at IS NULL AND id IN ( SELECT contact_id FROM ( SELECT DISTINCT contact_id FROM %i WHERE type = 'form' AND user_id = 0 AND created_at >= %s ) recent )", $c, $a, $c, $a, gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time repair of the plugin's own table. }, ); $changed = false; foreach ( $steps as $key => $run ) { if ( in_array( $key, $done, true ) ) { continue; } if ( $run() ) { $done[] = $key; $changed = true; } } if ( $changed ) { update_option( self::MIGRATIONS, array_values( array_unique( $done ) ), false ); } } /** * Before 1.10.1, order-received and confirmation pages counted as high intent. Remove that flag from * the last 7 days of page views and recalculate "hot" for the contacts it touched. */ public static function unflag_confirmation_views() { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time repair of the plugin's own tables. $a = Nodebook_Activities::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, contact_id, url, title, data FROM %i WHERE type = 'pageview' AND created_at >= %s AND data LIKE %s LIMIT 20000", $a, gmdate( 'Y-m-d H:i:s', time() - 8 * DAY_IN_SECONDS ), '%' . $wpdb->esc_like( '"intent":true' ) . '%' ), ARRAY_A ); $hit = array(); foreach ( (array) $rows as $r ) { if ( ! Nodebook_Alerts::is_after_conversion( $r['url'], $r['title'] ) ) { continue; } $d = json_decode( (string) $r['data'], true ); $d = is_array( $d ) ? $d : array(); unset( $d['intent'] ); $wpdb->update( $a, array( 'data' => wp_json_encode( $d ) ), array( 'id' => (int) $r['id'] ) ); $hit[ (int) $r['contact_id'] ] = true; } foreach ( array_keys( $hit ) as $cid ) { if ( ! $cid ) { continue; } $last = $wpdb->get_var( $wpdb->prepare( "SELECT MAX(created_at) FROM %i WHERE contact_id = %d AND type IN ('pageview','click') AND data LIKE %s", $a, $cid, '%' . $wpdb->esc_like( '"intent":true' ) . '%' ) ); $wpdb->update( Nodebook_Contacts::table(), array( 'last_intent_at' => $last ? $last : null ), array( 'id' => $cid ) ); } // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return count( $hit ); } public static function drop_tables() { global $wpdb; foreach ( self::tables() as $table ) { $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- uninstall clean-up of the plugin's own tables. } delete_option( 'nodebook_db_version' ); } }