Initial commit

This commit is contained in:
Ben Goldsworthy 2017-01-12 23:10:07 +00:00
commit 28e6ddf404
1083 changed files with 191734 additions and 0 deletions

View file

@ -0,0 +1,811 @@
<?php
/**
* VideoPress playback module markup generator.
*
* @since 1.3
*/
class VideoPress_Player {
/**
* Video data for the requested guid and maximum width
*
* @since 1.3
* @var VideoPress_Video
*/
protected $video;
/**
* DOM identifier of the video container
*
* @var string
* @since 1.3
*/
protected $video_container_id;
/**
* DOM identifier of the video element (video, object, embed)
*
* @var string
* @since 1.3
*/
protected $video_id;
/**
* Array of playback options: force_flash or freedom
*
* @var array
* @since 1.3
*/
protected $options;
/**
* Array of video GUIDs shown and their counts,
* moved from the old VideoPress class.
*/
public static $shown = array();
/**
* Initiate a player object based on shortcode values and possible blog-level option overrides
*
* @since 1.3
* @var string $guid VideoPress unique identifier
* @var int $maxwidth maximum desired width of the video player if specified
* @var array $options player customizations
*/
public function __construct( $guid, $maxwidth = 0, $options = array() ) {
if ( empty( self::$shown[ $guid ] ) )
self::$shown[ $guid ] = 0;
self::$shown[ $guid ]++;
$this->video_container_id = 'v-' . $guid . '-' . self::$shown[ $guid ];
$this->video_id = $this->video_container_id . '-video';
if ( is_array( $options ) )
$this->options = $options;
else
$this->options = array();
// set up the video
$cache_key = null;
// disable cache in debug mode
if ( defined('WP_DEBUG') && WP_DEBUG === true ) {
$cached_video = null;
} else {
$cache_key_pieces = array( 'video' );
if ( is_multisite() && is_subdomain_install() )
$cache_key_pieces[] = get_current_blog_id();
$cache_key_pieces[] = $guid;
if ( $maxwidth > 0 )
$cache_key_pieces[] = $maxwidth;
if ( is_ssl() )
$cache_key_pieces[] = 'ssl';
$cache_key = implode( '-', $cache_key_pieces );
unset( $cache_key_pieces );
$cached_video = wp_cache_get( $cache_key, 'video' );
}
if ( empty( $cached_video ) ) {
$video = new VideoPress_Video( $guid, $maxwidth );
if ( empty( $video ) ) {
return;
} elseif ( isset( $video->error ) ) {
$this->video = $video->error;
return;
} elseif ( is_wp_error( $video ) ) {
$this->video = $video;
return;
}
$this->video = $video;
unset( $video );
if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG !== true ) {
$expire = 3600;
if ( isset( $video->expires ) && is_int( $video->expires ) ) {
$expires_diff = time() - $video->expires;
if ( $expires_diff > 0 && $expires_diff < 86400 ) // allowed range: 1 second to 1 day
$expire = $expires_diff;
unset( $expires_diff );
}
wp_cache_set( $cache_key, serialize( $this->video ), 'video', $expire );
unset( $expire );
}
} else {
$this->video = unserialize( $cached_video );
}
unset( $cache_key );
unset( $cached_video );
}
/**
* Wrap output in a VideoPress player container
*
* @since 1.3
* @var string $content HTML string
* @return string HTML string or blank string if nothing to wrap
*/
private function html_wrapper( $content ) {
if ( empty( $content ) )
return '';
else
return '<div id="' . esc_attr( $this->video_container_id ) . '" class="video-player">' . $content . '</div>';
}
/**
* Output content suitable for a feed reader displaying RSS or Atom feeds
* We do not display error messages in the feed view due to caching concerns.
* Flash content presented using <embed> markup for feed reader compatibility.
*
* @since 1.3
* @return string HTML string or empty string if error
*/
public function asXML() {
if ( empty( $this->video ) || is_wp_error( $this->video ) )
return '';
if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
$content = $this->html5_static();
else
$content = $this->flash_embed();
return $this->html_wrapper( $content );
}
/**
* Video player markup for best matching the current request and publisher options
* @since 1.3
* @return string HTML markup string or empty string if no video property found
*/
public function asHTML() {
if ( empty( $this->video ) ) {
$content = '';
} elseif ( is_wp_error( $this->video ) ) {
$content = $this->error_message( $this->video );
} elseif ( isset( $this->options['force_flash'] ) && $this->options['force_flash'] === true ) {
$content = $this->flash_object();
} elseif ( isset( $this->video->restricted_embed ) && $this->video->restricted_embed === true ) {
if( $this->options['forcestatic'] )
$content = $this->flash_object();
else
$content = $this->html5_dynamic();
} elseif ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) {
$content = $this->html5_static();
} else {
$content = $this->html5_dynamic();
}
return $this->html_wrapper( $content );
}
/**
* Display an error message to users capable of doing something about the error
*
* @since 1.3
* @uses current_user_can() to test if current user has edit_posts capability
* @var WP_Error $error WordPress error
* @return string HTML string
*/
private function error_message( $error ) {
if ( ! current_user_can( 'edit_posts' ) || empty( $error ) )
return '';
$html = '<div class="videopress-error" style="background-color:rgb(255,0,0);color:rgb(255,255,255);font-family:font-family:\'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-size:140%;min-height:10em;padding-top:1.5em;padding-bottom:1.5em">';
$html .= '<h1 style="font-size:180%;font-style:bold;line-height:130%;text-decoration:underline">' . esc_html( sprintf( __( '%s Error', 'jetpack' ), 'VideoPress' ) ) . '</h1>';
foreach( $error->get_error_messages() as $message ) {
$html .= $message;
}
$html .= '</div>';
return $html;
}
/**
* Rating agencies and industry associations require a potential viewer verify his or her age before a video or its poster frame are displayed.
* Content rated for audiences 17 years of age or older requires such verification across multiple rating agencies and industry associations
*
* @since 1.3
* @return bool true if video requires the viewer verify he or she is 17 years of age or older
*/
private function age_gate_required() {
if ( isset( $this->video->age_rating ) && $this->video->age_rating >= 17 )
return true;
else
return false;
}
/**
* Select a date of birth using HTML form elements.
*
* @since 1.5
* @return string HTML markup
*/
private function html_age_gate() {
global $wp_locale;
$text_align = 'left';
if ( $this->video->text_direction === 'rtl' )
$text_align = 'right';
$html = '<div class="videopress-age-gate" style="margin:0 60px">';
$html .= '<p class="instructions" style="color:rgb(255, 255, 255);font-size:21px;padding-top:60px;padding-bottom:20px;text-align:' . $text_align . '">' . esc_html( __( 'This video is intended for mature audiences.', 'jetpack' ) ) . '<br />' . esc_html( __( 'Please verify your birthday.', 'jetpack' ) ) . '</p>';
$html .= '<fieldset id="birthday" style="border:0 none;text-align:' . $text_align . ';padding:0;">';
$inputs_style = 'border:1px solid #444;margin-';
if ( $this->video->text_direction === 'rtl' )
$inputs_style .= 'left';
else
$inputs_style .= 'right';
$inputs_style .= ':10px;background-color:rgb(0, 0, 0);font-size:14px;color:rgb(255,255,255);padding:4px 6px;line-height: 2em;vertical-align: middle';
/**
* Display a list of months in the Gregorian calendar.
* Set values to 0-based to match JavaScript Date.
* @link https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date Mozilla JavaScript Reference: Date
*/
$html .= '<select name="month" style="' . $inputs_style . '">';
for( $i=0; $i<12; $i++ ) {
$html .= '<option value="' . esc_attr( $i ) . '">' . esc_html( $wp_locale->get_month( $i + 1 ) ) . '</option>';
}
$html .= '</select>';
/**
* todo: numdays variance by month
*/
$html .= '<select name="day" style="' . $inputs_style . '">';
for ( $i=1; $i<32; $i++ ) {
$html .= '<option>' . $i . '</option>';
}
$html .= '</select>';
/**
* Current record for human life is 122. Go back 130 years and no one is left out.
* Don't ask infants younger than 2 for their birthday
* Default to 13
*/
$html .= '<select name="year" style="' . $inputs_style . '">';
$start_year = date('Y') - 2;
$default_year = $start_year - 11;
$end_year = $start_year - 128;
for ( $year=$start_year; $year>$end_year; $year-- ) {
$html .= '<option';
if ( $year === $default_year )
$html .= ' selected="selected"';
$html .= '>' . $year . '</option>';
}
unset( $start_year );
unset( $default_year );
unset( $end_year );
$html .= '</select>';
$html .= '<input type="submit" value="' . __( 'Submit', 'jetpack' ) . '" style="cursor:pointer;border-radius: 1em;border:1px solid #333;background-color:#333;background:-webkit-gradient( linear, left top, left bottom, color-stop(0.0, #444), color-stop(1, #111) );background:-moz-linear-gradient(center top, #444 0%, #111 100%);font-size:13px;padding:4px 10px 5px;line-height:1em;vertical-align:top;color:white;text-decoration:none;margin:0" />';
$html .= '</fieldset>';
$html .= '<p style="padding-top:20px;padding-bottom:60px;text-align:' . $text_align . ';"><a rel="nofollow" href="http://videopress.com/" target="_blank" style="color:rgb(128,128,128);text-decoration:underline;font-size:15px">' . __( 'More information', 'jetpack' ) . '</a></p>';
$html .= '</div>';
return $html;
}
/**
* Return HTML5 video static markup for the given video parameters.
* Use default browser player controls.
* No Flash fallback.
*
* @since 1.2
* @link http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html HTML5 video
* @return string HTML5 video element and children
*/
private function html5_static() {
wp_enqueue_script( 'videopress' );
$thumbnail = esc_url( $this->video->poster_frame_uri );
$html = "<video id=\"{$this->video_id}\" width=\"{$this->video->calculated_width}\" height=\"{$this->video->calculated_height}\" poster=\"$thumbnail\" controls=\"true\"";
if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
$html .= ' autoplay="true"';
else
$html .= ' preload="metadata"';
if ( isset( $this->video->text_direction ) )
$html .= ' dir="' . esc_attr( $this->video->text_direction ) . '"';
if ( isset( $this->video->language ) )
$html .= ' lang="' . esc_attr( $this->video->language ) . '"';
$html .= '>';
if ( ! isset( $this->options['freedom'] ) || $this->options['freedom'] === false ) {
$mp4 = $this->video->videos->mp4->url;
if ( ! empty( $mp4 ) )
$html .= '<source src="' . esc_url( $mp4 ) . '" type="video/mp4; codecs=&quot;' . esc_attr( $this->video->videos->mp4->codecs ) . '&quot;" />';
unset( $mp4 );
}
$ogg = $this->video->videos->ogv->url;
if ( ! empty( $ogg ) )
$html .= '<source src="' . esc_url( $ogg ) . '" type="video/ogg; codecs=&quot;' . esc_attr( $this->video->videos->ogv->codecs ) . '&quot;" />';
unset( $ogg );
$html .= '<div><img alt="';
if ( isset( $this->video->title ) )
$html .= esc_attr( $this->video->title );
$html .= '" src="' . $thumbnail . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" /></div>';
if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
$html .= '<p class="robots-nocontent">' . sprintf( __( 'You do not have sufficient <a rel="nofollow" href="%s" target="_blank">freedom levels</a> to view this video. Support free software and upgrade.', 'jetpack' ), 'http://www.gnu.org/philosophy/free-sw.html' ) . '</p>';
elseif ( isset( $this->video->title ) )
$html .= '<p>' . esc_html( $this->video->title ) . '</p>';
$html .= '</video>';
return $html;
}
/**
* Click to play dynamic HTML5-capable player.
* The player displays a video preview section including poster frame,
* video title, play button and watermark on the original page load
* and calculates the playback capabilities of the browser. The video player
* is loaded when the visitor clicks on the video preview area.
* If Flash Player 10 or above is available the browser will display
* the Flash version of the video. If HTML5 video appears to be supported
* and the browser may be capable of MP4 (H.264, AAC) or OGV (Theora, Vorbis)
* playback the browser will display its native HTML5 player.
*
* @since 1.5
* @return string HTML markup
*/
private function html5_dynamic() {
/**
* Filter the VideoPress legacy player feature
*
* This filter allows you to control whether the legacy VideoPress player should be used
* instead of the improved one.
*
* @module videopress
*
* @since 3.7.0
*
* @param boolean $videopress_use_legacy_player
*/
if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
return $this->html5_dynamic_next();
}
wp_enqueue_script( 'videopress' );
$video_placeholder_id = $this->video_container_id . '-placeholder';
$age_gate_required = $this->age_gate_required();
$width = absint( $this->video->calculated_width );
$height = absint( $this->video->calculated_height );
$html = '<div id="' . $video_placeholder_id . '" class="videopress-placeholder" style="';
if ( $age_gate_required )
$html .= "min-width:{$width}px;min-height:{$height}px";
else
$html .= "width:{$width}px;height:{$height}px";
$html .= ';display:none;cursor:pointer !important;position:relative;';
if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) )
$html .= 'background-color:' . esc_attr( $this->video->skin->background_color ) . ';';
$html .= 'font-family: \'Helvetica Neue\',Arial,Helvetica,\'Nimbus Sans L\',sans-serif;font-weight:bold;font-size:18px">' . PHP_EOL;
/**
* Do not display a poster frame, title, or any other content hints for mature content.
*/
if ( ! $age_gate_required ) {
if ( ! empty( $this->video->title ) ) {
$html .= '<div class="videopress-title" style="display:inline;position:absolute;margin:20px 20px 0 20px;padding:4px 8px;vertical-align:top;text-align:';
if ( $this->video->text_direction === 'rtl' )
$html .= 'right" dir="rtl"';
else
$html .= 'left" dir="ltr"';
if ( isset( $this->video->language ) )
$html .= ' lang="' . esc_attr( $this->video->language ) . '"';
$html .= '><span style="padding:3px 0;line-height:1.5em;';
if ( isset( $this->video->skin ) && isset( $this->video->skin->background_color ) ) {
$html .= 'background-color:';
if ( $this->video->skin->background_color === 'rgb(0,0,0)' )
$html .= 'rgba(0,0,0,0.8)';
else
$html .= esc_attr( $this->video->skin->background_color );
$html .= ';';
}
$html .= 'color:rgb(255,255,255)">' . esc_html( $this->video->title ) . '</span></div>';
}
$html .= '<img class="videopress-poster" alt="';
if ( ! empty( $this->video->title ) )
$html .= esc_attr( $this->video->title ) . '" title="' . esc_attr( sprintf( _x( 'Watch: %s', 'watch a video title', 'jetpack' ), $this->video->title ) );
$html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $width . '" height="' . $height . '" />' . PHP_EOL;
//style a play button hovered over the poster frame
$html .= '<div class="play-button"><span style="z-index:2;display:block;position:absolute;top:50%;left:50%;text-align:center;vertical-align:middle;color:rgb(255,255,255);opacity:0.9;margin:0 0 0 -0.45em;padding:0;line-height:0;font-size:500%;text-shadow:0 0 40px rgba(0,0,0,0.5)">&#9654;</span></div>' . PHP_EOL;
// watermark
if ( isset( $this->video->skin ) && isset( $this->video->skin->watermark ) ) {
$html .= '<div style="position:relative;margin-top:-40px;height:25px;margin-bottom:35px;';
if ( $this->video->text_direction === 'rtl' )
$html .= 'margin-left:20px;text-align:left;';
else
$html .= 'margin-right:20px;text-align:right;';
$html .= 'vertical-align:bottom;z-index:3">';
$html .= '<img alt="" src="' . esc_url( $this->video->skin->watermark, array( 'http', 'https' ) ) . '" width="90" height="13" style="background-color:transparent;background-image:none;background-repeat:no-repeat;border:none;margin:0;padding:0"/>';
$html .= '</div>' . PHP_EOL;
}
}
$data = array(
'blog' => absint( $this->video->blog_id ),
'post' => absint( $this->video->post_id ),
'duration'=> absint( $this->video->duration ),
'poster' => esc_url_raw( $this->video->poster_frame_uri, array( 'http', 'https' ) ),
'hd' => (bool) $this->options['hd']
);
if ( isset( $this->video->videos ) ) {
if ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
$data['mp4'] = array( 'size' => $this->video->videos->mp4->format, 'uri' => esc_url_raw( $this->video->videos->mp4->url, array( 'http', 'https' ) ) );
if ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
$data['ogv'] = array( 'size' => 'std', 'uri' => esc_url_raw( $this->video->videos->ogv->url, array( 'http', 'https' ) ) );
}
$locale = array( 'dir' => $this->video->text_direction );
if ( isset( $this->video->language ) )
$locale['lang'] = $this->video->language;
$data['locale'] = $locale;
unset( $locale );
$guid = $this->video->guid;
$guid_js = json_encode( $guid );
$html .= '<script type="text/javascript">' . PHP_EOL;
$html .= 'jQuery(document).ready(function() {';
$html .= 'if ( !jQuery.VideoPress.data[' . json_encode($guid) . '] ) { jQuery.VideoPress.data[' . json_encode($guid) . '] = new Array(); }' . PHP_EOL;
$html .= 'jQuery.VideoPress.data[' . json_encode( $guid ) . '][' . self::$shown[ $guid ] . ']=' . json_encode($data) . ';' . PHP_EOL;
unset( $data );
$jq_container = json_encode( '#' . $this->video_container_id );
$jq_placeholder = json_encode( '#' . $video_placeholder_id );
$player_config = "{width:{$width},height:{$height},";
if ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true )
$player_config .= 'freedom:"true",';
$player_config .= 'container:jQuery(' . $jq_container . ')}';
$html .= "jQuery({$jq_placeholder}).show(0,function(){jQuery.VideoPress.analytics.impression({$guid_js})});" . PHP_EOL;
if ( $age_gate_required ) {
$html .= 'if ( jQuery.VideoPress.support.flash() ) {' . PHP_EOL;
/**
* @link http://code.google.com/p/swfobject/wiki/api#swfobject.embedSWF(swfUrlStr,_replaceElemIdStr,_widthStr,_height
*/
$html .= 'swfobject.embedSWF(' . implode( ',', array(
'jQuery.VideoPress.video.flash.player_uri',
json_encode( $this->video_container_id ),
json_encode( $width ),
json_encode( $height ),
'jQuery.VideoPress.video.flash.min_version',
'jQuery.VideoPress.video.flash.expressinstall', // attempt to upgrade the Flash player if less than min_version. requires a 310x137 container or larger but we will always try to include
'{guid:' . $guid_js . '}', // FlashVars
'jQuery.VideoPress.video.flash.params',
'null', // no attributes
'jQuery.VideoPress.video.flash.embedCallback' // error fallback
) ) . ');';
$html .= '} else {' . PHP_EOL;
$html .= "if ( jQuery.VideoPress.video.prepare({$guid_js},{$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
$html .= 'if ( jQuery(' . $jq_container . ').data( "player" ) === "flash" ){jQuery.VideoPress.video.play(jQuery(' . json_encode('#' . $this->video_container_id) . '));}else{';
$html .= 'jQuery(' . $jq_placeholder . ').html(' . json_encode( $this->html_age_date() ) . ');' . PHP_EOL;
$html .= 'jQuery(' . json_encode( '#' . $video_placeholder_id . ' input[type=submit]' ) . ').one("click", function(event){jQuery.VideoPress.requirements.isSufficientAge(jQuery(' . $jq_container . '),' . absint( $this->video->age_rating ) . ')});' . PHP_EOL;
$html .= '}}}' . PHP_EOL;
} else {
$html .= "if ( jQuery.VideoPress.video.prepare({$guid_js}, {$player_config}," . self::$shown[ $guid ] . ') ) {' . PHP_EOL;
if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
$html .= "jQuery.VideoPress.video.play(jQuery({$jq_container}));";
else
$html .= 'jQuery(' . $jq_placeholder . ').one("click",function(){jQuery.VideoPress.video.play(jQuery(' . $jq_container . '))});';
$html .= '}';
// close the jQuery(document).ready() function
$html .= '});';
}
$html .= '</script>' . PHP_EOL;
$html .= '</div>' . PHP_EOL;
/*
* JavaScript required
*/
$noun = __( 'this video', 'jetpack' );
if ( ! $age_gate_required ) {
$vid_type = '';
if ( ( isset( $this->options['freedom'] ) && $this->options['freedom'] === true ) && ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) ) )
$vid_type = 'ogv';
elseif ( isset( $this->video->videos->mp4 ) && isset( $this->video->videos->mp4->url ) )
$vid_type = 'mp4';
elseif ( isset( $this->video->videos->ogv ) && isset( $this->video->videos->ogv->url ) )
$vid_type = 'ogv';
if ( $vid_type !== '' ) {
$noun = '<a ';
if ( isset( $this->video->language ) )
$noun .= 'hreflang="' . esc_attr( $this->video->language ) . '" ';
if ( $vid_type === 'mp4' )
$noun .= 'type="video/mp4" href="' . esc_url( $this->video->videos->mp4->url, array( 'http', 'https' ) );
elseif ( $vid_type === 'ogv' )
$noun .= 'type="video/ogv" href="' . esc_url( $this->video->videos->ogv->url, array( 'http', 'https' ) );
$noun .= '">';
if ( isset( $this->video->title ) )
$noun .= esc_html( $this->video->title );
else
$noun .= __( 'this video', 'jetpack' );
$noun .= '</a>';
} elseif ( ! empty( $this->title ) ) {
$noun = esc_html( $this->title );
}
unset( $vid_type );
}
$html .= '<noscript><p>' . sprintf( _x( 'JavaScript required to play %s.', 'Play as in playback or view a movie', 'jetpack' ), $noun ) . '</p></noscript>';
return $html;
}
function html5_dynamic_next() {
$video_container_id = 'v-' . $this->video->guid;
// Must not use iframes for IE11 due to a fullscreen bug
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && stristr( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0' ) ) {
$iframe_embed = false;
} else {
/**
* Filter the VideoPress iframe embed
*
* This filter allows you to control whether the videos will be embedded using an iframe.
* Set this to false in order to use an in-page embed rather than an iframe.
*
* @module videopress
*
* @since 3.7.0
*
* @param boolean $videopress_player_use_iframe
*/
$iframe_embed = apply_filters( 'jetpack_videopress_player_use_iframe', true );
}
if ( ! array_key_exists( 'hd', $this->options ) ) {
$this->options['hd'] = (bool) get_option( 'video_player_high_quality', false );
}
$videopress_options = array(
'width' => absint( $this->video->calculated_width ),
'height' => absint( $this->video->calculated_height ),
);
foreach ( $this->options as $option => $value ) {
switch ( $option ) {
case 'at':
if ( intval( $value ) ) {
$videopress_options[ $option ] = intval( $value );
}
break;
case 'autoplay':
$option = 'autoPlay';
case 'hd':
case 'loop':
case 'permalink':
if ( in_array( $value, array( 1, 'true' ) ) ) {
$videopress_options[ $option ] = true;
} elseif ( in_array( $value, array( 0, 'false' ) ) ) {
$videopress_options[ $option ] = false;
}
break;
case 'defaultlangcode':
$option = 'defaultLangCode';
if ( $value ) {
$videopress_options[ $option ] = $value;
}
break;
}
}
if ( $iframe_embed ) {
$iframe_url = "https://videopress.com/embed/{$this->video->guid}";
foreach ( $videopress_options as $option => $value ) {
if ( ! in_array( $option, array( 'width', 'height' ) ) ) {
// add_query_arg ignores false as a value, so replacing it with 0
$iframe_url = add_query_arg( $option, ( false === $value ) ? 0 : $value, $iframe_url );
}
}
$js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js';
$js_url = add_query_arg( 'jetpack_version', JETPACK__VERSION, $js_url );
return "<iframe width='" . esc_attr( $videopress_options['width'] )
. "' height='" . esc_attr( $videopress_options['height'] )
. "' src='" . esc_attr( $iframe_url )
. "' frameborder='0' allowfullscreen></iframe>"
. "<script src='" . esc_attr( $js_url ) . "'></script>";
} else {
$videopress_options = json_encode( $videopress_options );
$js_url = 'https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress.js';
$js_url = add_query_arg( 'jetpack_version', JETPACK__VERSION, $js_url );
return "<div id='{$video_container_id}'></div>
<script src='{$js_url}'></script>
<script>
videopress('{$this->video->guid}', document.querySelector('#{$video_container_id}'), {$videopress_options});
</script>";
}
}
/**
* Only allow legitimate Flash parameters and their values
*
* @since 1.2
* @link http://kb2.adobe.com/cps/127/tn_12701.html Flash object and embed attributes
* @link http://kb2.adobe.com/cps/133/tn_13331.html devicefont
* @link http://kb2.adobe.com/cps/164/tn_16494.html allowscriptaccess
* @link http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html full screen mode
* @link http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001079.html allownetworking
* @param array $flash_params Flash parameters expressed in key-value form
* @return array validated Flash parameters
*/
public static function esc_flash_params( $flash_params ) {
$allowed_params = array(
'swliveconnect' => array('true', 'false'),
'play' => array('true', 'false'),
'loop' => array('true', 'false'),
'menu' => array('true', 'false'),
'quality' => array('low', 'autolow', 'autohigh', 'medium', 'high', 'best'),
'scale' => array('default', 'noborder', 'exactfit', 'noscale'),
'align' => array('l', 'r', 't'),
'salign' => array('l', 'r', 't', 'tl', 'tr', 'bl', 'br'),
'wmode' => array('window', 'opaque', 'transparent','direct','gpu'),
'devicefont' => array('_sans', '_serif', '_typewriter'),
'allowscriptaccess' => array('always', 'samedomain', 'never'),
'allownetworking' => array('all','internal', 'none'),
'seamlesstabbing' => array('true', 'false'),
'allowfullscreen' => array('true', 'false'),
'fullScreenAspectRatio' => array('portrait', 'landscape'),
'base',
'bgcolor',
'flashvars'
);
$allowed_params_keys = array_keys( $allowed_params );
$filtered_params = array();
foreach( $flash_params as $param=>$value ) {
if ( empty($param) || empty($value) )
continue;
$param = strtolower($param);
if ( in_array($param, $allowed_params_keys) ) {
if ( isset( $allowed_params[$param] ) && is_array( $allowed_params[$param] ) ) {
$value = strtolower($value);
if ( in_array( $value, $allowed_params[$param] ) )
$filtered_params[$param] = $value;
} else {
$filtered_params[$param] = $value;
}
}
}
unset( $allowed_params_keys );
/**
* Flash specifies sameDomain, not samedomain. change from lowercase value for preciseness
*/
if ( isset( $filtered_params['allowscriptaccess'] ) && $filtered_params['allowscriptaccess'] === 'samedomain' )
$filtered_params['allowscriptaccess'] = 'sameDomain';
return $filtered_params;
}
/**
* Filter Flash variables from the response, taking into consideration player options.
*
* @since 1.3
* @return array Flash variable key value pairs
*/
private function get_flash_variables() {
if ( ! isset( $this->video->players->swf->vars ) )
return array();
$flashvars = (array) $this->video->players->swf->vars;
if ( isset( $this->options['autoplay'] ) && $this->options['autoplay'] === true )
$flashvars['autoPlay'] = 'true';
return $flashvars;
}
/**
* Validate and filter Flash parameters
*
* @since 1.3
* @return array Flash parameters passed through key and value validation
*/
private function get_flash_parameters() {
if ( ! isset( $this->video->players->swf->params ) )
return array();
else
return self::esc_flash_params(
/**
* Filters the Flash parameters of the VideoPress player.
*
* @module videopress
*
* @since 1.2.0
*
* @param array $this->video->players->swf->params Array of swf parameters for the VideoPress flash player.
*/
apply_filters( 'video_flash_params', (array) $this->video->players->swf->params, 10, 1 )
);
}
/**
* Flash player markup in a HTML embed element.
*
* @since 1.1
* @link http://www.whatwg.org/specs/web-apps/current-work/multipage/the-iframe-element.html#the-embed-element embed element
* @link http://www.google.com/support/reader/bin/answer.py?answer=70664 Google Reader markup support
* @return string HTML markup. Embed element with no children
*/
private function flash_embed() {
wp_enqueue_script( 'videopress' );
if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
return '';
$embed = array(
'id' => $this->video_id,
'src' => esc_url_raw( $this->video->players->swf->url . '&' . http_build_query( $this->get_flash_variables(), null, '&' ) , array( 'http', 'https' ) ),
'type' => 'application/x-shockwave-flash',
'width' => $this->video->calculated_width,
'height' => $this->video->calculated_height
);
if ( isset( $this->video->title ) )
$embed['title'] = $this->video->title;
$embed = array_merge( $embed, $this->get_flash_parameters() );
$html = '<embed';
foreach ( $embed as $attribute => $value ) {
$html .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
}
unset( $embed );
$html .= '></embed>';
return $html;
}
/**
* Double-baked Flash object markup for Internet Explorer and more standards-friendly consuming agents.
*
* @since 1.1
* @return HTML markup. Object and children.
*/
private function flash_object() {
wp_enqueue_script( 'videopress' );
if ( ! isset( $this->video->players->swf ) || ! isset( $this->video->players->swf->url ) )
return '';
$thumbnail_html = '<img alt="';
if ( isset( $this->video->title ) )
$thumbnail_html .= esc_attr( $this->video->title );
$thumbnail_html .= '" src="' . esc_url( $this->video->poster_frame_uri, array( 'http', 'https' ) ) . '" width="' . $this->video->calculated_width . '" height="' . $this->video->calculated_height . '" />';
$flash_vars = esc_attr( http_build_query( $this->get_flash_variables(), null, '&' ) );
$flash_params = '';
foreach ( $this->get_flash_parameters() as $attribute => $value ) {
$flash_params .= '<param name="' . esc_attr( $attribute ) . '" value="' . esc_attr( $value ) . '" />';
}
$flash_help = sprintf( __( 'This video requires <a rel="nofollow" href="%s" target="_blank">Adobe Flash</a> for playback.', 'jetpack' ), 'http://www.adobe.com/go/getflashplayer');
$flash_player_url = esc_url( $this->video->players->swf->url, array( 'http', 'https' ) );
$description = '';
if ( isset( $this->video->title ) ) {
$standby = $this->video->title;
$description = '<p><strong>' . esc_html( $this->video->title ) . '</strong></p>';
} else {
$standby = __( 'Loading video...', 'jetpack' );
}
$standby = ' standby="' . esc_attr( $standby ) . '"';
return <<<OBJECT
<script type="text/javascript">if(typeof swfobject!=="undefined"){swfobject.registerObject("{$this->video_id}", "{$this->video->players->swf->version}");}</script>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}" id="{$this->video_id}"{$standby}>
<param name="movie" value="{$flash_player_url}" />
{$flash_params}
<param name="flashvars" value="{$flash_vars}" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="{$flash_player_url}" width="{$this->video->calculated_width}" height="{$this->video->calculated_height}"{$standby}>
{$flash_params}
<param name="flashvars" value="{$flash_vars}" />
<!--<![endif]-->
{$thumbnail_html}{$description}<p class="robots-nocontent">{$flash_help}</p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
OBJECT;
}
}

View file

@ -0,0 +1,330 @@
<?php
/**
* VideoPress video object retrieved from VideoPress servers and parsed.
* @since 1.3
*/
class VideoPress_Video {
public $version = 3;
/**
* Manifest version returned by remote service.
*
* @var string
* @since 1.3
*/
const manifest_version = '1.5';
/**
* Expiration of the video expressed in Unix time
*
* @var int
* @since 1.3
*/
public $expires;
/**
* VideoPress unique identifier
*
* @var string
* @since 1.3
*/
public $guid;
/**
* WordPress.com blog identifier
*
* @var int
* @since 1.5
*/
public $blog_id;
/**
* Remote blog attachment identifier
*
* @var int
* @since 1.5
*/
public $post_id;
/**
* Maximum desired width.
*
* @var int
* @since 1.3
*/
public $maxwidth;
/**
* Video width calculated based on original video dimensions and the requested maxwidth
*
* @var int
* @since 1.3
*/
public $calculated_width;
/**
* Video height calculated based on original video dimensions and the requested maxwidth
*
* @var int
* @since 1.3
*/
public $calculated_height;
/**
* Video title
*
* @var string
* @since 1.3
*/
public $title;
/**
* Directionality of title text. ltr or rtl
*
* @var string
* @since 1.3
*/
public $text_direction;
/**
* Text and audio language as ISO 639-2 language code
*
* @var string
* @since 1.3
*/
public $language;
/**
* Video duration in whole seconds
*
* @var int
* @since 1.3
*/
public $duration;
/**
* Recommended minimum age of the viewer.
*
* @var int
* @since 1.3
*/
public $age_rating;
/**
* Video author has restricted video embedding or sharing
*
* @var bool
* @since 1.3
*/
public $restricted_embed;
/**
* Poster frame image URI for the given video guid and calculated dimensions.
*
* @var string
* @since 1.3
*/
public $poster_frame_uri;
/**
* Video files associated with the given guid for the calculated dimensions.
*
* @var stdClass
* @since 1.3
*/
public $videos;
/**
* Video player information
*
* @var stdClass
* @since 1.3
*/
public $players;
/**
* Video player skinning preferences including background color and watermark
*
* @var array
* @since 1.5
*/
public $skin;
/**
* Closed captions if available for the given video. Associative array of ISO 639-2 language code and a WebVTT URI
*
* @var array
* @since 1.5
*/
public $captions;
/**
* Setup the object.
* Request video information from VideoPress servers and process the response.
*
* @since 1.3
* @var string $guid VideoPress unique identifier
* @var int $maxwidth maximum requested video width. final width and height are calculated on VideoPress servers based on the aspect ratio of the original video upload.
*/
public function __construct( $guid, $maxwidth = 640 ) {
$this->guid = $guid;
$maxwidth = absint( $maxwidth );
if ( $maxwidth > 0 )
$this->maxwidth = $maxwidth;
$data = $this->get_data();
if ( is_wp_error( $data ) || empty( $data ) ) {
/** This filter is documented in modules/videopress/class.videopress-player.php */
if ( ! apply_filters( 'jetpack_videopress_use_legacy_player', false ) ) {
// Unlike the Flash player, the new player does it's own error checking, age gate, etc.
$data = (object) array( 'guid' => $guid, 'width' => $maxwidth, 'height' => $maxwidth / 16 * 9 );
} else {
$this->error = $data;
return;
}
}
if ( isset( $data->blog_id ) )
$this->blog_id = absint( $data->blog_id );
if ( isset( $data->post_id ) )
$this->post_id = absint( $data->post_id );
if ( isset( $data->title ) && $data->title !== '' )
$this->title = trim( str_replace( '&nbsp;', ' ', $data->title ) );
if ( isset( $data->text_direction ) && $data->text_direction === 'rtl' )
$this->text_direction = 'rtl';
else
$this->text_direction = 'ltr';
if ( isset( $data->language ) )
$this->language = $data->language;
if ( isset( $data->duration ) && $data->duration > 0 )
$this->duration = absint( $data->duration );
if ( isset( $data->width ) && $data->width > 0 )
$this->calculated_width = absint( $data->width );
if ( isset( $data->height ) && $data->height > 0 )
$this->calculated_height = absint( $data->height );
if ( isset( $data->age_rating ) )
$this->age_rating = absint( $this->age_rating );
if ( isset( $data->restricted_embed ) && $data->restricted_embed === true )
$this->restricted_embed = true;
else
$this->restricted_embed = false;
if ( isset( $data->posterframe ) && $data->posterframe !== '' )
$this->poster_frame_uri = esc_url_raw( $data->posterframe, array( 'http', 'https' ) );
if ( isset( $data->mp4 ) || isset( $data->ogv ) ) {
$this->videos = new stdClass();
if ( isset( $data->mp4 ) )
$this->videos->mp4 = $data->mp4;
if ( isset( $data->ogv ) )
$this->videos->ogv = $data->ogv;
}
if ( isset( $data->swf ) ) {
if ( ! isset( $this->players ) )
$this->players = new stdClass();
$this->players->swf = $data->swf;
}
if ( isset( $data->skin ) )
$this->skin = $data->skin;
if ( isset( $data->captions ) )
$this->captions = (array) $data->captions;
}
/**
* Convert an Expires HTTP header value into Unix time for use in WP Cache
*
* @since 1.3
* @var string $expires_header
* @return int|bool Unix time or false
*/
public static function calculate_expiration( $expires_header ) {
if ( empty( $expires_header ) || ! is_string( $expires_header ) )
return false;
if ( class_exists( 'DateTime' ) && class_exists( 'DateTimeZone' ) ) {
$expires_date = DateTime::createFromFormat( 'D, d M Y H:i:s T', $expires_header, new DateTimeZone( 'UTC' ) );
if ( $expires_date instanceOf DateTime )
return date_format( $expires_date, 'U' );
} else {
$expires_array = strptime( $expires_header, '%a, %d %b %Y %H:%M:%S %Z' );
if ( is_array( $expires_array ) && isset( $expires_array['tm_hour'] ) && isset( $expires_array['tm_min'] ) && isset( $expires_array['tm_sec'] ) && isset( $expires_array['tm_mon'] ) && isset( $expires_array['tm_mday'] ) && isset( $expires_array['tm_year'] ) )
return gmmktime( $expires_array['tm_hour'], $expires_array['tm_min'], $expires_array['tm_sec'], 1 + $expires_array['tm_mon'], $expires_array['tm_mday'], 1900 + $expires_array['tm_year'] );
}
return false;
}
/**
* Extract the site's host domain for statistics and comparison against an allowed site list in the case of restricted embeds.
*
* @since 1.2
* @param string $url absolute URL
* @return bool|string host component of the URL, or false if none found
*/
public static function hostname( $url ) {
return parse_url( esc_url_raw( $url ), PHP_URL_HOST );
}
/**
* Request data from WordPress.com for the given guid, maxwidth, and calculated blog hostname.
*
* @since 1.3
* @return stdClass|WP_Error parsed JSON response or WP_Error if request unsuccessful
*/
private function get_data() {
global $wp_version;
$domain = self::hostname( home_url() );
$request_params = array( 'guid' => $this->guid, 'domain' => $domain );
if ( isset( $this->maxwidth ) && $this->maxwidth > 0 )
$request_params['maxwidth'] = $this->maxwidth;
$url = 'http://videopress.com/data/wordpress.json';
if ( is_ssl() )
$url = 'https://v.wordpress.com/data/wordpress.json';
$response = wp_remote_get( add_query_arg( $request_params, $url ), array(
'redirection' => 1,
'user-agent' => 'VideoPress plugin ' . $this->version . '; WordPress ' . $wp_version . ' (' . home_url('/') . ')',
) );
unset( $request_params );
unset( $url );
$response_body = wp_remote_retrieve_body( $response );
$response_code = absint( wp_remote_retrieve_response_code( $response ) );
if ( is_wp_error( $response ) ) {
return $response;
} elseif ( $response_code === 400 ) {
return new WP_Error( 'bad_config', __( 'The VideoPress plugin could not communicate with the VideoPress servers. This error is most likely caused by a misconfigured plugin. Please reinstall or upgrade.', 'jetpack' ) );
} elseif ( $response_code === 403 ) {
return new WP_Error( 'http_forbidden', '<p>' . sprintf( __( '<strong>%s</strong> is not an allowed embed site.' , 'jetpack' ), esc_html( $domain ) ) . '</p><p>' . __( 'Publisher limits playback of video embeds.', 'jetpack' ) . '</p>' );
} elseif ( $response_code === 404 ) {
return new WP_Error( 'http_not_found', '<p>' . sprintf( __( 'No data found for VideoPress identifier: <strong>%s</strong>.', 'jetpack' ), $this->guid ) . '</p>' );
} elseif ( $response_code !== 200 || empty( $response_body ) ) {
return;
} else {
$expires_header = wp_remote_retrieve_header( $response, 'Expires' );
if ( ! empty( $expires_header ) ) {
$expires = self::calculate_expiration( $expires_header );
if ( ! empty( $expires ) )
$this->expires = $expires;
}
return json_decode( $response_body );
}
}
}

View file

@ -0,0 +1,264 @@
/* global tinyMCE, vpEditorView */
(function( $, wp, vpEditorView ){
wp.mce = wp.mce || {};
if ( 'undefined' === typeof wp.mce.views ) {
return;
}
wp.mce.videopress_wp_view_renderer = {
shortcode_string : 'videopress',
shortcode_data : {},
defaults : {
w : '',
at : '',
permalink : true,
hd : false,
loop : false,
freedom : false,
autoplay : false,
flashonly : false
},
coerce : wp.media.coerce,
template : wp.template( 'videopress_iframe_vnext' ),
getContent : function() {
var urlargs = 'for=' + encodeURIComponent( vpEditorView.home_url_host ),
named = this.shortcode.attrs.named,
options, key, width;
for ( key in named ) {
switch ( key ) {
case 'at' :
if ( parseInt( named[ key ], 10 ) ) {
urlargs += '&' + key + '=' + parseInt( named[ key ], 10 );
} // Else omit, as it's the default.
break;
case 'permalink' :
if ( 'false' === named[ key ] ) {
urlargs += '&' + key + '=0';
} // Else omit, as it's the default.
break;
case 'hd' :
case 'loop' :
case 'autoplay' :
if ( 'true' === named[ key ] ) {
urlargs += '&' + key + '=1';
} // Else omit, as it's the default.
break;
default:
// Unknown parameters? Ditch it!
break;
}
}
options = {
width : vpEditorView.content_width,
height : ( vpEditorView.content_width * 0.5625 ),
guid : this.shortcode.attrs.numeric[0],
urlargs : urlargs
};
if ( typeof named.w !== 'undefined' ) {
width = parseInt( named.w, 10 );
if ( width >= vpEditorView.min_content_width && width < vpEditorView.content_width ) {
options.width = width;
options.height = parseInt( width * 0.5625, 10 );
}
}
options.ratio = 100 * ( options.height / options.width );
return this.template( options );
},
edit: function( data ) {
var shortcode_data = wp.shortcode.next( this.shortcode_string, data),
named = shortcode_data.shortcode.attrs.named,
editor = tinyMCE.activeEditor,
renderer = this,
oldRenderFormItem = tinyMCE.ui.FormItem.prototype.renderHtml;
/**
* Override TextBox renderHtml to support html5 attrs.
* @link https://github.com/tinymce/tinymce/pull/2784
*
* @returns {string}
*/
tinyMCE.ui.TextBox.prototype.renderHtml = function() {
var self = this,
settings = self.settings,
element = document.createElement( settings.multiline ? 'textarea' : 'input' ),
extraAttrs = [
'rows',
'spellcheck',
'maxLength',
'size',
'readonly',
'min',
'max',
'step',
'list',
'pattern',
'placeholder',
'required',
'multiple'
],
i, key;
for ( i = 0; i < extraAttrs.length; i++ ) {
key = extraAttrs[ i ];
if ( typeof settings[ key ] !== 'undefined' ) {
element.setAttribute( key, settings[ key ] );
}
}
if ( settings.multiline ) {
element.innerText = self.state.get( 'value' );
} else {
element.setAttribute( 'type', settings.subtype ? settings.subtype : 'text' );
element.setAttribute( 'value', self.state.get( 'value' ) );
}
element.id = self._id;
element.className = self.classes;
element.setAttribute( 'hidefocus', 1 );
if ( self.disabled() ) {
element.disabled = true;
}
return element.outerHTML;
};
tinyMCE.ui.FormItem.prototype.renderHtml = function() {
_.each( vpEditorView.modal_labels, function( value, key ) {
if ( value === this.settings.items.text ) {
this.classes.add( 'videopress-field-' + key );
}
}, this );
if ( _.contains( [
vpEditorView.modal_labels.hd,
vpEditorView.modal_labels.permalink,
vpEditorView.modal_labels.autoplay,
vpEditorView.modal_labels.loop,
vpEditorView.modal_labels.freedom,
vpEditorView.modal_labels.flashonly
], this.settings.items.text ) ) {
this.classes.add( 'videopress-checkbox' );
}
return oldRenderFormItem.call( this );
};
/**
* Populate the defaults.
*/
_.each( this.defaults, function( value, key ) {
named[ key ] = this.coerce( named, key);
}, this );
/**
* Declare the fields that will show in the popup when editing the shortcode.
*/
editor.windowManager.open( {
title : vpEditorView.modal_labels.title,
id : 'videopress-shortcode-settings-modal',
width : 520,
height : 240,
body : [
{
type : 'textbox',
disabled : true,
name : 'guid',
label : vpEditorView.modal_labels.guid,
value : shortcode_data.shortcode.attrs.numeric[0]
}, {
type : 'textbox',
subtype : 'number',
min : vpEditorView.min_content_width, // The `min` may supported be in the future. https://github.com/tinymce/tinymce/pull/2784
name : 'w',
label : vpEditorView.modal_labels.w,
value : named.w
}, {
type : 'textbox',
subtype : 'number',
min : 0, // The `min` may supported be in the future. https://github.com/tinymce/tinymce/pull/2784
name : 'at',
label : vpEditorView.modal_labels.at,
value : named.at
}, {
type : 'checkbox',
name : 'hd',
label : vpEditorView.modal_labels.hd,
checked : named.hd
}, {
type : 'checkbox',
name : 'permalink',
label : vpEditorView.modal_labels.permalink,
checked : named.permalink
}, {
type : 'checkbox',
name : 'autoplay',
label : vpEditorView.modal_labels.autoplay,
checked : named.autoplay
}, {
type : 'checkbox',
name : 'loop',
label : vpEditorView.modal_labels.loop,
checked : named.loop
}, {
type : 'checkbox',
name : 'freedom',
label : vpEditorView.modal_labels.freedom,
checked : named.freedom
}, {
type : 'checkbox',
name : 'flashonly',
label : vpEditorView.modal_labels.flashonly,
checked : named.flashonly
}
],
onsubmit : function( e ) {
var args = {
tag : renderer.shortcode_string,
type : 'single',
attrs : {
named : _.pick( e.data, _.keys( renderer.defaults ) ),
numeric : [ e.data.guid ]
}
};
if ( '0' === args.attrs.named.at ) {
args.attrs.named.at = '';
}
_.each( renderer.defaults, function( value, key ) {
args.attrs.named[ key ] = this.coerce( args.attrs.named, key );
if ( value === args.attrs.named[ key ] ) {
delete args.attrs.named[ key ];
}
}, renderer );
editor.insertContent( wp.shortcode.string( args ) );
},
onopen : function ( e ) {
var prefix = 'mce-videopress-field-';
_.each( ['w', 'at'], function( value ) {
e.target.$el.find( '.' + prefix + value + ' .mce-container-body' ).append( '<span class="' + prefix + 'unit ' + prefix + 'unit-' + value + '">' + vpEditorView.modal_labels[ value + '_unit' ] );
} );
$('body').addClass( 'modal-open' );
},
onclose: function () {
$('body').removeClass( 'modal-open' );
}
} );
// Set it back to its original renderer.
tinyMCE.ui.FormItem.prototype.renderHtml = oldRenderFormItem;
}
};
wp.mce.views.register( 'videopress', wp.mce.videopress_wp_view_renderer );
// Extend the videopress one to also handle `wpvideo` instances.
wp.mce.wpvideo_wp_view_renderer = _.extend( {}, wp.mce.videopress_wp_view_renderer, {
shortcode_string : 'wpvideo'
});
wp.mce.views.register( 'wpvideo', wp.mce.wpvideo_wp_view_renderer );
}( jQuery, wp, vpEditorView ));

View file

@ -0,0 +1,502 @@
/* jshint onevar: false, smarttabs: true, devel: true */
/* global VideoPressAdminSettings, setUserSetting */
/**
* VideoPress Admin
*
* @todo i18n
*/
(function($) {
var media = wp.media;
var VideoPress = VideoPress || {};
VideoPress.caps = VideoPressAdminSettings.caps;
VideoPress.l10n = VideoPressAdminSettings.l10n;
/**
* Create a new controller that simply adds a videopress key
* to the library query
*/
media.controller.VideoPress = media.controller.Library.extend({
defaults: _.defaults({
id: 'videopress',
router: 'videopress',
toolbar: 'videopress-toolbar',
title: 'VideoPress',
priority: 200,
searchable: true,
sortable: false
}, media.controller.Library.prototype.defaults ),
initialize: function() {
if ( ! this.get('library') ) {
this.set( 'library', media.query({ videopress: true }) );
}
media.controller.Library.prototype.initialize.apply( this, arguments );
},
/**
* The original function saves content for the browse router only,
* so we hi-jack it a little bit.
*/
saveContentMode: function() {
if ( 'videopress' !== this.get('router') ) {
return;
}
var mode = this.frame.content.mode(),
view = this.frame.router.get();
if ( view && view.get( mode ) ) {
// Map the Upload a Video back to the regular Upload Files.
if ( 'upload_videopress' === mode ) {
mode = 'upload';
}
setUserSetting( 'libraryContent', mode );
}
}
});
/**
* VideoPress Uploader
*/
media.view.VideoPressUploader = media.View.extend({
tagName: 'div',
className: 'uploader-videopress',
template: media.template('videopress-uploader'),
events: {
'submit .videopress-upload-form': 'submitForm'
},
initialize: function() {
var that = this;
if ( ! window.addEventListener ) {
window.attachEvent( 'onmessage', function() { return that.messageHandler.apply( that, arguments ); } );
} else {
window.addEventListener( 'message', function() { return that.messageHandler.apply( that, arguments ); }, false );
}
return media.View.prototype.initialize.apply( this, arguments );
},
submitForm: function() {
var data = false;
this.clearErrors();
if ( this.$( 'input[name="videopress_file"]').val().length < 1 ) {
this.error( VideoPress.l10n.selectVideoFile );
return false;
}
// Prevent multiple submissions.
this.$( '.videopress-upload-form .button' ).prop( 'disabled', true );
// A non-async request for an upload token.
media.ajax( 'videopress-get-upload-token', { async: false } ).done( function( response ) {
data = response;
data.success = true;
}).fail( function( response ) {
data = response;
data.success = false;
});
if ( ! data.success ) {
// Re-enable form elements.
this.$( '.videopress-upload-form .button' ).prop( 'disabled', false );
// Display an error message and cancel form submission.
this.error( data.message );
return false;
}
this.error( VideoPress.l10n.videoUploading, 'updated' );
// Set the form token.
this.$( 'input[name="videopress_blog_id"]' ).val( data.videopress_blog_id );
this.$( 'input[name="videopress_token"]' ).val( data.videopress_token );
this.$( '.videopress-upload-form' ).attr( 'action', data.videopress_action_url );
return true;
},
error: function( message, type ) {
type = type || 'error';
var div = $( '<div />' ).html( $( '<p />' ).text( message ) ).addClass( type );
this.$( '.videopress-errors' ).html( div );
return this;
},
success: function( message ) {
return this.error( message, 'updated' );
},
clearErrors: function() {
this.$( '.videopress-errors' ).html('');
return this;
},
messageHandler: function( event ) {
if ( ! event.origin.match( /\.wordpress\.com$/ ) ) {
return;
}
if ( event.data.indexOf && event.data.indexOf( 'vpUploadResult::' ) === 0 ) {
var result = JSON.parse( event.data.substr( 16 ) );
if ( ! result || ! result.code ) {
this.error( VideoPress.l10n.unknownError );
this.$( '.videopress-upload-form .button' ).prop( 'disabled', false );
return;
}
if ( 'success' === result.code && result.data ) {
var that = this, controller = this.controller,
state = controller.states.get( 'videopress' );
// Our new video has been added, so we need to reset the library.
// Since the Media API caches all queries, we add a random attribute
// to avoid the cache, then call more() to actually fetch the data.
state.set( 'library', media.query({ videopress:true, vp_random:Math.random() }) );
state.get( 'library' ).more().done(function(){
var model = state.get( 'library' ).get( result.data.attachment_id );
// Clear errors and select the uploaded item.
that.clearErrors();
state.get( 'selection' ).reset([ model ]);
controller.content.mode( 'browse' );
});
} else {
this.error( result.code );
// Re-enable form elements.
this.$( '.videopress-upload-form .button' ).prop( 'disabled', false );
}
}
}
});
/**
* Add a custom sync function that would add a few extra
* options for models which are VideoPress videos.
*/
var attachmentSync = media.model.Attachment.prototype.sync;
media.model.Attachment.prototype.sync = function( method, model, options ) {
if ( model.get( 'vp_isVideoPress' ) ) {
console.log( 'syncing ' + model.get( 'vp_guid' ) );
options.data = _.extend( options.data || {}, {
is_videopress: true,
vp_nonces: model.get( 'vp_nonces' )
} );
}
// Call the original sync routine.
return attachmentSync.apply( this, arguments );
};
/**
* Extend the default Attachment Details view. Check for vp_isVideoPress before
* adding anything to these methods.
*/
var AttachmentDetails = media.view.Attachment.Details;
media.view.Attachment.Details = AttachmentDetails.extend({
initialize: function() {
if ( this.model.get( 'vp_isVideoPress' ) ) {
_.extend( this.events, {
'click a.videopress-preview': 'vpPreview',
'change .vp-radio': 'vpRadioChange',
'change .vp-checkbox': 'vpCheckboxChange'
});
}
return AttachmentDetails.prototype.initialize.apply( this, arguments );
},
render: function() {
var r = AttachmentDetails.prototype.render.apply( this, arguments );
if ( this.model.get( 'vp_isVideoPress' ) ) {
var template = media.template( 'videopress-attachment' );
var options = this.model.toJSON();
options.can = {};
options.can.save = !! options.nonces.update;
this.$el.append( template( options ) );
}
return r;
},
// Handle radio buttons
vpRadioChange: function(e) {
$( e.target ).parents( '.vp-setting' ).find( '.vp-radio-text' ).val( e.target.value ).change();
},
// And checkboxes
vpCheckboxChange: function(e) {
$( e.target ).parents( '.vp-setting' ).find( '.vp-checkbox-text' ).val( Number( e.target.checked ) ).change();
},
vpPreview: function() {
VideoPressModal.render( this );
return this;
}
});
/**
* Don't display the uploader dropzone for the VideoPress router.
*/
var UploaderWindow = media.view.UploaderWindow;
media.view.UploaderWindow = UploaderWindow.extend({
show: function() {
if ( 'videopress' !== this.controller.state().get('id') ) {
UploaderWindow.prototype.show.apply( this, arguments );
}
return this;
}
});
/**
* Don't display the uploader in the attachments browser.
*/
var AttachmentsBrowser = media.view.AttachmentsBrowser;
media.view.AttachmentsBrowser = AttachmentsBrowser.extend({
/**
* Snag the Core 3.9.2 versions as a quick fix to avoid
* the breakage introduced by r29364-core
*/
updateContent: function() {
var view = this;
if( ! this.attachments ) {
this.createAttachments();
}
if ( ! this.collection.length ) {
this.toolbar.get( 'spinner' ).show();
this.collection.more().done(function() {
if ( ! view.collection.length ) {
view.createUploader();
}
view.toolbar.get( 'spinner' ).hide();
});
} else {
view.toolbar.get( 'spinner' ).hide();
}
},
/**
* Empty out to avoid breakage.
*/
toggleUploader: function() {},
createUploader: function() {
if ( 'videopress' !== this.controller.state().get('id') ) {
return AttachmentsBrowser.prototype.createUploader.apply( this, arguments );
}
}
});
/**
* Add VideoPress-specific methods for all frames.
*/
_.extend( media.view.MediaFrame.prototype, { VideoPress: { // this.VideoPress.method()
// When the VideoPress router is activated.
activate: function() {
var view = _.first( this.views.get( '.media-frame-router' ) ),
viewSettings = {};
if ( VideoPress.caps.read_videos ) {
viewSettings.browse = { text: VideoPress.l10n.VideoPressLibraryRouter, priority: 40 };
}
if ( VideoPress.caps.upload_videos ) {
viewSettings.upload_videopress = { text: VideoPress.l10n.uploadVideoRouter, priority: 20 };
}
view.set( viewSettings );
// Intercept and clear all incoming uploads
wp.Uploader.queue.on( 'add', this.VideoPress.disableUpload, this );
// Map the Upload Files view to the Upload a Video one (upload_videopress vs. upload)
if ( 'upload' === this.content.mode() && VideoPress.caps.upload_videos ) {
this.content.mode( 'upload_videopress' );
} else {
this.content.mode( 'browse' );
}
},
// When navigated away from the VideoPress router.
deactivate: function( /*view*/ ) {
wp.Uploader.queue.off( 'add', this.VideoPress.disableUpload );
},
// Disable dragdrop uploads in the VideoPress router.
disableUpload: function( attachment ) {
var uploader = this.uploader.uploader.uploader;
uploader.stop();
uploader.splice();
attachment.destroy();
},
// Runs on videopress:insert event fired by our custom toolbar
insert: function( selection ) {
var guid = selection.models[0].get( 'vp_guid' ).replace( /[^a-zA-Z0-9]+/, '' );
media.editor.insert( '[wpvideo ' + guid + ']' );
return this;
},
// Triggered by the upload_videopress router item.
uploadVideo: function() {
this.content.set( new media.view.VideoPressUploader({
controller: this
}) );
return this;
},
// Create a custom toolbar
createToolbar: function( /*toolbar*/ ) {
// Alow an option to hide the toolbar.
if ( this.options.VideoPress && this.options.VideoPress.hideToolbar ) {
return this;
}
var controller = this;
this.toolbar.set( new media.view.Toolbar({
controller: this,
items: {
insert: {
style: 'primary',
text: VideoPress.l10n.insertVideoButton,
priority: 80,
requires: {
library: true,
selection: true
},
click: function() {
var state = controller.state(),
selection = state.get('selection');
controller.close();
state.trigger( 'videopress:insert', selection ).reset();
}
}
}
}) );
}
}});
var MediaFrame = {};
/**
* Extend the selection media frame
*/
MediaFrame.Select = media.view.MediaFrame.Select;
media.view.MediaFrame.Select = MediaFrame.Select.extend({
bindHandlers: function() {
MediaFrame.Select.prototype.bindHandlers.apply( this, arguments );
this.on( 'router:create:videopress', this.createRouter, this );
this.on( 'router:activate:videopress', this.VideoPress.activate, this );
this.on( 'router:deactivate:videopress', this.VideoPress.deactivate, this );
this.on( 'content:render:upload_videopress', this.VideoPress.uploadVideo, this );
this.on( 'toolbar:create:videopress-toolbar', this.VideoPress.createToolbar, this );
this.on( 'videopress:insert', this.VideoPress.insert, this );
}
});
/**
* Extend the post editor media frame with our own
*/
MediaFrame.Post = media.view.MediaFrame.Post;
media.view.MediaFrame.Post = MediaFrame.Post.extend({
createStates: function() {
MediaFrame.Post.prototype.createStates.apply( this, arguments );
this.states.add([ new media.controller.VideoPress() ]);
}
});
/**
* A VideoPress Modal view that we can use to preview videos.
* Expects a controller object on render.
*/
var VideoPressModalView = Backbone.View.extend({
'className': 'videopress-modal-container',
'template': wp.media.template( 'videopress-media-modal' ),
// Render the VideoPress modal with a video object by guid.
render: function( controller ) {
this.delegateEvents( {
'click .videopress-modal-close': 'closeModal',
'click .videopress-modal-backdrop': 'closeModal'
} );
this.model = controller.model;
this.guid = this.model.get( 'vp_guid' );
if ( ! this.$frame ) {
this.$frame = $( '.media-frame-content' );
}
this.$el.html( this.template( { 'video' : this.model.get( 'vp_embed' ) } ) );
this.$modal = this.$( '.videopress-modal' );
this.$modal.hide();
this.$frame.append( this.$el );
this.$modal.slideDown( 'fast' );
return this;
},
closeModal: function() {
var view = this;
this.$modal.slideUp( 'fast', function() { view.remove(); } );
return this;
}
});
var VideoPressModal = new VideoPressModalView();
// Configuration screen behavior
$(document).on( 'ready', function() {
var $form = $( '#videopress-settings' );
// Not on a configuration screen
if ( ! $form.length ) {
return;
}
var $access = $form.find( 'input[name="videopress-access"]' ),
$upload = $form.find( 'input[name="videopress-upload"]' );
$access.on( 'change', function() {
var access = $access.filter( ':checked' ).val();
$upload.attr( 'disabled', ! access );
if ( ! access ) {
$upload.attr( 'checked', false );
}
});
$access.trigger( 'change' );
});
// Media -> VideoPress menu
$(document).on( 'click', '#videopress-browse', function() {
wp.media({
state: 'videopress',
states: [ new media.controller.VideoPress() ],
VideoPress: { hideToolbar: true }
}).open();
return false;
});
})(jQuery);

View file

@ -0,0 +1,255 @@
<?php
/**
* VideoPress Shortcode Handler
*
* This file may or may not be included from the Jetpack VideoPress module.
*/
/**
* 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
*/
function videopress_shortcode_callback( $attr ) {
global $content_width;
/**
* We only accept GUIDs as a first unnamed argument.
*/
$guid = $attr[0];
/**
* Make sure the GUID passed in matches how actual GUIDs are formatted.
*/
if ( ! videopress_is_valid_guid( $guid ) ) {
return '';
}
/**
* 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' );
/**
* Cast the attributes, post-input.
*/
$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;
}
/**
* If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`.
*/
if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) {
$attr['width'] = $content_width;
}
/**
* If the width isn't an even number, reduce it by one (making it even).
*/
if ( 1 === ( $attr['width'] % 2 ) ) {
$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(
'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' );
/**
* 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 );
/**
* 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' );
/**
* WordPress Shortcode Editor View JS Code
*/
function videopress_handle_editor_view_js() {
global $content_width;
$current_screen = get_current_screen();
if ( ! isset( $current_screen->id ) || $current_screen->base !== 'post' ) {
return;
}
add_action( 'admin_print_footer_scripts', 'videopress_editor_view_js_templates' );
wp_enqueue_script( 'videopress-editor-view', plugins_url( 'js/editor-view.js', __FILE__ ), array( 'wp-util', 'jquery' ), false, true );
wp_localize_script( 'videopress-editor-view', 'vpEditorView', array(
'home_url_host' => parse_url( home_url(), PHP_URL_HOST ),
'min_content_width' => VIDEOPRESS_MIN_WIDTH,
'content_width' => $content_width,
'modal_labels' => array(
'title' => esc_html__( 'VideoPress Shortcode', 'jetpack' ),
'guid' => esc_html__( 'Video ID', 'jetpack' ),
'w' => esc_html__( 'Video Width', 'jetpack' ),
'w_unit' => esc_html__( 'pixels', 'jetpack' ),
/* Translators: example of usage of this is "Start Video After 10 seconds" */
'at' => esc_html__( 'Start Video After', 'jetpack' ),
'at_unit' => esc_html__( 'seconds', 'jetpack' ),
'hd' => esc_html__( 'High definition on by default', 'jetpack' ),
'permalink' => esc_html__( 'Link the video title to its URL on VideoPress.com', 'jetpack' ),
'autoplay' => esc_html__( 'Autoplay video on page load', 'jetpack' ),
'loop' => esc_html__( 'Loop video playback', 'jetpack' ),
'freedom' => esc_html__( 'Use only Open Source codecs (may degrade performance)', 'jetpack' ),
'flashonly' => esc_html__( 'Use legacy Flash Player (not recommended)', 'jetpack' ),
)
) );
add_editor_style( plugins_url( 'videopress-editor-style.css', __FILE__ ) );
}
add_action( 'admin_notices', 'videopress_handle_editor_view_js' );
/**
* WordPress Editor Views
*/
function videopress_editor_view_js_templates() {
/**
* This template uses the following parameters, and displays the video as an iframe:
* - data.guid // The guid of the video.
* - data.width // The width of the iframe.
* - data.height // The height of the iframe.
* - data.urlargs // Arguments serialized into a get string.
*
* In addition, the calling script will need to ensure that the following
* JS file is added to the header of the editor iframe:
* - https://s0.wp.com/wp-content/plugins/video/assets/js/next/videopress-iframe.js
*/
?>
<script type="text/html" id="tmpl-videopress_iframe_vnext">
<div class="tmpl-videopress_iframe_next" style="max-height:{{ data.height }}px;">
<div class="videopress-editor-wrapper" style="padding-top:{{ data.ratio }}%;">
<iframe style="display: block;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
</div>
</div>
</script>
<!-- VideoPress Settings Modal style overrides -->
<style type="text/css">
.mce-videopress-field-guid, .mce-videopress-field-freedom, .mce-videopress-field-flashonly {
display: none;
}
.mce-videopress-checkbox .mce-checkbox {
left: 120px !important;
width: 100% !important; /* assigning a full width so the label area is clickable */
}
.mce-videopress-checkbox .mce-label {
left: 150px !important;
}
.mce-videopress-checkbox .mce-label-unit {
position: absolute;
left: 210px;
top: 5px;
}
.mce-videopress-checkbox i.mce-i-checkbox {
background-color: #fff;
color: #1e8cbe;
}
.mce-videopress-checkbox .mce-i-checkbox:before {
display: inline-block;
vertical-align: middle;
width: 16px;
font: 400 21px/1 dashicons;
speak: none;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
margin: -3px 0 0 -3px;
content: "\f147";
}
.mce-videopress-checkbox .mce-i-checkbox.mce-checked:before {
content: "\f147";
}
div[class*=mce-videopress-field] input[type=number] {
width: 70px !important;
left: 120px !important;
}
.mce-videopress-field-w .mce-label,
.mce-videopress-field-at .mce-label {
width: 115px !important;
text-align: right;
}
.mce-videopress-field-unit {
position: absolute;
left: 210px;
top: 5px;
}
</style>
<?php
}

View file

@ -0,0 +1,99 @@
<?php
/**
* Validate user-supplied guid values against expected inputs
*
* @since 1.1
* @param string $guid video identifier
* @return bool true if passes validation test
*/
function videopress_is_valid_guid( $guid ) {
if ( ! empty( $guid ) && strlen( $guid ) === 8 && ctype_alnum( $guid ) ) {
return true;
}
return false;
}
/**
* Get details about a specific video by GUID:
*
* @param $guid string
* @return object
*/
function videopress_get_video_details( $guid ) {
if ( ! videopress_is_valid_guid( $guid ) ) {
return new WP_Error( 'bad-guid-format', __( 'Invalid Video GUID!', 'jetpack' ) );
}
$version = '1.1';
$endpoint = sprintf( '/videos/%1$s', $guid );
$response = wp_remote_get( sprintf( 'https://public-api.wordpress.com/rest/v%1$s%2$s', $version, $endpoint ) );
$data = json_decode( wp_remote_retrieve_body( $response ) );
/**
* Allow functions to modify fetched video details.
*
* This filter allows third-party code to modify the return data
* about a given video. It may involve swapping some data out or
* adding new parameters.
*
* @since 4.0.0
*
* @param object $data The data returned by the WPCOM API. See: https://developer.wordpress.com/docs/api/1.1/get/videos/%24guid/
* @param string $guid The GUID of the VideoPress video in question.
*/
return apply_filters( 'videopress_get_video_details', $data, $guid );
}
/**
* Get an attachment ID given a URL.
*
* Modified from http://wpscholar.com/blog/get-attachment-id-from-wp-image-url/
*
* @todo: Add some caching in here.
*
* @param string $url
*
* @return int|bool Attachment ID on success, false on failure
*/
function videopress_get_attachment_id_by_url( $url ) {
$wp_upload_dir = wp_upload_dir();
// Strip out protocols, so it doesn't fail because searching for http: in https: dir.
$dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' );
// Is URL in uploads directory?
if ( false !== strpos( $url, $dir ) ) {
$file = basename( $url );
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_wp_attachment_metadata',
'compare' => 'LIKE',
'value' => $file,
),
)
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $attachment_id ) {
$meta = wp_get_attachment_metadata( $attachment_id );
$original_file = basename( $meta['file'] );
$cropped_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_files ) ) {
return (int) $attachment_id;
}
}
}
}
return false;
}

View file

@ -0,0 +1,97 @@
/**
* VideoPress admin media styles
*/
.videopress-modal-backdrop {
background: #000;
opacity: 0.7;
position: absolute;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: 100;
}
.videopress-modal {
padding: 10px 20px;
background: white;
position: absolute;
top: 0;
width: 440px;
overflow: hidden;
right: 50%;
margin-right: -220px;
z-index: 101;
box-shadow: -2px 2px 5px 2px rgba( 0, 0, 0, 0.5 );
-webkit-border-bottom-left-radius: 2px;
-webkit-border-bottom-right-radius: 2px;
border-bottom-left-radius: 2px;
border-bottom-right-radius: 2px;
}
.videopress-modal .submit {
text-align: left;
padding: 10px 0 5px;
}
.videopress-preview {
display: block;
float: left;
width: 65%;
margin-top: 18px;
background: black;
min-height: 97px;
text-decoration: none;
}
.vp-preview span.videopress-preview-unavailable {
width: 65%;
float: left;
text-align: right;
margin-left: 0;
}
.videopress-preview img {
float: right;
width: 100%;
}
.videopress-preview span {
display: block;
padding-top: 40px;
color: white !important;
text-align: center;
}
.vp-setting .help {
margin: 0 35% 4px 0;
}
.media-sidebar .vp-setting input[type="checkbox"] {
float: right;
margin-top: 10px;
}
.vp-setting label {
float: right;
margin: 8px 5px 0 8px;
max-width: 135px;
}
.vp-setting input[type='radio'] {
float: right;
margin-top: 9px;
width: auto;
}
.vp-preview span {
margin-top: 18px;
}
.uploader-videopress {
margin: 16px;
}
.uploader-videopress .videopress-errors div {
margin: 16px 0;
}

View file

@ -0,0 +1 @@
.videopress-modal-backdrop{background:#000;opacity:.7;position:absolute;top:0;width:100%;height:100%;overflow:hidden;z-index:100}.videopress-modal{padding:10px 20px;background:#fff;position:absolute;top:0;width:440px;overflow:hidden;right:50%;margin-right:-220px;z-index:101;box-shadow:-2px 2px 5px 2px rgba(0,0,0,.5);-webkit-border-bottom-left-radius:2px;-webkit-border-bottom-right-radius:2px;border-bottom-left-radius:2px;border-bottom-right-radius:2px}.videopress-modal .submit{text-align:left;padding:10px 0 5px}.videopress-preview{display:block;float:left;width:65%;margin-top:18px;background:#000;min-height:97px;text-decoration:none}.vp-preview span.videopress-preview-unavailable{width:65%;float:left;text-align:right;margin-left:0}.videopress-preview img{float:right;width:100%}.videopress-preview span{display:block;padding-top:40px;color:#fff!important;text-align:center}.vp-setting .help{margin:0 35% 4px 0}.media-sidebar .vp-setting input[type=checkbox]{float:right;margin-top:10px}.vp-setting label{float:right;margin:8px 5px 0 8px;max-width:135px}.vp-setting input[type=radio]{float:right;margin-top:9px;width:auto}.vp-preview span{margin-top:18px}.uploader-videopress{margin:16px}.uploader-videopress .videopress-errors div{margin:16px 0}

View file

@ -0,0 +1,97 @@
/**
* VideoPress admin media styles
*/
.videopress-modal-backdrop {
background: #000;
opacity: 0.7;
position: absolute;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: 100;
}
.videopress-modal {
padding: 10px 20px;
background: white;
position: absolute;
top: 0;
width: 440px;
overflow: hidden;
left: 50%;
margin-left: -220px;
z-index: 101;
box-shadow: 2px 2px 5px 2px rgba( 0, 0, 0, 0.5 );
-webkit-border-bottom-right-radius: 2px;
-webkit-border-bottom-left-radius: 2px;
border-bottom-right-radius: 2px;
border-bottom-left-radius: 2px;
}
.videopress-modal .submit {
text-align: right;
padding: 10px 0 5px;
}
.videopress-preview {
display: block;
float: right;
width: 65%;
margin-top: 18px;
background: black;
min-height: 97px;
text-decoration: none;
}
.vp-preview span.videopress-preview-unavailable {
width: 65%;
float: right;
text-align: left;
margin-right: 0;
}
.videopress-preview img {
float: left;
width: 100%;
}
.videopress-preview span {
display: block;
padding-top: 40px;
color: white !important;
text-align: center;
}
.vp-setting .help {
margin: 0 0 4px 35%;
}
.media-sidebar .vp-setting input[type="checkbox"] {
float: left;
margin-top: 10px;
}
.vp-setting label {
float: left;
margin: 8px 8px 0 5px;
max-width: 135px;
}
.vp-setting input[type='radio'] {
float: left;
margin-top: 9px;
width: auto;
}
.vp-preview span {
margin-top: 18px;
}
.uploader-videopress {
margin: 16px;
}
.uploader-videopress .videopress-errors div {
margin: 16px 0;
}

View file

@ -0,0 +1 @@
.videopress-modal-backdrop{background:#000;opacity:.7;position:absolute;top:0;width:100%;height:100%;overflow:hidden;z-index:100}.videopress-modal{padding:10px 20px;background:#fff;position:absolute;top:0;width:440px;overflow:hidden;left:50%;margin-left:-220px;z-index:101;box-shadow:2px 2px 5px 2px rgba(0,0,0,.5);-webkit-border-bottom-right-radius:2px;-webkit-border-bottom-left-radius:2px;border-bottom-right-radius:2px;border-bottom-left-radius:2px}.videopress-modal .submit{text-align:right;padding:10px 0 5px}.videopress-preview{display:block;float:right;width:65%;margin-top:18px;background:#000;min-height:97px;text-decoration:none}.vp-preview span.videopress-preview-unavailable{width:65%;float:right;text-align:left;margin-right:0}.videopress-preview img{float:left;width:100%}.videopress-preview span{display:block;padding-top:40px;color:#fff!important;text-align:center}.vp-setting .help{margin:0 0 4px 35%}.media-sidebar .vp-setting input[type=checkbox]{float:left;margin-top:10px}.vp-setting label{float:left;margin:8px 8px 0 5px;max-width:135px}.vp-setting input[type=radio]{float:left;margin-top:9px;width:auto}.vp-preview span{margin-top:18px}.uploader-videopress{margin:16px}.uploader-videopress .videopress-errors div{margin:16px 0}

View file

@ -0,0 +1,21 @@
/**
* VideoPress styles for Editor
*/
.videopress-editor-wrapper {
position: relative;
max-width: 100%;
padding: 56.25% 0 0;
height: 0;
overflow: hidden;
}
.tmpl-videopress_iframe_next iframe {
position: absolute;
top: 0;
left: 0;
max-width: 100%;
max-height: 100%;
}
body.rtl .tmpl-videopress_iframe_next iframe {
left: auto;
right: 0;
}

View file

@ -0,0 +1,737 @@
<?php
/**
* VideoPress in Jetpack
*
*/
class Jetpack_VideoPress {
public $module = 'videopress';
public $option_name = 'videopress';
public $version = 4;
/**
* Singleton
*/
public static function init() {
static $instance = false;
if ( ! $instance )
$instance = new Jetpack_VideoPress;
return $instance;
}
function __construct() {
$this->version = time(); // <s>ghost</s> cache busters!
add_action( 'init', array( $this, 'on_init' ) );
add_action( 'jetpack_activate_module_videopress', array( $this, 'jetpack_module_activated' ) );
add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
}
/**
* Fires on init since is_connection_owner should wait until the user is initialized by $wp->init();
*/
function on_init() {
$options = $this->get_options();
// Only the connection owner can configure this module.
if ( $this->is_connection_owner() ) {
Jetpack::enable_module_configurable( $this->module );
Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) );
Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) );
}
// Only if the current user can manage the VideoPress library and one has been connected.
if ( $this->can( 'read_videos' ) && $options['blog_id'] ) {
add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
// Load these at priority -1 so they're fired before Core's are.
add_action( 'wp_ajax_query-attachments', array( $this, 'wp_ajax_query_attachments' ), -1 );
add_action( 'wp_ajax_save-attachment', array( $this, 'wp_ajax_save_attachment' ), -1 );
add_action( 'wp_ajax_save-attachment-compat', array( $this, 'wp_ajax_save_attachment' ), -1 );
add_action( 'wp_ajax_delete-post', array( $this, 'wp_ajax_delete_post' ), -1 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
if ( $this->can( 'upload_videos' ) && $options['blog_id'] ) {
add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
}
add_filter( 'videopress_shortcode_options', array( $this, 'videopress_shortcode_options' ) );
}
function wp_ajax_videopress_get_upload_token() {
if ( ! $this->can( 'upload_videos' ) )
return wp_send_json_error();
$result = $this->query( 'jetpack.vpGetUploadToken' );
if ( is_wp_error( $result ) )
return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
$response = $result;
if ( empty( $response['videopress_blog_id'] ) || empty( $response['videopress_token'] ) || empty( $response[ 'videopress_action_url' ] ) )
return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
return wp_send_json_success( $response );
}
/**
* Get VideoPress options
*/
function get_options() {
$defaults = array(
'blogs' => array(),
'blog_id' => 0,
'access' => '',
'allow-upload' => false,
'freedom' => false,
'hd' => false,
'meta' => array(
'max_upload_size' => 0,
),
);
$options = Jetpack_Options::get_option( $this->option_name, array() );
// If options have not been saved yet, check for older VideoPress plugin options.
if ( empty( $options ) ) {
$options['freedom'] = (bool) get_option( 'video_player_freedom', false );
$options['hd'] = (bool) get_option( 'video_player_high_quality', false );
}
$options = array_merge( $defaults, $options );
return $options;
}
/**
* Update VideoPress options
*/
function update_options( $options ) {
Jetpack_Options::update_option( $this->option_name, $options );
}
/**
* Runs when the VideoPress module is activated.
*/
function jetpack_module_activated() {
if ( ! $this->is_connection_owner() )
return;
$options = $this->get_options();
// Ask WordPress.com for a list of VideoPress blogs
$result = $this->query( 'jetpack.vpGetBlogs' );
if ( ! is_wp_error( $result ) )
$options['blogs'] = $result;
// If there's at least one available blog, let's use it.
if ( is_array( $options['blogs'] ) && count( $options['blogs'] ) > 0 )
$options['blog_id'] = $options['blogs'][0]['blog_id'];
$this->update_options( $options );
}
/**
* Runs when the VideoPress module is deactivated.
*/
function jetpack_module_deactivated() {
Jetpack_Options::delete_option( $this->option_name );
}
/**
* Remote Query
*
* Performs a remote XML-RPC query using Jetpack's IXR Client. And also
* appends some useful stuff about this setup to the query.
*
* @return the Jetpack_IXR_Client object after querying.
*/
function query( $method, $args = null ) {
$options = $this->get_options();
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client( array(
'user_id' => JETPACK_MASTER_USER, // All requests are on behalf of the connection owner.
) );
$params = array(
'args' => $args,
'video_blog_id' => $options['blog_id'],
'caps' => array(),
);
// Let Jetpack know about our local caps.
foreach ( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
if ( $this->can( $cap ) )
$params['caps'][] = $cap;
$xml->query( $method, $params );
if ( $xml->isError() )
return new WP_Error( 'xml_rpc_error', 'An XML-RPC error has occurred.' );
$response = $xml->getResponse();
// If there's any metadata with the response, save it for future use.
if ( is_array( $response ) && isset( $response['meta'] ) ) {
$options = $this->get_options();
if ( $response['meta'] !== $options['meta'] ) {
$options['meta'] = array_merge( $options['meta'], $response['meta'] );
$this->update_options( $options );
}
}
if ( is_array( $response ) && isset( $response['result'] ) )
return $response['result'];
return $response;
}
/**
* Runs before the VideoPress Configuration screen loads, useful
* to update options and yield errors.
*/
function jetpack_configuration_load() {
$this->enqueue_admin_scripts();
/**
* Save configuration
*/
if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'videopress-save' ) {
check_admin_referer( 'videopress-settings' );
$options = $this->get_options();
if ( isset( $_POST['blog_id'] ) && in_array( $_POST['blog_id'], wp_list_pluck( $options['blogs'], 'blog_id' ) ) )
$options['blog_id'] = $_POST['blog_id'];
// Allow the None setting too.
if ( isset( $_POST['blog_id'] ) && $_POST['blog_id'] == 0 )
$options['blog_id'] = 0;
/**
* @see $this->can()
*/
if ( isset( $_POST['videopress-access'] ) && in_array( $_POST['videopress-access'], array( '', 'read', 'edit', 'delete' ) ) )
$options['access'] = $_POST['videopress-access'];
$options['freedom'] = isset( $_POST['videopress-freedom'] );
$options['hd'] = isset( $_POST['videopress-hd'] );
// Allow upload only if some level of access has been granted, and uploads were allowed.
$options['allow-upload'] = false;
if ( ! empty( $options['access'] ) && isset( $_POST['videopress-upload'] ) )
$options['allow-upload'] = true;
$this->update_options( $options );
Jetpack::state( 'message', 'module_configured' );
wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
}
/**
* Refresh the list of available WordPress.com blogs
*/
if ( ! empty( $_GET['videopress'] ) && $_GET['videopress'] == 'refresh-blogs' ) {
check_admin_referer( 'videopress-settings' );
$options = $this->get_options();
$result = $this->query( 'jetpack.vpGetBlogs' );
if ( ! is_wp_error( $result ) ) {
$options['blogs'] = $result;
$this->update_options( $options );
}
wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
}
}
/**
* Renders the VideoPress Configuration screen in Jetpack.
*/
function jetpack_configuration_screen() {
$options = $this->get_options();
$refresh_url = wp_nonce_url( add_query_arg( 'videopress', 'refresh-blogs' ), 'videopress-settings' );
?>
<div class="narrow">
<form method="post" id="videopress-settings">
<input type="hidden" name="action" value="videopress-save" />
<?php wp_nonce_field( 'videopress-settings' ); ?>
<table id="menu" class="form-table">
<tr>
<th scope="row" colspan="2">
<p><?php _e( 'Please note that the VideoPress module requires a WordPress.com account with an active <a href="http://store.wordpress.com/premium-upgrades/videopress/" target="_blank">VideoPress subscription</a>.', 'jetpack' ); ?></p>
</th>
</tr>
<tr>
<th scope="row">
<label><?php _e( 'Connected WordPress.com Blog', 'jetpack' ); ?></label>
</th>
<td>
<select name="blog_id">
<option value="0" <?php selected( $options['blog_id'], 0 ); ?>> <?php esc_html_e( 'None', 'jetpack' ); ?></option>
<?php foreach ( $options['blogs'] as $blog ) : ?>
<option value="<?php echo absint( $blog['blog_id'] ); ?>" <?php selected( $options['blog_id'], $blog['blog_id'] ); ?>><?php echo esc_html( $blog['name'] ); ?> (<?php echo esc_html( $blog['domain'] ); ?>)</option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e( 'Only videos from the selected blog will be available in your media library.', 'jetpack' ); ?>
<?php printf( __( '<a href="%s">Click here</a> to refresh this list.', 'jetpack' ), esc_url( $refresh_url ) ); ?>
</p>
</td>
</tr>
<tr>
<th scope="row">
<label><?php _e( 'Video Library Access', 'jetpack' ); ?></label>
</th>
<td>
<label><input type="radio" name="videopress-access" value="" <?php checked( '', $options['access'] ); ?> />
<?php _e( 'Do not allow other users to access my VideoPress library', 'jetpack' ); ?></label><br/>
<label><input type="radio" name="videopress-access" value="read" <?php checked( 'read', $options['access'] ); ?> />
<?php _e( 'Allow users to access my videos', 'jetpack' ); ?></label><br/>
<label><input type="radio" name="videopress-access" value="edit" <?php checked( 'edit', $options['access'] ); ?> />
<?php _e( 'Allow users to access and edit my videos', 'jetpack' ); ?></label><br/>
<label><input type="radio" name="videopress-access" value="delete" <?php checked( 'delete', $options['access'] ); ?> />
<?php _e( 'Allow users to access, edit, and delete my videos', 'jetpack' ); ?></label><br/><br />
<label><input type="checkbox" name="videopress-upload" value="1" <?php checked( $options['allow-upload'] ); ?> />
<?php _e( 'Allow users to upload videos', 'jetpack' ); ?></label><br />
</td>
</tr>
<tr>
<th scope="row">
<label for="videopress-freedom"><?php _e( 'Free formats', 'jetpack' ); ?></label>
</th>
<td>
<label><input type="checkbox" name="videopress-freedom" id="videopress-freedom" <?php checked( $options['freedom'] ); ?> />
<?php _e( 'Only display videos in free software formats', 'jetpack' ); ?></label>
<p class="description"><?php _e( 'Ogg file container with Theora video and Vorbis audio. Note that some browsers are unable to play free software video formats, including Internet Explorer and Safari.', 'jetpack' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="videopress-hd"><?php _e( 'Default quality', 'jetpack' ); ?></label>
</th>
<td>
<label><input type="checkbox" name="videopress-hd" id="videopress-hd" <?php checked( $options['hd'] ); ?> />
<?php _e( 'Display higher quality video by default.', 'jetpack' ); ?></label>
<p class="description"><?php _e( 'This setting may be overridden for individual videos.', 'jetpack' ); ?></p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function admin_menu() {
add_media_page( __( 'VideoPress Library', 'jetpack' ), __( 'VideoPress', 'jetpack' ), 'upload_files', 'videopress-library', array( $this, 'admin_menu_library' ) );
}
function admin_menu_library() {
wp_enqueue_media();
$this->enqueue_admin_scripts();
?>
<div class="wrap" style="max-width: 600px;">
<?php screen_icon(); ?>
<h2><?php _e( 'VideoPress Library', 'jetpack' ); ?></h2>
<p><?php _e( 'Use the button below to browse your VideoPress Library. Note that you can also browse your VideoPress Library while editing a post or page by using the <strong>Add Media</strong> button in the post editor.', 'jetpack' ); ?></p>
<p class="hide-if-no-js"><a href="#" id="videopress-browse" class="button"><?php _e( 'Browse Your VideoPress Library', 'jetpack' ); ?></a></p>
<p class="hide-if-js description"><?php _e( 'Please enable JavaScript support in your browser to use VideoPress.', 'jetpack' ); ?></p>
</div>
<?php
}
/**
* A can of coke
*
* Similar to current_user_can, but internal to VideoPress. Returns
* true if the given VideoPress capability is allowed by the given user.
*/
function can( $cap, $user_id = false ) {
if ( ! $user_id )
$user_id = get_current_user_id();
// Connection owners are allowed to do all the things.
if ( $this->is_connection_owner( $user_id ) )
return true;
/**
* The access setting can be set by the connection owner, to allow sets
* of operations to other site users. Each access value corresponds to
* an array of things they can do.
*/
$options = $this->get_options();
$map = array(
'read' => array( 'read_videos' ),
'edit' => array( 'read_videos', 'edit_videos' ),
'delete' => array( 'read_videos', 'edit_videos', 'delete_videos' ),
);
if ( ! array_key_exists( $options['access'], $map ) )
return false;
if ( ! in_array( $cap, $map[ $options['access'] ] ) && 'upload_videos' != $cap )
return false;
// Additional and intrenal caps checks
if ( ! user_can( $user_id, 'upload_files' ) )
return false;
if ( 'edit_videos' == $cap && ! user_can( $user_id, 'edit_others_posts' ) )
return false;
if ( 'delete_videos' == $cap && ! user_can( $user_id, 'delete_others_posts' ) )
return false;
if ( 'upload_videos' == $cap && ! $options['allow-upload'] )
return false;
return true;
}
/**
* Returns true if the provided user is the Jetpack connection owner.
*/
function is_connection_owner( $user_id = false ) {
if ( ! $user_id )
$user_id = get_current_user_id();
$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && $user_id === $user_token->external_user_id;
}
/**
* Our custom AJAX callback for the query-attachments action
* used in the media modal. By-passed if not for VideoPress.
*/
function wp_ajax_query_attachments() {
// Watch for VideoPress calls
if ( ! isset( $_POST['query']['videopress'] ) )
return;
if ( ! $this->can( 'read_videos' ) )
return wp_send_json_error( 'permission denied' );
// Get and sanitize query arguments.
$query_args = $this->sanitize_wp_query_args( $_POST['query'] );
// Fire a remote WP_Query
$result = $this->query( 'jetpack.vpQuery', $query_args );
if ( is_wp_error( $result ) )
return wp_send_json_error( 'xml rpc request error' );
$items = $result;
foreach ( $items as $key => $item ) {
// Check local permissions
if ( ! $this->can( 'edit_videos' ) )
unset( $item['vp_nonces']['update'] );
if ( ! $this->can( 'delete_videos' ) )
unset( $item['vp_nonces']['delete'] );
// Add a second pair of nonces for the .org blog.
$item['nonces'] = array();
if ( ! empty( $item['vp_nonces']['update'] ) )
$item['nonces']['update'] = wp_create_nonce( 'update-videopress-post_' . $item['id'] );
if ( ! empty( $item['vp_nonces']['delete'] ) )
$item['nonces']['delete'] = wp_create_nonce( 'delete-videopress-post_' . $item['id'] );
$item['vp_embed'] = videopress_shortcode_callback( array(
$item['vp_guid'],
'autoplay' => true,
'flashonly' => true,
'w' => 440,
) );
$items[ $key ] = $item;
}
wp_send_json_success( $items );
}
/**
* Sanitize user-provided WP_Query arguments
*
* These might be sent to the VideoPress server, for a remote WP_Query
* call so let's make sure they're sanitized and safe to send.
*/
function sanitize_wp_query_args( $args ) {
$args = shortcode_atts( array(
'posts_per_page' => 40,
'orderby' => 'date',
'order' => 'desc',
'paged' => 1,
's' => '',
), (array) $args, 'wpvideo' );
$args['posts_per_page'] = absint( $args['posts_per_page'] );
$args['orderby'] = strtolower( $args['orderby'] );
$args['orderby'] = ( in_array( $args['orderby'], array( 'date' ) ) ) ? $args['orderby'] : 'date';
$args['order'] = strtolower( $args['order'] );
$args['order'] = ( in_array( $args['order'], array( 'asc', 'desc' ) ) ) ? $args['order'] : 'desc';
$args['paged'] = absint( $args['paged'] );
$args['s'] = sanitize_text_field( $args['s'] );
return $args;
}
/**
* Custom AJAX callback for the save-attachment action. If the request was
* not for a VideoPress object, core's fallback action will kick in.
*/
function wp_ajax_save_attachment() {
if ( ! isset( $_POST['is_videopress'] ) )
return;
if ( ! $this->can( 'edit_videos' ) )
return wp_send_json_error( 'permission denied' );
$post_id = 0;
if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
wp_send_json_error();
if ( ! isset( $_POST['vp_nonces']['update'] ) )
wp_send_json_error();
check_ajax_referer( 'update-videopress-post_' . $post_id, 'nonce' );
$changes = ( ! empty( $_POST['changes'] ) ) ? (array) $_POST['changes'] : array();
$changes = shortcode_atts( array(
'title' => null,
'caption' => null,
'description' => null,
'vp_share' => null,
'vp_rating' => null,
), $changes, 'wpvideo' );
if ( ! is_null( $changes['vp_share'] ) )
$changes['vp_share'] = (bool) $changes['vp_share'];
if ( ! is_null( $changes['vp_rating'] ) )
$changes['vp_rating'] = ( array_key_exists( $changes['vp_rating'], $this->get_available_ratings() ) ) ? $changes['vp_rating'] : null;
// Remove null-values
foreach ( $changes as $key => $value )
if ( is_null( $value ) )
unset( $changes[ $key ] );
$result = $this->query( 'jetpack.vpSaveAttachment', array(
'post_id' => $post_id,
'changes' => $changes,
'nonce' => $_POST['vp_nonces']['update'],
) );
if ( is_wp_error( $result ) )
return wp_send_json_error( 'xml rpc request error' );
wp_send_json_success();
}
/**
* Custom AJAX callback for the delete-post action, only for VideoPress objects.
*/
function wp_ajax_delete_post() {
if ( ! isset( $_POST['is_videopress'] ) )
return;
if ( ! $this->can( 'delete_videos' ) )
return wp_send_json_error( 'permission denied' );
$post_id = 0;
if ( ! isset( $_POST['id'] ) || ! $post_id = absint( $_POST['id'] ) )
wp_send_json_error();
if ( ! isset( $_POST['vp_nonces']['delete'] ) )
wp_send_json_error();
check_ajax_referer( 'delete-videopress-post_' . $post_id );
$result = $this->query( 'jetpack.vpDeleteAttachment', array(
'post_id' => $post_id,
'nonce' => $_POST['vp_nonces']['delete'],
) );
if ( is_wp_error( $result ) )
return wp_send_json_error( 'xml rpc request error' );
wp_send_json_success();
}
/**
* Register VideoPress admin scripts.
*/
function enqueue_admin_scripts() {
if ( did_action( 'videopress_enqueue_admin_scripts' ) )
return;
wp_enqueue_script( 'videopress-admin', plugins_url( 'js/videopress-admin.js', __FILE__ ), array( 'jquery', 'media-views', 'media-models' ), $this->version );
wp_enqueue_style( 'videopress-admin', plugins_url( 'videopress-admin.css', __FILE__ ), array(), $this->version );
$caps = array();
foreach( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
$caps[ $cap ] = $this->can( $cap );
$l10n = array(
'selectVideoFile' => __( 'Please select a video file to upload.', 'jetpack' ),
'videoUploading' => __( 'Your video is uploading... Please do not close this window.', 'jetpack' ),
'unknownError' => __( 'An unknown error has occurred. Please try again later.', 'jetpack' ),
'videoUploaded' => __( 'Your video has successfully been uploaded. It will appear in your VideoPress Library shortly.', 'jetpack' ),
'VideoPressLibraryRouter' => __( 'VideoPress Library', 'jetpack' ),
'uploadVideoRouter' => __( 'Upload a Video', 'jetpack' ),
'insertVideoButton' => __( 'Insert Video', 'jetpack' ),
);
wp_localize_script( 'videopress-admin', 'VideoPressAdminSettings', array(
'caps' => $caps,
'l10n' => $l10n,
) );
/**
* Fires after VideoPress scripts are enqueued in the dashboard.
*
* @since 2.5.0
*/
do_action( 'videopress_enqueue_admin_scripts' );
}
/**
* Get an array of available ratings. Keys are options, values are labels.
*/
function get_available_ratings() {
return array(
'G' => 'G',
'PG-13' => 'PG-13',
'R-17' => 'R',
'X-18' => 'X',
);
}
/**
* Additional VideoPress media templates.
*/
function print_media_templates() {
$options = $this->get_options();
?>
<script type="text/html" id="tmpl-videopress-attachment">
<# if ( data.vp_ogg_url ) { #>
<label class="setting vp-setting">
<span><?php _e( 'Ogg File URL', 'jetpack' ); ?></span>
<input type="text" value="{{ data.vp_ogg_url }}" onclick="this.focus();this.select();" readonly />
<p class="help"><?php _e( 'Location of the Ogg video file.', 'jetpack' ); ?></p>
</label>
<# } #>
<label class="setting vp-setting">
<span><?php _e( 'Share', 'jetpack' ); ?></span>
<input class="vp-checkbox" type="checkbox" <# if ( '1' === data.vp_share ) { #>checked<# } #> <# if ( ! data.can.save ) { #>disabled<# } #> />
<label>
<?php _e( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ); ?>
</label>
<input class="vp-checkbox-text" type="text" value="{{ data.vp_share }}" data-setting="vp_share" style="display:none;" />
</label>
<label class="setting vp-setting">
<span><?php _e( 'Rating', 'jetpack' ); ?></span>
<?php foreach ( $this->get_available_ratings() as $value => $label ) : ?>
<input class="vp-radio" type="radio" name="vp-radio-group" id="vp-rating-<?php echo sanitize_html_class( $value ); ?>" value="<?php echo esc_attr( $value ); ?>"
<# if ( '<?php echo esc_attr( $value ); ?>' === data.vp_rating ) { #>checked<# } #>
<# if ( ! data.can.save ) { #>disabled<# } #> />
<label for="vp-rating-<?php echo sanitize_html_class( $value ); ?>"><?php echo esc_html( $label ); ?></label>
<?php endforeach; ?>
<input class="vp-radio-text" type="text" value="{{ data.vp_rating }}" data-setting="vp_rating" style="display:none;" />
</label>
<label class="setting vp-setting">
<span><?php _e( 'Shortcode', 'jetpack' ); ?></span>
<input type="text" value="[wpvideo {{ data.vp_guid }}]" onclick="this.focus();this.select();" readonly />
</label>
<label class="setting vp-setting vp-preview">
<span><?php _e( 'Preview', 'jetpack' ); ?></span>
<# if ( ! data.vp_thumbnail_url ) { #>
<span class="videopress-preview-unavailable"><?php esc_html_e( 'The preview is unavailable while this video is being processed.', 'jetpack' ); ?></span>
<# } else { #>
<a href="#" class="videopress-preview" id="videopress-thumbnail-{{ data.vp_guid }}" data-videopress-guid="{{ data.vp_guid }}"><img src="{{ data.vp_thumbnail_url }}" /></a>
<# } #>
</label>
</script>
<script type="text/html" id="tmpl-videopress-media-modal">
<div class="videopress-modal">
<p><?php _e( 'Video Preview:', 'jetpack' ); ?></p>
<div class="videopress-video-container">{{{ data.video }}}</div>
<p class="submit">
<a class="videopress-modal-close button" href="#"><?php _e( 'Close', 'jetpack' ); ?></a>
</p>
</div>
<div class="videopress-modal-backdrop"></div>
</script>
<script type="text/html" id="tmpl-videopress-uploader">
<div class="videopress-errors"></div>
<form class="videopress-upload-form" action="" method="post" target="videopress_upload_frame" enctype="multipart/form-data">
<input type="hidden" name="action" value="videopress_upload" />
<input type="hidden" name="videopress_blog_id" value="0" />
<input type="hidden" name="videopress_token" value="0" />
<?php $formats = 'ogv, mp4, m4v, mov, wmv, avi, mpg, 3gp, 3g2'; ?>
<?php
$max_upload_size = 0;
if ( ! empty( $options['meta']['max_upload_size'] ) )
$max_upload_size = absint( $options['meta']['max_upload_size'] );
$upload_size_unit = $max_upload_size;
$byte_sizes = array( 'KB', 'MB', 'GB' );
for ( $u = -1; $upload_size_unit > 1024 && $u < count( $byte_sizes ) - 1; $u++ )
$upload_size_unit /= 1024;
if ( $u < 0 ) {
$upload_size_unit = 0;
$u = 0;
} else {
$upload_size_unit = (int) $upload_size_unit;
}
?>
<p><?php printf( __( 'Use the form below to upload a video to your VideoPress Library. The following video formats are supported: %s. Maximum upload file size is %d%s.', 'jetpack' ), esc_html( $formats ), esc_html( $upload_size_unit ), esc_html( $byte_sizes[ $u ] ) ); ?></p>
<input type="file" name="videopress_file" />
<?php submit_button( __( 'Upload Video', 'jetpack' ) ); ?>
</form>
<iframe width="0" height="0" name="videopress_upload_frame"></iframe>
</script>
<?php
}
/**
* Filters the VideoPress shortcode options, makes sure that
* the settings set in Jetpack's VideoPress module are applied.
*/
function videopress_shortcode_options( $options ) {
$videopress_options = $this->get_options();
if ( false === $options['freedom'] )
$options['freedom'] = $videopress_options['freedom'];
$options['hd'] = $videopress_options['hd'];
return $options;
}
}
// Initialize the module.
Jetpack_VideoPress::init();