/**
  * Save a movie in the queue list.
  * 
  * This is used to pre-import the movies submitted from a list
  * and for which the metadata have been fetched, without not saving
  * them as movies, meaning we save the metadata but don't import
  * the posters. This is indicated for (very) large list of movies
  * that can be a pain to import if anything goes wrong when
  * downloading the poster.
  *
  * @since    1.0
  * 
  * @param    array    $metadata Movie metadata.
  * 
  * @return   int|WP_Error    Movie Post ID if successfully enqueued, WP_Error if failed.
  */
 public static function enqueue_movie($movie)
 {
     $post_id = esc_attr($movie['post_id']);
     $post_title = esc_attr($movie['title']);
     $post_title = str_replace("'", "'", $post_title);
     $metadata = $movie;
     $post_date = current_time('mysql');
     $post_date = wp_checkdate(substr($post_date, 5, 2), substr($post_date, 8, 2), substr($post_date, 0, 4), $post_date);
     $post_date_gmt = get_gmt_from_date($post_date);
     $_post = array('ID' => $post_id, 'post_date' => $post_date, 'post_date_gmt' => $post_date_gmt, 'post_name' => sanitize_title($post_title), 'post_title' => $post_title, 'post_status' => 'import-queued');
     $update = wp_update_post($_post, $wp_error = true);
     if (is_wp_error($update)) {
         return new WP_Error('error', sprintf(__('An error occured when adding "%s" to the queue: %s', 'wpmovielibrary'), $post_title, $update->get_error_message()));
     }
     $update = WPMOLY_Edit_Movies::save_movie($update, $post = null, $queue = true, $metadata);
     if (is_wp_error($update)) {
         return new WP_Error('error', sprintf(__('An error occured when adding "%s" to the queue: %s', 'wpmovielibrary'), $post_title, $update->get_error_message()));
     }
     return $post_id;
 }
 /**
  * Render movie import page
  *
  * @since    1.0
  */
 public static function import_page()
 {
     $errors = new WP_Error();
     $imported = array();
     $_section = '';
     $movies = (array) wp_count_posts('movie');
     $_imported = $movies['import-draft'];
     $_queued = $movies['import-queued'];
     if (isset($_POST['wpmoly_save_imported']) && '' != $_POST['wpmoly_save_imported'] && isset($_POST['wpmoly_imported_ids']) && '' != $_POST['wpmoly_imported_ids'] && isset($_POST['movies']) && count($_POST['movies'])) {
         wpmoly_check_admin_referer('save-imported-movies');
         $post_ids = explode(',', $_POST['wpmoly_imported_ids']);
         foreach ($_POST['movies'] as $movie) {
             if (0 != $movie['tmdb_id'] && in_array($movie['post_id'], $post_ids)) {
                 $update = WPMOLY_Edit_Movies::save_movie($movie['post_id'], $post = null, $queue = false, $movie);
                 if (is_wp_error($update)) {
                     $errors->add($update->get_error_code(), $update->get_error_message());
                 } else {
                     $imported[] = $update;
                 }
             }
         }
         if (!empty($errors->errors)) {
             $_errors = array();
             foreach ($errors->errors as $error) {
                 if (is_array($error)) {
                     foreach ($error as $e) {
                         $_errors[] = '<li>' . $e . '</li>';
                     }
                 } else {
                     $_errors[] = '<li>' . $error . '</li>';
                 }
             }
             WPMOLY_Utils::admin_notice(sprintf(__('The following errors occured: <ul>%s</ul>', 'wpmovielibrary'), implode('', $_errors)), 'error');
         }
         if (!empty($imported)) {
             WPMOLY_Utils::admin_notice(sprintf(_n('One movie imported successfully!', '%d movies imported successfully!', count($imported), 'wpmovielibrary'), count($imported)), 'updated');
         }
     } else {
         if (isset($_POST['wpmoly_importer']) && '' != $_POST['wpmoly_importer']) {
         }
     }
     if (isset($_GET['wpmoly_section']) && in_array($_GET['wpmoly_section'], array('wpmoly_import', 'wpmoly_import_queue', 'wpmoly_imported'))) {
         $_section = $_GET['wpmoly_section'];
     }
     $attributes = array('_section' => $_section, '_queued' => $_queued, '_imported' => $_imported);
     echo self::render_admin_template('/import/import.php', $attributes);
 }