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 / Component / Settings / Fields / IntegrationMethod / IntegrationMethod.php

IntegrationMethod.php in BeyondWords – AI audio for publishers 6.3.0, at src/Component/Settings/Fields/IntegrationMethod/IntegrationMethod.php

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