class name implementing
* label(), available(), counts() and step( array $state ) (see Nodebook_Import_Fluentcrm).
*
* @package Nodebook
*/
defined( 'ABSPATH' ) || exit;
final class Nodebook_Import {
const STATE = 'nodebook_import_';
/** @return array id => class */
public static function sources() {
return (array) apply_filters(
'nodebook_import_sources',
array(
'fluentcrm' => 'Nodebook_Import_Fluentcrm',
'groundhogg' => 'Nodebook_Import_Groundhogg',
'jetpackcrm' => 'Nodebook_Import_Jetpackcrm',
)
);
}
/** Sources whose data is in this database. */
public static function available() {
$out = array();
foreach ( self::sources() as $id => $class ) {
if ( class_exists( $class ) && call_user_func( array( $class, 'available' ) ) ) {
$out[ $id ] = $class;
}
}
return $out;
}
public static function state( $id ) {
$s = get_option( self::STATE . sanitize_key( $id ), array() );
return is_array( $s ) ? $s : array();
}
/**
* Run the next batch. Call until 'done' is true.
*
* @param string $id Source ID.
* @param bool $fresh Start a new run (keeps what was imported before; re-runs only add what is new).
* @return array|WP_Error state: step, label, stats, done, progress (0-100)
*/
public static function run( $id, $fresh = false ) {
$sources = self::available();
if ( ! isset( $sources[ $id ] ) ) {
return new WP_Error( 'nodebook_import', __( 'That CRM\'s data was not found in this WordPress database.', 'nodebook' ) );
}
$class = $sources[ $id ];
$state = self::state( $id );
if ( $fresh || empty( $state['step'] ) || 'done' === $state['step'] ) {
// Keep the high-water marks of notes and events, so a second run does not import them twice.
$state = array(
'step' => 'setup',
'stats' => array(
'created' => 0,
'updated' => 0,
'skipped' => 0,
'notes' => 0,
'events' => 0,
'segments' => 0,
'fields' => 0,
),
'marks' => $state['marks'] ?? array(),
'total' => (int) ( call_user_func( array( $class, 'counts' ) )['contacts'] ?? 0 ),
'seen' => 0,
);
}
$start = microtime( true );
// Several small steps per request, up to about 8 seconds.
do {
$state = call_user_func( array( $class, 'step' ), $state );
} while ( 'done' !== $state['step'] && microtime( true ) - $start < 8 );
$state['done'] = 'done' === $state['step'];
$state['progress'] = $state['done'] ? 100 : min( 99, (int) floor( (int) $state['seen'] / max( 1, (int) $state['total'] ) * 95 ) );
if ( $state['done'] ) {
$state['finished_at'] = current_time( 'mysql', true );
Nodebook_Contacts::flush_caches();
}
update_option( self::STATE . $id, $state, false );
return $state;
}
/* ------------------------------------------------------------------ *
* Helpers shared by sources
* ------------------------------------------------------------------ */
/** Static segment by name, created when missing. */
public static function segment( $name ) {
foreach ( Nodebook_Segments::all( 'static' ) as $s ) {
if ( 0 === strcasecmp( $s['name'], $name ) ) {
return (int) $s['id'];
}
}
return Nodebook_Segments::save(
array(
'name' => $name,
'type' => 'static',
)
);
}
/**
* One of your fields for an imported field definition: an existing field with the same name, or a new one.
*
* @param string $label Field name.
* @param string $type Nodebook field type.
* @param array $options Choices for "select".
* @return string Field key, or '' when the maximum number of fields is reached.
*/
public static function field( $label, $type, array $options = array() ) {
$key = Nodebook_Props::match( 'contact', $label );
if ( $key ) {
return $key;
}
return Nodebook_Props::save_def(
'contact',
array(
'label' => $label,
'type' => $type,
'options' => $options,
)
);
}
/**
* A note, call or meeting from another CRM, on the timeline at its original date.
*
* @param int $contact_id Contact.
* @param string $kind "Call", "Meeting"… ('' for a plain note).
* @param string $title Short title (may be empty).
* @param string $body Text or HTML.
* @param string $at When (GMT datetime).
* @param int $user_id Author.
* @param string $source Import ID, kept with the note.
* @return bool
*/
public static function note( $contact_id, $kind, $title, $body, $at, $user_id, $source ) {
$body = trim( html_entity_decode( wp_strip_all_tags( str_replace( array( '
', '
', '
', '
' ), "\n", (string) $body ) ), ENT_QUOTES, 'UTF-8' ) );
$title = trim( (string) $title );
$text = trim( ( '' !== $title && $title !== $body ? $title . "\n" : '' ) . $body );
if ( '' === $text ) {
return false;
}
return (bool) Nodebook_Activities::add(
$contact_id,
'note',
( '' !== $kind ? $kind . ': ' : '' ) . wp_trim_words( '' !== $title ? $title : $body, 12, '…' ),
array(
'user_id' => get_userdata( (int) $user_id ) ? (int) $user_id : 0,
'created_at' => $at && strtotime( $at ) ? gmdate( 'Y-m-d H:i:s', strtotime( $at ) ) : current_time( 'mysql', true ),
'data' => array(
'text' => $text,
'import' => $source,
),
)
);
}
/** Owner: kept only when that WordPress user may use Nodebook. */
public static function owner( $user_id ) {
$user_id = (int) $user_id;
return $user_id > 0 && isset( Nodebook_Team::users()[ $user_id ] ) ? $user_id : 0;
}
/** Contact data from an import: create or fill in (never overwrite what Nodebook already knows). */
public static function contact( array $data, $source, $created_at = '' ) {
$data['source'] = $data['source'] ?? $source;
$r = Nodebook_Contacts::upsert( $data, 'fill' );
if ( $r['id'] && $r['created'] ) {
global $wpdb;
$when = $created_at && strtotime( $created_at ) ? gmdate( 'Y-m-d H:i:s', strtotime( $created_at ) ) : '';
if ( $when ) {
// Keep when the relationship really started.
$wpdb->update( Nodebook_Contacts::table(), array( 'created_at' => $when, 'status_at' => $when ), array( 'id' => $r['id'] ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table.
}
Nodebook_Activities::add(
$r['id'],
'import',
/* translators: %s: CRM name */
sprintf( __( 'Imported from %s', 'nodebook' ), $source ),
array(
'user_id' => 0,
'created_at' => $when ? $when : current_time( 'mysql', true ),
)
);
}
return $r;
}
}