-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-admin-assets.php
More file actions
180 lines (164 loc) · 5.16 KB
/
Copy pathclass-admin-assets.php
File metadata and controls
180 lines (164 loc) · 5.16 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
/**
* Admin Assets Controller
*
* Handles enqueuing of admin CSS and JavaScript assets.
*
* @package MSKD\Admin
* @since 2.0.0
*/
namespace MSKD\Admin;
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Admin_Assets
*
* Controller for admin asset management.
*/
class Admin_Assets {
/**
* Plugin pages prefix.
*/
const PAGE_PREFIX = 'mskd-';
/**
* Enqueue admin assets.
*
* @param string $hook Current admin page hook.
* @return void
*/
public function enqueue( string $hook ): void {
// Load minimal styles on the WordPress dashboard for the widget.
if ( $this->is_wordpress_dashboard( $hook ) ) {
$this->enqueue_dashboard_widget_styles();
return;
}
// Only load full assets on plugin pages.
if ( ! $this->is_plugin_page( $hook ) ) {
return;
}
$this->enqueue_vendor_assets();
$this->enqueue_plugin_assets();
$this->localize_scripts();
}
/**
* Check if current page is the WordPress dashboard.
*
* @param string $hook Current admin page hook.
* @return bool True if on WordPress dashboard.
*/
private function is_wordpress_dashboard( string $hook ): bool {
return 'index.php' === $hook;
}
/**
* Check if current page is a plugin page.
*
* @param string $hook Current admin page hook.
* @return bool True if on a plugin page.
*/
private function is_plugin_page( string $hook ): bool {
return false !== strpos( $hook, self::PAGE_PREFIX );
}
/**
* Enqueue minimal styles for the dashboard widget.
*
* @return void
*/
private function enqueue_dashboard_widget_styles(): void {
wp_enqueue_style(
'mskd-admin-style',
MSKD_PLUGIN_URL . 'admin/css/admin-style.css',
array(),
MSKD_VERSION
);
}
/**
* Enqueue vendor assets (SlimSelect).
*
* @return void
*/
private function enqueue_vendor_assets(): void {
// SlimSelect CSS.
wp_enqueue_style(
'slimselect',
'https://cdn.jsdelivr.net/npm/slim-select@2.9.2/dist/slimselect.min.css',
array(),
'2.9.2'
);
// SlimSelect JS.
wp_enqueue_script(
'slimselect',
'https://cdn.jsdelivr.net/npm/slim-select@2.9.2/dist/slimselect.min.js',
array(),
'2.9.2',
false
);
}
/**
* Enqueue plugin-specific assets.
*
* @return void
*/
private function enqueue_plugin_assets(): void {
wp_enqueue_style(
'mskd-admin-style',
MSKD_PLUGIN_URL . 'admin/css/admin-style.css',
array( 'slimselect' ),
MSKD_VERSION
);
wp_enqueue_script(
'mskd-admin-script',
MSKD_PLUGIN_URL . 'admin/js/admin-script.js',
array( 'jquery', 'slimselect' ),
MSKD_VERSION,
true
);
}
/**
* Localize scripts with translations and configuration.
*
* @return void
*/
private function localize_scripts(): void {
wp_localize_script(
'mskd-admin-script',
'mskd_admin',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'mskd_admin_nonce' ),
'preview_nonce' => wp_create_nonce( 'mskd_preview_nonce' ),
'strings' => $this->get_localized_strings(),
)
);
}
/**
* Get localized strings for JavaScript.
*
* @return array<string, string>
*/
private function get_localized_strings(): array {
return array(
'confirm_delete' => __( 'Are you sure you want to delete?', 'mail-system' ),
'sending' => __( 'Sending...', 'mail-system' ),
'success' => __( 'Success!', 'mail-system' ),
'error' => __( 'Error!', 'mail-system' ),
'timeout' => __( 'Connection timed out. Check SMTP settings.', 'mail-system' ),
'datetime_past' => __( 'Please select a future date and time.', 'mail-system' ),
'processing' => __( 'Processing...', 'mail-system' ),
'confirm_truncate_subscribers' => __( 'Are you sure you want to delete ALL subscribers? This action cannot be undone!', 'mail-system' ),
'confirm_truncate_lists' => __( 'Are you sure you want to delete ALL lists? This action cannot be undone!', 'mail-system' ),
'confirm_truncate_queue' => __( 'Are you sure you want to delete ALL campaigns? This action cannot be undone!', 'mail-system' ),
'confirm_delete_inactive_subscribers' => __( 'Are you sure you want to delete all unconfirmed, inactive, and unsubscribed subscribers? This action cannot be undone!', 'mail-system' ),
'select_lists_placeholder' => __( 'Select lists...', 'mail-system' ),
'search_placeholder' => __( 'Search...', 'mail-system' ),
'no_results' => __( 'No results found', 'mail-system' ),
'copied' => __( 'Copied!', 'mail-system' ),
'copy_error' => __( 'Copy failed', 'mail-system' ),
'no_subscribers_selected' => __( 'No subscribers selected.', 'mail-system' ),
'no_lists_selected' => __( 'No lists selected.', 'mail-system' ),
'status_active' => _x( 'Active', 'subscriber status', 'mail-system' ),
'status_inactive' => _x( 'Inactive', 'subscriber status', 'mail-system' ),
);
}
}