Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
125 / 130
60.00% covered (danger)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Utils
96.15% covered (success)
96.15%
125 / 130
60.00% covered (danger)
60.00%
3 / 5
20
0.00% covered (danger)
0.00%
0 / 1
 is_gutenberg_page
87.50% covered (success)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
7.10
 is_edit_screen
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
12.41
 is_amp
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
6
 get_post_meta_keys
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
1
 get_options
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Cross-cutting helpers and the canonical lists of plugin options + post-meta keys.
4 *
5 * @package BeyondWords\Core
6 * @since   3.5.0
7 * @since   7.0.0 Refactored to BeyondWords namespace with snake_case methods.
8 *                Renamed from CoreUtils to Utils.
9 */
10
11declare( strict_types = 1 );
12
13namespace BeyondWords\Core;
14
15defined( 'ABSPATH' ) || exit;
16
17/**
18 * Core-wide utilities: editor-screen detection and option/meta-key registries.
19 *
20 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
21 */
22class Utils {
23
24    /**
25     * Whether the current admin page is the Gutenberg/Block Editor.
26     *
27     * @link https://wordpress.stackexchange.com/a/324866
28     */
29    public static function is_gutenberg_page(): bool {
30        // phpcs:ignore PHPCompatibility.Extensions.RemovedExtensions.gd_isgutenbergDeprecated -- legacy gutenberg plugin compat
31        if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {
32            return true;
33        }
34
35        $current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
36
37        if ( null === $current_screen ) {
38            return false;
39        }
40
41        if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
42            return true;
43        }
44
45        return false;
46    }
47
48    /**
49     * Whether the current admin screen is a post-edit screen (single or list).
50     */
51    public static function is_edit_screen(): bool {
52        if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
53            return false;
54        }
55
56        $screen = get_current_screen();
57
58        if ( ! $screen instanceof \WP_Screen ) {
59            return false;
60        }
61
62        return 'edit' === $screen->parent_base || 'post' === $screen->base;
63    }
64
65    /**
66     * Whether the current request is being served as AMP.
67     */
68    public static function is_amp(): bool {
69        return (
70            ( function_exists( '\amp_is_request' ) && \amp_is_request() )
71            || ( function_exists( '\ampforwp_is_amp_endpoint' ) && \ampforwp_is_amp_endpoint() )
72            || ( function_exists( '\is_amp_endpoint' ) && \is_amp_endpoint() )
73        );
74    }
75
76    /**
77     * BeyondWords post-meta keys.
78     *
79     * The `current` set is registered with REST + sanitisation; the `deprecated`
80     * set is only used by the uninstaller.
81     *
82     * @param string $type One of `current`, `deprecated`, `all`.
83     *
84     * @return string[]
85     *
86     * @throws \Exception When `$type` is unrecognised.
87     */
88    public static function get_post_meta_keys( string $type = 'current' ): array {
89        // Order matches the editor Inspect panel display/copy order; every other
90        // consumer treats this as a set.
91        $current = [
92            'beyondwords_generate_audio',
93            'beyondwords_project_id',
94            'beyondwords_content_id',
95            'beyondwords_integration_method',
96            'beyondwords_preview_token',
97            'beyondwords_language_code',
98            'beyondwords_language_id',
99            'beyondwords_body_voice_id',
100            'beyondwords_error_message',
101            'beyondwords_delete_content',
102            'beyondwords_source',
103            'beyondwords_output',
104            'beyondwords_script_template_id',
105            'beyondwords_video_template_id',
106            'beyondwords_video_size',
107            'beyondwords_embed',
108        ];
109
110        $deprecated = [
111            'beyondwords_player_content',
112            'beyondwords_player_style',
113            'beyondwords_title_voice_id',
114            'beyondwords_summary_voice_id',
115            'beyondwords_disabled',
116            'beyondwords_podcast_id',
117            'beyondwords_hash',
118            'publish_post_to_speechkit',
119            'speechkit_hash',
120            'speechkit_generate_audio',
121            'speechkit_project_id',
122            'speechkit_podcast_id',
123            'speechkit_error_message',
124            'speechkit_disabled',
125            'speechkit_access_key',
126            'speechkit_error',
127            'speechkit_info',
128            'speechkit_response',
129            'speechkit_retries',
130            'speechkit_status',
131            'speechkit_updated_at',
132            '_speechkit_link',
133            '_speechkit_text',
134        ];
135
136        return match ( $type ) {
137            'current'    => $current,
138            'deprecated' => $deprecated,
139            'all'        => array_merge( $current, $deprecated ),
140            default      => throw new \Exception( 'Unexpected $type param for Utils::get_post_meta_keys()' ),
141        };
142    }
143
144    /**
145     * BeyondWords plugin option keys.
146     *
147     * Removed-in-7.0 settings are grouped into `deprecated` so the uninstaller
148     * still cleans them up for users upgrading from 6.x.
149     *
150     * @param string $type One of `current`, `deprecated`, `all`.
151     *
152     * @return string[]
153     *
154     * @throws \Exception When `$type` is unrecognised.
155     */
156    public static function get_options( string $type = 'current' ): array {
157        $current = [
158            // v7.x.
159            'beyondwords_api_key',
160            'beyondwords_date_activated',
161            'beyondwords_integration_method',
162            'beyondwords_notice_review_dismissed',
163            'beyondwords_player_ui',
164            'beyondwords_prepend_excerpt',
165            'beyondwords_preselect',
166            'beyondwords_project_id',
167            'beyondwords_settings_updated',
168            'beyondwords_valid_api_connection',
169            'beyondwords_version',
170            // Debug tool (extension plugin).
171            'beyondwords_debug_rest_api',
172            'beyondwords_debug_log_token',
173        ];
174
175        $deprecated = [
176            // Removed in v7.0.0 â€” player/project styling moved to the BeyondWords dashboard.
177            'beyondwords_player_call_to_action',
178            'beyondwords_player_clickable_sections',
179            'beyondwords_player_content',
180            'beyondwords_player_highlight_sections',
181            'beyondwords_player_skip_button_style',
182            'beyondwords_player_style',
183            'beyondwords_player_theme',
184            'beyondwords_player_theme_dark',
185            'beyondwords_player_theme_light',
186            'beyondwords_player_theme_video',
187            'beyondwords_player_version',
188            'beyondwords_player_widget_position',
189            'beyondwords_player_widget_style',
190            'beyondwords_project_auto_publish_enabled',
191            'beyondwords_project_body_voice_id',
192            'beyondwords_project_body_voice_speaking_rate',
193            'beyondwords_project_language_code',
194            'beyondwords_project_language_id',
195            'beyondwords_project_title_enabled',
196            'beyondwords_project_title_voice_id',
197            'beyondwords_project_title_voice_speaking_rate',
198            'beyondwords_video_enabled',
199            // v4.x.
200            'beyondwords_languages',
201            // v3.0.0 speechkit_*.
202            'speechkit_api_key',
203            'speechkit_prepend_excerpt',
204            'speechkit_preselect',
205            'speechkit_project_id',
206            'speechkit_version',
207            // Pre-v3.0.
208            'speechkit_settings',
209            'speechkit_enable',
210            'speechkit_id',
211            'speechkit_select_post_types',
212            'speechkit_selected_categories',
213            'speechkit_enable_telemetry',
214            'speechkit_rollbar_access_token',
215            'speechkit_rollbar_error_notice',
216            'speechkit_merge_excerpt',
217            'speechkit_enable_marfeel_comp',
218            'speechkit_wordpress_cron',
219        ];
220
221        return match ( $type ) {
222            'current'    => $current,
223            'deprecated' => $deprecated,
224            'all'        => array_merge( $current, $deprecated ),
225            default      => throw new \Exception( 'Unexpected $type param for Utils::get_options()' ),
226        };
227    }
228}