| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Beyondwords\Wordpress\Core\Player; |
| 6 |
|
| 7 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 8 |
use Beyondwords\Wordpress\Component\Settings\Fields\IntegrationMethod\IntegrationMethod; |
| 9 |
use Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI\PlayerUI; |
| 10 |
use Beyondwords\Wordpress\Core\Core; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class ConfigBuilder |
| 14 |
* |
| 15 |
* Constructs the parameters object for the BeyondWords JS SDK. |
| 16 |
*/ |
| 17 |
class ConfigBuilder |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Build JavaScript SDK parameters for the player. |
| 21 |
* |
| 22 |
* @since 6.0.0 Introduced. |
| 23 |
* |
| 24 |
* @param \WP_Post $post WordPress post object. |
| 25 |
* |
| 26 |
* @return object Parameters for JS SDK. |
| 27 |
*/ |
| 28 |
public static function build(\WP_Post $post): object |
| 29 |
{ |
| 30 |
$projectId = PostMetaUtils::getProjectId($post->ID); |
| 31 |
|
| 32 |
$params = [ |
| 33 |
'projectId' => is_numeric($projectId) ? (int) $projectId : $projectId, |
| 34 |
]; |
| 35 |
|
| 36 |
$params = self::mergePluginSettings($params); |
| 37 |
$params = self::mergePostSettings($post, $params); |
| 38 |
|
| 39 |
return (object) apply_filters('beyondwords_player_sdk_params', $params, $post->ID); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Merge global plugin settings into the SDK parameters. |
| 44 |
* |
| 45 |
* @param array $params Existing params. |
| 46 |
* |
| 47 |
* @return array Modified params. |
| 48 |
*/ |
| 49 |
public static function mergePluginSettings(array $params): array |
| 50 |
{ |
| 51 |
$mapping = [ |
| 52 |
'beyondwords_player_style' => 'playerStyle', |
| 53 |
'beyondwords_player_call_to_action' => 'callToAction', |
| 54 |
'beyondwords_player_highlight_sections' => 'highlightSections', |
| 55 |
'beyondwords_player_widget_style' => 'widgetStyle', |
| 56 |
'beyondwords_player_widget_position' => 'widgetPosition', |
| 57 |
'beyondwords_player_skip_button_style' => 'skipButtonStyle', |
| 58 |
]; |
| 59 |
|
| 60 |
foreach ($mapping as $wpOption => $sdkParam) { |
| 61 |
$val = get_option($wpOption); |
| 62 |
|
| 63 |
if (! empty($val)) { |
| 64 |
$params[$sdkParam] = $val; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if (! empty(get_option('beyondwords_player_clickable_sections'))) { |
| 69 |
$params['clickableSections'] = 'body'; |
| 70 |
} |
| 71 |
|
| 72 |
return $params; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Merge post-specific settings into the SDK parameters. |
| 77 |
* |
| 78 |
* @param \WP_Post $post WordPress post object. |
| 79 |
* @param array $params Existing params. |
| 80 |
* |
| 81 |
* @return array Modified params. |
| 82 |
*/ |
| 83 |
public static function mergePostSettings(\WP_Post $post, array $params): array |
| 84 |
{ |
| 85 |
$contentId = PostMetaUtils::getContentId($post->ID); |
| 86 |
|
| 87 |
if (! empty($contentId)) { |
| 88 |
$params['contentId'] = (string) $contentId; |
| 89 |
} |
| 90 |
|
| 91 |
$playerUI = get_option(PlayerUI::OPTION_NAME); |
| 92 |
|
| 93 |
if ($playerUI === PlayerUI::HEADLESS) { |
| 94 |
$params['showUserInterface'] = false; |
| 95 |
} |
| 96 |
|
| 97 |
$style = PostMetaUtils::getPlayerStyle($post->ID); |
| 98 |
|
| 99 |
if (! empty($style)) { |
| 100 |
$params['playerStyle'] = $style; |
| 101 |
} |
| 102 |
|
| 103 |
$content = get_post_meta($post->ID, 'beyondwords_player_content', true); |
| 104 |
|
| 105 |
if (! empty($content)) { |
| 106 |
$params['loadContentAs'] = [$content]; |
| 107 |
} |
| 108 |
|
| 109 |
$method = IntegrationMethod::getIntegrationMethod($post); |
| 110 |
|
| 111 |
if ($method === IntegrationMethod::CLIENT_SIDE && empty($params['contentId'])) { |
| 112 |
$params['clientSideEnabled'] = true; |
| 113 |
$params['sourceId'] = (string) $post->ID; |
| 114 |
unset($params['contentId']); |
| 115 |
} |
| 116 |
|
| 117 |
return $params; |
| 118 |
} |
| 119 |
} |
| 120 |
|