Posts_Import::run_import( $raw_feed, \Vimeotheque\Feed|array $import_options )
Imports videos from a given feed source.
Description
Used by automatic updates.
Parameters
- $raw_feed
-
(Required)
- $import_options
-
(Required)
Return
(array|void)
Source
File: includes/libs/posts-import.class.php
public function run_import( $raw_feed, $import_options ){
/**
* @var array $native_tax
* @var array $native_tag
* @var bool $import_description
* @var string $import_status
* @var bool $import_title
* @var bool $import_date
* @var int $import_user
*/
extract( $this->get_import_options( $import_options ), EXTR_SKIP );
// get import options
$options = Plugin::instance()->get_options();
// overwrite plugin import settings with import settings
$options['import_description'] = $import_description;
$options['import_status'] = $import_status;
$options['import_title'] = $import_title;
// store results
$result = [
'private' => 0,
'imported' => 0,
'skipped' => 0,
'total' => count( $raw_feed ),
'ids' => [],
'error' => []
];
$duplicates = $this->get_duplicate_posts( $raw_feed, $this->post_type->get_post_type() );
// parse feed
foreach( $raw_feed as $video ){
// video already exists, don't do anything
if( array_key_exists( $video['video_id'], $duplicates ) ){
/**
* Generate an error and pass it for debugging
* @var WP_Error
*/
$error = new WP_Error(
'cvm_import_skip_existing_video',
sprintf(
'%s %s',
sprintf(
__( 'Skipped video having ID %s because it already exists.', 'codeflavors-vimeo-video-post-lite' ),
$video['video_id']
),
sprintf(
__( 'Existing post has ID %s.', 'codeflavors-vimeo-video-post-lite' ),
$duplicates[ $video['video_id'] ][0]
)
),
[
'video_data' => $video,
'existing_posts' => $duplicates[ $video['video_id'] ]
]
);
$result['error'][] = $error;
/**
* Pass error to debug function
*/
Helper::debug_message(
'Import error: ' . $error->get_error_message(),
"\n",
$error
);
foreach( $duplicates[ $video['video_id'] ] as $_post_id ){
// retrieve the post object for backwards compatibility
$post = get_post( $_post_id );
/**
* Action triggered when duplicate posts were detected.
* Can be used to set extra taxonomies for already existing posts.
*
* @param \WP_Post $post The WordPress post object that was found as duplicate.
* @param string $taxonomy The taxonomy that must be imported for the post.
* @param string $taxonomy_value The plugin taxonomy that must be set up.
* @param string $tag_taxonomy The tag taxonomy that must be set up.
* @param string $tag_taxonomy_value The tag taxonomy value that must be set for the post.
*/
do_action( 'vimeotheque\import_duplicate_taxonomies',
$post,
$this->post_type->get_post_tax(),
$native_tax,
$this->post_type->get_tag_tax(),
$native_tag
);
}
$result['skipped'] += 1;
continue;
}
if( 'private' == $video['privacy'] ){
$result['private'] += 1;
if( 'skip' == $options['import_privacy'] ){
$result['skipped'] += 1;
/**
* Generate an error and pass it for debugging
* @var WP_Error
*/
$error = new WP_Error(
'cvm_import_skip_private_video',
sprintf(
__( 'Skipped private video having ID %s because of plugin settings.', 'cvm-video' ),
$video['video_id']
),
[
'video_data' => $video
]
);
$result['error'][] = $error;
/**
* Send error to debug function
*/
Helper::debug_message(
'Import error: ' . $error->get_error_message(),
"\n",
$error
);
continue;
}
}
$post_id = $this->import_video( [
'video' => $video, // video details retrieved from Vimeo
'category' => $native_tax, // category name (if any) - will be created if category_id is false
'tags' => $native_tag,
'user' => ( isset( $import_user ) ? absint( $import_user ) : false ), // save as a given user if any
'post_format' => 'video', // post format will default to video
'status' => $this->post_type->get_post_settings()->post_status( $import_status ), // post status
'options' => $options
] );
if( $post_id ){
$result['imported'] += 1;
$result['ids'][] = $post_id;
$video = Helper::get_video_post( $post_id );
$video->set_embed_options(
$this->get_import_options( $import_options ),
true
);
}
}
Helper::debug_message(
sprintf(
'Processed %d entries: created %d posts, skipped %d entries, %d entries were marked private.',
$result['total'],
$result['imported'],
$result['skipped'],
$result['private']
)
);
return $result;
}
