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