Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
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 */
17defined('ABSPATH') || exit;
18
19class 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}