/**
  * Output the Widget content.
  * 
  * @since    1.2
  *
  * @param    array    $args The array of form elements
  * @param    array    $instance The current instance of the widget
  * 
  * @return   void
  */
 public function widget($args, $instance)
 {
     // Caching
     $name = apply_filters('wpmoly_cache_name', 'taxonomies_widget', $args);
     // Naughty PHP 5.3 fix
     $widget =& $this;
     $content = WPMOLY_Cache::output($name, function () use($widget, $args, $instance) {
         return $widget->widget_content($args, $instance);
     });
     echo $content;
 }
 /**
  * Output the Widget content.
  *
  * @param	array	args		The array of form elements
  * @param	array	instance	The current instance of the widget
  */
 public function widget($args, $instance)
 {
     // Caching
     $name = apply_filters('wpmoly_cache_name', 'movies_widget', $args);
     // Naughty PHP 5.3 fix
     $widget =& $this;
     // Skip caching if random
     if (isset($args['orderby']) && 'random' == $args['orderby']) {
         return $widget->widget_content($args, $instance);
     }
     $content = WPMOLY_Cache::output($name, function () use($widget, $args, $instance) {
         return $widget->widget_content($args, $instance);
     });
     echo $content;
 }
 /**
  * Set the uninstallation instructions
  *
  * @since    1.0
  */
 public static function uninstall()
 {
     WPMOLY_Cache::clean_transient('uninstall');
     delete_option('rewrite_rules');
 }
 /**
  * Get configuration from TMDb
  * 
  * @since    1.0
  *
  * @return   array    TMDb result
  */
 public function getConfiguration()
 {
     $config = WPMOLY_Cache::get('tmdb_api_config');
     if (!$config) {
         $config = $this->_makeCall('configuration');
         if (is_wp_error($config)) {
             if (defined('DOING_AJAX') && DOING_AJAX) {
                 return $config;
             }
             WPMOLY_Utils::admin_notice($config->get_error_message(), 'error');
             return array();
         }
         if (!empty($config)) {
             $this->_config = $config;
         }
         WPMOLY_Cache::set('tmdb_api_config', $config);
     }
     return $config;
 }
 /**
  * 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;
 }
 /**
  * Render Custom Taxonomies Archives pages.
  * 
  * This method is a bit complex because it can handle a couple of
  * things. If a letter param is set, will get the list of terms
  * starting with that letter, plus sorting/pagination options.
  * 
  * If no letter is set, simply render a paginated list of all
  * taxonomy' terms.
  * 
  * @since    2.1
  * 
  * @param    string    $taxonomy Taxonomy slug
  * 
  * @return   string    HTML markup
  */
 public static function taxonomy_archives($taxonomy)
 {
     global $wpdb;
     $term_title = '';
     if ('collection' == $taxonomy) {
         $term_title = __('View all movies from collection « %s »', 'wpmovielibrary');
     } else {
         if ('genre' == $taxonomy) {
             $term_title = __('View all « %s » movies', 'wpmovielibrary');
         } else {
             if ('actor' == $taxonomy) {
                 $term_title = __('View all movies staring « %s »', 'wpmovielibrary');
             }
         }
     }
     global $wp_query;
     $params = self::parse_terms_query_vars($wp_query->query);
     // Allow URL params to override settings
     $vars = array('number', 'orderby', 'letter');
     foreach ($vars as $var) {
         $params[$var] = get_query_var($var, $params[$var]);
     }
     $name = WPMOLY_Cache::wpmoly_cache_name("{$taxonomy}_archive");
     $content = WPMOLY_Cache::output($name, function () use($wpdb, $taxonomy, $term_title, $params) {
         $has_menu = wpmoly_o('tax-archives-menu', $default = true);
         $hide_empty = wpmoly_o('tax-archives-hide-empty', $default = true);
         extract($params);
         $_orderby = 't.name';
         if ('count' == $orderby) {
             $_orderby = 'tt.count';
         }
         // Limit the maximum number of terms to get
         $number = min($number, wpmoly_o('tax-archives-terms-limit', $default = true));
         if (!$number) {
             $number = wpmoly_o('tax-archives-terms-per-page', $default = true);
         }
         // Calculate offset
         $offset = 0;
         if ($paged) {
             $offset = $number * ($paged - 1);
         }
         $limit = sprintf('LIMIT %d,%d', $offset, $number);
         $where = '';
         if ('0' != $hide_empty) {
             $where = 'tt.count > 0 AND';
         }
         // This is actually a hard rewriting of get_terms()
         // to get exactly what we want without getting into
         // trouble with multiple filters and stuff.
         if ('' != $letter) {
             $like = wpmoly_esc_like($letter) . '%';
             $query = "SELECT SQL_CALC_FOUND_ROWS t.*, tt.*\n\t\t\t\t\t\t    FROM {$wpdb->terms} AS t\n\t\t\t\t\t\t   INNER JOIN {$wpdb->term_taxonomy} AS tt\n\t\t\t\t\t\t      ON t.term_id = tt.term_id\n\t\t\t\t\t\t   WHERE {$where} tt.taxonomy = %s\n\t\t\t\t\t\t     AND t.name LIKE %s\n\t\t\t\t\t\t   ORDER BY {$_orderby} {$order}\n\t\t\t\t\t\t   {$limit}";
             $query = $wpdb->prepare($query, $taxonomy, $like);
             $terms = $wpdb->get_results($query);
         } else {
             $query = "SELECT SQL_CALC_FOUND_ROWS t.*, tt.*\n\t\t\t\t\t\t    FROM {$wpdb->terms} AS t\n\t\t\t\t\t\t   INNER JOIN {$wpdb->term_taxonomy} AS tt\n\t\t\t\t\t\t      ON t.term_id = tt.term_id\n\t\t\t\t\t\t   WHERE {$where} tt.taxonomy = %s\n\t\t\t\t\t\t   ORDER BY {$_orderby} {$order}\n\t\t\t\t\t\t   {$limit}";
             $query = $wpdb->prepare($query, $taxonomy);
             $terms = $wpdb->get_results($query);
         }
         $total = $wpdb->get_var('SELECT FOUND_ROWS() AS total');
         $terms = apply_filters('get_terms', $terms, (array) $taxonomy, array());
         $links = array();
         // Setting up the terms list...
         if (is_wp_error($terms)) {
             $links = $terms;
         } else {
             foreach ($terms as $term) {
                 $links[] = array('url' => get_term_link($term), 'attr_title' => sprintf($term_title, $term->name), 'title' => $term->name, 'count' => sprintf(_n('%d movie', '%d movies', $term->count, 'wpmovielibrary'), $term->count));
             }
         }
         // ... the main menu...
         $menu = '';
         if ($has_menu) {
             $args = compact('order', 'orderby', 'number', 'letter');
             // PHP 5.3
             $menu = WPMOLY_Archives::taxonomy_archive_menu($taxonomy, $args);
         }
         $args['letter'] = $letter;
         $args['baseurl'] = get_permalink();
         $url = WPMOLY_Utils::build_meta_permalink($args);
         global $wp_rewrite;
         $format = '/page/%#%';
         if ('' == $wp_rewrite->permalink_structure) {
             $format = '&paged=%#%';
         }
         // ... and the pagination menu.
         $args = array('type' => 'list', 'total' => ceil(($total - 1) / $number), 'current' => max(1, $paged), 'format' => $url . $format);
         $pagination = WPMOLY_Utils::paginate_links($args);
         $pagination = '<div id="wpmoly-movies-pagination">' . $pagination . '</div>';
         $attributes = array('taxonomy' => $taxonomy, 'links' => $links);
         $content = WPMovieLibrary::render_template('archives/archives.php', $attributes, $require = 'always');
         $content = $menu . $content . $pagination;
         return $content;
     });
     return $content;
 }
 /**
  * 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;
 }
 /**
  * Initialize the plugin by setting localization and loading public scripts
  * and styles.
  *
  * @since    1.0
  */
 protected function __construct()
 {
     $this->register_hook_callbacks();
     $this->modules = array('WPMOLY_Settings' => WPMOLY_Settings::get_instance(), 'WPMOLY_Cache' => WPMOLY_Cache::get_instance(), 'WPMOLY_L10n' => WPMOLY_L10n::get_instance(), 'WPMOLY_Utils' => WPMOLY_Utils::get_instance(), 'WPMOLY_Movies' => WPMOLY_Movies::get_instance(), 'WPMOLY_Headbox' => WPMOLY_Headbox::get_instance(), 'WPMOLY_Search' => WPMOLY_Search::get_instance(), 'WPMOLY_Collections' => WPMOLY_Collections::get_instance(), 'WPMOLY_Genres' => WPMOLY_Genres::get_instance(), 'WPMOLY_Actors' => WPMOLY_Actors::get_instance(), 'WPMOLY_Archives' => WPMOLY_Archives::get_instance(), 'WPMOLY_Shortcodes' => WPMOLY_Shortcodes::get_instance(), 'WPMOLY_Legacy' => WPMOLY_Legacy::get_instance());
     $this->widgets = array('WPMOLY_Statistics_Widget', 'WPMOLY_Taxonomies_Widget', 'WPMOLY_Details_Widget', 'WPMOLY_Movies_Widget');
 }
 /**
  * Movie Detail shortcode. This shortcode supports aliases.
  *
  * @since    1.1
  * 
  * @param    array     Shortcode attributes
  * @param    string    Shortcode content
  * @param    string    Shortcode tag name
  * 
  * @return   string    Shortcode display
  */
 public static function movie_detail_shortcode($atts = array(), $content = null, $tag = null)
 {
     // Is this an alias?
     if (!is_null($tag) && "{$tag}_shortcode" != __FUNCTION__) {
         $atts['key'] = str_replace('movie_', '', $tag);
     }
     $atts = self::filter_shortcode_atts('movie_detail', $atts);
     $movie_id = WPMOLY_Shortcodes::find_movie_id($atts['id'], $atts['title']);
     if (is_null($movie_id)) {
         return $content;
     }
     $atts['movie_id'] = $movie_id;
     // Caching
     $name = apply_filters('wpmoly_cache_name', 'movie_detail_shortcode', $atts);
     $content = WPMOLY_Cache::output($name, function () use($atts, $content) {
         extract($atts);
         if (!function_exists("wpmoly_get_movie_{$key}")) {
             return $content;
         }
         $content = call_user_func("wpmoly_get_movie_{$key}", $movie_id);
         $format = !$raw ? 'html' : 'raw';
         $content = apply_filters("wpmoly_format_movie_{$key}", $content, $format);
         return $content;
     }, $echo = false);
     return $content;
 }