Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
59 / 59
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
IntegrationMethod
100.00% covered (success)
100.00%
59 / 59
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addSetting
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
2
 getOptions
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getIntegrationMethod
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: Integration method
7 *
8 * @package Beyondwords\Wordpress
9 * @since   6.0.0 Introduced.
10 */
11
12namespace Beyondwords\Wordpress\Component\Settings\Fields\IntegrationMethod;
13
14use WP_Post;
15
16/**
17 * IntegrationMethod class.
18 *
19 * @since 6.0.0 Introduced.
20 */
21class IntegrationMethod
22{
23    /**
24     * Option name.
25     *
26     * @since 6.0.0 Introduced.
27     *
28     * @var string
29     */
30    public const OPTION_NAME = 'beyondwords_integration_method';
31
32    /**
33     * Client-side integration method.
34     *
35     * @since 6.0.0 Introduced.
36     *
37     * @var string
38     */
39    public const CLIENT_SIDE = 'client-side';
40
41    /**
42     * REST API integration method.
43     *
44     * @since 6.0.0 Introduced.
45     *
46     * @var string
47     */
48    public const REST_API = 'rest-api';
49
50    /**
51     * Default value.
52     *
53     * @since 6.0.0 Introduced.
54     *
55     * @var string
56     */
57    public const DEFAULT_VALUE = self::REST_API;
58
59    /**
60     * Constructor
61     *
62     * @since 6.0.0 Introduced.
63     */
64    public static function init()
65    {
66        add_action('admin_init', [self::class, 'addSetting']);
67    }
68
69    /**
70     * Add setting.
71     *
72     * @since 6.0.0 Introduced.
73     *
74     * @return void
75     */
76    public static function addSetting()
77    {
78        register_setting(
79            'beyondwords_content_settings',
80            self::OPTION_NAME,
81            [
82                'type'    => 'string',
83                'default' => self::DEFAULT_VALUE,
84            ]
85        );
86
87        add_settings_field(
88            'beyondwords-integration-method',
89            __('Integration method', 'speechkit'),
90            [self::class, 'render'],
91            'beyondwords_content',
92            'content'
93        );
94    }
95
96    /**
97     * Render setting field.
98     *
99     * @since 6.0.0 Introduced.
100     *
101     * @return void
102     **/
103    public static function render()
104    {
105        $options = self::getOptions();
106        $current = get_option(self::OPTION_NAME, self::DEFAULT_VALUE);
107        ?>
108        <div class="beyondwords-setting__content beyondwords-setting__content--integration-method">
109            <select name="<?php echo esc_attr(self::OPTION_NAME) ?>" id="<?php echo esc_attr(self::OPTION_NAME) ?>">
110                <?php foreach ($options as $option) : ?>
111                    <option
112                        value="<?php echo esc_attr($option['value']); ?>"
113                        <?php selected($option['value'], $current); ?>
114                    >
115                        <?php echo esc_html($option['label']); ?>
116                    </option>
117                <?php endforeach; ?>
118            </select>
119        </div>
120        <p class="description">
121            <?php
122            printf(
123                /* translators: %s is replaced with the "Client-Side integration" link */
124                esc_html__('REST API is currently the default method. %s should be selected if REST API does not work as expected on your site. It should, for instance, improve compatibility on sites using a page builder plugin/theme such as Elementor.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
125                sprintf(
126                    '<a href="https://github.com/beyondwords-io/player/blob/main/doc/client-side-integration.md" target="_blank" rel="nofollow">%s</a>', // phpcs:ignore Generic.Files.LineLength.TooLong
127                    esc_html__('Client-Side integration', 'speechkit')
128                )
129            );
130            ?>
131        </p>
132        <?php
133    }
134
135    /**
136     * Returns all options for the setting field.
137     *
138     * @since 6.0.0 Introduced.
139     *
140     * @return array Associative array of option values and labels.
141     **/
142    public static function getOptions()
143    {
144        return [
145            self::REST_API => [
146                'value' => self::REST_API,
147                'label' => __('REST API', 'speechkit'),
148            ],
149            self::CLIENT_SIDE => [
150                'value' => self::CLIENT_SIDE,
151                'label' => __('Magic Embed', 'speechkit'),
152            ],
153        ];
154    }
155
156    /**
157     * Get integration method. Tries the post meta if a post is passed, and falls back
158     * to the option.
159     *
160     * @since 6.0.0 Introduced.
161     *
162     * @param \WP_Post|false $post WordPress Post object (optional).
163     *
164     * @return string Integration method - either self::REST_API or self::CLIENT_SIDE.
165     **/
166    public static function getIntegrationMethod(\WP_Post|false $post = false): string
167    {
168        $method = '';
169
170        if ($post) {
171            $method = get_post_meta($post->ID, 'beyondwords_integration_method', true);
172        }
173
174        if (empty($method)) {
175            $method = get_option(self::OPTION_NAME, self::DEFAULT_VALUE);
176        }
177
178        if (! in_array($method, [self::REST_API, self::CLIENT_SIDE], true)) {
179            $method = self::DEFAULT_VALUE;
180        }
181
182        return $method;
183    }
184}