PluginProbe
BeyondWords – AI audio for publishers / 4.6.0
BeyondWords – AI audio for publishers v4.6.0
7.2.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 All 44 releases
speechkit / src / Component / SiteHealth / SiteHealth.php

SiteHealth.php in BeyondWords – AI audio for publishers 4.6.0, at src/Component/SiteHealth/SiteHealth.php

292 lines 9.3 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 * BeyondWords SiteHealth.
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.7.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\SiteHealth;
14
15 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16 use Beyondwords\Wordpress\Core\Environment;
17
18 /**
19 * BeyondWords SiteHealth.
20 *
21 * @since 3.7.0
22 */
23 class SiteHealth
24 {
25 /**
26 * @var string[] List of current filters to check.
27 *
28 * @since 3.7.0 Introduced.
29 * @since 4.3.0 Filters refactoring - many were removed and renamed.
30 */
31 public const FILTERS = [
32 'beyondwords_content_params',
33 'beyondwords_player_script_onload',
34 'beyondwords_player_html',
35 'beyondwords_player_sdk_params',
36 'beyondwords_settings_player_styles',
37 'beyondwords_settings_post_types',
38 'beyondwords_settings_post_statuses',
39 ];
40
41 /**
42 * @var string[] List of deprecated filters to check.
43 *
44 * @since 3.7.0 Introduced.
45 * @since 4.3.0 Filters refactoring - many were removed and renamed.
46 */
47 public const DEPRECATED_FILTERS = [
48 'beyondwords_amp_player_html',
49 'beyondwords_body_params',
50 'beyondwords_content',
51 'beyondwords_content_id',
52 'beyondwords_js_player_html',
53 'beyondwords_js_player_params',
54 'beyondwords_player_styles',
55 'beyondwords_post_audio_enabled_blocks',
56 'beyondwords_post_metadata',
57 'beyondwords_post_player_enabled',
58 'beyondwords_post_statuses',
59 'beyondwords_post_types',
60 'beyondwords_project_id',
61 'sk_player_after',
62 'sk_player_before',
63 'sk_the_content',
64 'speechkit_amp_player_html',
65 'speechkit_content',
66 'speechkit_js_player_html',
67 'speechkit_js_player_params',
68 'speechkit_post_player_enabled',
69 'speechkit_post_statuses',
70 'speechkit_post_types',
71 ];
72
73 /**
74 * Init
75 *
76 * @since 4.0.0
77 */
78 public function init()
79 {
80 add_filter('debug_information', array($this, 'debugInformation'));
81 }
82
83 /**
84 * Add "Site Health" navigation tab.
85 *
86 * @since 3.7.0
87 *
88 * @param array $info
89 *
90 * @return array
91 */
92 public function debugInformation($info)
93 {
94 $info['beyondwords']['label'] = __('BeyondWords - Text-to-Speech', 'speechkit');
95
96 $this->addPluginVersion($info);
97 $this->addRestApiConnection($info);
98
99 $info['beyondwords']['fields']['beyondwords_api_key'] = [
100 'label' => __('API Key', 'speechkit'),
101 'value' => SiteHealth::maskString(get_option('beyondwords_api_key')),
102 ];
103
104 $info['beyondwords']['fields']['beyondwords_project_id'] = [
105 'label' => __('Project ID', 'speechkit'),
106 'value' => get_option('beyondwords_project_id'),
107 ];
108
109 $info['beyondwords']['fields']['beyondwords_player_version'] = [
110 'label' => __('Player version', 'speechkit'),
111 'value' => get_option('beyondwords_player_version'),
112 ];
113
114 $info['beyondwords']['fields']['beyondwords_player_ui'] = [
115 'label' => __('Player UI', 'speechkit'),
116 'value' => get_option('beyondwords_player_ui'),
117 ];
118
119 $info['beyondwords']['fields']['beyondwords_prepend_excerpt'] = [
120 'label' => __('Process excerpts', 'speechkit'),
121 'value' => get_option('beyondwords_prepend_excerpt') ? __('Yes') : __('No'),
122 'debug' => get_option('beyondwords_prepend_excerpt') ? 'yes' : 'no',
123 ];
124
125 $info['beyondwords']['fields']['beyondwords_preselect'] = [
126 'label' => __('Preselect ‘Generate audio’', 'speechkit'),
127 'value' => wp_json_encode(get_option('beyondwords_preselect')),
128 ];
129
130 $info['beyondwords']['fields']['compatible-post-types'] = [
131 'label' => __('Compatible post types', 'speechkit'),
132 'value' => implode(', ', SettingsUtils::getCompatiblePostTypes()),
133 ];
134
135 $info['beyondwords']['fields']['incompatible-post-types'] = [
136 'label' => __('Incompatible post types', 'speechkit'),
137 'value' => implode(', ', SettingsUtils::getIncompatiblePostTypes()),
138 ];
139
140 $info['beyondwords']['fields']['beyondwords_settings_updated'] = [
141 'label' => __('Settings updated', 'speechkit'),
142 'value' => get_option('beyondwords_settings_updated'),
143 ];
144
145 $registered = array_values(array_filter(SiteHealth::FILTERS, 'has_filter'));
146
147 $info['beyondwords']['fields']['registered-filters'] = [
148 'label' => __('Registered filters', 'speechkit'),
149 'value' => empty($registered) ? __('None', 'speechkit') : implode(', ', $registered),
150 'debug' => empty($registered) ? 'none' : implode(', ', $registered),
151 ];
152
153 $registered = array_values(array_filter(SiteHealth::DEPRECATED_FILTERS, 'has_filter'));
154
155 $info['beyondwords']['fields']['registered-deprecated-filters'] = [
156 'label' => __('Registered deprecated filters', 'speechkit'),
157 'value' => empty($registered) ? __('None', 'speechkit') : implode(', ', $registered),
158 'debug' => empty($registered) ? 'none' : implode(', ', $registered),
159 ];
160
161 $this->addConstant($info, 'BEYONDWORDS_AUTOREGENERATE');
162
163 return $info;
164 }
165
166 /**
167 * Add plugin version to the info debugging array.
168 *
169 * @since 3.7.0
170 * @static
171 *
172 * @param array $info Debugging info array
173 *
174 * @return array
175 */
176 public function addPluginVersion(&$info)
177 {
178 $constVersion = defined('BEYONDWORDS__PLUGIN_VERSION') ? BEYONDWORDS__PLUGIN_VERSION : '';
179 $dbVersion = get_option('beyondwords_version');
180
181 if ($constVersion && $constVersion === $dbVersion) {
182 $info['beyondwords']['fields']['plugin-version'] = [
183 'label' => __('Plugin version', 'speechkit'),
184 'value' => BEYONDWORDS__PLUGIN_VERSION,
185 ];
186 } else {
187 $info['beyondwords']['fields']['plugin-version'] = [
188 'label' => __('Plugin version', 'speechkit'),
189 'value' => sprintf(
190 /* translators: 1: The plugin version found in the PHP files. 2: The plugin version found in the database. */ // phpcs:ignore Generic.Files.LineLength.TooLong
191 __('Version mismatch: file: %s / db: %s'),
192 $constVersion,
193 $dbVersion
194 ),
195 ];
196 }
197 }
198
199 /**
200 * Adds debugging data for the BeyondWords REST API connection.
201 *
202 * @since 3.7.0
203 * @static
204 *
205 * @param array $info Debugging info array
206 * @param string $name Constant name
207 *
208 * @return array
209 */
210 public function addRestApiConnection(&$info)
211 {
212 // translators: Tab heading for Site Health navigation.
213 $apiUrl = Environment::getApiUrl();
214
215 $info['beyondwords']['fields']['api-url'] = [
216 'label' => __('REST API URL', 'speechkit'),
217 'value' => $apiUrl,
218 ];
219
220 $response = wp_remote_request(Environment::getApiUrl(), [
221 'blocking' => true,
222 'body' => '',
223 'method' => 'GET',
224 'sslverify' => true,
225 ]);
226
227 if (! is_wp_error($response)) {
228 $info['beyondwords']['fields']['api-communication'] = array(
229 'label' => __('Communication with REST API'),
230 'value' => __('BeyondWords API is reachable'),
231 'debug' => 'true',
232 );
233 } else {
234 $info['beyondwords']['fields']['api-communication'] = array(
235 'label' => __('Communication with REST API'),
236 'value' => sprintf(
237 /* translators: 1: The IP address the REST API resolves to. 2: The error returned by the lookup. */
238 __('Unable to reach BeyondWords API at %1$s: %2$s'),
239 gethostbyname(Environment::getApiUrl()),
240 $response->get_error_message()
241 ),
242 'debug' => $response->get_error_message(),
243 );
244 }
245 }
246
247 /**
248 * Add a constant to the debugging info array.
249 *
250 * @since 3.7.0
251 * @static
252 *
253 * @param array $info Debugging info array
254 * @param string $name Constant name
255 *
256 * @return array
257 */
258 public function addConstant(&$info, $name)
259 {
260 $info['beyondwords']['fields'][$name] = [
261 'label' => $name,
262 'value' => ( defined($name) ? constant($name) : __('Undefined') ),
263 'debug' => ( defined($name) ? constant($name) : 'undefined' ),
264 ];
265 }
266
267 /**
268 * Mask a sensitive string for display in Site Health.
269 *
270 * @since 3.7.0
271 * @static
272 *
273 * @param string $string
274 * @param int $count
275 * @param string $char
276 *
277 * @return string
278 */
279 public static function maskString($string, $count = 4, $char = 'X')
280 {
281 if (! is_string($string)) {
282 return '';
283 }
284
285 if (strlen($string) < 8) {
286 return str_repeat($char, strlen($string));
287 } else {
288 return str_repeat($char, strlen($string) - $count) . substr($string, (0 - $count));
289 }
290 }
291 }
292