PluginProbe
BeyondWords – AI audio for publishers / 6.3.0
BeyondWords – AI audio for publishers v6.3.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Core / Player / ConfigBuilder.php

ConfigBuilder.php in BeyondWords – AI audio for publishers 6.3.0, at src/Core/Player/ConfigBuilder.php

122 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 defined('ABSPATH') || exit;
18
19 class ConfigBuilder
20 {
21 /**
22 * Build JavaScript SDK parameters for the player.
23 *
24 * @since 6.0.0 Introduced.
25 *
26 * @param \WP_Post $post WordPress post object.
27 *
28 * @return object Parameters for JS SDK.
29 */
30 public static function build(\WP_Post $post): object
31 {
32 $projectId = PostMetaUtils::getProjectId($post->ID);
33
34 $params = [
35 'projectId' => is_numeric($projectId) ? (int) $projectId : $projectId,
36 ];
37
38 $params = self::mergePluginSettings($params);
39 $params = self::mergePostSettings($post, $params);
40
41 return (object) apply_filters('beyondwords_player_sdk_params', $params, $post->ID);
42 }
43
44 /**
45 * Merge global plugin settings into the SDK parameters.
46 *
47 * @param array $params Existing params.
48 *
49 * @return array Modified params.
50 */
51 public static function mergePluginSettings(array $params): array
52 {
53 $mapping = [
54 'beyondwords_player_style' => 'playerStyle',
55 'beyondwords_player_call_to_action' => 'callToAction',
56 'beyondwords_player_highlight_sections' => 'highlightSections',
57 'beyondwords_player_widget_style' => 'widgetStyle',
58 'beyondwords_player_widget_position' => 'widgetPosition',
59 'beyondwords_player_skip_button_style' => 'skipButtonStyle',
60 ];
61
62 foreach ($mapping as $wpOption => $sdkParam) {
63 $val = get_option($wpOption);
64
65 if (! empty($val)) {
66 $params[$sdkParam] = $val;
67 }
68 }
69
70 if (! empty(get_option('beyondwords_player_clickable_sections'))) {
71 $params['clickableSections'] = 'body';
72 }
73
74 return $params;
75 }
76
77 /**
78 * Merge post-specific settings into the SDK parameters.
79 *
80 * @param \WP_Post $post WordPress post object.
81 * @param array $params Existing params.
82 *
83 * @return array Modified params.
84 */
85 public static function mergePostSettings(\WP_Post $post, array $params): array
86 {
87 $contentId = PostMetaUtils::getContentId($post->ID);
88
89 if (! empty($contentId)) {
90 $params['contentId'] = (string) $contentId;
91 }
92
93 $playerUI = get_option(PlayerUI::OPTION_NAME);
94
95 if ($playerUI === PlayerUI::HEADLESS) {
96 $params['showUserInterface'] = false;
97 }
98
99 $style = PostMetaUtils::getPlayerStyle($post->ID);
100
101 if (! empty($style)) {
102 $params['playerStyle'] = $style;
103 }
104
105 $content = get_post_meta($post->ID, 'beyondwords_player_content', true);
106
107 if (! empty($content)) {
108 $params['loadContentAs'] = [$content];
109 }
110
111 $method = IntegrationMethod::getIntegrationMethod($post);
112
113 if ($method === IntegrationMethod::CLIENT_SIDE && empty($params['contentId'])) {
114 $params['clientSideEnabled'] = true;
115 $params['sourceId'] = (string) $post->ID;
116 unset($params['contentId']);
117 }
118
119 return $params;
120 }
121 }
122