Skip to content

Commit

Permalink
Merge pull request #1125 from Automattic/add/force-videopress-use-html5
Browse files Browse the repository at this point in the history
Force VideoPress to use HTML5 player for AMP
  • Loading branch information
westonruter authored May 10, 2018
2 parents 861069a + 3903e1a commit 4dd1023
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
74 changes: 55 additions & 19 deletions jetpack-helper.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<?php
/**
* Jetpack bits.
*
* @todo Move this into Jetpack. See https://github.com/Automattic/amp-wp/issues/1021
* @package AMP
*/

// Jetpack bits.

add_action( 'pre_amp_render_post', 'amp_jetpack_mods' );
add_action( 'template_redirect', 'amp_jetpack_mods', 9 );

/**
* Disable Jetpack features that are not compatible with AMP.
*
**/
* @since 0.2
*/
function amp_jetpack_mods() {
if ( ! is_amp_endpoint() ) {
return;
}
if ( Jetpack::is_module_active( 'stats' ) ) {
add_action( 'amp_post_template_footer', 'jetpack_amp_add_stats_pixel' );
}
amp_jetpack_disable_sharing();
amp_jetpack_disable_related_posts();
add_filter( 'videopress_shortcode_options', 'amp_videopress_enable_freedom_mode' );
}

/**
* Disable Jetpack sharing.
*
* @since 0.3
*/
function amp_jetpack_disable_sharing() {
add_filter( 'sharing_show', '__return_false', 100 );
}
Expand All @@ -25,15 +39,21 @@ function amp_jetpack_disable_sharing() {
*
* That placeholder is useless since we can't ouput, and don't want to output Related Posts in AMP.
*
**/
* @since 0.2
*/
function amp_jetpack_disable_related_posts() {
if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
$jprp = Jetpack_RelatedPosts::init();
remove_filter( 'the_content', array( $jprp, 'filter_add_target_to_dom' ), 40 );
}
}

function jetpack_amp_add_stats_pixel( $amp_template ) {
/**
* Add Jetpack stats pixel.
*
* @since 0.3.2
*/
function jetpack_amp_add_stats_pixel() {
if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
return;
}
Expand All @@ -47,25 +67,41 @@ function jetpack_amp_add_stats_pixel( $amp_template ) {
*
* Looks something like:
* https://pixel.wp.com/g.gif?v=ext&j=1%3A3.9.1&blog=1234&post=5678&tz=-4&srv=example.com&host=example.com&ref=&rand=0.4107963021218808
*
* @since 0.3.2
*/
function jetpack_amp_build_stats_pixel_url() {
global $wp_the_query;
if ( function_exists( 'stats_build_view_data' ) ) { // added in https://github.com/Automattic/jetpack/pull/3445
if ( function_exists( 'stats_build_view_data' ) ) { // Added in <https://github.com/Automattic/jetpack/pull/3445>.
$data = stats_build_view_data();
} else {
$blog = Jetpack_Options::get_option( 'id' );
$tz = get_option( 'gmt_offset' );
$v = 'ext';
$blog_url = AMP_WP_Utils::parse_url( site_url() );
$srv = $blog_url['host'];
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
$post = $wp_the_query->get_queried_object_id();
$data = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
$blog = Jetpack_Options::get_option( 'id' );
$tz = get_option( 'gmt_offset' );
$v = 'ext';
$blog_url = wp_parse_url( site_url() );
$srv = $blog_url['host'];
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
$post = $wp_the_query->get_queried_object_id();
$data = compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
}

$data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? rawurlencode( $_SERVER['HTTP_HOST'] ) : ''; // input var ok
$data['rand'] = 'RANDOM'; // amp placeholder
$data['ref'] = 'DOCUMENT_REFERRER'; // amp placeholder
$data = array_map( 'rawurlencode' , $data );
$data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; // input var ok.
$data['rand'] = 'RANDOM'; // AMP placeholder.
$data['ref'] = 'DOCUMENT_REFERRER'; // AMP placeholder.
$data = array_map( 'rawurlencode', $data );
return add_query_arg( $data, 'https://pixel.wp.com/g.gif' );
}

/**
* Force videopress to use html5 player that would generate <video /> tag
* that will be later converted to <amp-video />
*
* @since 0.7.1
*
* @param array $options videopress shortcode options.
* @return array videopress shortcode options with `freedom` set to true
*/
function amp_videopress_enable_freedom_mode( $options ) {
$options['freedom'] = true;
return $options;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve
- Limit showing AMP validation warnings to when `amp` theme support is present. See [#1132](https://github.com/Automattic/amp-wp/pull/1132).
- Supply the extracted dimensions to images determined to need them; fixes regression from 0.6 this is key for Gutenberg compat. See [#1117](https://github.com/Automattic/amp-wp/pull/1117).
- Ensure before/after is amended to filtered comment_reply_link. See [#1118](https://github.com/Automattic/amp-wp/pull/1118).
- Force VideoPress to use html5 player for AMP. See [#1125](https://github.com/Automattic/amp-wp/pull/1125). Props yurynix.

See [0.7.1 milestone](https://github.com/Automattic/amp-wp/milestone/8?closed=1).

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Follow along with or [contribute](https://github.com/Automattic/amp-wp/blob/deve
- Limit showing AMP validation warnings to when `amp` theme support is present. See [#1132](https://github.com/Automattic/amp-wp/pull/1132).
- Supply the extracted dimensions to images determined to need them; fixes regression from 0.6 this is key for Gutenberg compat. See [#1117](https://github.com/Automattic/amp-wp/pull/1117).
- Ensure before/after is amended to filtered comment_reply_link. See [#1118](https://github.com/Automattic/amp-wp/pull/1118).
- Force VideoPress to use html5 player for AMP. See [#1125](https://github.com/Automattic/amp-wp/pull/1125). Props yurynix.

See [0.7.1 milestone](https://github.com/Automattic/amp-wp/milestone/8?closed=1).

Expand Down

0 comments on commit 4dd1023

Please sign in to comment.