• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

equalizedigital / accessibility-checker / 42839880401

10 Nov 2026 03:00PM UTC coverage: 83.22%. Remained the same
42839880401

push

github

web-flow
Merge pull request #2022 from faisalahammad/faisal/1982-plugin-path-guard

chore: forbid ad-hoc plugin path and url calls

9 of 13 new or added lines in 7 files covered. (69.23%)

8957 of 10763 relevant lines covered (83.22%)

6.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.41
/admin/class-enqueue-admin.php
1
<?php
2
/**
3
 * Class file for Admin enqueueing styles and scripts.
4
 *
5
 * @package Accessibility_Checker
6
 */
7

8
namespace EDAC\Admin;
9

10
use EDAC\Admin\OptIn\Email_Opt_In;
11
use EqualizeDigital\AccessibilityChecker\Admin\IgnoreUI;
12

13
/**
14
 * Class that initializes and handles enqueueing styles and scripts for the admin.
15
 */
16
class Enqueue_Admin {
17

18

19
        /**
20
         * Constructor
21
         */
22
        public function __construct() {
26 ✔
23
        }
26 ✔
24

25

26
        /**
27
         * Enqueue the scripts and styles.
28
         */
29
        public static function enqueue() {
1 ✔
30
                self::enqueue_styles();
1 ✔
31
                self::maybe_enqueue_admin_and_editor_app_scripts();
1 ✔
32
                self::maybe_enqueue_sidebar_script();
1 ✔
33
                self::maybe_enqueue_issue_modal_script();
1 ✔
34
                self::maybe_enqueue_email_opt_in_script();
1 ✔
35
        }
36

37
        /**
38
         * Enqueue the admin styles.
39
         *
40
         * @return void
41
         */
42
        public static function enqueue_styles() {
1 ✔
43
                wp_enqueue_style( 'edac', EDAC_PLUGIN_URL . 'build/css/admin.css', [], EDAC_VERSION, 'all' );
1 ✔
44
        }
45

46
        /**
47
         * Enqueue the admin and editorApp scripts.
48
         *
49
         * @return void
50
         */
51
        public static function maybe_enqueue_admin_and_editor_app_scripts() {
18 ✔
52

53
                global $pagenow;
18 ✔
54
                $post_types        = Settings::get_scannable_post_types();
18 ✔
55
                $has_post_types    = is_array( $post_types ) && count( $post_types );
18 ✔
56
                $is_scannable_post = Helpers::is_current_post_type_scannable( $post_types );
18 ✔
57
                $page              = self::get_current_page_slug();
18 ✔
58
                $enabled_pages     = apply_filters(
18 ✔
59
                        'edac_filter_admin_scripts_slugs',
18 ✔
60
                        [
18 ✔
61
                                'accessibility_checker',
18 ✔
62
                                'accessibility_checker_settings',
18 ✔
63
                                'accessibility_checker_issues',
18 ✔
64
                                'accessibility_checker_ignored',
18 ✔
65
                        ]
18 ✔
66
                );
18 ✔
67

68
                if (
69
                        (
70
                                $has_post_types &&
18 ✔
71
                                (
18 ✔
72
                                        $is_scannable_post ||
18 ✔
73
                                        in_array( $page, $enabled_pages, true )
18 ✔
74
                                )
18 ✔
75
                        ) ||
76
                        'site-editor.php' !== $pagenow
18 ✔
77
                ) {
78

79
                        global $post;
18 ✔
80
                        $post_id = is_object( $post ) ? $post->ID : null;
18 ✔
81

82
                        // On a latest-posts homepage the global $post is the first blog post, not the page;
83
                        // let extensions supply the correct ID (e.g. a Pro virtual-page ID).
84
                        $post_id = apply_filters( 'edac_filter_admin_post_id', $post_id );
18 ✔
85

86
                        wp_enqueue_script( 'edac', EDAC_PLUGIN_URL . 'build/admin.bundle.js', [ 'jquery', 'wp-a11y' ], EDAC_VERSION, false );
18 ✔
87
                        wp_set_script_translations( 'edac', 'accessibility-checker', EDAC_PLUGIN_DIR . 'languages' );
18 ✔
88
                        wp_localize_script(
18 ✔
89
                                'edac',
18 ✔
90
                                'edac_script_vars',
18 ✔
91
                                [
18 ✔
92
                                        'postID'                   => $post_id,
18 ✔
93
                                        'nonce'                    => wp_create_nonce( 'ajax-nonce' ),
18 ✔
94
                                        'edacApiUrl'               => esc_url_raw( rest_url( 'accessibility-checker/v1' ) ),
18 ✔
95
                                        'fixesRestUrl'             => esc_url_raw( rest_url( 'accessibility-checker/v1' ) ),
18 ✔
96
                                        'restNonce'                => wp_create_nonce( 'wp_rest' ),
18 ✔
97
                                        'proUrl'                   => esc_url_raw( edac_generate_link_type( [ 'utm_content' => '__name__' ] ) ),
18 ✔
98
                                        'hasDismissEndpoint'       => method_exists( \EDAC\Inc\REST_Api::class, 'dismiss_issue' ),
18 ✔
99
                                        'showMetaboxInBlockEditor' => ! Helpers::is_block_editor() || (bool) get_option( 'edac_show_metabox_in_block_editor', 1 ),
18 ✔
100
                                        'pro'                      => defined( 'EDACP_VERSION' ) && EDAC_KEY_VALID,
18 ✔
101
                                ]
18 ✔
102
                        );
18 ✔
103

104
                        if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
18 ✔
105

106
                                // Base the scannable check on the filtered $post_id, not the original global $post.
107
                                $filtered_post_type = $post_id ? get_post_type( $post_id ) : false;
10 ✔
108
                                $active             = $filtered_post_type && is_array( $post_types ) && in_array( $filtered_post_type, $post_types, true );
10 ✔
109

110
                                $pro = defined( 'EDACP_VERSION' ) && EDAC_KEY_VALID;
10 ✔
111

112
                                if ( EDAC_DEBUG || strpos( EDAC_VERSION, '-beta' ) !== false ) {
10 ✔
113
                                        $debug = true; // @codeCoverageIgnore
114
                                } else {
115
                                        $debug = false;
10 ✔
116
                                }
117

118
                                wp_enqueue_script( 'edac-editor-app', EDAC_PLUGIN_URL . 'build/editorApp.bundle.js', false, EDAC_VERSION, false );
10 ✔
119
                                wp_set_script_translations( 'edac-editor-app', 'accessibility-checker', EDAC_PLUGIN_DIR . 'languages' );
10 ✔
120

121
                                // Preview URLs don't work for the homepage. On a latest-posts homepage (including the
122
                                // show_on_front=page fallback with no static front page) use the live home URL instead.
123
                                $show_on_front        = get_option( 'show_on_front', 'posts' );
10 ✔
124
                                $is_latest_posts_home = ( 'posts' === $show_on_front || ( 'page' === $show_on_front && ! get_option( 'page_on_front' ) ) )
10 ✔
125
                                        && apply_filters( 'edac_filter_post_is_latest_posts_home', false, $post_id );
10 ✔
126

127
                                if ( $is_latest_posts_home ) {
10 ✔
128
                                        $scan_url = add_query_arg( 'edac_pageScanner', 1, trailingslashit( get_home_url() ) );
2 ✔
129
                                } elseif ( (int) get_option( 'page_on_front' ) === $post_id || (int) get_option( 'page_for_posts' ) === $post_id ) {
8 ✔
130
                                        $scan_url = add_query_arg( 'edac_pageScanner', 1, get_permalink( $post_id ) );
2 ✔
131
                                } else {
132
                                        $post_view_link = apply_filters(
6 ✔
133
                                                'edac_get_origin_url_for_virtual_page',
6 ✔
134
                                                get_preview_post_link( $post_id ),
6 ✔
135
                                                $post_id
6 ✔
136
                                        );
6 ✔
137

138
                                        $scan_url = add_query_arg(
6 ✔
139
                                                [
6 ✔
140
                                                        'edac_pageScanner' => 1,
6 ✔
141
                                                ],
6 ✔
142
                                                $post_view_link
6 ✔
143
                                        );
6 ✔
144
                                }
145

146
                                wp_localize_script(
10 ✔
147
                                        'edac-editor-app',
10 ✔
148
                                        'edac_editor_app',
10 ✔
149
                                        [
10 ✔
150
                                                'postID'        => $post_id,
10 ✔
151
                                                'edacUrl'       => esc_url_raw( get_site_url() ),
10 ✔
152
                                                'edacApiUrl'    => esc_url_raw( rest_url( 'accessibility-checker/v1' ) ),
10 ✔
153
                                                'baseurl'       => EDAC_PLUGIN_URL,
10 ✔
154
                                                'active'        => $active,
10 ✔
155
                                                'pro'           => $pro,
10 ✔
156
                                                'debug'         => $debug,
10 ✔
157
                                                'scanUrl'       => $scan_url,
10 ✔
158
                                                'maxAltLength'  => max( 1, absint( apply_filters( 'edac_max_alt_length', 300 ) ) ),
10 ✔
159
                                                'landmarkTypes' => edac_get_landmark_types(),
10 ✔
160
                                                'version'       => EDAC_VERSION,
10 ✔
161
                                                'postStatus'    => get_post_status( $post_id ),
10 ✔
162
                                                'restNonce'     => wp_create_nonce( 'wp_rest' ),
10 ✔
163
                                        ]
10 ✔
164
                                );
10 ✔
165

166
                        }
167
                }
168
        }
169

170
        /**
171
         * Enqueue the Gutenberg sidebar script.
172
         *
173
         * @return void
174
         */
175
        public static function maybe_enqueue_sidebar_script() {
4 ✔
176
                global $pagenow;
4 ✔
177

178
                // Only load on post edit screens.
179
                if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
4 ✔
180
                        return;
×
181
                }
182

183
                if ( ! Helpers::is_block_editor() ) {
4 ✔
184
                        return;
1 ✔
185
                }
186

187
                // Check if this post type is scannable.
188
                $post_types = Settings::get_scannable_post_types();
3 ✔
189
                if ( ! Helpers::is_current_post_type_scannable( $post_types ) ) {
3 ✔
190
                        return;
1 ✔
191
                }
192

193
                // Enqueue the sidebar script with WordPress dependencies.
194
                wp_enqueue_script(
2 ✔
195
                        'edac-sidebar',
2 ✔
196
                        EDAC_PLUGIN_URL . 'build/sidebar.bundle.js',
2 ✔
197
                        [
2 ✔
198
                                'wp-plugins',
2 ✔
199
                                'wp-edit-post',
2 ✔
200
                                'wp-editor',
2 ✔
201
                                'wp-element',
2 ✔
202
                                'wp-data',
2 ✔
203
                                'wp-i18n',
2 ✔
204
                                'wp-api-fetch',
2 ✔
205
                                'wp-components',
2 ✔
206
                        ],
2 ✔
207
                        EDAC_VERSION,
2 ✔
208
                        false
2 ✔
209
                );
2 ✔
210

211
                // Set translations for the sidebar.
212
                wp_set_script_translations( 'edac-sidebar', 'accessibility-checker', EDAC_PLUGIN_DIR . 'languages' );
2 ✔
213

214
                // Localize script with necessary data.
215
                wp_localize_script(
2 ✔
216
                        'edac-sidebar',
2 ✔
217
                        'edac_sidebar_app',
2 ✔
218
                        [
2 ✔
219
                                'gutenbergEnabled'        => true,
2 ✔
220
                                'postID'                  => get_the_ID(),
2 ✔
221
                                'highlightNonce'          => wp_create_nonce( 'edac_highlight' ),
2 ✔
222
                                'ajaxNonce'               => wp_create_nonce( 'ajax-nonce' ),
2 ✔
223
                                'ajaxUrl'                 => admin_url( 'admin-ajax.php' ),
2 ✔
224
                                'edacApiUrl'              => esc_url_raw( rest_url() . 'accessibility-checker/v1' ),
2 ✔
225
                                'settingsUrl'             => esc_url_raw( admin_url( 'admin.php?page=accessibility_checker_settings' ) ),
2 ✔
226
                                'canManageSettings'       => current_user_can( apply_filters( 'edac_filter_settings_capability', 'manage_options' ) ),
2 ✔
227
                                'canDismiss'              => edac_user_can_ignore(),
2 ✔
228
                                'canDismissGlobally'      => edac_user_can_dismiss_issues_globally(),
2 ✔
229
                                'readabilityHelpUrl'      => esc_url_raw( edac_link_wrapper( 'https://a11ychecker.com/help3265', 'wordpress-general', 'content-analysis-sidebar', false ) ),
2 ✔
230
                                'dismissReasons'          => IgnoreUI::get_reasons(),
2 ✔
231
                                'simplifiedSummaryPrompt' => get_option( 'edac_simplified_summary_prompt', 'none' ),
2 ✔
232
                        ]
2 ✔
233
                );
2 ✔
234

235
                // Enqueue sidebar styles.
236
                wp_enqueue_style(
2 ✔
237
                        'edac-sidebar',
2 ✔
238
                        EDAC_PLUGIN_URL . 'build/css/sidebar.css',
2 ✔
239
                        [],
2 ✔
240
                        EDAC_VERSION,
2 ✔
241
                        'all'
2 ✔
242
                );
2 ✔
243

244
                // Enqueue CodeMirror for HTML syntax highlighting in the issue modal.
245
                wp_enqueue_code_editor( [ 'type' => 'text/html' ] );
2 ✔
246
        }
247

248
        /**
249
         * Enqueue the issue modal bundle (used in sidebar and other admin areas).
250
         *
251
         * @return void
252
         */
253
        public static function maybe_enqueue_issue_modal_script() {
4 ✔
254
                global $pagenow;
4 ✔
255

256
                // Only load on post edit screens where the sidebar is active.
257
                if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
4 ✔
258
                        return;
1 ✔
259
                }
260

261
                // Check if this post type is scannable.
262
                $post_types = Settings::get_scannable_post_types();
3 ✔
263
                if ( ! Helpers::is_current_post_type_scannable( $post_types ) ) {
3 ✔
264
                        return;
1 ✔
265
                }
266

267
                // Enqueue the issue modal script with WordPress dependencies.
268
                wp_enqueue_script(
2 ✔
269
                        'edac-issue-modal',
2 ✔
270
                        EDAC_PLUGIN_URL . 'build/issueModal.bundle.js',
2 ✔
271
                        [
2 ✔
272
                                'wp-element',
2 ✔
273
                                'wp-components',
2 ✔
274
                                'wp-i18n',
2 ✔
275
                                'wp-api-fetch',
2 ✔
276
                                'wp-html-entities',
2 ✔
277
                                'wp-a11y',
2 ✔
278
                        ],
2 ✔
279
                        EDAC_VERSION,
2 ✔
280
                        true
2 ✔
281
                );
2 ✔
282

283
                // Set translations for the issue modal.
284
                wp_set_script_translations( 'edac-issue-modal', 'accessibility-checker', EDAC_PLUGIN_DIR . 'languages' );
2 ✔
285

286
                // Enqueue issue modal styles.
287
                wp_enqueue_style(
2 ✔
288
                        'edac-issue-modal',
2 ✔
289
                        EDAC_PLUGIN_URL . 'build/css/issueModal.css',
2 ✔
290
                        [ 'wp-components' ],
2 ✔
291
                        EDAC_VERSION,
2 ✔
292
                        'all'
2 ✔
293
                );
2 ✔
294

295
                // Enqueue CodeMirror for HTML syntax highlighting.
296
                wp_enqueue_code_editor( [ 'type' => 'text/html' ] );
2 ✔
297
        }
298

299
        /**
300
         * Enqueue the email opt-in script on the welcome page.
301
         *
302
         * @return void
303
         */
304
        public static function maybe_enqueue_email_opt_in_script() {
3 ✔
305

306
                $page = self::get_current_page_slug();
3 ✔
307
                if ( 'accessibility_checker' !== $page ) {
3 ✔
308
                        return;
1 ✔
309
                }
310

311
                $user_already_opted_in = (bool) get_user_meta( get_current_user_id(), Email_Opt_In::EDAC_USER_OPTIN_META_KEY, true );
2 ✔
312
                if ( $user_already_opted_in ) {
2 ✔
313
                        return;
1 ✔
314
                }
315

316
                $email_opt_in = new Email_Opt_In();
1 ✔
317
                $email_opt_in->enqueue_scripts();
1 ✔
318
        }
319

320

321
        /**
322
         * Enqueue the screen reader only format script and styles for the block editor.
323
         *
324
         * Loads on post edit screens for scannable post types, and also on the
325
         * Full Site Editor (site-editor.php) where there is no post type context.
326
         *
327
         * @return void
328
         */
329
        public static function maybe_enqueue_sr_only_format(): void {
7 ✔
330
                if ( ! self::should_load_sr_only_format() ) {
7 ✔
331
                        return;
3 ✔
332
                }
333

334
                global $pagenow;
4 ✔
335
                $is_fse = 'site-editor.php' === $pagenow;
4 ✔
336

337
                wp_enqueue_script(
4 ✔
338
                        'edac-sr-only-format',
4 ✔
339
                        EDAC_PLUGIN_URL . 'build/srOnlyFormat.bundle.js',
4 ✔
340
                        [ 'wp-rich-text', 'wp-block-editor', 'wp-element', 'wp-i18n', 'wp-plugins', 'wp-editor', 'wp-api-fetch' ],
4 ✔
341
                        EDAC_VERSION,
4 ✔
342
                        false
4 ✔
343
                );
4 ✔
344

345
                wp_set_script_translations( 'edac-sr-only-format', 'accessibility-checker', EDAC_PLUGIN_DIR . 'languages' );
4 ✔
346

347
                wp_localize_script(
4 ✔
348
                        'edac-sr-only-format',
4 ✔
349
                        'edacSrOnlyFormat',
4 ✔
350
                        [
4 ✔
351
                                'showSrTextInEditor' => (bool) get_user_meta( get_current_user_id(), 'show_sr_text_in_editor', true ),
4 ✔
352
                                'isFSE'              => $is_fse,
4 ✔
353
                        ]
4 ✔
354
                );
4 ✔
355
        }
356

357
        /**
358
         * Inject screen reader only format styles into the block editor iframe.
359
         *
360
         * @param array $editor_settings Default editor settings.
361
         * @return array
362
         */
363
        public static function maybe_inject_sr_only_editor_styles( array $editor_settings ): array {
8 ✔
364
                if ( ! self::should_load_sr_only_format() ) {
8 ✔
365
                        return $editor_settings;
1 ✔
366
                }
367

368
                $css = self::get_sr_only_editor_styles();
7 ✔
369
                if ( '' === $css ) {
7 ✔
370
                        return $editor_settings;
×
371
                }
372

373
                if ( ! isset( $editor_settings['styles'] ) || ! is_array( $editor_settings['styles'] ) ) {
7 ✔
374
                        $editor_settings['styles'] = [];
6 ✔
375
                }
376

377
                if ( get_user_meta( get_current_user_id(), 'show_sr_text_in_editor', true ) ) {
7 ✔
378
                        $body_classes = trim( (string) ( $editor_settings['bodyClassName'] ?? '' ) );
2 ✔
379
                        if ( false === strpos( " {$body_classes} ", ' sr-only-show-always ' ) ) {
2 ✔
380
                                $editor_settings['bodyClassName'] = trim( $body_classes . ' sr-only-show-always' );
1 ✔
381
                        }
382
                }
383

384
                $editor_settings['styles'][] = [
7 ✔
385
                        'css' => $css,
7 ✔
386
                ];
7 ✔
387

388
                return $editor_settings;
7 ✔
389
        }
390

391
        /**
392
         * Determine whether the screen reader only format assets should load.
393
         *
394
         * Returns true for the post editor on scannable post types, and also
395
         * for the Full Site Editor where there is no post type context.
396
         *
397
         * @return bool
398
         */
399
        private static function should_load_sr_only_format(): bool {
6 ✔
400
                global $pagenow;
6 ✔
401

402
                $is_post_editor = 'post.php' === $pagenow || 'post-new.php' === $pagenow;
6 ✔
403
                $is_fse         = 'site-editor.php' === $pagenow;
6 ✔
404

405
                if ( ! $is_post_editor && ! $is_fse ) {
6 ✔
406
                        return false;
1 ✔
407
                }
408

409
                if ( ! Helpers::is_block_editor() ) {
5 ✔
410
                        return false;
1 ✔
411
                }
412

413
                // The FSE has no post type context, so always load there.
414
                if ( $is_fse ) {
4 ✔
415
                        return true;
1 ✔
416
                }
417

418
                $post_types = Settings::get_scannable_post_types();
3 ✔
419

420
                return Helpers::is_current_post_type_scannable( $post_types );
3 ✔
421
        }
422

423
        /**
424
         * Build the CSS that should be injected into the block editor iframe.
425
         *
426
         * @return string
427
         */
428
        private static function get_sr_only_editor_styles(): string {
1 ✔
429
                static $cached_css = null;
1 ✔
430

431
                if ( null !== $cached_css ) {
1 ✔
432
                        return $cached_css;
1 ✔
433
                }
434

NEW
435
                $css_file = EDAC_PLUGIN_DIR . 'build/css/srOnlyFormat.css';
×
436
                if ( ! file_exists( $css_file ) || ! is_readable( $css_file ) ) {
×
437
                        $cached_css = '';
×
438
                        return $cached_css;
×
439
                }
440

441
                $css = file_get_contents( $css_file ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown -- Reads a local built CSS asset from the plugin directory.
×
442
                if ( false === $css ) {
×
443
                        $cached_css = '';
×
444
                        return $cached_css;
×
445
                }
446

447
                $css .= sprintf(
×
448
                        '
×
449
                        .is-selected .text-format-sr-only:hover:after,
450
                        .is-selected .text-format-sr-only:focus:after {
451
                                content: %s;
452
                        }',
×
453
                        wp_json_encode( __( '* Screen Reader Text', 'accessibility-checker' ) )
×
454
                );
×
455

456
                $cached_css = $css;
×
457
                return $cached_css;
×
458
        }
459

460
        /**
461
         * Gets the current admin page slug.
462
         *
463
         * @since 1.31.0
464
         * @return string|null The current page slug or null if not set.
465
         */
466
        private static function get_current_page_slug(): ?string {
18 ✔
467
                return isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only.
18 ✔
468
        }
469
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc