Example #1
0
/**
 * Retrieve list of allowed mime types and file extensions.
 *
 * @since 0.0.1
 *
 * @param int|HQ_User $user Optional. User to check. Defaults to current user.
 * @return array Array of mime types keyed by the file extension regex corresponding
 *               to those types.
 */
function get_allowed_mime_types($user = null)
{
    $t = hq_get_mime_types();
    unset($t['swf'], $t['exe']);
    if (function_exists('current_user_can')) {
        $unfiltered = $user ? user_can($user, 'unfiltered_html') : current_user_can('unfiltered_html');
    }
    if (empty($unfiltered)) {
        unset($t['htm|html']);
    }
    /**
     * Filter list of allowed mime types and file extensions.
     *
     * @since 0.0.1
     *
     * @param array            $t    Mime types keyed by the file extension regex corresponding to
     *                               those types. 'swf' and 'exe' removed from full list. 'htm|html' also
     *                               removed depending on '$user' capabilities.
     * @param int|HQ_User|null $user User ID, User object or null if not provided (indicates current user).
     */
    return apply_filters('upload_mimes', $t, $user);
}
Example #2
0
/**
 * Builds the Video shortcode output.
 *
 * This implements the functionality of the Video Shortcode for displaying
 * HiveQueen mp4s in a post.
 *
 * @since 0.0.1
 *
 * @global int $content_width
 * @staticvar int $instance
 *
 * @param array  $attr {
 *     Attributes of the shortcode.
 *
 *     @type string $src      URL to the source of the video file. Default empty.
 *     @type int    $height   Height of the video embed in pixels. Default 360.
 *     @type int    $width    Width of the video embed in pixels. Default $content_width or 640.
 *     @type string $poster   The 'poster' attribute for the `<video>` element. Default empty.
 *     @type string $loop     The 'loop' attribute for the `<video>` element. Default empty.
 *     @type string $autoplay The 'autoplay' attribute for the `<video>` element. Default empty.
 *     @type string $preload  The 'preload' attribute for the `<video>` element.
 *                            Default 'metadata'.
 *     @type string $class    The 'class' attribute for the `<video>` element.
 *                            Default 'hq-video-shortcode'.
 * }
 * @param string $content Shortcode content.
 * @return string|void HTML content to display video.
 */
function hq_video_shortcode($attr, $content = '')
{
    global $content_width;
    $post_id = get_post() ? get_the_ID() : 0;
    static $instance = 0;
    $instance++;
    /**
     * Filter the default video shortcode output.
     *
     * If the filtered output isn't empty, it will be used instead of generating
     * the default video template.
     *
     * @since 0.0.1
     *
     * @see hq_video_shortcode()
     *
     * @param string $html     Empty variable to be replaced with shortcode markup.
     * @param array  $attr     Attributes of the video shortcode.
     * @param string $content  Video shortcode content.
     * @param int    $instance Unique numeric ID of this video shortcode instance.
     */
    $override = apply_filters('hq_video_shortcode_override', '', $attr, $content, $instance);
    if ('' !== $override) {
        return $override;
    }
    $video = null;
    $default_types = hq_get_video_extensions();
    $defaults_atts = array('src' => '', 'poster' => '', 'loop' => '', 'autoplay' => '', 'preload' => 'metadata', 'width' => 640, 'height' => 360);
    foreach ($default_types as $type) {
        $defaults_atts[$type] = '';
    }
    $atts = shortcode_atts($defaults_atts, $attr, 'video');
    if (is_admin()) {
        // shrink the video so it isn't huge in the admin
        if ($atts['width'] > $defaults_atts['width']) {
            $atts['height'] = round($atts['height'] * $defaults_atts['width'] / $atts['width']);
            $atts['width'] = $defaults_atts['width'];
        }
    } else {
        // if the video is bigger than the theme
        if (!empty($content_width) && $atts['width'] > $content_width) {
            $atts['height'] = round($atts['height'] * $content_width / $atts['width']);
            $atts['width'] = $content_width;
        }
    }
    $is_vimeo = $is_youtube = false;
    $yt_pattern = '#^https?://(?:www\\.)?(?:youtube\\.com/watch|youtu\\.be/)#';
    $vimeo_pattern = '#^https?://(.+\\.)?vimeo\\.com/.*#';
    $primary = false;
    if (!empty($atts['src'])) {
        $is_vimeo = preg_match($vimeo_pattern, $atts['src']);
        $is_youtube = preg_match($yt_pattern, $atts['src']);
        if (!$is_youtube && !$is_vimeo) {
            $type = hq_check_filetype($atts['src'], hq_get_mime_types());
            if (!in_array(strtolower($type['ext']), $default_types)) {
                return sprintf('<a class="hq-embedded-video" href="%s">%s</a>', esc_url($atts['src']), esc_html($atts['src']));
            }
        }
        if ($is_vimeo) {
            hq_enqueue_script('froogaloop');
        }
        $primary = true;
        array_unshift($default_types, 'src');
    } else {
        foreach ($default_types as $ext) {
            if (!empty($atts[$ext])) {
                $type = hq_check_filetype($atts[$ext], hq_get_mime_types());
                if (strtolower($type['ext']) === $ext) {
                    $primary = true;
                }
            }
        }
    }
    if (!$primary) {
        $videos = get_attached_media('video', $post_id);
        if (empty($videos)) {
            return;
        }
        $video = reset($videos);
        $atts['src'] = hq_get_attachment_url($video->ID);
        if (empty($atts['src'])) {
            return;
        }
        array_unshift($default_types, 'src');
    }
    /**
     * Filter the media library used for the video shortcode.
     *
     * @since 0.0.1
     *
     * @param string $library Media library used for the video shortcode.
     */
    $library = apply_filters('hq_video_shortcode_library', 'mediaelement');
    if ('mediaelement' === $library && did_action('init')) {
        hq_enqueue_style('hq-mediaelement');
        hq_enqueue_script('hq-mediaelement');
    }
    /**
     * Filter the class attribute for the video shortcode output container.
     *
     * @since 0.0.1
     *
     * @param string $class CSS class or list of space-separated classes.
     */
    $html_atts = array('class' => apply_filters('hq_video_shortcode_class', 'hq-video-shortcode'), 'id' => sprintf('video-%d-%d', $post_id, $instance), 'width' => absint($atts['width']), 'height' => absint($atts['height']), 'poster' => esc_url($atts['poster']), 'loop' => hq_validate_boolean($atts['loop']), 'autoplay' => hq_validate_boolean($atts['autoplay']), 'preload' => $atts['preload']);
    // These ones should just be omitted altogether if they are blank
    foreach (array('poster', 'loop', 'autoplay', 'preload') as $a) {
        if (empty($html_atts[$a])) {
            unset($html_atts[$a]);
        }
    }
    $attr_strings = array();
    foreach ($html_atts as $k => $v) {
        $attr_strings[] = $k . '="' . esc_attr($v) . '"';
    }
    $html = '';
    if ('mediaelement' === $library && 1 === $instance) {
        $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
    }
    $html .= sprintf('<video %s controls="controls">', join(' ', $attr_strings));
    $fileurl = '';
    $source = '<source type="%s" src="%s" />';
    foreach ($default_types as $fallback) {
        if (!empty($atts[$fallback])) {
            if (empty($fileurl)) {
                $fileurl = $atts[$fallback];
            }
            if ('src' === $fallback && $is_youtube) {
                $type = array('type' => 'video/youtube');
            } elseif ('src' === $fallback && $is_vimeo) {
                $type = array('type' => 'video/vimeo');
            } else {
                $type = hq_check_filetype($atts[$fallback], hq_get_mime_types());
            }
            $url = add_query_arg('_', $instance, $atts[$fallback]);
            $html .= sprintf($source, $type['type'], esc_url($url));
        }
    }
    if (!empty($content)) {
        if (false !== strpos($content, "\n")) {
            $content = str_replace(array("\r\n", "\n", "\t"), '', $content);
        }
        $html .= trim($content);
    }
    if ('mediaelement' === $library) {
        $html .= hq_mediaelement_fallback($fileurl);
    }
    $html .= '</video>';
    $width_rule = '';
    if (!empty($atts['width'])) {
        $width_rule = sprintf('width: %dpx; ', $atts['width']);
    }
    $output = sprintf('<div style="%s" class="hq-video">%s</div>', $width_rule, $html);
    /**
     * Filter the output of the video shortcode.
     *
     * @since 0.0.1
     *
     * @param string $output  Video shortcode HTML output.
     * @param array  $atts    Array of video shortcode attributes.
     * @param string $video   Video file.
     * @param int    $post_id Post ID.
     * @param string $library Media library used for the video shortcode.
     */
    return apply_filters('hq_video_shortcode', $output, $atts, $video, $post_id, $library);
}