prefix . 'zbs_' . $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' ); } private static function log_type( $type ) { return str_replace( ' ', '_', strtolower( trim( (string) $type ) ) ); } public static function counts() { global $wpdb; $notes = 0; if ( self::exists( 'logs' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %s placeholder per log type. $notes = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE zbsl_objtype = %d AND LOWER(REPLACE(zbsl_type, ' ', '_')) IN (" . Nodebook_Sql::list_of( count( self::LOG_TYPES ), '%s' ) . ')', array_merge( array( self::t( 'logs' ), self::CONTACT ), self::LOG_TYPES ) ) ); } 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 WHERE zbstag_objtype = %d', self::t( 'tags' ), self::CONTACT ) ) : 0, 'lists' => 0, 'notes' => $notes, 'events' => 0, 'fields' => count( self::field_defs() ), ); } /** Custom contact fields: [ type, label, options, slug ] from Jetpack CRM's settings table. */ private static function field_defs() { global $wpdb; if ( ! self::exists( 'settings' ) ) { return array(); } $raw = $wpdb->get_var( $wpdb->prepare( "SELECT zbsset_val FROM %i WHERE zbsset_key = 'customfields' ORDER BY ID DESC LIMIT 1", self::t( 'settings' ) ) ); $all = is_string( $raw ) ? json_decode( $raw, true ) : null; $defs = is_array( $all ) && isset( $all['customers'] ) && is_array( $all['customers'] ) ? $all['customers'] : array(); $out = array(); foreach ( $defs as $d ) { if ( is_array( $d ) && ! empty( $d[1] ) && ! empty( $d[3] ) && ! in_array( $d[0], array( 'encrypted', 'password' ), true ) ) { $out[] = array( 'type' => (string) $d[0], 'label' => (string) $d[1], 'options' => (string) ( $d[2] ?? '' ), 'slug' => (string) $d[3], ); } } return $out; } 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 ID, zbstag_name FROM %i WHERE zbstag_objtype = %d', self::t( 'tags' ), self::CONTACT ), ARRAY_A ) as $r ) { /* translators: %s: tag name */ $s['segments'][ (int) $r['ID'] ] = Nodebook_Import::segment( sprintf( __( 'Tag: %s', 'nodebook' ), $r['zbstag_name'] ) ); ++$s['stats']['segments']; } } $types = array( 'numberint' => 'number', 'numberfloat' => 'number', 'date' => 'date', 'textarea' => 'textarea', 'select' => 'select', 'radio' => 'select', ); $s['fields'] = array(); $s['dates'] = array(); foreach ( self::field_defs() as $f ) { $type = $types[ $f['type'] ] ?? 'text'; $options = array_filter( array_map( 'trim', explode( ',', $f['options'] ) ), 'strlen' ); $key = Nodebook_Import::field( $f['label'], $type, 'select' === $type ? $options : array() ); if ( $key ) { $s['fields'][ $f['slug'] ] = $key; if ( 'date' === $type ) { $s['dates'][] = $f['slug']; } ++$s['stats']['fields']; } } $s['companies'] = array(); if ( self::exists( 'companies' ) ) { foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i LIMIT 5000', self::t( 'companies' ) ), ARRAY_A ) as $co ) { if ( '' === trim( (string) $co['zbsco_name'] ) ) { continue; } $email = (string) $co['zbsco_email']; $domain = $email && is_email( $email ) && ! Nodebook_Companies::is_free_mail( substr( strrchr( $email, '@' ), 1 ) ) ? Nodebook_Companies::normalize_domain( $email ) : ''; $r = Nodebook_Companies::upsert( array_filter( array( 'name' => (string) $co['zbsco_name'], 'domain' => $domain, 'phone' => (string) $co['zbsco_maintel'], 'address' => trim( $co['zbsco_addr1'] . ' ' . $co['zbsco_addr2'] ), 'city' => (string) $co['zbsco_city'], 'state' => (string) $co['zbsco_county'], 'postcode' => (string) $co['zbsco_postcode'], 'country' => (string) $co['zbsco_country'], ), 'strlen' ), 'fill' ); $s['companies'][ (int) $co['ID'] ] = (int) $r['id']; } } $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' ) ) ); $tags = array(); if ( self::exists( 'tags_links' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT zbstl_objid, zbstl_tagid FROM %i WHERE zbstl_objtype = %d AND zbstl_objid IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'tags_links' ), self::CONTACT ), $in_ids ) ), ARRAY_A ) as $t ) { $tags[ (int) $t['zbstl_objid'] ][] = (int) $t['zbstl_tagid']; } } $cos = array(); if ( self::exists( 'object_links' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT zbsol_objid_from, zbsol_objid_to FROM %i WHERE zbsol_objtype_from = %d AND zbsol_objtype_to = %d AND zbsol_objid_from IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'object_links' ), self::CONTACT, self::COMPANY ), $in_ids ) ), ARRAY_A ) as $l ) { $cos[ (int) $l['zbsol_objid_from'] ] = (int) $l['zbsol_objid_to']; } } $no_email = array(); if ( self::exists( 'meta' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_col( $wpdb->prepare( "SELECT zbsm_objid FROM %i WHERE zbsm_objtype = %d AND zbsm_key = 'do-not-email' AND zbsm_val NOT IN ('', '0', 'false') AND zbsm_objid IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'meta' ), self::CONTACT ), $in_ids ) ) ) as $id ) { $no_email[ (int) $id ] = true; } } $values = array(); if ( $s['fields'] && self::exists( 'customfields' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT zbscf_objid, zbscf_objkey, zbscf_objval FROM %i WHERE zbscf_objtype = %d AND zbscf_objid IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'customfields' ), self::CONTACT ), $in_ids ) ), ARRAY_A ) as $v ) { $values[ (int) $v['zbscf_objid'] ][ (string) $v['zbscf_objkey'] ] = (string) $v['zbscf_objval']; } } $aliases = array(); if ( self::exists( 'aka' ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- one %d placeholder per ID. foreach ( (array) $wpdb->get_results( $wpdb->prepare( "SELECT aka_id, aka_alias FROM %i WHERE aka_type = %d AND aka_id IN (" . Nodebook_Sql::list_of( count( $in_ids ) ) . ")", array_merge( array( self::t( 'aka' ), self::CONTACT ), $in_ids ) ), ARRAY_A ) as $a ) { $aliases[ (int) $a['aka_id'] ][] = (string) $a['aka_alias']; } } foreach ( $rows as $r ) { $s['cursor'] = (int) $r['ID']; ++$s['seen']; $email = strtolower( trim( (string) $r['zbsc_email'] ) ); if ( ! is_email( $email ) ) { ++$s['stats']['skipped']; continue; } $phones = array_values( array_unique( array_filter( array_map( 'trim', array( (string) $r['zbsc_mobtel'], (string) $r['zbsc_hometel'], (string) $r['zbsc_worktel'] ) ), 'strlen' ) ) ); $data = array_filter( array( 'email' => $email, 'first_name' => (string) $r['zbsc_fname'], 'last_name' => (string) $r['zbsc_lname'], 'phone' => $phones[0] ?? '', 'address' => trim( $r['zbsc_addr1'] . ' ' . $r['zbsc_addr2'] ), 'city' => (string) $r['zbsc_city'], 'state' => (string) $r['zbsc_county'], 'postcode' => (string) $r['zbsc_postcode'], 'country' => (string) $r['zbsc_country'], 'wp_user_id' => max( 0, (int) $r['zbsc_wpid'] ), 'owner_id' => Nodebook_Import::owner( $r['zbs_owner'] ), 'status' => 'customer' === strtolower( (string) $r['zbsc_status'] ) ? 'customer' : '', 'company_id' => $s['companies'][ $cos[ (int) $r['ID'] ] ?? 0 ] ?? 0, 'source' => self::NAME, ), static function ( $v ) { return '' !== $v && 0 !== $v; } ); $extra = array(); if ( count( $phones ) > 1 ) { $extra[ __( 'Other phone', 'nodebook' ) ] = implode( ', ', array_slice( $phones, 1 ) ); } if ( '' !== (string) $r['zbsc_status'] && ! in_array( strtolower( (string) $r['zbsc_status'] ), array( 'customer', 'lead' ), true ) ) { /* translators: %s: Jetpack CRM */ $extra[ sprintf( __( '%s status', 'nodebook' ), self::NAME ) ] = (string) $r['zbsc_status']; } if ( $extra ) { $data['meta'] = $extra; } $created = (int) $r['zbsc_created'] > 0 ? gmdate( 'Y-m-d H:i:s', (int) $r['zbsc_created'] ) : ''; $res = Nodebook_Import::contact( $data, self::NAME, $created ); if ( ! $res['id'] ) { ++$s['stats']['skipped']; continue; } ++$s['stats'][ $res['created'] ? 'created' : 'updated' ]; $cid = (int) $res['id']; // Jetpack CRM has no opt-in status. "Do not email" (an unsubscribe link) and blacklisted contacts are a clear no. if ( isset( $no_email[ (int) $r['ID'] ] ) || 'blacklisted' === strtolower( (string) $r['zbsc_status'] ) ) { Nodebook_Subscription::set( $cid, 'unsubscribed', array( /* translators: %s: Jetpack CRM */ 'source' => sprintf( __( 'Imported from %s', 'nodebook' ), self::NAME ), '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 ) ); } } foreach ( $aliases[ (int) $r['ID'] ] ?? array() as $alias ) { if ( strtolower( $alias ) !== $email && ! Nodebook_Contacts::find_by_email( $alias ) ) { Nodebook_Contacts::add_alias( $alias, $cid ); } } $props = array(); foreach ( $values[ (int) $r['ID'] ] ?? array() as $slug => $v ) { if ( ! isset( $s['fields'][ $slug ] ) || '' === trim( $v ) ) { continue; } // Jetpack CRM stores dates as Unix timestamps. if ( in_array( $slug, $s['dates'], true ) && ctype_digit( $v ) ) { $v = gmdate( 'Y-m-d', (int) $v ); } $props[ $s['fields'][ $slug ] ] = $v; } if ( $props ) { Nodebook_Props::set( 'contact', $cid, $props, 'fill' ); } } return $s; } private static function notes( array $s ) { global $wpdb; if ( ! self::exists( 'logs' ) ) { $s['step'] = 'done'; return $s; } $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ID > %d AND zbsl_objtype = %d ORDER BY ID ASC LIMIT %d', self::t( 'logs' ), (int) $s['cursor'], self::CONTACT, self::BATCH ), ARRAY_A ); if ( ! $rows ) { $s['step'] = 'done'; return $s; } $ids = array_values( array_unique( array_map( 'intval', wp_list_pluck( $rows, 'zbsl_objid' ) ) ) ); $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 z.ID, c.id AS cid FROM %i z INNER JOIN %i c ON c.email = LOWER(z.zbsc_email) WHERE z.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' ), 'mail' => __( 'Mail', 'nodebook' ), 'meeting' => __( 'Meeting', 'nodebook' ), 'feedback' => __( 'Feedback', 'nodebook' ), 'other_contact' => __( 'Contact', 'nodebook' ), ); foreach ( $rows as $r ) { $s['cursor'] = (int) $r['ID']; $s['marks']['notes'] = (int) $r['ID']; $type = self::log_type( $r['zbsl_type'] ); $cid = $map[ (int) $r['zbsl_objid'] ] ?? 0; if ( ! $cid || ! in_array( $type, self::LOG_TYPES, true ) ) { continue; } $at = (int) $r['zbsl_created'] > 0 ? gmdate( 'Y-m-d H:i:s', (int) $r['zbsl_created'] ) : ''; if ( Nodebook_Import::note( $cid, $kinds[ $type ] ?? '', (string) $r['zbsl_shortdesc'], (string) $r['zbsl_longdesc'], $at, (int) $r['zbs_owner'], 'jetpackcrm' ) ) { ++$s['stats']['notes']; } } return $s; } }