/** * Specifies the classname and description, instantiates the widget. No * stylesheets or JavaScript needed, localization loaded in public class. */ public function __construct() { $this->widget_name = __('WPMovieLibrary Details', 'wpmovielibrary'); $this->widget_description = __('Display a list of the available details: status, media and rating.', 'wpmovielibrary'); $this->widget_css = 'wpmoly details'; $this->widget_id = 'wpmovielibrary-details-widget'; $this->widget_form = 'details-widget/details-admin.php'; $this->widget_params = array('title' => array('type' => 'text', 'std' => __('Movie Details', 'wpmovielibrary')), 'description' => array('type' => 'text', 'std' => ''), 'detail' => array('type' => 'select', 'std' => ''), 'list' => array('type' => 'checkbox', 'std' => 0), 'css' => array('type' => 'checkbox', 'std' => 0)); $this->details = array(); $supported = WPMOLY_Settings::get_supported_movie_details(); foreach ($supported as $slug => $detail) { $this->details[$slug] = array('default' => sprintf(__('Select a %s', 'wpmovielibrary'), $slug), 'empty' => sprintf(__('No %s to display.', 'wpmovielibrary'), $slug)); } parent::__construct(); }
<label for="<?php echo $widget->get_field_id('detail'); ?> "><strong class="wpmoly-widget-title"><?php _e('Detail', 'wpmovielibrary'); ?> </strong></label><br /> <select id="<?php echo $widget->get_field_id('detail'); ?> " name="<?php echo $widget->get_field_name('detail'); ?> "> <?php $supported = WPMOLY_Settings::get_supported_movie_details(); foreach ($supported as $slug => $s) { ?> <option value="<?php echo $slug; ?> " <?php selected($slug, $detail); ?> ><?php _e($s['title'], 'wpmovielibrary'); ?> </option> <?php }
/** * Generate rating stars block. * * @since 2.0 * * @param float $rating movie to turn into stars * @param int $post_id movie's post ID * @param int $base 5-stars or 10-stars? * * @return string Formatted output */ public static function get_movie_rating_stars($rating, $post_id = null, $base = null, $include_empty = false) { $defaults = WPMOLY_Settings::get_supported_movie_details(); if (is_null($post_id) || !intval($post_id)) { $post_id = get_the_ID(); } if (is_null($base)) { $base = wpmoly_o('format-rating'); } if (10 != $base) { $base = 5; } if (0 > $rating) { $rating = 0.0; } if (5.0 < $rating) { $rating = 5.0; } $title = ''; if (isset($defaults['rating']['options'][$rating])) { $title = $defaults['rating']['options'][$rating]; } $_rating = preg_replace('/([0-5])(\\.|_)(0|5)/i', '$1-$3', $rating); $class = "wpmoly-movie-rating wpmoly-movie-rating-{$_rating}"; $filled = '<span class="wpmolicon icon-star-filled"></span>'; $half = '<span class="wpmolicon icon-star-half"></span>'; $empty = '<span class="wpmolicon icon-star-empty"></span>'; $_filled = floor($rating); $_half = ceil($rating - floor($rating)); $_empty = ceil(5.0 - ($_filled + $_half)); if (0.0 == $rating) { if (true === $include_empty) { $stars = '<div id="wpmoly-movie-rating-' . $post_id . '" class="' . $class . '" title="' . $title . '">'; $stars .= str_repeat($empty, 10); $stars .= '</div>'; } else { $stars = '<div id="wpmoly-movie-rating-' . $post_id . '" class="not-rated" title="' . $title . '">'; $stars .= sprintf('<small><em>%s</em></small>', __('Not rated yet!', 'wpmovielibrary')); $stars .= '</div>'; } } else { if (10 == $base) { $_filled = $rating * 2; $_empty = 10 - $_filled; $title = "{$_filled}/10 − {$title}"; $stars = '<div id="wpmoly-movie-rating-' . $post_id . '" class="' . $class . '" title="' . $title . '">'; $stars .= str_repeat($filled, $_filled); $stars .= str_repeat($empty, $_empty); $stars .= '</div>'; } else { $title = "{$rating}/5 − {$title}"; $stars = '<div id="wpmoly-movie-rating-' . $post_id . '" class="' . $class . '" title="' . $title . '">'; $stars .= str_repeat($filled, $_filled); $stars .= str_repeat($half, $_half); $stars .= str_repeat($empty, $_empty); $stars .= '</div>'; } } /** * Filter generated HTML markup. * * @since 2.0 * * @param string Stars HTML markup * @param float Rating value */ $stars = apply_filters('wpmoly_movie_rating_stars_html', $stars, $rating); return $stars; }
/** * Filter the Movie Details submitted when saving a post to * avoid storing unexpected data to the database. * * @since 2.1 * * @param array $data The Movie Details to filter * * @return array The filtered Details */ private static function validate_details($data) { if (!is_array($data) || empty($data)) { return $data; } $data = wpmoly_filter_empty_array($data); $supported = WPMOLY_Settings::get_supported_movie_details(); $movie_details = array(); foreach ($supported as $slug => $detail) { if (isset($data[$slug])) { $_detail = $data[$slug]; if (is_array($_detail) && 1 == $detail['multi']) { $_d = array(); foreach ($_detail as $d) { if (in_array($d, array_keys($detail['options']))) { $_d[] = $d; } } $movie_details[$slug] = $_d; } else { if ('select' == $detail['type'] && in_array($_detail, array_keys($detail['options']))) { $movie_details[$slug] = $_detail; } else { if ('text' == $detail['type']) { $movie_details[$slug] = $_detail; } } } } else { $movie_details[$slug] = null; } } return $movie_details; }
/** * Render Allocine styled Headbox 'Details' Tab. * * @since 2.1.4 * * @return string Headbox Tab HTML content */ private function render_details_tab() { $id = get_the_ID(); $overview = self::get_movie_meta($id, 'overview'); $tagline = self::get_movie_meta($id, 'tagline'); $details = wpmoly_get_movie_details(); $default_fields = WPMOLY_Settings::get_supported_movie_details(); foreach ($details as $slug => $detail) { if (!is_array($detail)) { $detail = array($detail); } if (isset($default_fields[$slug]['panel']) && 'custom' == $default_fields[$slug]['panel']) { unset($details[$slug]); } else { foreach ($detail as $i => $d) { if (!empty($d)) { if (isset($default_fields[$slug]['options'])) { $value = $default_fields[$slug]['options'][$d]; } else { $value = $d; } if ('rating' == $slug) { $d = apply_filters("wpmoly_movie_meta_link", array('key' => 'rating', 'value' => array_search($value, $default_fields[$slug]['options']), 'type' => 'detail', 'text' => $value)); } else { $d = apply_filters("wpmoly_movie_meta_link", array('key' => $slug, 'value' => $value, 'meta' => 'detail', 'text' => $value)); } $detail[$i] = apply_filters("wpmoly_format_movie_field", $d); } } $detail = implode(', ', $detail); if (empty($detail)) { $detail = apply_filters("wpmoly_format_movie_field", ''); } $title = ''; if (isset($default_fields[$slug])) { $title = __($default_fields[$slug]['title'], 'wpmovielibrary'); } $details[$slug] = array('slug' => $slug, 'title' => $title, 'value' => $detail); } } $metas = wpmoly_get_movie_meta(); $metas = wpmoly_filter_undimension_array($metas); $default_fields = WPMOLY_Settings::get_supported_movie_meta(); if (!empty($metas)) { unset($metas['title'], $metas['cast'], $metas['overview'], $metas['tagline']); foreach ($metas as $slug => $field) { if (isset($default_fields[$slug])) { // Custom filter if available if (has_filter("wpmoly_format_movie_{$slug}")) { $field = apply_filters("wpmoly_format_movie_{$slug}", $field); } // Filter empty field $field = apply_filters("wpmoly_format_movie_field", $field); $metas[$slug] = array('slug' => $slug, 'title' => __($default_fields[$slug]['title'], 'wpmovielibrary'), 'value' => $field); } else { unset($metas[$slug]); } } } $attributes = compact('id', 'overview', 'tagline', 'details', 'metas'); $content = self::render_template('movies/headbox-allocine/tabs/details.php', $attributes, $require = 'always'); return $content; }
/** * Retrieve a list of Movies based on media * * @since 2.1 * * @param array $args Arguments to retrieve movies * * @return array Array of Post objects */ public static function get_movies($args = null) { $defaults = array('number' => 5, 'offset' => 0, 'collection' => '', 'genre' => null, 'actor' => null, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'media' => null, 'status' => null, 'rating' => null, 'language' => null, 'subtitles' => null, 'format' => null, 'meta' => null, 'meta_value' => null, 'post_status' => 'publish'); $r = wp_parse_args($args, $defaults); $meta_query = array(); if (!empty($r['numberposts']) && empty($r['posts_per_page'])) { $r['posts_per_page'] = $r['numberposts']; } if (!empty($r['include'])) { $r['post__in'] = wp_parse_id_list($r['include']); } if (!empty($r['exclude'])) { $r['post__not_in'] = wp_parse_id_list($r['exclude']); } if (!is_null($r['meta_value'])) { $meta = array_keys(WPMOLY_Settings::get_supported_movie_meta()); $details = array_keys(WPMOLY_Settings::get_supported_movie_details()); foreach ($details as $detail) { if (!is_null($r[$detail])) { $meta_query[] = array('key' => "_wpmoly_movie_{$detail}", 'value' => $r['meta_value'], 'compare' => 'LIKE'); } } if (!is_null($r['meta']) && in_array($r['meta'], $meta)) { $meta_query[] = array('key' => "_wpmoly_movie_{$r['meta']}", 'value' => $r['meta_value'], 'compare' => 'LIKE'); } } $r['posts_per_page'] = $r['number']; $r['post_type'] = 'movie'; $r['meta_query'] = $meta_query; unset($r['media'], $r['status'], $r['rating'], $r['language'], $r['subtitle'], $r['format'], $r['meta'], $r['meta_value'], $r['number']); $_query = new WP_Query(); $movies = $_query->query($r); return $movies; }
/** * Modern headbox details tab content callback. * * @since 2.0 * * @return string Tab content HTML markup */ public function get_wpmoly_headbox_details_tab() { // TODO: better filtering/formatting $details = wpmoly_get_movie_details(); $fields = wpmoly_o('sort-details'); $default_fields = WPMOLY_Settings::get_supported_movie_details(); if (empty($fields) || !isset($fields['used'])) { return null; } $fields = $fields['used']; if (isset($fields['placebo'])) { unset($fields['placebo']); } $post_id = get_the_ID(); $items = array(); foreach ($fields as $slug => $field) { if (isset($details[$slug])) { $detail = $details[$slug]; if (!is_array($detail)) { $detail = array($detail); } foreach ($detail as $i => $d) { if ('' != $d) { if (isset($default_fields[$slug]['options'])) { $value = $default_fields[$slug]['options'][$d]; } else { $value = $d; } if ('rating' == $slug) { $d = apply_filters("wpmoly_movie_meta_link", array('key' => 'rating', 'value' => array_search($value, $default_fields[$slug]['options']), 'type' => 'detail', 'text' => $value)); } else { $d = apply_filters("wpmoly_movie_meta_link", array('key' => $slug, 'value' => $value, 'meta' => 'detail', 'text' => $value)); } } $detail[$i] = apply_filters("wpmoly_format_movie_field", $d); } if (empty($detail)) { $detail[] = apply_filters("wpmoly_format_movie_field", ''); } $title = ''; if (isset($default_fields[$slug])) { $title = __($default_fields[$slug]['title'], 'wpmovielibrary'); } $items[] = array('slug' => $slug, 'title' => $title, 'value' => $detail); } } $attributes = array('details' => $items); $content = WPMovieLibrary::render_template('movies/headbox/tabs/details.php', $attributes, $require = 'always'); return $content; }
/** * Generate a list of possible translated rewrite rules * * Rewrite rules are more limited than rewrites as we only need * to adapt structures. * * @since 2.0 * * @return array Translated rewrite rules */ public static function set_l10n_rewrite_rules() { $l10n_rules = array(); $translate = wpmoly_o('rewrite-enable'); $movies = wpmoly_o('rewrite-movie'); $collection = wpmoly_o('rewrite-collection'); $genre = wpmoly_o('rewrite-genre'); $actor = wpmoly_o('rewrite-actor'); $l10n_rules['movies'] = $translate && '' != $movies ? $movies : 'movies'; $l10n_rules['collection'] = $translate && '' != $collection ? $collection : 'collection'; $l10n_rules['genre'] = $translate && '' != $genre ? $genre : 'genre'; $l10n_rules['actor'] = $translate && '' != $actor ? $actor : 'actor'; $l10n_rules['list'] = $translate ? __('list', 'wpmovielibrary') : 'list'; $l10n_rules['grid'] = $translate ? __('grid', 'wpmovielibrary') : 'grid'; $l10n_rules['archives'] = $translate ? __('archives', 'wpmovielibrary') : 'archives'; $details = WPMOLY_Settings::get_supported_movie_details(); $meta = WPMOLY_Settings::get_supported_movie_meta(); foreach ($details as $slug => $detail) { if ($translate) { $l10n_rules['detail'][$slug] = array_pop($detail['rewrite']); } else { $l10n_rules['detail'][$slug] = key($detail['rewrite']); } } foreach ($meta as $slug => $m) { if (!is_null($m['rewrite'])) { if ($translate) { $l10n_rules['meta'][$slug] = array_pop($m['rewrite']); } else { $l10n_rules['meta'][$slug] = key($m['rewrite']); } } } /** * Filter the rewrite rules list * * @since 2.0 * * @param array $l10n_rules Existing rewrite rules */ $l10n_rules = apply_filters('wpmoly_filter_l10n_rewrite_rules', $l10n_rules); self::delete_l10n_rewrite_rules(); add_option('wpmoly_l10n_rewrite_rules', $l10n_rules); return $l10n_rules; }