playerHtml();
}
/**
* Auto-prepends the BeyondWords player to WordPress content.
*
* @since 3.0.0
* @since 4.2.0 Renamed from addPlayerToContent to autoPrependPlayer.
* @since 4.2.0 Perform hasCustomPlayer() check here.
*
* @param string $content WordPress content.
*
* @return string
*/
public function autoPrependPlayer($content)
{
if ($this->hasCustomPlayer($content)) {
return $content;
}
return $this->playerHtml() . $content;
}
/**
* Player HTML.
*
* Displays JS SDK variant of the BeyondWords audio player, for both
* AMP and non-AMP content.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*
* @param WP_Post $post WordPress Post.
*
* @since 3.0.0
* @since 3.1.0 Added _doing_it_wrong deprecation warnings
*
* @return string
*/
public function playerHtml($post = false)
{
if (! ($post instanceof \WP_Post)) {
$post = get_post($post);
}
if (! $post) {
return '';
}
if (! $this->isPlayerEnabled($post)) {
return '';
}
$projectId = PostMetaUtils::getProjectId($post->ID);
if (! $projectId) {
return '';
}
$contentId = PostMetaUtils::getContentId($post->ID);
if (! $contentId) {
return '';
}
// AMP or JS Player?
if ($this->useAmpPlayer()) {
$html = $this->ampPlayerHtml($post->ID, $projectId, $contentId);
} else {
$html = $this->jsPlayerHtml($post->ID, $projectId, $contentId);
}
return $html;
}
/**
* Has custom player?
*
* Checks the post content to see whether a custom player has been added.
*
* @since 3.2.0
* @since 4.2.0 Pass $content as a parameter, check for [beyondwords_player] shortcode
* @since 4.2.4 Check $content is a string
*
* @param string $content WordPress content.
*
* @return boolean
*/
public function hasCustomPlayer($content)
{
if (! is_string($content)) {
return false;
}
if (strpos($content, '[beyondwords_player]') !== false) {
return true;
}
$crawler = new Crawler($content);
return count($crawler->filterXPath('//div[@data-beyondwords-player="true"]')) > 0;
}
/**
* JS Player HTML.
*
* Displays the HTML required for the JS player.
*
* @param int $postId WordPress Post ID.
* @param int $projectId BeyondWords Project ID.
* @param int $contentId BeyondWords Content ID.
*
* @since 3.0.0
* @since 3.1.0 Added speechkit_js_player_html filter
* @since 4.2.0 Remove hasCustomPlayer() check from here.
*
* @return string
*/
public function jsPlayerHtml($postId, $projectId, $contentId)
{
$html = '
';
/**
* Filters the HTML of the BeyondWords Player.
*
* @since 4.0.0
*
* @param string $html The HTML for the JS audio player. The audio player JavaScript may
* fail to locate the target element if you remove or replace the
* default contents of this parameter.
* @param int $postId WordPress post ID.
* @param int $projectId BeyondWords project ID.
* @param int $contentId BeyondWords content ID.
*/
$html = apply_filters('beyondwords_player_html', $html, $postId, $projectId, $contentId);
/**
* Filters the HTML of the BeyondWords JS audio player.
*
* @since 3.3.3
* @deprecated Scheduled for removal in v5.0
*
* @param string $html The HTML for the JS audio player. The audio player JavaScript may
* fail to locate the target element if you remove or replace the
* default contents of this parameter.
* @param int $postId WordPress post ID.
* @param int $projectId BeyondWords project ID.
* @param int $contentId BeyondWords content ID.
*/
$html = apply_filters('beyondwords_js_player_html', $html, $postId, $projectId, $contentId);
return $html;
}
/**
* AMP Player HTML.
*
* Displays the HTML required for the AMP player.
*
* @param int $postId WordPress Post ID.
* @param int $projectId BeyondWords Project ID.
* @param int $contentId BeyondWords Content ID.
*
* @since 3.0.0
* @since 3.1.0 Added speechkit_amp_player_html filter
*
* @return string
*/
public function ampPlayerHtml($postId, $projectId, $contentId)
{
$src = sprintf(Environment::getAmpPlayerUrl(), $projectId, $contentId);
// Turn on output buffering
ob_start();
?>
ID)) {
$enabled = false;
}
// Is the player ui enabled in plugin settings?
if ($enabled) {
$enabled = get_option('beyondwords_player_ui', PlayerUI::ENABLED) === PlayerUI::ENABLED;
}
/**
* Filters the enabled/disabled (shown/hidden) status of the player for each post.
*
* @since 3.3.3
*
* @param boolean $enabled Is the player enabled (shown) for this post?
* @param int $post_id WordPress post ID.
*/
$enabled = apply_filters('beyondwords_post_player_enabled', $enabled, $post->ID);
return $enabled;
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 3.0.0
*
* @return void
*/
public function enqueueScripts()
{
if (! is_singular()) {
return;
}
if (get_option('beyondwords_player_ui', PlayerUI::ENABLED) === PlayerUI::DISABLED) {
return;
}
// JS SDK Player inline script, filtered by $this->scriptLoaderTag()
add_filter('script_loader_tag', array($this, 'scriptLoaderTag'), 10, 3);
wp_enqueue_script(
'beyondwords-sdk',
Environment::getJsSdkUrl(),
array(),
null,
true
);
}
/**
* Use the AMP player?
*
* There are multiple AMP plugins for WordPress, so multiple checks are performed.
*
* @since 3.0.7
*
* @return bool
*/
public function useAmpPlayer()
{
// https://amp-wp.org/reference/function/amp_is_request/
if (function_exists('amp_is_request')) {
return \amp_is_request();
}
// https://ampforwp.com/tutorials/article/detect-amp-page-function/
if (function_exists('ampforwp_is_amp_endpoint')) {
return \ampforwp_is_amp_endpoint();
}
// https://amp-wp.org/reference/function/is_amp_endpoint/
if (function_exists('is_amp_endpoint')) {
return \is_amp_endpoint();
}
return false;
}
/**
* Filters the HTML script tag of an enqueued script.
*
* @param string $tag The
ID);
$contentId = PostMetaUtils::getContentId($post->ID);
$playerStyle = PostMetaUtils::getPlayerStyle($post->ID);
$params = [
'projectId' => is_numeric($projectId) ? (int)$projectId : $projectId,
'contentId' => is_numeric($contentId) ? (int)$contentId : $contentId,
'playerStyle' => $playerStyle,
];
$playerUI = get_option('beyondwords_player_ui', PlayerUI::ENABLED);
if ($playerUI === PlayerUI::HEADLESS) {
$params['showUserInterface'] = false;
}
/**
* Use legacy JS SDK params if player version setting is "0": "Legacy"
*/
if (SettingsUtils::useLegacyPlayer()) {
$params = $this->convertLatestToLegacyParams($params);
}
/**
* Filters the BeyondWords JavaScript SDK parameters.
*
* @since 4.0.0
*
* @param array $params The default JS SDK params.
* @param int $postId The Post ID.
*/
$params = apply_filters('beyondwords_player_sdk_params', $params, $post->ID);
return $params;
}
/**
* Convert latest JS SDK params into legacy format.
*
* @since 4.0.0
*
* @see https://docs.beyondwords.io/docs/javascript-sdk-automatic-player
*
* @param array $latestParams Latest JS SDK params
*
* @return array Legacy JS SDK params
*/
public function convertLatestToLegacyParams($latestParams)
{
$skBackend = Environment::getBackendUrl();
$skBackendApi = Environment::getApiUrl();
$legacyParams = [
'projectId' => $latestParams['projectId'],
'podcastId' => $latestParams['contentId'],
];
if ($latestParams['playerStyle'] = 'large') {
$legacyParams['playerType'] = 'manual';
}
if (strlen($skBackend)) {
$legacyParams['skBackend'] = esc_url($skBackend);
}
if (is_admin()) {
$legacyParams['apiWriteKey'] = $latestParams['writeToken'];
$legacyParams['processingStatus'] = true;
if (strlen($skBackendApi)) {
$legacyParams['skBackendApi'] = esc_url($skBackendApi);
}
}
if (defined('BEYONDWORDS_DEBUG') && BEYONDWORDS_DEBUG) {
$legacyParams['debug'] = true;
}
return $legacyParams;
}
/**
* Use Player JS SDK?
*
* @since 3.0.7
*
* @return string
*/
public function usePlayerJsSdk()
{
// AMP requests don't use the Player JS SDK
if ($this->useAmpPlayer()) {
return false;
}
// Both Gutenberg/Classic editors have their own player scripts
if (CoreUtils::isGutenbergPage() || CoreUtils::isEditScreen()) {
return false;
}
// Disable audio player in Preview, because we have not sent updates to BeyondWords API yet
if (function_exists('is_preview') && is_preview()) {
return false;
}
$post = get_post();
if (! $post) {
return false;
}
$projectId = PostMetaUtils::getProjectId($post->ID);
if (! $projectId) {
return false;
}
$contentId = PostMetaUtils::getContentId($post->ID);
if (! $contentId) {
return false;
}
return true;
}
}