load_settings( $post ); $this->schedule_cron_event(); if ( $this->config()->get( 'post_type' ) === 'custom' ) { $this->register_post_type(); } } /** * Register associated post type */ public function register_post_type() { $cpt_slug = sanitize_key( $this->config()->get( 'post_type_slug' ) ); $cpt_name = $this->config()->get( 'post_type_name' ); if ( $cpt_slug && $cpt_name ) { register_post_type( $cpt_slug, array( 'labels' => array( 'name' => $cpt_name, 'singular_name' => $cpt_name, ), 'public' => true, 'supports' => array( 'title', 'editor', 'author', 'excerpt', 'thumbnail', 'custom-fields' ), 'rewrite' => array( 'slug' => $cpt_slug, 'with_front' => false, ), ) ); } } /** * Infos getter */ public function infos() { return $this->infos; } /** * Config getter */ public function config() { return $this->config; } /** * Scheduled Sync next getter */ public function get_next_scheduled_sync() { return wp_next_scheduled( $this->get_schedule_slug() ); } /** * Post Type getter */ public function get_post_type() { return $this->config()->get( 'post_type' ) === 'custom' ? $this->config()->get( 'post_type_slug' ) : $this->config()->get( 'post_type' ); } /** * Fields getter */ public function get_airtable_fields() { return get_post_meta( $this->infos()->get( 'id' ), 'table_fields', true ); } /** * Run ID getter */ public function get_run_id() { return get_post_meta( $this->infos()->get( 'id' ), 'run', true ); } /** * Cron action */ public function cron() { return $this->run(); } /** * Run importer */ public function run() { if ( $this->get_run_id() ) { return new WP_Error( 'air-wp-sync-run-error', __( 'A sync is already running.', 'air-wpsync' ) ); } try { // Define a unique id for this run $run_id = uniqid(); // Save run update_post_meta( $this->infos()->get( 'id' ), 'run', $run_id ); $this->log( sprintf( 'Starting importer...' ) ); // Save table schema update_post_meta( $this->infos()->get( 'id' ), 'table_fields', $this->get_table_fields() ); // Loop through all pages $offset = null; do { wp_cache_delete( $this->infos()->get( 'id' ), 'post_meta' ); $offset = $this->get_records( $run_id, $offset ); } while ( ! empty( $offset ) && $this->get_run_id() ); return true; } catch ( Exception $e ) { $this->log( $e->getMessage() ); $this->end_run( 'error', $e->getMessage() ); return new WP_Error( 'air-wp-sync-run-error', $e->getMessage() ); } catch ( TypeError $e ) { $this->log( $e->getMessage() ); $this->end_run( 'error', $e->getMessage() ); return new WP_Error( 'air-wp-sync-run-error', $e->getMessage() ); } } /** * Log message to file and WPCLI output */ public function log( $message, $level = 'log' ) { if ( ! is_dir( AIR_WP_SYNC_LOGDIR ) ) { wp_mkdir_p( AIR_WP_SYNC_LOGDIR ); } $file = fopen( AIR_WP_SYNC_LOGDIR . '/' . $this->infos()->get( 'slug' ) . '-' . gmdate( 'Y-m-d' ) . '-' . $this->get_run_id() . '.log', 'a' ); if ( ! is_string( $message ) ) { $message = var_export( $message, true ); } fwrite( $file, "\n" . gmdate( 'Y-m-d H:i:s' ) . ' ' . $level . ' :: ' . $message ); fclose( $file ); if ( class_exists( 'WP_CLI' ) ) { $method = method_exists( 'WP_CLI', $level ) ? $level : 'log'; WP_CLI::$method( $message ); } } /** * Process an airtable record import */ public function process_airtable_record( $record ) { $this->log( sprintf( 'Record ID %s', $record->id ) ); try { // Check if we have existing post for this record $post_id = $this->get_object_id_from_record_id( $record->id, $this->get_post_type() ); if ( $post_id ) { $this->log( sprintf( '- Found matching post, ID %s', $post_id ) ); // Check if we must update it if ( 'add' !== $this->config()->get( 'sync_strategy' ) && $this->needs_update( $post_id, $record ) ) { $post_id = $this->import_record( $record, $post_id ); } else { $this->log( sprintf( '- No update needed' ) ); } } else { $post_id = $this->import_record( $record ); } return $post_id; } catch ( Exception $e ) { $this->log( $e->getMessage() ); return new WP_Error( 'air-wp-sync-run-error', $e->getMessage() ); } catch ( TypeError $e ) { $this->log( $e->getMessage() ); return new WP_Error( 'air-wp-sync-run-error', $e->getMessage() ); } } /** * Delete other posts existing in WP but deleted in AT */ public function delete_removed_posts() { if ( 'add_update_delete' !== $this->config()->get( 'sync_strategy' ) ) { return; } $post_ids = get_post_meta( $this->infos()->get( 'id' ), 'post_ids', true ) ?: array(); if ( ! is_array( $post_ids ) ) { $post_ids = array(); } $posts = get_posts( array( 'post_type' => $this->get_post_type(), 'post_status' => 'any', 'post__not_in' => $post_ids, 'fields' => 'ids', 'posts_per_page' => -1, 'meta_query' => array( array( 'relation' => 'OR', array( 'key' => '_air_wp_sync_importer_id', 'value' => $this->infos()->get( 'id' ), ), array( 'key' => '_air_wp_sync_importer_id', 'compare' => 'NOT EXISTS', ), ), array( 'key' => '_air_wp_sync_record_id', 'compare' => 'EXISTS', ), ), ) ); foreach ( $posts as $post_id ) { wp_delete_post( $post_id, true ); } } /** * End run */ public function end_run( $status = 'success', $error = null ) { global $wpdb; $importer_id = $this->infos()->get( 'id' ); $run_id = $this->get_run_id(); // Delete any remaining AS actions $action_ids = \ActionScheduler::store()->query_actions( array( 'hook' => 'airwpsync_process_records', 'status' => \ActionScheduler_Store::STATUS_PENDING, 'partial_args_matching' => 'like', 'args' => array( 'importer_id' => $importer_id, 'run_id' => $run_id, ), 'per_page' => -1, ) ); foreach ( $action_ids as $action_id ) { \ActionScheduler::store()->cancel_action( $action_id ); } // Delete temporary options $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( sprintf( 'airwpsync-%s-run-%s-', $importer_id, $run_id ) ) . '%' ) ); // Remove temporary metas update_post_meta( $importer_id, 'post_ids', null ); update_post_meta( $importer_id, 'run', null ); update_post_meta( $importer_id, 'table_fields', null ); // Update status and error update_post_meta( $importer_id, 'status', $status ); update_post_meta( $importer_id, 'last_error', $error ); // Save date if success if ( 'success' === $status ) { update_post_meta( $importer_id, 'last_updated', gmdate( 'Y-m-d H:i:s' ) ); } } /** * Get table fields from API */ protected function get_table_fields() { $app_id = $this->config()->get( 'app_id' ); $data = $this->get_api_client()->get_tables( $app_id ); $table = null; if ( $data->tables ) { foreach ( $data->tables as $t ) { if ( $t->id === $this->config()->get( 'table' ) ) { $table = $t; break; } } } $fields = $table && $table->fields ? $table->fields : array(); return apply_filters( 'airwpsync/get_table_fields', $fields ); } /** * Get AT records from API */ protected function get_records( $run_id, $offset = null ) { $api_list_options = array( 'offset' => $offset, 'view' => $this->config()->get( 'view' ), 'filterByFormula' => $this->config()->get( 'formula_filter' ), 'returnFieldsByFieldId' => true, ); // Remove empty values $api_list_options = array_filter( $api_list_options ); // Get records $app_id = $this->config()->get( 'app_id' ); $table_id = $this->config()->get( 'table' ); $data = $this->get_api_client()->list_records( $app_id, $table_id, $api_list_options ); // Loop through all records $chunks = array_chunk( $data->records, 10 ); foreach ( $chunks as $chunk ) { // Save Airtable record as a temporary option $item_id = uniqid( 'airwpsync-' . $this->infos()->get( 'id' ) . '-run-' . $this->get_run_id() . '-item-' ); update_option( $item_id, $chunk ); // Add it to queue as_enqueue_async_action( 'airwpsync_process_records', array( 'importer_id' => $this->infos()->get( 'id' ), 'run_id' => $run_id, 'item_id' => $item_id, ) ); } return ! empty( $data->offset ) ? $data->offset : false; } /** * Load importer settings from post object */ protected function load_settings( $post ) { $infos = array( 'id' => $post->ID, 'slug' => $post->post_name, 'title' => $post->post_title, 'modified' => $post->post_modified_gmt, 'hash' => wp_hash( $post->ID ), ); $this->infos = new Air_WP_Sync_Importer_Settings( $infos ); $config = json_decode( $post->post_content, true ); $this->config = new Air_WP_Sync_Importer_Settings( $config ); } /** * Get cron schedule slug */ protected function get_schedule_slug() { return 'air_wp_sync_importer_' . $this->infos()->get( 'id' ); } /** * Init cron events */ protected function schedule_cron_event() { if ( 'cron' === $this->config()->get( 'scheduled_sync.type' ) && $this->config()->get( 'scheduled_sync.recurrence' ) ) { add_action( $this->get_schedule_slug(), array( $this, 'cron' ) ); if ( false === $this->get_next_scheduled_sync() ) { wp_schedule_event( $this->get_schedule_timestamp(), $this->config()->get( 'scheduled_sync.recurrence' ), $this->get_schedule_slug() ); } } else { if ( $this->get_next_scheduled_sync() ) { wp_clear_scheduled_hook( $this->get_schedule_slug() ); } } } /** * Get Schedule timestamp */ protected function get_schedule_timestamp() { $datetime = new DateTime( 'now', new DateTimeZone( wp_timezone_string() ) ); $recurrence = $this->config()->get( 'scheduled_sync.recurrence' ); if ( 'weekly' === $recurrence ) { if ( $this->config()->get( 'scheduled_sync.weekday' ) ) { $datetime->modify( 'next ' . $this->config()->get( 'scheduled_sync.weekday' ) ); } } if ( in_array( $recurrence, array( 'weekly', 'daily' ), true ) ) { if ( $this->config()->get( 'scheduled_sync.time' ) ) { $time = explode( ':', $this->config()->get( 'scheduled_sync.time' ) ); $datetime->setTime( $time[0], $time[1] ); } } else { $schedules = wp_get_schedules(); $interval = isset( $schedules[ $recurrence ] ) ? $schedules[ $recurrence ]['interval'] : HOUR_IN_SECONDS; $datetime->add( new DateInterval( 'PT' . $interval . 'S' ) ); } return $datetime->getTimestamp(); } /** * Get or instanciate Airtable API client */ protected function get_api_client() { if ( null === $this->api_client ) { $this->api_client = new Air_WP_Sync_Airtable_Api_Client( $this->config()->get( 'api_key' ) ); } return $this->api_client; } /** * Import AT record */ protected function import_record( $record, $post_id = null ) { $this->log( sprintf( $post_id ? '- Update record %s' : '- Create record %s', $record->id ) ); $record = apply_filters( 'airwpsync/import_record_data', $record, $this ); // Airtable omit keys for empty fields, lets add them with an empty string $mapping = ! empty( $this->config()->get( 'mapping' ) ) ? $this->config()->get( 'mapping' ) : array(); $airtable_keys = array_map( function( $mapping_pair ) { if ( preg_match( '/(.+)\.(.+)/', $mapping_pair['airtable'], $matches ) ) { $airtable_id = $matches[1]; } else { $airtable_id = $mapping_pair['airtable']; } return $airtable_id; }, $mapping ); $fields = array(); foreach ( $airtable_keys as $airtable_key ) { $fields[ $airtable_key ] = isset( $record->fields->$airtable_key ) ? $record->fields->$airtable_key : ''; } $fields = apply_filters( 'airwpsync/import_record_fields', $fields, $this ); $post_data = array( 'post_type' => $this->get_post_type(), 'post_author' => $this->config()->get( 'post_author' ), 'post_status' => $this->config()->get( 'post_status' ), 'post_title' => 'Airtable Imported Content', ); $post_metas = array( '_air_wp_sync_record_id' => $record->id, '_air_wp_sync_hash' => $this->generate_hash( $record ), '_air_wp_sync_importer_id' => $this->infos()->get( 'id' ), '_air_wp_sync_updated_at' => gmdate( 'Y-m-d H:i:s' ), ); $post_data = apply_filters( 'airwpsync/import_post_data', $post_data, $this, $fields, $record, $post_id ); // Insert or update post if ( $post_id ) { $post_id = wp_update_post( array_merge( array( 'ID' => $post_id ), $post_data ), true ); } else { $post_id = wp_insert_post( $post_data, true ); } if ( is_wp_error( $post_id ) ) { throw new Exception( $post_id->get_error_message() ); } // Handle metas foreach ( $post_metas as $meta_key => $meta_value ) { update_post_meta( $post_id, $meta_key, $meta_value ); } do_action( 'airwpsync/import_record_after', $this, $fields, $record, $post_id ); // Force wp_insert_post retrigger after metas do_action( 'wp_insert_post', $post_id, get_post( $post_id ), true ); return $post_id; } /** * Get WP object id from AT record id */ protected function get_object_id_from_record_id( $record_id, $post_type ) { $objects = get_posts( array( 'fields' => 'ids', 'post_type' => $post_type, 'post_status' => 'any', 'meta_query' => array( array( 'relation' => 'OR', array( 'key' => '_air_wp_sync_importer_id', 'value' => $this->infos()->get( 'id' ), ), array( 'key' => '_air_wp_sync_importer_id', 'compare' => 'NOT EXISTS', ), ), array( 'key' => '_air_wp_sync_record_id', 'value' => $record_id, ), ), ) ); return array_shift( $objects ); } /** * Compare hashes to check if WP object needs update */ protected function needs_update( $post_id, $record ) { return $this->generate_hash( $record ) !== $this->get_post_hash( $post_id ); } /** * Get stored post hash */ protected function get_post_hash( $post_id ) { return get_post_meta( $post_id, '_air_wp_sync_hash', true ); } /** * Generate hash for given Airtable record */ protected function generate_hash( $record ) { // Remove cs & ts query strings from urls in AT record $record_json = preg_replace( '/cs=[^"]+/i', '', wp_json_encode( $record ) ); $record_json = preg_replace( '/ts=[^"]+/i', '', $record_json ); return md5( $record_json . wp_json_encode( $this->config()->to_array() ) ); } }