/**
  * 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;
 }
 /**
  * Update metas.
  * 
  * @since    2.0
  *
  * @param    int    $movie_id
  * 
  * @return   int|bool    False if update failed, true else
  */
 private static function update_meta($movie_id)
 {
     $meta = get_post_meta($movie_id, '_wpmoly_movie_data', $single = true);
     if ('' == $meta) {
         return false;
     }
     $update = WPMOLY_Edit_Movies::save_movie_meta($movie_id, $meta, $clean = false);
     if (!wpmoly_o('legacy-safety') && !is_wp_error($update) && $update == $movie_id) {
         delete_post_meta($movie_id, '_wpmoly_movie_data', $meta);
     }
     return $update;
 }
 /**
  * Initializes variables
  *
  * @since    1.0
  */
 public function init()
 {
     $this->modules = array('WPMOLY_Dashboard' => WPMOLY_Dashboard::get_instance(), 'WPMOLY_Settings' => WPMOLY_Settings::get_instance(), 'WPMOLY_TMDb' => WPMOLY_TMDb::get_instance(), 'WPMOLY_Utils' => WPMOLY_Utils::get_instance(), 'WPMOLY_Metaboxes' => WPMOLY_Metaboxes::get_instance(), 'WPMOLY_Edit_Movies' => WPMOLY_Edit_Movies::get_instance(), 'WPMOLY_Media' => WPMOLY_Media::get_instance(), 'WPMOLY_Import' => WPMOLY_Import::get_instance(), 'WPMOLY_Queue' => WPMOLY_Queue::get_instance());
     $this->screen_hooks = array('edit' => 'post.php', 'new' => 'post-new.php', 'movie' => 'movie', 'movies' => 'edit.php', 'widgets' => 'widgets.php', 'settings' => sprintf('%s_page_wpmovielibrary-settings', strtolower(__('Movies', 'wpmovielibrary'))));
     $this->hidden_pages = array();
 }
 /**
  * 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);
 }