Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.73% |
122 / 133 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CoreUtils | |
91.73% |
122 / 133 |
|
0.00% |
0 / 4 |
24.33 | |
0.00% |
0 / 1 |
isGutenbergPage | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
7.39 | |||
isEditScreen | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
8.32 | |||
getPostMetaKeys | |
97.92% |
47 / 48 |
|
0.00% |
0 / 1 |
5 | |||
getOptions | |
92.31% |
60 / 65 |
|
0.00% |
0 / 1 |
5.01 |
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() |
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() |
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 | * Get the BeyondWords post meta keys. |
82 | * |
83 | * @since 4.1.0 |
84 | * |
85 | * @param string $type Type (current|deprecated|all). |
86 | * |
87 | * @throws Exception |
88 | * |
89 | * @return string[] Post meta keys. |
90 | **/ |
91 | public static function getPostMetaKeys($type = 'current') |
92 | { |
93 | $current = [ |
94 | 'beyondwords_generate_audio', |
95 | 'beyondwords_project_id', |
96 | 'beyondwords_content_id', |
97 | 'beyondwords_preview_token', |
98 | 'beyondwords_player_content', |
99 | 'beyondwords_player_style', |
100 | 'beyondwords_language_code', |
101 | 'beyondwords_language_id', // @todo deprecate in v5.6 |
102 | 'beyondwords_title_voice_id', |
103 | 'beyondwords_body_voice_id', |
104 | 'beyondwords_summary_voice_id', |
105 | 'beyondwords_error_message', |
106 | 'beyondwords_disabled', |
107 | 'beyondwords_delete_content', |
108 | ]; |
109 | |
110 | $deprecated = [ |
111 | 'beyondwords_podcast_id', |
112 | 'beyondwords_hash', |
113 | 'publish_post_to_speechkit', |
114 | 'speechkit_hash', |
115 | 'speechkit_generate_audio', |
116 | 'speechkit_project_id', |
117 | 'speechkit_podcast_id', |
118 | 'speechkit_error_message', |
119 | 'speechkit_disabled', |
120 | 'speechkit_access_key', |
121 | 'speechkit_error', |
122 | 'speechkit_info', |
123 | 'speechkit_response', |
124 | 'speechkit_retries', |
125 | 'speechkit_status', |
126 | 'speechkit_updated_at', |
127 | '_speechkit_link', |
128 | '_speechkit_text', |
129 | ]; |
130 | |
131 | $keys = []; |
132 | |
133 | switch ($type) { |
134 | case 'current': |
135 | $keys = $current; |
136 | break; |
137 | case 'deprecated': |
138 | $keys = $deprecated; |
139 | break; |
140 | case 'all': |
141 | $keys = array_merge($current, $deprecated); |
142 | break; |
143 | default: |
144 | throw \Exception('Unexpected $type param for CoreUtils::getPostMetaKeys()'); |
145 | break; |
146 | } |
147 | |
148 | return $keys; |
149 | } |
150 | |
151 | /** |
152 | * Get the BeyondWords post meta keys. |
153 | * |
154 | * @since 4.1.0 |
155 | * |
156 | * @param string $type Type (current|deprecated|all). |
157 | * |
158 | * @throws Exception |
159 | * |
160 | * @return string[] Post meta keys. |
161 | **/ |
162 | public static function getOptions($type = 'current') |
163 | { |
164 | $current = [ |
165 | // v5.x |
166 | 'beyondwords_date_activated', |
167 | 'beyondwords_notice_review_dismissed', |
168 | 'beyondwords_player_call_to_action', |
169 | 'beyondwords_player_clickable_sections', |
170 | 'beyondwords_player_content', |
171 | 'beyondwords_player_highlight_sections', |
172 | 'beyondwords_player_skip_button_style', |
173 | 'beyondwords_player_theme', |
174 | 'beyondwords_player_theme_dark', |
175 | 'beyondwords_player_theme_light', |
176 | 'beyondwords_player_theme_video', |
177 | 'beyondwords_player_widget_position', |
178 | 'beyondwords_player_widget_style', |
179 | 'beyondwords_project_auto_publish_enabled', |
180 | 'beyondwords_project_body_voice_id', |
181 | 'beyondwords_project_body_voice_speaking_rate', |
182 | 'beyondwords_project_language_code', |
183 | 'beyondwords_project_language_id', // @todo deprecate in v5.6 |
184 | 'beyondwords_project_title_enabled', |
185 | 'beyondwords_project_title_voice_id', |
186 | 'beyondwords_project_title_voice_speaking_rate', |
187 | 'beyondwords_video_enabled', |
188 | 'beyondwords_player_ui', |
189 | 'beyondwords_player_style', |
190 | 'beyondwords_player_version', |
191 | 'beyondwords_settings_updated', |
192 | 'beyondwords_valid_api_connection', |
193 | // v3.7.0 beyondwords_* |
194 | 'beyondwords_api_key', |
195 | 'beyondwords_prepend_excerpt', |
196 | 'beyondwords_preselect', |
197 | 'beyondwords_project_id', |
198 | 'beyondwords_version', |
199 | ]; |
200 | |
201 | $deprecated = [ |
202 | // v4.x |
203 | 'beyondwords_languages', |
204 | // v3.0.0 speechkit_* |
205 | 'speechkit_api_key', |
206 | 'speechkit_prepend_excerpt', |
207 | 'speechkit_preselect', |
208 | 'speechkit_project_id', |
209 | 'speechkit_version', |
210 | // deprecated < v3.0 |
211 | 'speechkit_settings', |
212 | 'speechkit_enable', |
213 | 'speechkit_id', |
214 | 'speechkit_select_post_types', |
215 | 'speechkit_selected_categories', |
216 | 'speechkit_enable_telemetry', |
217 | 'speechkit_rollbar_access_token', |
218 | 'speechkit_rollbar_error_notice', |
219 | 'speechkit_merge_excerpt', |
220 | 'speechkit_enable_marfeel_comp', |
221 | 'speechkit_wordpress_cron', |
222 | ]; |
223 | |
224 | $keys = []; |
225 | |
226 | switch ($type) { |
227 | case 'current': |
228 | $keys = $current; |
229 | break; |
230 | case 'deprecated': |
231 | $keys = $deprecated; |
232 | break; |
233 | case 'all': |
234 | $keys = array_merge($current, $deprecated); |
235 | break; |
236 | default: |
237 | throw \Exception('Unexpected $type param for CoreUtils::getOptions()'); |
238 | break; |
239 | } |
240 | |
241 | return $keys; |
242 | } |
243 | } |