/**
  * 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;
 }
 /**
  * Generate Movie Grid
  * 
  * If a current letter is passed to the query use it to narrow
  * the list of movies.
  * 
  * @since    2.0
  * 
  * @param    array       Shortcode arguments to use as parameters
  * @param    boolean     Are we actually doing a Shortcode?
  * 
  * @return   string    HTML content
  */
 public static function get_content($args = array(), $shortcode = false)
 {
     global $wpdb, $wp_query;
     $defaults = array('columns' => wpmoly_o('movie-archives-grid-columns', $default = true), 'rows' => wpmoly_o('movie-archives-grid-rows', $default = true), 'paged' => 1, 'category' => null, 'tag' => null, 'collection' => null, 'actor' => null, 'genre' => null, 'meta' => null, 'detail' => null, 'value' => null, 'title' => false, 'year' => false, 'rating' => false, 'letter' => null, 'order' => wpmoly_o('movie-archives-movies-order', $default = true), 'orderby' => 'post_title', 'view' => 'grid');
     $args = wp_parse_args($args, $defaults);
     // Allow URL params to override Shortcode settings
     $_args = WPMOLY_Archives::parse_query_vars($wp_query->query);
     $args = wp_parse_args($_args, $args);
     // debug
     $main_args = $args;
     extract($args, EXTR_SKIP);
     $total = 0;
     $grid_meta = (array) wpmoly_o('movie-archives-movies-meta', $default = true);
     $grid_meta = array_keys($grid_meta['used']);
     $title = $title || in_array('title', $grid_meta);
     $rating = $rating || in_array('rating', $grid_meta);
     $year = $year || in_array('year', $grid_meta);
     $views = array('grid', 'archives', 'list');
     if ('1' == wpmoly_o('rewrite-enable')) {
         $views = array('grid' => __('grid', 'wpmovielibrary'), 'archives' => __('archives', 'wpmovielibrary'), 'list' => __('list', 'wpmovielibrary'));
     }
     if (!isset($views[$view])) {
         $_view = array_search($view, $views);
         if (false != $_view) {
             $view = $_view;
         } else {
             $view = 'grid';
         }
     }
     $movies = array();
     $total = wp_count_posts('movie');
     $total = $total->publish;
     $select = array('SQL_CALC_FOUND_ROWS DISTINCT ID');
     // Limit the maximum number of terms to get
     $number = $columns * $rows;
     $limit = wpmoly_o('movie-archives-movies-limit', $default = true);
     if (-1 == $number) {
         $number = $limit;
     }
     $columns = min($columns, 8);
     if (0 > $columns) {
         $columns = wpmoly_o('movie-archives-grid-columns', $default = true);
     }
     $rows = min($rows, 12);
     if (0 > $rows) {
         $rows = wpmoly_o('movie-archives-grid-rows', $default = true);
     }
     // Calculate offset
     $offset = 0;
     if ($paged) {
         $offset = max(0, $number * ($paged - 1));
     }
     if ('' == $meta && '' != $detail) {
         $meta = $detail;
         $type = 'detail';
     } else {
         $type = 'meta';
     }
     // Don't use LIMIT with weird values
     $limit = "LIMIT 0,{$number}";
     if ($offset >= $number) {
         $limit = sprintf('LIMIT %d,%d', $offset, $number);
     }
     $where = array("post_type='movie'", " AND post_status='publish'");
     if ('' != $letter) {
         $where[] = " AND post_title LIKE '" . wpmoly_esc_like($letter) . "%'";
     }
     $join = array();
     $meta_query = array('join' => array(), 'where' => array());
     if ('' != $value && '' != $meta) {
         $meta_query = call_user_func("WPMOLY_Search::by_{$meta}", $value, 'sql');
         $join[] = $meta_query['join'];
         $where[] = $meta_query['where'];
     }
     $tax_query = array();
     if (!is_null($collection) && !empty($collection)) {
         $tax_query = array('taxonomy' => 'collection', 'terms' => $collection);
     } elseif (!is_null($genre) && !empty($genre)) {
         $tax_query = array('taxonomy' => 'genre', 'terms' => $genre);
     } elseif (!is_null($actor) && !empty($actor)) {
         $tax_query = array('taxonomy' => 'actor', 'terms' => $actor);
     } elseif (!is_null($category) && !empty($category)) {
         $tax_query = array('taxonomy' => 'category', 'terms' => $category);
     } elseif (!is_null($tag) && !empty($tag)) {
         $tax_query = array('taxonomy' => 'post_tag', 'terms' => $tag);
     }
     if (!empty($tax_query)) {
         $tax_query = array('relation' => 'OR', array('taxonomy' => $tax_query['taxonomy'], 'field' => 'slug', 'terms' => $tax_query['terms']), array('taxonomy' => $tax_query['taxonomy'], 'field' => 'name', 'terms' => $tax_query['terms']));
         $tax_query = get_tax_sql($tax_query, $wpdb->posts, 'ID');
         $join[] = $tax_query['join'];
         $where[] = $tax_query['where'];
     }
     $_orderby = array('year' => 'release_date', 'date' => 'release_date', 'localdate' => 'local_release_date', 'rating' => 'rating');
     if (in_array($orderby, array_keys($_orderby))) {
         $select[] = ' pm.meta_value AS value';
         $join[] = ' INNER JOIN wp_postmeta AS pm ON ( wp_posts.ID = pm.post_id )';
         $where[] = ' AND pm.meta_key = "_wpmoly_movie_' . $_orderby[$orderby] . '"';
         $orderby = 'value';
     } elseif ('post_date' == $orderby) {
         $orderby = 'post_date';
     } else {
         $orderby = 'post_title';
     }
     $where = implode('', $where);
     $join = implode('', $join);
     $select = implode(',', $select);
     $query = "SELECT {$select} FROM {$wpdb->posts} {$join} WHERE {$where} ORDER BY {$orderby} {$order} {$limit}";
     $movies = $wpdb->get_col($query);
     $total = $wpdb->get_var('SELECT FOUND_ROWS() AS total');
     $movies = array_map('get_post', $movies);
     if ('list' == $view) {
         $movies = self::prepare_list_view($movies);
     }
     $baseurl = get_permalink();
     /*if ( true === $shortcode ) {
     			$baseurl = get_permalink();
     		} else {
     			$baseurl = get_post_type_archive_link( 'movie' );
     		}*/
     $args = array('order' => $order, 'columns' => $columns, 'rows' => $rows, 'letter' => $letter, 'value' => $value, $type => $meta, 'l10n' => false, 'view' => $view, 'baseurl' => $baseurl);
     $url = WPMOLY_Utils::build_meta_permalink($args);
     // debug
     $permalinks_args = $args;
     global $wp_rewrite;
     $format = '/page/%#%';
     if ('' == $wp_rewrite->permalink_structure) {
         $format = '&paged=%#%';
     }
     $args = array('type' => 'list', 'total' => ceil($total / $number), 'current' => max(1, $paged), 'format' => $url . $format);
     $paginate = WPMOLY_Utils::paginate_links($args);
     $paginate = '<div id="wpmoly-movies-pagination">' . $paginate . '</div>';
     $theme = wp_get_theme();
     if (!is_null($theme->stylesheet)) {
         $theme = ' theme-' . $theme->stylesheet;
     } else {
         $theme = '';
     }
     // debug
     $debug = null;
     if (current_user_can('manage_options') && '1' == wpmoly_o('debug-mode')) {
         $debug = compact('main_args', 'permalinks_args');
     }
     $attributes = compact('movies', 'columns', 'title', 'year', 'rating', 'theme', 'debug');
     $content = self::render_template("movies/grid/{$view}-loop.php", $attributes, $require = 'always');
     $content = $content . $paginate;
     return $content;
 }
 /**
  * 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');
 }