/**
  * 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;
 }
 /**
  * Set the uninstallation instructions
  *
  * @since    1.0
  */
 public static function uninstall()
 {
     WPMOLY_Cache::clean_transient('uninstall');
     delete_option('rewrite_rules');
 }
 /**
  * Process the submitted movie list
  * 
  * This method can be used through an AJAX callback; in this case
  * the nonce check is already done in callback so we only check
  * for nonce we're not doing AJAX. List is exploded by comma and
  * fed to import_movie() to create import drafts.
  * 
  * If AJAX, the function echo a status message and simply dies.
  * If no AJAX, ir returns false on failure, and a status message
  * on success.
  *
  * @since    1.0
  * 
  * @param    array    $movies Array of movie titles to import
  * 
  * @return   mixed
  */
 private static function import_movies($movies)
 {
     $errors = new WP_Error();
     $response = array();
     $movies = explode(',', $movies);
     $movies = array_map(__CLASS__ . '::prepare_movie_import', $movies);
     if (is_null($movies) || !is_array($movies)) {
         $errors->add('invalid', __('Invalid movie list submitted.', 'wpmovielibrary'));
         return $errors;
     }
     $response = wpmoly_ajax_filter(array(__CLASS__, 'import_movie'), array($movies), $loop = true);
     WPMOLY_Cache::clean_transient('clean', $force = true);
     return $response;
 }