diff --git a/modules/videopress.php b/modules/videopress.php index f1fc110ffce3d..cdbab44e9f53b 100644 --- a/modules/videopress.php +++ b/modules/videopress.php @@ -14,6 +14,11 @@ '//v0.wordpress.com', ) ); +/** + * We won't have any videos less than sixty pixels wide. That would be silly. + */ +define( 'VIDEOPRESS_MIN_WIDTH', 60 ); + include_once dirname( __FILE__ ) . '/videopress/utility-functions.php'; include_once dirname( __FILE__ ) . '/videopress/shortcode.php'; include_once dirname( __FILE__ ) . '/videopress/videopress.php'; diff --git a/modules/videopress/shortcode.php b/modules/videopress/shortcode.php index c0fb87e864141..f962571c0b957 100644 --- a/modules/videopress/shortcode.php +++ b/modules/videopress/shortcode.php @@ -1,151 +1,139 @@ 0, // Width of the video player, in pixels + 'at' => 0, // How many seconds in to initially seek to + 'hd' => false, // Whether to display a high definition version + 'loop' => false, // Whether to loop the video repeatedly + 'freedom' => false, // Whether to use only free/libre codecs + 'autoplay' => false, // Whether to autoplay the video on load + 'permalink' => true, // Whether to display the permalink to the video + 'flashonly' => false, // Whether to support the Flash player exclusively + 'defaultlangcode' => false, // Default language code + ); + + $attr = shortcode_atts( $defaults, $attr, 'videopress' ); /** - * Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display. - * - * @link http://codex.wordpress.org/Shortcode_API Shortcode API - * @param array $attr shortcode attributes - * @return string HTML markup or blank string on fail + * Cast the attributes, post-input. */ - public function shortcode_callback( $attr, $content = '' ) { - global $content_width; - - $guid = $attr[0]; - if ( ! $this->is_valid_guid( $guid ) ) - return ''; - - $attr = shortcode_atts( array( - 'w' => 0, - 'h' => 0, - 'freedom' => false, - 'flashonly' => false, - 'autoplay' => false, - 'hd' => false, - 'permalink' => true, - 'loop' => false, - 'at' => 0, - 'defaultlangcode' => false, - ), $attr ); - - $attr['forcestatic'] = false; - - $attr['freedom'] = (bool) $attr['freedom']; - $attr['hd'] = (bool) $attr['hd']; - $attr['width'] = absint( $attr['w'] ); - - if ( $attr['width'] < $this->min_width ) - $attr['width'] = 0; - elseif ( isset( $content_width ) && $content_width > $this->min_width && $attr['width'] > $content_width ) - $attr['width'] = 0; - - if ( $attr['width'] === 0 && isset( $content_width ) && $content_width > $this->min_width ) - $attr['width'] = $content_width; - - if ( ( $attr['width'] % 2 ) === 1 ) - $attr['width']--; - - /** - * Filter the default VideoPress shortcode options. - * - * @module videopress - * - * @since 2.5.0 - * - * @param array $args Array of VideoPress shortcode options. - */ - $options = apply_filters( 'videopress_shortcode_options', array( - 'freedom' => $attr['freedom'], - 'force_flash' => (bool) $attr['flashonly'], - 'autoplay' => $attr['autoplay'], - 'forcestatic' => $attr['forcestatic'], - 'hd' => $attr['hd'], - 'permalink' => $attr['permalink'], - 'loop' => $attr['autoplay'], - 'at' => (int) $attr['at'], - 'defaultlangcode' => $attr['defaultlangcode'] - ) ); - - // Enqueue VideoPress scripts - self::register_scripts(); - - require_once( dirname( __FILE__ ) . '/class.videopress-video.php' ); - require_once( dirname( __FILE__ ) . '/class.videopress-player.php' ); - - $player = new VideoPress_Player( $guid, $attr['width'], $options ); - - if ( is_feed() ) - return $player->asXML(); - else - return $player->asHTML(); + $attr['width'] = absint( $attr['w'] ); + $attr['hd'] = (bool) $attr['hd']; + $attr['freedom'] = (bool) $attr['freedom']; + + /** + * If the provided width is less than the minimum allowed + * width, or greater than `$content_width` ignore. + */ + if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) { + $attr['width'] = 0; + } elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) { + $attr['width'] = 0; } /** - * Validate user-supplied guid values against expected inputs - * - * @since 1.1 - * @param string $guid video identifier - * @return bool true if passes validation test + * If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`. */ - public function is_valid_guid( $guid ) { - if ( ! empty( $guid ) && strlen( $guid ) === 8 && ctype_alnum( $guid ) ) - return true; - else - return false; + if ( $attr['width'] === 0 && isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH ) { + $attr['width'] = $content_width; } /** - * Register scripts needed to play VideoPress videos. One of the player methods will - * enqueue thoe script if needed. - * - * @uses is_ssl() - * @uses wp_register_script() - * @return null + * If the width isn't an even number, reduce it by one (making it even). */ - public static function register_scripts() { - $js_url = ( is_ssl() ) ? 'https://v0.wordpress.com/js/videopress.js' : 'http://s0.videopress.com/js/videopress.js'; - wp_register_script( 'videopress', $js_url, array( 'jquery', 'swfobject' ), '1.09' ); + if ( ( $attr['width'] % 2 ) === 1 ) { + $attr['width'] --; } /** - * Adds a `for` query parameter to the oembed provider request URL. - * @param String $oembed_provider - * @return String $ehnanced_oembed_provider + * Filter the default VideoPress shortcode options. + * + * @module videopress + * + * @since 2.5.0 + * + * @param array $args Array of VideoPress shortcode options. */ - public static function add_oembed_parameter( $oembed_provider ) { - if ( false === stripos( $oembed_provider, 'videopress.com' ) ) { - return $oembed_provider; - } - return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider ); + $options = apply_filters( 'videopress_shortcode_options', array( + 'at' => (int) $attr['at'], + 'hd' => $attr['hd'], + 'loop' => $attr['autoplay'] || $attr['loop'], + 'freedom' => $attr['freedom'], + 'autoplay' => $attr['autoplay'], + 'permalink' => $attr['permalink'], + 'force_flash' => (bool) $attr['flashonly'], + 'defaultlangcode' => $attr['defaultlangcode'], + 'forcestatic' => false, // This used to be a displayed option, but now is only + // accessible via the `videopress_shortcode_options` filter. + ) ); + + // Register VideoPress scripts + wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' ); + + require_once( dirname( __FILE__ ) . '/class.videopress-video.php' ); + require_once( dirname( __FILE__ ) . '/class.videopress-player.php' ); + + $player = new VideoPress_Player( $guid, $attr['width'], $options ); + + if ( is_feed() ) { + return $player->asXML(); + } else { + return $player->asHTML(); } } +add_shortcode( 'videopress', 'videopress_shortcode_callback' ); +add_shortcode( 'wpvideo', 'videopress_shortcode_callback' ); -// Initialize the shortcode handler. -Jetpack_VideoPress_Shortcode::init(); - +/** + * By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery. + */ wp_oembed_add_provider( '#^https?://videopress.com/v/.*#', 'http://public-api.wordpress.com/oembed/1.0/', true ); -add_filter( 'oembed_fetch_url', 'Jetpack_VideoPress_Shortcode::add_oembed_parameter' ); +/** + * Adds a `for` query parameter to the oembed provider request URL. + * @param String $oembed_provider + * @return String $ehnanced_oembed_provider + */ +function videopress_add_oembed_for_parameter( $oembed_provider ) { + if ( false === stripos( $oembed_provider, 'videopress.com' ) ) { + return $oembed_provider; + } + return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider ); +} +add_filter( 'oembed_fetch_url', 'videopress_add_oembed_for_parameter' ); \ No newline at end of file