Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AMP Brid Player plugin for WordPress, with support for non-AMP fallback: https://github.com/ampproject/amp-wp/issues/3638
<?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' );
@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Apr 4, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment