| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Beyondwords\Wordpress\Core\Player\Renderer; |
| 6 |
|
| 7 |
use Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI\PlayerUI; |
| 8 |
use Beyondwords\Wordpress\Core\Environment; |
| 9 |
use Beyondwords\Wordpress\Core\Player\ConfigBuilder; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Javascript. |
| 13 |
* |
| 14 |
* Responsible for rendering the JavaScript BeyondWords player. |
| 15 |
*/ |
| 16 |
defined('ABSPATH') || exit; |
| 17 |
|
| 18 |
class Javascript extends Base |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Render the JavaScript player HTML. |
| 22 |
* |
| 23 |
* @param \WP_Post $post WordPress post object for which to render the player. |
| 24 |
* @param string $context Rendering context, either 'auto' or 'shortcode'. |
| 25 |
* @return string HTML output. |
| 26 |
*/ |
| 27 |
public static function render(\WP_Post $post, string $context = 'shortcode'): string |
| 28 |
{ |
| 29 |
if (PlayerUI::DISABLED === get_option(PlayerUI::OPTION_NAME)) { |
| 30 |
return ''; |
| 31 |
} |
| 32 |
|
| 33 |
$params = ConfigBuilder::build($post); |
| 34 |
|
| 35 |
$jsonParams = wp_json_encode($params, JSON_UNESCAPED_SLASHES); |
| 36 |
$jsonParams = sprintf('{target:this, ...%s}', $jsonParams); |
| 37 |
|
| 38 |
$onload = sprintf('new BeyondWords.Player(%s);', $jsonParams); |
| 39 |
$onload = apply_filters('beyondwords_player_script_onload', $onload, $params); |
| 40 |
|
| 41 |
return sprintf( |
| 42 |
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript |
| 43 |
'<script data-beyondwords-player-context="%s" async defer src="%s" onload=\'%s\'></script>', |
| 44 |
esc_attr($context), |
| 45 |
Environment::getJsSdkUrl(), |
| 46 |
$onload |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|