Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ConfigBuilder
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
1 / 1
 build
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 mergePluginSettings
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 mergePostSettings
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5namespace Beyondwords\Wordpress\Core\Player;
6
7use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
8use Beyondwords\Wordpress\Component\Settings\Fields\IntegrationMethod\IntegrationMethod;
9use Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI\PlayerUI;
10use Beyondwords\Wordpress\Core\Core;
11
12/**
13 * Class ConfigBuilder
14 *
15 * Constructs the parameters object for the BeyondWords JS SDK.
16 */
17class 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}