/**
  * 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();
 }
 /**
  * Convert a queued movie to regular movie.
  * 
  * Simply change the movie's post_status from 'import-queued' to
  * 'publish' and 
  *
  * @since    1.0
  * 
  * @param    int    $post_id Movie Post ID
  * 
  * @return   int|boolean        ID of the updated movie if everything worked, false else.
  */
 private static function import_queued_movie($post_id)
 {
     if (is_null($post_id) || !($post = get_post($post_id) || 'movie' != get_post_type($post_id))) {
         return new WP_Error('invalid_movie', sprintf(__('Error: submitted Post ID doesn\\t match any valid movie.', 'wpmovielibrary')));
     }
     $meta = wpmoly_get_movie_meta($post_id);
     if ('' == $meta || !is_array($meta) || !isset($meta['poster']) || !isset($meta['tmdb_id'])) {
         return new WP_Error('invalid_meta', sprintf(__('Error: cannot find submitted movie\'s metadata, try enqueuing it again.', 'wpmovielibrary')));
     }
     $_post = array('ID' => $post_id, 'post_status' => 'publish');
     if (wpmoly_o('poster-featured')) {
         $id = WPMOLY_Media::set_image_as_featured($meta['poster'], $post_id, $meta['tmdb_id'], $meta['title']);
         update_post_meta($post_id, '_thumbnail_id', $id);
     }
     $update = wp_update_post($_post, $wp_error = true);
     if (is_wp_error($update)) {
         return new WP_Error('error', sprintf(__('An error occured when trying to imported queued movie "%s": %s', 'wpmovielibrary'), get_the_title($post_id), $update->get_error_message()));
     }
     return $update;
 }
 /**
  * Save TMDb fetched data.
  * 
  * Uses the 'save_post_movie' action hook to save the movie metadata
  * as a postmeta. This method is used in regular post creation as
  * well as in movie import. If no $movie_meta is passed, we're 
  * most likely creating a new movie, use $_REQUEST to get the data.
  * 
  * Saves the movie details as well.
  *
  * @since    1.0
  * 
  * @param    int        $post_ID ID of the current Post
  * @param    object     $post Post Object of the current Post
  * @param    boolean    $queue Queued movie?
  * @param    array      $movie_meta Movie Metadata to save with the post
  * 
  * @return   int|WP_Error
  */
 public static function save_movie($post_ID, $post, $queue = false, $movie_meta = null)
 {
     if (!current_user_can('edit_post', $post_ID)) {
         return new WP_Error(__('You are not allowed to edit posts.', 'wpmovielibrary'));
     }
     if (!($post = get_post($post_ID) || 'movie' != get_post_type($post))) {
         return new WP_Error(sprintf(__('Posts with #%s is invalid or is not a movie.', 'wpmovielibrary'), $post_ID));
     }
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return $post_ID;
     }
     $errors = new WP_Error();
     if (!is_null($movie_meta) && count($movie_meta)) {
         // Save TMDb data
         self::save_movie_meta($post_ID, $movie_meta);
         // Set poster as featured image
         if (wpmoly_o('poster-featured') && !$queue) {
             $upload = WPMOLY_Media::set_image_as_featured($movie_meta['poster'], $post_ID, $movie_meta['tmdb_id'], $movie_meta['title']);
             if (is_wp_error($upload)) {
                 $errors->add($upload->get_error_code(), $upload->get_error_message());
             } else {
                 update_post_meta($post_ID, '_thumbnail_id', $upload);
             }
         }
         // Switch status from import draft to published
         if ('import-draft' == get_post_status($post_ID) && !$queue) {
             $update = wp_update_post(array('ID' => $post_ID, 'post_name' => sanitize_title_with_dashes($movie_meta['title']), 'post_status' => 'publish', 'post_title' => $movie_meta['title'], 'post_date' => current_time('mysql')), $wp_error = true);
             if (is_wp_error($update)) {
                 $errors->add($update->get_error_code(), $update->get_error_message());
             }
         }
         // Autofilling Actors
         if (wpmoly_o('enable-actor') && wpmoly_o('actor-autocomplete')) {
             $limit = intval(wpmoly_o('actor-limit'));
             $actors = explode(',', $movie_meta['cast']);
             if ($limit) {
                 $actors = array_slice($actors, 0, $limit);
             }
             $actors = wp_set_object_terms($post_ID, $actors, 'actor', false);
         }
         // Autofilling Genres
         if (wpmoly_o('enable-genre') && wpmoly_o('genre-autocomplete')) {
             $genres = explode(',', $movie_meta['genres']);
             $genres = wp_set_object_terms($post_ID, $genres, 'genre', false);
         }
         // Autofilling Collections
         if (wpmoly_o('enable-collection') && wpmoly_o('collection-autocomplete')) {
             $collections = explode(',', $movie_meta['director']);
             $collections = wp_set_object_terms($post_ID, $collections, 'collection', false);
         }
     } else {
         if (isset($_REQUEST['meta']) && '' != $_REQUEST['meta']) {
             self::save_movie_meta($post_ID, $_POST['meta']);
         }
     }
     if (isset($_REQUEST['wpmoly_details']) && !is_null($_REQUEST['wpmoly_details'])) {
         if (isset($_REQUEST['is_quickedit']) || isset($_REQUEST['is_bulkedit'])) {
             wpmoly_check_admin_referer('quickedit-movie-details');
         }
         $wpmoly_details = $_REQUEST['wpmoly_details'];
         if (true === $_REQUEST['is_bulkedit']) {
             foreach ($_REQUEST['post'] as $post_id) {
                 self::save_movie_details($post_id, $wpmoly_details);
             }
         } else {
             self::save_movie_details($post_ID, $wpmoly_details);
         }
     }
     WPMOLY_Cache::clean_transient('clean', $force = true);
     return !empty($errors->errors) ? $errors : $post_ID;
 }