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