Last active
June 2, 2020 05:58
-
-
Save westonruter/5f8dc16aba36c0ec9eef3750a22ac417 to your computer and use it in GitHub Desktop.
AMP Brid Player plugin for WordPress, with support for non-AMP fallback: https://github.com/ampproject/amp-wp/issues/3638
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* AMP Brid Player | |
* | |
* Installation instructions: | |
* 1. Click the “Download ZIP” button. | |
* 2. Rename the downloaded ZIP file to “amp-brid-player.zip” | |
* 3. In the WordPress admin, go to Plugins > Add New | |
* 4. Click the Upload Plugin button. | |
* 5. Select the “amp-brid-player.zip” file and click “Install now”. | |
* 6. Click “Activate” | |
* | |
* @package AMP_Brid_Player | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2019 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: AMP Brid Player | |
* Description: Demonstration for how to add the Brid player, with both non-AMP and AMP-compatible versions served via shortcode. Example: <code>[brid partner=14896 player=19331 video=483405 autoplay=true]</code>. | |
* Plugin URI: https://gist.github.com/westonruter/5f8dc16aba36c0ec9eef3750a22ac417 | |
* Version: 0.1.0 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
* Gist Plugin URI: https://gist.github.com/westonruter/5f8dc16aba36c0ec9eef3750a22ac417 | |
*/ | |
namespace AMP_Brid_Player; | |
const SHORTCODE = 'brid'; | |
/** | |
* Determines whether it is an AMP response. | |
* | |
* @return bool Is AMP. | |
*/ | |
function is_amp() { | |
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint(); | |
} | |
/** | |
* Render shortcode. | |
* | |
* @param array $attr Shortcode attributes. | |
* @return string | |
*/ | |
function render_shortcode( $attr ) { | |
$attr = shortcode_atts( | |
[ | |
'partner' => '', | |
'player' => '', | |
'video' => '', | |
'width' => 700, | |
'height' => 393, | |
'autoplay' => false, | |
], | |
$attr, | |
SHORTCODE | |
); | |
// Ensure casting to array. | |
$attr['autoplay'] = isset( $attr['autoplay'] ) && rest_sanitize_boolean( $attr['autoplay'] ); | |
if ( empty( $attr['partner'] ) || empty( $attr['player'] ) || empty( $attr['video'] ) ) { | |
return ''; | |
} | |
if ( is_amp() ) { | |
return sprintf( | |
'<amp-brid-player data-partner="%s" %s data-player="%s" data-video="%s" layout="responsive" width="%d" height="%s"></amp-brid-player>', | |
esc_attr( $attr['partner'] ), | |
$attr['autoplay'] ? 'autoplay' : '', | |
esc_attr( $attr['player'] ), | |
esc_attr( $attr['video'] ), | |
esc_attr( $attr['width'] ), | |
esc_attr( $attr['height'] ) | |
); | |
} else { | |
$element_id = wp_unique_id( 'brid-player' ); | |
$args = [ | |
'div' => $element_id, | |
'obj' => [ | |
'id' => $attr['player'], | |
'width' => (string) $attr['width'], | |
'height' => (string) $attr['height'], | |
'video' => $attr['video'], | |
'autoplay' => $attr['autoplay'], | |
], | |
]; | |
return sprintf( | |
' | |
<div id="%s" class="brid" style="width:%dpx; height:%dpx;" ></div> | |
<script type="text/javascript"> var _bp = _bp||[]; _bp.push(%s); </script> | |
<script type="text/javascript" async src="//services.brid.tv/player/build/brid.min.js"></script> | |
', | |
esc_attr( $element_id ), | |
esc_attr( $attr['width'] ), | |
esc_attr( $attr['height'] ), | |
wp_json_encode( $args ) | |
); | |
} | |
} | |
add_shortcode( SHORTCODE, __NAMESPACE__ . '\render_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c