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