PluginProbe
BeyondWords – AI audio for publishers / 4.4.0
BeyondWords – AI audio for publishers v4.4.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 / SiteHealth / SiteHealth.php

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

294 lines 9.4 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 <stu@beyondwords.io>
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 * Constructor
75 *
76 * @since 3.7.0
77 */
78 public function __construct()
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 $tabs
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 // translators: Tab heading for Site Health navigation.
131 $info['beyondwords']['fields']['allowed-post-types'] = [
132 'label' => __('Allowed post types', 'speechkit'),
133 'value' => implode(', ', SettingsUtils::getAllowedPostTypes()),
134 ];
135
136 $info['beyondwords']['fields']['supported-post-types'] = [
137 'label' => __('Supported post types', 'speechkit'),
138 'value' => implode(', ', SettingsUtils::getSupportedPostTypes()),
139 ];
140
141 $info['beyondwords']['fields']['beyondwords_settings_updated'] = [
142 'label' => __('Settings updated', 'speechkit'),
143 'value' => get_option('beyondwords_settings_updated'),
144 ];
145
146 $registered = array_values(array_filter(SiteHealth::FILTERS, 'has_filter'));
147
148 $info['beyondwords']['fields']['registered-filters'] = [
149 'label' => __('Registered filters', 'speechkit'),
150 'value' => empty($registered) ? __('None', 'speechkit') : implode(', ', $registered),
151 'debug' => empty($registered) ? 'none' : implode(', ', $registered),
152 ];
153
154 $registered = array_values(array_filter(SiteHealth::DEPRECATED_FILTERS, 'has_filter'));
155
156 $info['beyondwords']['fields']['registered-deprecated-filters'] = [
157 'label' => __('Registered deprecated filters', 'speechkit'),
158 'value' => empty($registered) ? __('None', 'speechkit') : implode(', ', $registered),
159 'debug' => empty($registered) ? 'none' : implode(', ', $registered),
160 ];
161
162 $this->addConstant($info, 'BEYONDWORDS_AUTOREGENERATE');
163 $this->addConstant($info, 'BEYONDWORDS_DEBUG');
164
165 return $info;
166 }
167
168 /**
169 * Add plugin version to the info debugging array.
170 *
171 * @since 3.7.0
172 * @static
173 *
174 * @param array $info Debugging info array
175 *
176 * @return array
177 */
178 public function addPluginVersion(&$info)
179 {
180 $constVersion = defined('BEYONDWORDS__PLUGIN_VERSION') ? BEYONDWORDS__PLUGIN_VERSION : '';
181 $dbVersion = get_option('beyondwords_version');
182
183 if ($constVersion && $constVersion === $dbVersion) {
184 $info['beyondwords']['fields']['plugin-version'] = [
185 'label' => __('Plugin version', 'speechkit'),
186 'value' => BEYONDWORDS__PLUGIN_VERSION,
187 ];
188 } else {
189 $info['beyondwords']['fields']['plugin-version'] = [
190 'label' => __('Plugin version', 'speechkit'),
191 'value' => sprintf(
192 /* translators: 1: The plugin version found in the PHP files. 2: The plugin version found in the database. */ // phpcs:ignore Generic.Files.LineLength.TooLong
193 __('Version mismatch: file: %s / db: %s'),
194 $constVersion,
195 $dbVersion
196 ),
197 ];
198 }
199 }
200
201 /**
202 * Adds debugging data for the BeyondWords REST API connection.
203 *
204 * @since 3.7.0
205 * @static
206 *
207 * @param array $info Debugging info array
208 * @param string $name Constant name
209 *
210 * @return array
211 */
212 public function addRestApiConnection(&$info)
213 {
214 // translators: Tab heading for Site Health navigation.
215 $apiUrl = Environment::getApiUrl();
216
217 $info['beyondwords']['fields']['api-url'] = [
218 'label' => __('REST API URL', 'speechkit'),
219 'value' => $apiUrl,
220 ];
221
222 $response = wp_remote_request(Environment::getApiUrl(), [
223 'blocking' => true,
224 'body' => '',
225 'method' => 'GET',
226 'sslverify' => true,
227 ]);
228
229 if (! is_wp_error($response)) {
230 $info['beyondwords']['fields']['api-communication'] = array(
231 'label' => __('Communication with REST API'),
232 'value' => __('BeyondWords API is reachable'),
233 'debug' => 'true',
234 );
235 } else {
236 $info['beyondwords']['fields']['api-communication'] = array(
237 'label' => __('Communication with REST API'),
238 'value' => sprintf(
239 /* translators: 1: The IP address the REST API resolves to. 2: The error returned by the lookup. */
240 __('Unable to reach BeyondWords API at %1$s: %2$s'),
241 gethostbyname(Environment::getApiUrl()),
242 $response->get_error_message()
243 ),
244 'debug' => $response->get_error_message(),
245 );
246 }
247 }
248
249 /**
250 * Add a constant to the debugging info array.
251 *
252 * @since 3.7.0
253 * @static
254 *
255 * @param array $info Debugging info array
256 * @param string $name Constant name
257 *
258 * @return array
259 */
260 public function addConstant(&$info, $name)
261 {
262 $info['beyondwords']['fields'][$name] = [
263 'label' => $name,
264 'value' => ( defined($name) ? constant($name) : __('Undefined') ),
265 'debug' => ( defined($name) ? constant($name) : 'undefined' ),
266 ];
267 }
268
269 /**
270 * Mask a sensitive string for display in Site Health.
271 *
272 * @since 3.7.0
273 * @static
274 *
275 * @param string $string
276 * @param int $count
277 * @param string $char
278 *
279 * @return string
280 */
281 public static function maskString($string, $count = 4, $char = 'X')
282 {
283 if (! is_string($string)) {
284 return '';
285 }
286
287 if (strlen($string) < 8) {
288 return str_repeat($char, strlen($string));
289 } else {
290 return str_repeat($char, strlen($string) - $count) . substr($string, (0 - $count));
291 }
292 }
293 }
294