prefix . 'gh_' . $name; } private static function exists( $name ) { global $wpdb; $t = self::t( $name ); return $t === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $t ) ) ); } public static function available() { return self::exists( 'contacts' ) && self::exists( 'contactmeta' ); } public static function counts() { global $wpdb; return array( 'contacts' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', self::t( 'contacts' ) ) ), 'tags' => self::exists( 'tags' ) ? (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', self::t( 'tags' ) ) ) : 0, 'lists' => 0, 'notes' => self::exists( 'notes' ) ? (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE object_type = 'contact'", self::t( 'notes' ) ) ) : 0, 'events' => 0, 'fields' => count( self::field_defs() ), ); } /** Groundhogg's custom properties (option gh_contact_custom_properties). */ private static function field_defs() { $all = get_option( 'gh_contact_custom_properties', array() ); $out = array(); foreach ( (array) ( is_array( $all ) ? ( $all['fields'] ?? array() ) : array() ) as $f ) { if ( is_array( $f ) && ! empty( $f['name'] ) && ! empty( $f['label'] ) && ! in_array( $f['name'], self::CORE_META, true ) ) { $out[] = $f; } } return $out; } /** Groundhogg opt-in status → Nodebook email consent. */ private static function email_status( $status ) { $map = array( 1 => 'pending', // Unconfirmed. 2 => 'subscribed', // Confirmed. 3 => 'unsubscribed', 4 => 'subscribed', // Subscribed weekly. 5 => 'subscribed', // Subscribed monthly. 6 => 'bounced', 7 => 'complained', // Spam. 8 => 'complained', ); return $map[ (int) $status ] ?? ''; } public static function step( array $s ) { switch ( $s['step'] ) { case 'setup': return self::setup( $s ); case 'contacts': return self::contacts( $s ); case 'notes': return self::notes( $s ); } $s['step'] = 'done'; return $s; } private static function setup( array $s ) { global $wpdb; $s['segments'] = array(); if ( self::exists( 'tags' ) ) { foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT tag_id, tag_name FROM %i', self::t( 'tags' ) ), ARRAY_A ) as $r ) { /* translators: %s: tag name */ $s['segments'][ (int) $r['tag_id'] ] = Nodebook_Import::segment( sprintf( __( 'Tag: %s', 'nodebook' ), $r['tag_name'] ) ); ++$s['stats']['segments']; } } $types = array( 'number' => 'number', 'date' => 'date', 'datetime' => 'date', 'textarea' => 'textarea', 'url' => 'url', 'dropdown' => 'select', 'radio' => 'select', ); $s['fields'] = array(); foreach ( self::field_defs() as $f ) { $type = $types[ $f['type'] ?? '' ] ?? 'text'; $options = array_map( 'strval', (array) ( $f['options'] ?? array() ) ); if ( 'checkbox' === ( $f['type'] ?? '' ) ) { $type = 'checkbox'; } $key = Nodebook_Import::field( (string) $f['label'], $type, 'select' === $type ? $options : array() ); if ( $key ) { $s['fields'][ (string) $f['name'] ] = $key; ++$s['stats']['fields']; } } $s['step'] = 'contacts'; $s['cursor'] = 0; return $s; } private static function contacts( array $s ) { global $wpdb; $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ID > %d ORDER BY ID ASC LIMIT %d', self::t( 'contacts' ), (int) $s['cursor'], self::BATCH ), ARRAY_A ); if ( ! $rows ) { $s['step'] = 'notes'; $s['cursor'] = (int) ( $s['marks']['notes'] ?? 0 ); return $s; } $in_ids = array_values( array_map( 'intval', wp_list_pluck( $rows, 'ID' ) ) ); $meta = array(); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT contact_id, meta_key, meta_value FROM %i WHERE contact_id IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'contactmeta' ) ), $in_ids ) ), ARRAY_A ) as $m ) { $meta[ (int) $m['contact_id'] ][ (string) $m['meta_key'] ] = $m['meta_value']; } $tags = array(); if ( self::exists( 'tag_relationships' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT contact_id, tag_id FROM %i WHERE contact_id IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'tag_relationships' ) ), $in_ids ) ), ARRAY_A ) as $t ) { $tags[ (int) $t['contact_id'] ][] = (int) $t['tag_id']; } } foreach ( $rows as $r ) { $s['cursor'] = (int) $r['ID']; ++$s['seen']; $email = strtolower( (string) $r['email'] ); if ( ! is_email( $email ) ) { ++$s['stats']['skipped']; continue; } $m = $meta[ (int) $r['ID'] ] ?? array(); $get = static function ( $k ) use ( $m ) { $v = isset( $m[ $k ] ) ? maybe_unserialize( $m[ $k ] ) : ''; return is_scalar( $v ) ? trim( (string) $v ) : ''; }; $data = array_filter( array( 'email' => $email, 'first_name' => (string) $r['first_name'], 'last_name' => (string) $r['last_name'], 'phone' => '' !== $get( 'mobile_phone' ) ? $get( 'mobile_phone' ) : $get( 'primary_phone' ), 'company' => $get( 'company_name' ), 'job_title' => $get( 'job_title' ), 'address' => trim( $get( 'street_address_1' ) . ' ' . $get( 'street_address_2' ) ), 'city' => $get( 'city' ), 'state' => $get( 'region' ), 'postcode' => $get( 'postal_zip' ), 'country' => $get( 'country' ), 'wp_user_id' => (int) $r['user_id'], 'owner_id' => Nodebook_Import::owner( $r['owner_id'] ), 'source' => self::NAME, ), static function ( $v ) { return '' !== $v && 0 !== $v; } ); // The other phone number is kept too, rather than lost. if ( '' !== $get( 'mobile_phone' ) && '' !== $get( 'primary_phone' ) && $get( 'mobile_phone' ) !== $get( 'primary_phone' ) ) { $data['meta'] = array( __( 'Other phone', 'nodebook' ) => $get( 'primary_phone' ) ); } if ( '' !== $get( 'lead_source' ) ) { $data['meta'] = ( $data['meta'] ?? array() ) + array( __( 'Lead source', 'nodebook' ) => $get( 'lead_source' ) ); } $res = Nodebook_Import::contact( $data, self::NAME, (string) $r['date_created'] ); if ( ! $res['id'] ) { ++$s['stats']['skipped']; continue; } ++$s['stats'][ $res['created'] ? 'created' : 'updated' ]; $cid = (int) $res['id']; $st = self::email_status( $r['optin_status'] ); if ( '' !== $st ) { Nodebook_Subscription::set( $cid, $st, array( /* translators: %s: Groundhogg */ 'source' => sprintf( __( 'Imported from %s', 'nodebook' ), self::NAME ), 'at' => (string) ( $r['date_optin_status_changed'] ? $r['date_optin_status_changed'] : $r['date_created'] ), 'user_id' => get_current_user_id(), ) ); } foreach ( $tags[ (int) $r['ID'] ] ?? array() as $tid ) { if ( isset( $s['segments'][ $tid ] ) ) { Nodebook_Segments::add_contacts( (int) $s['segments'][ $tid ], array( $cid ) ); } } $props = array(); foreach ( $s['fields'] as $name => $key ) { if ( ! isset( $m[ $name ] ) ) { continue; } $v = maybe_unserialize( $m[ $name ] ); if ( is_array( $v ) ) { $v = implode( ', ', array_map( 'strval', array_filter( $v, 'is_scalar' ) ) ); } if ( '' !== trim( (string) $v ) ) { $props[ $key ] = (string) $v; } } if ( $props ) { Nodebook_Props::set( 'contact', $cid, $props, 'fill' ); } } return $s; } private static function notes( array $s ) { global $wpdb; if ( ! self::exists( 'notes' ) ) { $s['step'] = 'done'; return $s; } $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE ID > %d AND object_type = 'contact' ORDER BY ID ASC LIMIT %d", self::t( 'notes' ), (int) $s['cursor'], self::BATCH ), ARRAY_A ); if ( ! $rows ) { $s['step'] = 'done'; return $s; } $ids = array_values( array_unique( array_map( 'intval', wp_list_pluck( $rows, 'object_id' ) ) ) ); $map = array(); $in_ids = array_values( $ids ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT g.ID, c.id AS cid FROM %i g INNER JOIN %i c ON c.email = g.email WHERE g.ID IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'contacts' ), Nodebook_Contacts::table() ), $in_ids ) ), ARRAY_A ) as $r ) { $map[ (int) $r['ID'] ] = (int) $r['cid']; } $kinds = array( 'call' => __( 'Call', 'nodebook' ), 'email' => __( 'Email', 'nodebook' ), 'meeting' => __( 'Meeting', 'nodebook' ), ); foreach ( $rows as $r ) { $s['cursor'] = (int) $r['ID']; $s['marks']['notes'] = (int) $r['ID']; $cid = $map[ (int) $r['object_id'] ] ?? 0; if ( ! $cid ) { continue; } $at = (int) $r['timestamp'] ? gmdate( 'Y-m-d H:i:s', (int) $r['timestamp'] ) : (string) $r['date_created']; if ( Nodebook_Import::note( $cid, $kinds[ strtolower( (string) $r['type'] ) ] ?? '', (string) $r['summary'], (string) $r['content'], $at, (int) $r['user_id'], 'groundhogg' ) ) { ++$s['stats']['notes']; } } return $s; } }