PluginProbe
WProofreader spell & grammar check plugin for WordPress / trunk
WProofreader spell & grammar check plugin for WordPress vtrunk
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / includes / class-wproofreader-assets.php

class-wproofreader-assets.php in WProofreader spell & grammar check plugin for WordPress trunk, at includes/class-wproofreader-assets.php

269 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Registers and enqueues scripts for admin, editor, frontend, and settings demo.
8 */
9 class WProofreader_Assets {
10
11 /** @var WProofreader */
12 private $plugin;
13
14 /** @var WProofreader_Context_Detector */
15 private $context_detector;
16
17 /** @var WProofreader_Context_Evaluator */
18 private $context_evaluator;
19
20 /** @var WProofreader_Config_Builder */
21 private $config_builder;
22
23 /** @var string */
24 private $plugin_url;
25
26 /** @var string */
27 private $plugin_dir;
28
29 /**
30 * @param WProofreader $plugin Plugin facade.
31 * @param WProofreader_Context_Detector $context_detector Context detector.
32 * @param WProofreader_Context_Evaluator $context_evaluator Context evaluator.
33 * @param WProofreader_Config_Builder $config_builder Config builder.
34 * @param string $plugin_url Plugin URL.
35 * @param string $plugin_dir Plugin directory.
36 */
37 public function __construct(
38 WProofreader $plugin,
39 WProofreader_Context_Detector $context_detector,
40 WProofreader_Context_Evaluator $context_evaluator,
41 WProofreader_Config_Builder $config_builder,
42 string $plugin_url,
43 string $plugin_dir
44 ) {
45 $this->plugin = $plugin;
46 $this->context_detector = $context_detector;
47 $this->context_evaluator = $context_evaluator;
48 $this->config_builder = $config_builder;
49 $this->plugin_url = $plugin_url;
50 $this->plugin_dir = $plugin_dir;
51 }
52
53 /**
54 * Register all plugin scripts.
55 */
56 public function register_scripts() {
57 wp_register_script(
58 WProofreader::SCRIPT_HANDLE_BUNDLE,
59 $this->config_builder->bundle_url(),
60 array(),
61 WProofreader::PLUGIN_VERSION,
62 true
63 );
64
65 $this->register_local_script( WProofreader::SCRIPT_HANDLE_CONFIG, 'assets/proofreaderConfig.js', array(), false );
66 $this->register_local_script( WProofreader::SCRIPT_HANDLE_ENV_CHECKER, 'assets/environmentChecker.js', array(), false );
67 $this->register_local_script( WProofreader::SCRIPT_HANDLE_INSTANCE, 'assets/instance.js', array( 'jquery' ), true );
68 $this->register_local_script( WProofreader::SCRIPT_HANDLE_GUTENBERG_ENV, 'assets/gutenberg-environment.js', array(), true );
69 $this->register_local_script( WProofreader::SCRIPT_HANDLE_CLASSIC_ENV, 'assets/classic-environment.js', array( WProofreader::SCRIPT_HANDLE_BUNDLE ), true );
70 }
71
72 /**
73 * Enqueue assets for the block editor.
74 */
75 public function enqueue_for_block_editor() {
76 if ( ! is_admin() ) {
77 return;
78 }
79
80 $screen = $this->context_detector->get_screen();
81 if ( ! $this->context_detector->is_block_editor_screen( $screen ) || $this->context_detector->is_site_editor_screen( $screen ) ) {
82 return;
83 }
84
85 $context = $this->context_detector->detect();
86 if ( ! $this->context_evaluator->should_enable_filtered( $context ) ) {
87 return;
88 }
89
90 $this->enqueue_runtime_config();
91 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_ENV_CHECKER );
92 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_GUTENBERG_ENV );
93 }
94
95 /**
96 * Enqueue assets for classic admin pages.
97 *
98 * @param string $hook_suffix Hook suffix.
99 */
100 public function enqueue_for_admin( $hook_suffix ) {
101 if ( ! is_admin() ) {
102 return;
103 }
104
105 $screen = $this->context_detector->get_screen();
106 if ( ! $this->is_eligible_admin_screen( $screen ) ) {
107 return;
108 }
109
110 if ( WProofreader_Context_Detector::SCREEN_SETTINGS === ( $screen->base ?? '' ) ) {
111 $this->enqueue_settings_demo();
112 return;
113 }
114
115 $context = $this->context_detector->detect();
116 if ( ! $this->context_evaluator->should_enable_filtered( $context ) ) {
117 return;
118 }
119
120 $badge_config = $this->classic_editor_badge_config( $screen, $context );
121
122 $this->enqueue_runtime_config( $badge_config );
123 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_BUNDLE );
124
125 if ( ! empty( $badge_config['globalBadge'] ) ) {
126 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_CLASSIC_ENV );
127 }
128 }
129
130 /**
131 * Enqueue assets on the frontend.
132 */
133 public function enqueue_for_frontend() {
134 if ( WProofreader::OPTION_ON !== $this->plugin->get_option( WProofreader::SETTING_ENABLE_FRONTEND, WProofreader::OPTION_OFF ) ) {
135 return;
136 }
137
138 $this->enqueue_runtime_config( array( 'globalBadge' => false ) );
139 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_BUNDLE );
140 }
141
142 /**
143 * Enqueue assets inside the Elementor editor shell.
144 */
145 public function enqueue_for_elementor_editor() {
146 // Elementor resets the global WP_Scripts registry before this hook.
147 $this->register_scripts();
148
149 $context = $this->elementor_context();
150 if ( ! $this->context_evaluator->should_enable_filtered( $context ) ) {
151 return;
152 }
153
154 $this->enqueue_runtime_config( array( 'globalBadge' => false ) );
155 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_BUNDLE );
156 }
157
158 /**
159 * @param string $handle Script handle.
160 * @param string $relative_path Relative asset path.
161 * @param array $deps Dependencies.
162 * @param bool $in_footer Whether to load in footer.
163 */
164 private function register_local_script( string $handle, string $relative_path, array $deps, bool $in_footer ) {
165 wp_register_script(
166 $handle,
167 $this->plugin_url . $relative_path,
168 $deps,
169 $this->asset_version_for( $relative_path ),
170 $in_footer
171 );
172 }
173
174 /**
175 * @param string $relative_path Relative asset path.
176 * @return string
177 */
178 private function asset_version_for( string $relative_path ): string {
179 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
180 return (string) time();
181 }
182
183 $file_path = $this->plugin_dir . ltrim( $relative_path, '/\\' );
184
185 if ( ! is_readable( $file_path ) ) {
186 return WProofreader::PLUGIN_VERSION;
187 }
188
189 $mtime = filemtime( $file_path );
190
191 return $mtime ? (string) $mtime : WProofreader::PLUGIN_VERSION;
192 }
193
194 /**
195 * @param WP_Screen|null $screen Screen object.
196 * @return bool
197 */
198 private function is_eligible_admin_screen( $screen ): bool {
199 if ( ! $screen ) {
200 return false;
201 }
202
203 if ( $this->context_detector->is_block_editor_screen( $screen ) || $this->context_detector->is_site_editor_screen( $screen ) ) {
204 return false;
205 }
206
207 if ( 'plugins' === ( $screen->base ?? '' ) ) {
208 return false;
209 }
210
211 return ! $this->context_detector->is_permalinks_settings_screen( $screen );
212 }
213
214 /**
215 * @param WP_Screen|null $screen Screen object.
216 * @param array $context Context.
217 * @return array
218 */
219 private function classic_editor_badge_config( $screen, array $context ): array {
220 $is_single_editor_screen = ( isset( $screen->base ) && 'post' === $screen->base );
221 $post_type = $context['post_type'] ?? '';
222 $is_post_or_page = in_array( $post_type, array( WProofreader::POST_TYPE_POST, WProofreader::POST_TYPE_PAGE ), true );
223
224 return array( 'globalBadge' => (bool) ( $is_single_editor_screen && $is_post_or_page ) );
225 }
226
227
228 /**
229 * Build an editor context for Elementor's custom admin app.
230 *
231 * @return array
232 */
233 private function elementor_context(): array {
234 $post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0;
235 $post_type = $post_id ? get_post_type( $post_id ) : '';
236
237 return array(
238 'place' => WProofreader_Context_Detector::PLACE_POST_TYPE,
239 'screen_id' => 'elementor',
240 'screen_base' => 'elementor',
241 'post_type' => $post_type ? (string) $post_type : WProofreader::POST_TYPE_POST,
242 'taxonomy' => '',
243 );
244 }
245
246 /**
247 * @param array $overrides Config overrides.
248 */
249 private function enqueue_runtime_config( array $overrides = array() ) {
250 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_CONFIG );
251 wp_localize_script( WProofreader::SCRIPT_HANDLE_CONFIG, WProofreader::L10N_OBJECT_CONFIG, $this->config_builder->build( $overrides ) );
252 wp_localize_script( WProofreader::SCRIPT_HANDLE_CONFIG, WProofreader::L10N_OBJECT_SERVICE, $this->config_builder->service_config() );
253 }
254
255 /**
256 * Enqueue the settings page demo instance.
257 */
258 private function enqueue_settings_demo() {
259 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_INSTANCE );
260 wp_localize_script(
261 WProofreader::SCRIPT_HANDLE_INSTANCE,
262 WProofreader::L10N_OBJECT_INSTANCE,
263 $this->config_builder->build_instance( wp_create_nonce( 'webspellchecker-proofreader' ) )
264 );
265 wp_localize_script( WProofreader::SCRIPT_HANDLE_INSTANCE, WProofreader::L10N_OBJECT_SERVICE, $this->config_builder->service_config() );
266 wp_enqueue_script( WProofreader::SCRIPT_HANDLE_BUNDLE );
267 }
268 }
269