-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract it from a class, as it wasn't really needing to be a class. Also pretty up some formatting and such.
- Loading branch information
1 parent
6f00ad8
commit ba4af49
Showing
2 changed files
with
112 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,139 @@ | ||
<?php | ||
|
||
/** | ||
* VideoPress Shortcode Handler | ||
* | ||
* This file may or may not be included from the Jetpack VideoPress module. | ||
*/ | ||
class Jetpack_VideoPress_Shortcode { | ||
public $min_width = 60; | ||
|
||
/** | ||
* Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display. | ||
* | ||
* Expected input formats: | ||
* | ||
* [videopress OcobLTqC] | ||
* [wpvideo OcobLTqC] | ||
* | ||
* @link http://codex.wordpress.org/Shortcode_API Shortcode API | ||
* @param array $attr shortcode attributes | ||
* @return string HTML markup or blank string on fail | ||
*/ | ||
public function videopress_shortcode_callback( $attr, $content = '' ) { | ||
global $content_width; | ||
|
||
/** | ||
* Singleton | ||
* We only accept GUIDs as a first unnamed argument. | ||
*/ | ||
public static function init() { | ||
static $instance = false; | ||
|
||
if ( ! $instance ) | ||
$instance = new Jetpack_VideoPress_Shortcode; | ||
$guid = $attr[0]; | ||
|
||
return $instance; | ||
/** | ||
* Make sure the GUID passed in matches how actual GUIDs are formatted. | ||
*/ | ||
if ( ! videopress_is_valid_guid( $guid ) ) { | ||
return ''; | ||
} | ||
|
||
function __construct() { | ||
add_shortcode( 'videopress', array( $this, 'shortcode_callback' ) ); | ||
add_shortcode( 'wpvideo', array( $this, 'shortcode_callback' ) ); | ||
} | ||
/** | ||
* Set the defaults | ||
*/ | ||
$defaults = array( | ||
'w' => 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' ); |