markdown_formatter = $markdown_formatter; $this->interval_formatter = $interval_formatter; add_filter( 'airwpsync/import_post_data', array( $this, 'add_to_post_data' ), 20, 3 ); } /** * Handle post data importing */ public function add_to_post_data( $post_data, $importer, $fields ) { $mapped_fields = $this->get_destination_mapping( $importer, $fields ); foreach ( $mapped_fields as $mapped_field ) { $value = $this->get_airtable_value( $fields, $mapped_field['airtable'], $importer ); $post_data[ $mapped_field['wordpress'] ] = $this->format( $value, $mapped_field, $importer ); } return $post_data; } /** * Add field features for each post types */ protected function get_features_by_post_type( $post_type ) { $features = array( 'post_name', 'post_date', ); if ( post_type_supports( $post_type, 'title' ) ) { $features[] = 'post_title'; } if ( post_type_supports( $post_type, 'editor' ) ) { $features[] = 'post_content'; } if ( post_type_supports( $post_type, 'excerpt' ) ) { $features[] = 'post_excerpt'; } return $features; } /** * Assign fields to mapping group */ protected function get_group() { return array( 'label' => __( 'Post', 'air-wp-sync' ), 'slug' => 'post', ); } /** * Get mapping fields */ protected function get_mapping_fields() { return array( array( 'value' => 'post_title', 'label' => __( 'Title', 'air-wp-sync' ), 'enabled' => true, 'supported_sources' => $this->string_supported_sources, ), array( 'value' => 'post_content', 'label' => __( 'Content', 'air-wp-sync' ), 'enabled' => true, 'supported_sources' => $this->string_supported_sources, ), array( 'value' => 'post_excerpt', 'label' => __( 'Excerpt', 'air-wp-sync' ), 'enabled' => true, 'supported_sources' => $this->string_supported_sources, ), array( 'value' => 'post_name', 'label' => __( 'Slug', 'air-wp-sync' ), 'enabled' => true, 'supported_sources' => $this->string_supported_sources, ), array( 'value' => 'post_date', 'label' => __( 'Publication Date', 'air-wp-sync' ), 'enabled' => true, 'supported_sources' => array( 'date', 'dateTime', 'lastModifiedTime', ), ), ); } /** * Format imported value */ protected function format( $value, $mapped_field, $importer ) { $airtable_id = $mapped_field['airtable']; $destination = $mapped_field['wordpress']; $source_type = $this->get_source_type( $airtable_id, $importer ); if ( 'post_date' === $destination ) { if ( ! empty( $value ) && false === strtotime( $value ) ) { $value = null; } $value = iso8601_to_datetime( $value ); } else { // Markdown if ( 'richText' === $source_type ) { $value = $this->markdown_formatter->format( $value ); } // Date elseif ( in_array( $source_type, array( 'date', 'dateTime' ), true ) ) { $value = date_i18n( get_option( 'date_format' ), strtotime( $value ) ); } elseif ( 'duration' === $source_type ) { $field = $this->get_field_by_id( $airtable_id, $importer ); $value = $this->interval_formatter->format( $value, $field ); } // Multiple values elseif ( is_array( $value ) ) { $value = implode( ', ', $value ); } // Default string else { $value = strval( $value ); } // Remove html tags if saving as post title or slug if ( 'post_title' === $destination ) { $value = wp_strip_all_tags( $value ); } } return $value; } }