-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclass-enqueue-admin.php
More file actions
180 lines (153 loc) · 5.06 KB
/
Copy pathclass-enqueue-admin.php
File metadata and controls
180 lines (153 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Class file for Admin enqueueing styles and scripts.
*
* @package Accessibility_Checker
*/
namespace EDAC\Admin;
use EDAC\Admin\OptIn\Email_Opt_In;
/**
* Class that initializes and handles enqueueing styles and scripts for the admin.
*/
class Enqueue_Admin {
/**
* Constructor
*/
public function __construct() {
}
/**
* Enqueue the scripts and styles.
*/
public static function enqueue() {
self::enqueue_styles();
self::maybe_enqueue_admin_and_editor_app_scripts();
self::maybe_enqueue_email_opt_in_script();
}
/**
* Enqueue the admin styles.
*
* @return void
*/
public static function enqueue_styles() {
wp_enqueue_style( 'edac', plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/css/admin.css', [], EDAC_VERSION, 'all' );
}
/**
* Enqueue the admin and editorApp scripts.
*
* @return void
*/
public static function maybe_enqueue_admin_and_editor_app_scripts() {
global $pagenow;
$post_types = Settings::get_scannable_post_types();
$current_post_type = get_post_type();
$page = self::get_current_page_slug();
$enabled_pages = apply_filters(
'edac_filter_admin_scripts_slugs',
[
'accessibility_checker',
'accessibility_checker_settings',
'accessibility_checker_issues',
'accessibility_checker_ignored',
]
);
if (
(
is_array( $post_types ) &&
count( $post_types ) &&
(
in_array( $current_post_type, $post_types, true ) ||
in_array( $page, $enabled_pages, true )
)
) ||
'site-editor.php' !== $pagenow
) {
global $post;
$post_id = is_object( $post ) ? $post->ID : null;
wp_enqueue_script( 'edac', plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/admin.bundle.js', [ 'jquery' ], EDAC_VERSION, false );
wp_set_script_translations( 'edac', 'accessibility-checker', plugin_dir_path( EDAC_PLUGIN_FILE ) . 'languages' );
wp_localize_script(
'edac',
'edac_script_vars',
[
'postID' => $post_id,
'nonce' => wp_create_nonce( 'ajax-nonce' ),
'edacApiUrl' => esc_url_raw( rest_url() . 'accessibility-checker/v1' ),
'restNonce' => wp_create_nonce( 'wp_rest' ),
'proUrl' => esc_url_raw( edac_generate_link_type( [ 'utm-content' => '__name__' ] ) ),
]
);
if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
// Is this posttype setup to be checked?
$post_types = Settings::get_scannable_post_types();
$current_post_type = get_post_type();
$active = ( is_array( $post_types ) && in_array( $current_post_type, $post_types, true ) );
$pro = defined( 'EDACP_VERSION' ) && EDAC_KEY_VALID;
if ( EDAC_DEBUG || strpos( EDAC_VERSION, '-beta' ) !== false ) {
$debug = true; // @codeCoverageIgnore
} else {
$debug = false;
}
wp_enqueue_script( 'edac-editor-app', plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/editorApp.bundle.js', false, EDAC_VERSION, false );
wp_set_script_translations( 'edac-editor-app', 'accessibility-checker', plugin_dir_path( EDAC_PLUGIN_FILE ) . 'languages' );
// If this is the frontpage or homepage, preview URLs won't work. Use the live URL.
if ( (int) get_option( 'page_on_front' ) === $post_id || (int) get_option( 'page_for_posts' ) === $post_id ) {
$scan_url = add_query_arg( 'edac_pageScanner', 1, get_permalink( $post_id ) );
} else {
$post_view_link = apply_filters(
'edac_get_origin_url_for_virtual_page',
$post_id
);
$scan_url = add_query_arg(
[
'edac_pageScanner' => 1,
],
is_string( $post_view_link ) ? $post_view_link : get_preview_post_link( $post_id )
);
}
wp_localize_script(
'edac-editor-app',
'edac_editor_app',
[
'postID' => $post_id,
'edacUrl' => esc_url_raw( get_site_url() ),
'edacApiUrl' => esc_url_raw( rest_url() . 'accessibility-checker/v1' ),
'baseurl' => plugin_dir_url( __DIR__ ),
'active' => $active,
'pro' => $pro,
'debug' => $debug,
'scanUrl' => $scan_url,
'maxAltLength' => max( 1, absint( apply_filters( 'edac_max_alt_length', 300 ) ) ),
'version' => EDAC_VERSION,
'restNonce' => wp_create_nonce( 'wp_rest' ),
]
);
}
}
}
/**
* Enqueue the email opt-in script on the welcome page.
*
* @return void
*/
public static function maybe_enqueue_email_opt_in_script() {
$page = self::get_current_page_slug();
if ( 'accessibility_checker' !== $page ) {
return;
}
$user_already_opted_in = (bool) get_user_meta( get_current_user_id(), Email_Opt_In::EDAC_USER_OPTIN_META_KEY, true );
if ( $user_already_opted_in ) {
return;
}
$email_opt_in = new Email_Opt_In();
$email_opt_in->enqueue_scripts();
}
/**
* Gets the current admin page slug.
*
* @since 1.31.0
* @return string|null The current page slug or null if not set.
*/
private static function get_current_page_slug(): ?string {
return isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only.
}
}