-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssets.php
More file actions
241 lines (203 loc) · 7.76 KB
/
Assets.php
File metadata and controls
241 lines (203 loc) · 7.76 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Enqueue assets for OneUpdate.
*
* @package OneUpdate
*/
namespace OneUpdate\Modules\Core;
use OneUpdate\Contracts\Interfaces\Registrable;
use OneUpdate\Modules\Settings\Settings;
/**
* Class Assets
*/
class Assets implements Registrable {
/**
* The relative path to the built assets directory.
* No preceding or trailing slashes.
*/
private const ASSETS_DIR = 'build';
/**
* Prefix for all asset handles.
*/
private const PREFIX = 'oneupdate-';
/**
* Asset handles
*/
public const ADMIN_STYLES_HANDLE = self::PREFIX . 'admin';
public const EDITOR_STYLES_HANDLE = self::PREFIX . 'editor';
public const SETTINGS_SCRIPT_HANDLE = self::PREFIX . 'settings';
public const ONBOARDING_SCRIPT_HANDLE = self::PREFIX . 'onboarding';
public const PLUGIN_MANAGER_SCRIPT_HANDLE = self::PREFIX . 'plugin-manager';
public const PULL_REQUESTS_SCRIPT_HANDLE = self::PREFIX . 'pull-requests';
/**
* Localized data for scripts.
*
* @var array<string,mixed>
*/
private static array $localized_data;
/**
* Plugin directory path.
*
* @var string
*/
private string $plugin_dir;
/**
* Plugin URL.
*
* @var string
*/
private string $plugin_url;
/**
* Prepare localized data.
*
* @return array<string,mixed> Localized data array.
*/
public static function get_localized_data(): array {
if ( empty( self::$localized_data ) ) {
self::$localized_data = [
'restUrl' => esc_url( home_url( '/wp-json' ) ),
'restNonce' => wp_create_nonce( 'wp_rest' ),
'api_key' => Settings::get_api_key(),
'settingsLink' => esc_url( admin_url( 'admin.php?page=oneupdate-settings' ) ),
'siteType' => Settings::get_site_type(),
];
}
return self::$localized_data;
}
/**
* Constructor.
*/
public function __construct() {
$this->plugin_dir = (string) ONEUPDATE_DIR;
$this->plugin_url = (string) ONEUPDATE_URL;
}
/**
* {@inheritDoc}
*/
public function register_hooks(): void {
add_action( 'admin_enqueue_scripts', [ $this, 'register_assets' ], 20, 1 );
// Add defer attribute to certain plugin bundles to improve admin load performance.
add_filter( 'script_loader_tag', [ $this, 'defer_scripts' ], 10, 2 );
}
/**
* Register admin assets to WordPress.
*
* Assets are registered once centrally, and enqueued in the modules that need them.
*/
public function register_assets(): void {
$this->register_script( self::SETTINGS_SCRIPT_HANDLE, 'settings' );
$this->register_style( self::SETTINGS_SCRIPT_HANDLE, 'settings' );
$this->register_script( self::ONBOARDING_SCRIPT_HANDLE, 'onboarding' );
$this->register_style( self::ONBOARDING_SCRIPT_HANDLE, 'onboarding', [ 'wp-components' ] );
$this->register_script( self::PLUGIN_MANAGER_SCRIPT_HANDLE, 'plugin-manager' );
$this->register_style( self::PLUGIN_MANAGER_SCRIPT_HANDLE, 'plugin-manager', [ 'wp-components' ] );
$this->register_script( self::PULL_REQUESTS_SCRIPT_HANDLE, 'pull-requests' );
$this->register_style( self::PULL_REQUESTS_SCRIPT_HANDLE, 'pull-requests', [ 'wp-components' ] );
$this->register_style(
self::ADMIN_STYLES_HANDLE,
'admin',
[ 'wp-components' ],
);
wp_enqueue_style( self::ADMIN_STYLES_HANDLE );
}
/**
* Add defer attribute to certain plugin bundle scripts to improve loading performance.
*
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string Modified script tag.
*/
public function defer_scripts( string $tag, string $handle ): string {
$defer_handles = [
self::SETTINGS_SCRIPT_HANDLE,
self::PLUGIN_MANAGER_SCRIPT_HANDLE,
self::PULL_REQUESTS_SCRIPT_HANDLE,
];
// Bail if we don't need to defer.
if ( ! in_array( $handle, $defer_handles, true ) || false !== strpos( $tag, ' defer' ) ) {
return $tag;
}
return str_replace( ' src', ' defer src', $tag );
}
/**
* Register a script.
*
* @param string $handle Name of the script. Should be unique.
* @param string $filename Path of the script relative to js directory.
* excluding the .js extension.
* @param string[] $deps Optional. An array of registered script handles this script depends on. If not set, the dependencies will be inherited from the asset file.
* @param ?string $ver Optional. String specifying script version number, if not set, the version will be inherited from the asset file.
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
*/
private function register_script( string $handle, string $filename, array $deps = [], $ver = null, bool $in_footer = true ): bool {
$asset_file = sprintf( '%s/%s.asset.php', $this->plugin_dir . untrailingslashit( self::ASSETS_DIR ), $filename );
// Bail if the asset file does not exist. Log error and optionally show admin notice.
if ( ! file_exists( $asset_file ) ) {
return false;
}
// phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- The file is checked for existence above.
$asset = require_once $asset_file;
$version = $ver ?? ( $asset['version'] ?? filemtime( $asset_file ) );
$asset_src = sprintf( '%s/%s.js', $this->plugin_url . untrailingslashit( self::ASSETS_DIR ), $filename );
return wp_register_script(
$handle,
$asset_src,
$deps ?: $asset['dependencies'],
$version ?: false,
$in_footer
);
}
/**
* Register a CSS stylesheet
*
* @param string $handle Name of the stylesheet. Should be unique.
* @param string $filename Path of the stylesheet relative to the css directory,
* excluding the .css extension.
* @param string[] $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
* @param ?string $ver Optional. String specifying style version number, if not set, the version will be inherited from the asset file.
*
* @param string $media Optional. The media for which this stylesheet has been defined.
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
* '(orientation: portrait)' and '(max-width: 640px)'.
*/
private function register_style( string $handle, string $filename, array $deps = [], $ver = null, string $media = 'all' ): bool {
// CSS doesnt have a PHP assets file so we infer from the file itself.
$asset_file = sprintf( '%s/%s.css', $this->plugin_dir . untrailingslashit( self::ASSETS_DIR ), $filename );
// Bail if the asset file does not exist.
if ( ! file_exists( $asset_file ) ) {
return false;
}
$version = $ver ?? (string) filemtime( $asset_file );
$asset_src = sprintf( '%s/%s.css', $this->plugin_url . untrailingslashit( self::ASSETS_DIR ), $filename );
// Register as a style.
return wp_register_style(
$handle,
$asset_src,
$deps,
$version ?: false,
$media
);
}
/**
* AJAX handler for plugin search and filter.
*/
public function handle_plugin_search(): void {
check_ajax_referer( 'plugin_search_nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => 'Unauthorized' ], 403 );
}
$search_term = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
$plugins = $this->get_plugins(); // Assume this method fetches the plugin list.
$filtered_plugins = array_filter( $plugins, function ( $plugin ) use ( $search_term ) {
return stripos( $plugin['name'], $search_term ) !== false ||
stripos( $plugin['status'], $search_term ) !== false;
});
wp_send_json_success( [ 'plugins' => $filtered_plugins ] );
}
/**
* Register AJAX actions.
*/
public function register_ajax_actions(): void {
add_action( 'wp_ajax_plugin_search', [ $this, 'handle_plugin_search' ] );
}
}