Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.97% |
119 / 128 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CoreUtils | |
92.97% |
119 / 128 |
|
60.00% |
3 / 5 |
22.17 | |
0.00% |
0 / 1 |
| isGutenbergPage | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
7.05 | |||
| isEditScreen | |
20.00% |
2 / 10 |
|
0.00% |
0 / 1 |
32.09 | |||
| isAmp | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
6 | |||
| getPostMetaKeys | |
100.00% |
43 / 43 |
|
100.00% |
1 / 1 |
1 | |||
| getOptions | |
100.00% |
60 / 60 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Beyondwords\Wordpress\Core; |
| 6 | |
| 7 | /** |
| 8 | * BeyondWords Core Utilities. |
| 9 | * |
| 10 | * @package Beyondwords |
| 11 | * @subpackage Beyondwords/includes |
| 12 | * @author Stuart McAlpine <stu@beyondwords.io> |
| 13 | * @since 3.5.0 |
| 14 | */ |
| 15 | class CoreUtils |
| 16 | { |
| 17 | /** |
| 18 | * Check to see if the Gutenberg Editor is being used. |
| 19 | * |
| 20 | * @link https://wordpress.stackexchange.com/a/324866 |
| 21 | * |
| 22 | * @since 3.0.0 |
| 23 | * @since 3.5.0 Moved from Core\Utils to Core\CoreUtils |
| 24 | */ |
| 25 | public static function isGutenbergPage(): bool |
| 26 | { |
| 27 | if (function_exists('is_gutenberg_page') && is_gutenberg_page()) { |
| 28 | // The Gutenberg plugin is on. |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | $currentScreen = null; |
| 33 | |
| 34 | if (function_exists('get_current_screen')) { |
| 35 | $currentScreen = get_current_screen(); |
| 36 | } |
| 37 | |
| 38 | if ($currentScreen === null) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | if (method_exists($currentScreen, 'is_block_editor') && $currentScreen->is_block_editor()) { |
| 43 | // Gutenberg page on 5+. |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check to see if current screen is an edit screen |
| 52 | * (this includes the screen that lists the posts). |
| 53 | * |
| 54 | * @since 4.0.0 |
| 55 | * @since 4.0.5 Ensure is_admin() and $screen |
| 56 | */ |
| 57 | public static function isEditScreen(): bool |
| 58 | { |
| 59 | if (! is_admin()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (! function_exists('get_current_screen')) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $screen = get_current_screen(); |
| 68 | |
| 69 | if (! $screen || ! ($screen instanceof \WP_Screen)) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | if ($screen->parent_base === 'edit' || $screen->base === 'post') { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Check if the current request is an AMP request. |
| 82 | * |
| 83 | * @return bool True if AMP. |
| 84 | */ |
| 85 | public static function isAmp(): bool |
| 86 | { |
| 87 | return ( |
| 88 | (function_exists('\amp_is_request') && \amp_is_request()) || |
| 89 | (function_exists('\ampforwp_is_amp_endpoint') && \ampforwp_is_amp_endpoint()) || |
| 90 | (function_exists('\is_amp_endpoint') && \is_amp_endpoint()) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the BeyondWords post meta keys. |
| 96 | * |
| 97 | * @since 4.1.0 |
| 98 | * |
| 99 | * @param string $type Type (current|deprecated|all). |
| 100 | * |
| 101 | * @throws Exception |
| 102 | * |
| 103 | * @return string[] Post meta keys. |
| 104 | **/ |
| 105 | public static function getPostMetaKeys(string $type = 'current'): array |
| 106 | { |
| 107 | $current = [ |
| 108 | 'beyondwords_generate_audio', |
| 109 | 'beyondwords_integration_method', |
| 110 | 'beyondwords_project_id', |
| 111 | 'beyondwords_content_id', |
| 112 | 'beyondwords_preview_token', |
| 113 | 'beyondwords_player_content', |
| 114 | 'beyondwords_player_style', |
| 115 | 'beyondwords_language_code', |
| 116 | 'beyondwords_language_id', // @todo deprecate in v5.6 |
| 117 | 'beyondwords_title_voice_id', |
| 118 | 'beyondwords_body_voice_id', |
| 119 | 'beyondwords_summary_voice_id', |
| 120 | 'beyondwords_error_message', |
| 121 | 'beyondwords_disabled', |
| 122 | 'beyondwords_delete_content', |
| 123 | ]; |
| 124 | |
| 125 | $deprecated = [ |
| 126 | 'beyondwords_podcast_id', |
| 127 | 'beyondwords_hash', |
| 128 | 'publish_post_to_speechkit', |
| 129 | 'speechkit_hash', |
| 130 | 'speechkit_generate_audio', |
| 131 | 'speechkit_project_id', |
| 132 | 'speechkit_podcast_id', |
| 133 | 'speechkit_error_message', |
| 134 | 'speechkit_disabled', |
| 135 | 'speechkit_access_key', |
| 136 | 'speechkit_error', |
| 137 | 'speechkit_info', |
| 138 | 'speechkit_response', |
| 139 | 'speechkit_retries', |
| 140 | 'speechkit_status', |
| 141 | 'speechkit_updated_at', |
| 142 | '_speechkit_link', |
| 143 | '_speechkit_text', |
| 144 | ]; |
| 145 | |
| 146 | return match ($type) { |
| 147 | 'current' => $current, |
| 148 | 'deprecated' => $deprecated, |
| 149 | 'all' => array_merge($current, $deprecated), |
| 150 | default => throw new \Exception('Unexpected $type param for CoreUtils::getPostMetaKeys()'), |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the BeyondWords post meta keys. |
| 156 | * |
| 157 | * @since 4.1.0 |
| 158 | * |
| 159 | * @param string $type Type (current|deprecated|all). |
| 160 | * |
| 161 | * @throws Exception |
| 162 | * |
| 163 | * @return string[] Post meta keys. |
| 164 | **/ |
| 165 | public static function getOptions(string $type = 'current'): array |
| 166 | { |
| 167 | $current = [ |
| 168 | // v6.x |
| 169 | 'beyondwords_integration_method', |
| 170 | // v5.x |
| 171 | 'beyondwords_date_activated', |
| 172 | 'beyondwords_notice_review_dismissed', |
| 173 | 'beyondwords_player_call_to_action', |
| 174 | 'beyondwords_player_clickable_sections', |
| 175 | 'beyondwords_player_content', |
| 176 | 'beyondwords_player_highlight_sections', |
| 177 | 'beyondwords_player_skip_button_style', |
| 178 | 'beyondwords_player_theme', |
| 179 | 'beyondwords_player_theme_dark', |
| 180 | 'beyondwords_player_theme_light', |
| 181 | 'beyondwords_player_theme_video', |
| 182 | 'beyondwords_player_widget_position', |
| 183 | 'beyondwords_player_widget_style', |
| 184 | 'beyondwords_project_auto_publish_enabled', |
| 185 | 'beyondwords_project_body_voice_id', |
| 186 | 'beyondwords_project_body_voice_speaking_rate', |
| 187 | 'beyondwords_project_language_code', |
| 188 | 'beyondwords_project_language_id', // @todo deprecate in v5.6 |
| 189 | 'beyondwords_project_title_enabled', |
| 190 | 'beyondwords_project_title_voice_id', |
| 191 | 'beyondwords_project_title_voice_speaking_rate', |
| 192 | 'beyondwords_video_enabled', |
| 193 | 'beyondwords_player_ui', |
| 194 | 'beyondwords_player_style', |
| 195 | 'beyondwords_player_version', |
| 196 | 'beyondwords_settings_updated', |
| 197 | 'beyondwords_valid_api_connection', |
| 198 | // v3.7.0 beyondwords_* |
| 199 | 'beyondwords_api_key', |
| 200 | 'beyondwords_prepend_excerpt', |
| 201 | 'beyondwords_preselect', |
| 202 | 'beyondwords_project_id', |
| 203 | 'beyondwords_version', |
| 204 | ]; |
| 205 | |
| 206 | $deprecated = [ |
| 207 | // v4.x |
| 208 | 'beyondwords_languages', |
| 209 | // v3.0.0 speechkit_* |
| 210 | 'speechkit_api_key', |
| 211 | 'speechkit_prepend_excerpt', |
| 212 | 'speechkit_preselect', |
| 213 | 'speechkit_project_id', |
| 214 | 'speechkit_version', |
| 215 | // deprecated < v3.0 |
| 216 | 'speechkit_settings', |
| 217 | 'speechkit_enable', |
| 218 | 'speechkit_id', |
| 219 | 'speechkit_select_post_types', |
| 220 | 'speechkit_selected_categories', |
| 221 | 'speechkit_enable_telemetry', |
| 222 | 'speechkit_rollbar_access_token', |
| 223 | 'speechkit_rollbar_error_notice', |
| 224 | 'speechkit_merge_excerpt', |
| 225 | 'speechkit_enable_marfeel_comp', |
| 226 | 'speechkit_wordpress_cron', |
| 227 | ]; |
| 228 | |
| 229 | return match ($type) { |
| 230 | 'current' => $current, |
| 231 | 'deprecated' => $deprecated, |
| 232 | 'all' => array_merge($current, $deprecated), |
| 233 | default => throw new \Exception('Unexpected $type param for CoreUtils::getOptions()'), |
| 234 | }; |
| 235 | } |
| 236 | } |