PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.1.10
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.1.10
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Admin / AdminAssets.php

AdminAssets.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.1.10, at includes/Admin/AdminAssets.php

335 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Assets Class
4 *
5 * @package Easy_Invoice
6 * @subpackage Admin
7 */
8
9 namespace EasyInvoice\Admin;
10
11 use EasyInvoice\Constants\PagesSlugs;
12
13 /**
14 * AdminAssets Class
15 *
16 * Handles registration and enqueuing of admin CSS and JS assets.
17 */
18 class AdminAssets {
19 /**
20 * Register hooks
21 */
22 public function register() {
23 add_action('admin_enqueue_scripts', array($this, 'enqueueAdminAssets'));
24 }
25
26 /**
27 * Enqueue admin assets
28 *
29 * @param string $hook The current admin page
30 */
31 public function enqueueAdminAssets($hook) {
32 // Only load on our plugin pages
33 if (empty($hook) || strpos($hook, 'easy-invoice') === false) {
34 return;
35 }
36
37 // Enqueue styles
38 $this->enqueueStyles();
39
40 // Enqueue scripts
41 $this->enqueueScripts($hook);
42
43 // Localize script data
44 $this->localizeScripts($hook);
45 }
46
47 /**
48 * Enqueue stylesheets
49 */
50 private function enqueueStyles() {
51
52 wp_enqueue_style(
53 'easy-invoice-tailwind',
54 EASY_INVOICE_PLUGIN_URL . 'assets/lib/tailwind/tailwind.min.css',
55 array(),
56 EASY_INVOICE_VERSION
57 );
58
59 $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
60
61 if(strpos($page, 'easy_invoice') === false) {
62 wp_enqueue_style(
63 'easy-invoice-builder',
64 EASY_INVOICE_PLUGIN_URL . 'assets/css/easy-invoice.css',
65 array(),
66 EASY_INVOICE_VERSION
67 );
68 }
69
70 if($page === 'easy-invoice-builder') {
71 // Main admin styles
72
73 wp_enqueue_style(
74 'easy-invoice-preview',
75 EASY_INVOICE_PLUGIN_URL . 'assets/css/invoice-preview.css',
76 array(),
77 EASY_INVOICE_VERSION
78 );
79 }
80
81 wp_enqueue_style(
82 'font-awesome',
83 EASY_INVOICE_PLUGIN_URL . 'assets/lib/font-awesome/css/all.min.css',
84 array(),
85 EASY_INVOICE_VERSION
86 );
87
88
89 // Add RTL support
90 wp_style_add_data('easy-invoice-admin', 'rtl', 'replace');
91 }
92
93 /**
94 * Enqueue scripts
95 *
96 * @param string $hook The current admin page
97 */
98 private function enqueueScripts($hook) {
99 // Only load on Easy Invoice pages
100 if (empty($hook) || strpos($hook, 'easy-invoice') === false) {
101 return;
102 }
103
104 // Common admin scripts
105 wp_enqueue_script('jquery');
106 wp_enqueue_script('jquery-ui-core');
107 wp_enqueue_script('jquery-ui-datepicker');
108 wp_enqueue_script('wp-util');
109
110 // Add WordPress core dependencies that provide the 'wp' object
111 wp_enqueue_script('wp-api-fetch');
112 wp_enqueue_script('wp-i18n');
113 wp_enqueue_script('wp-a11y');
114 wp_enqueue_script('wp-hooks');
115
116 // Settings page script
117 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-settings') {
118 wp_enqueue_script(
119 'easy-invoice-settings',
120 EASY_INVOICE_PLUGIN_URL . 'assets/js/settings.js',
121 array('jquery', 'wp-util', 'wp-api-fetch', 'wp-i18n'),
122 EASY_INVOICE_VERSION,
123 true
124 );
125
126 // Localize the script with necessary data
127 wp_localize_script('easy-invoice-settings', 'easyInvoiceSettings', array(
128 'nonce' => wp_create_nonce('easy_invoice_settings'),
129 'ajaxurl' => admin_url('admin-ajax.php')
130 ));
131 return; // Don't load other scripts on settings page
132 }
133
134 // Load dependencies
135 wp_enqueue_script('jquery-ui-sortable');
136
137 // Main admin script
138 wp_enqueue_script('jquery');
139
140 // Register and enqueue our scripts
141 wp_register_script('easy-invoice-scripts', EASY_INVOICE_PLUGIN_URL . 'assets/js/easy-invoice.js', array('jquery', 'wp-api-fetch', 'wp-i18n', 'wp-a11y'), EASY_INVOICE_VERSION, true);
142 wp_enqueue_script('easy-invoice-scripts');
143
144 // Conditionally load client manager only on invoice pages (not quote pages or invoice builder)
145 if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
146 wp_register_script('easy-invoice-client-manager', EASY_INVOICE_PLUGIN_URL . 'assets/js/client-manager.js', array('jquery', 'easy-invoice-scripts', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
147 wp_enqueue_script('easy-invoice-client-manager');
148 }
149
150 // Load clients.js on the clients page
151 if (isset($_GET['page']) && $_GET['page'] === PagesSlugs::CLIENTS) {
152 wp_register_script('easy-invoice-clients', EASY_INVOICE_PLUGIN_URL . 'assets/js/clients.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-confirmation-modal', 'easy-invoice-toast', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
153 wp_enqueue_script('easy-invoice-clients');
154 }
155
156 wp_register_script('easy-invoice-payment-manager', EASY_INVOICE_PLUGIN_URL . 'assets/js/payment-manager.js', array('jquery', 'easy-invoice-scripts', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
157 wp_enqueue_script('easy-invoice-payment-manager');
158
159 // Tooltip manager - reusable across the plugin
160 wp_register_script('easy-invoice-tooltip', EASY_INVOICE_PLUGIN_URL . 'assets/js/tooltip-manager.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
161 wp_enqueue_script('easy-invoice-tooltip');
162
163 // Confirmation modal - reusable across the plugin
164 wp_register_script('easy-invoice-confirmation-modal', EASY_INVOICE_PLUGIN_URL . 'assets/js/confirmation-modal.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
165 wp_enqueue_script('easy-invoice-confirmation-modal');
166
167 // Toast notification system - global across the plugin
168 wp_register_script('easy-invoice-toast', EASY_INVOICE_PLUGIN_URL . 'assets/js/easy-invoice-toast.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
169 wp_enqueue_script('easy-invoice-toast');
170
171 // jsPDF for PDF generation - available on all Easy Invoice pages
172 wp_register_script('jspdf', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', true);
173 wp_enqueue_script('jspdf');
174
175
176 // Conditionally load invoice-specific scripts only on invoice pages
177 if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
178 // Invoice builder scripts
179 wp_register_script('easy-invoice-builder', EASY_INVOICE_PLUGIN_URL . 'assets/js/invoice-builder.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
180 wp_enqueue_script('easy-invoice-builder');
181
182 // Use invoice-save.js for comprehensive save functionality
183 wp_register_script('easy-invoice-save', EASY_INVOICE_PLUGIN_URL . 'assets/js/invoice-save.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
184 wp_enqueue_script('easy-invoice-save');
185 }
186
187 // Conditionally load quote-specific scripts only on quote pages
188 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quotes' && isset($_GET['action']) && $_GET['action'] === 'edit') {
189 // Quote builder scripts - these are loaded directly in the quote builder template
190 // to avoid conflicts with invoice scripts
191 }
192 }
193
194 /**
195 * Localize script data
196 *
197 * @param string $hook The current admin page
198 */
199 private function localizeScripts($hook) {
200 global $pagenow, $post;
201
202 // Get currency settings
203 $settings_controller = new \EasyInvoice\Controllers\SettingsController();
204 $settings = $settings_controller->getSettings();
205 $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD';
206 $currency_position = $settings['easy_invoice_currency_position'] ?? 'left';
207 $currency_symbol = easy_invoice_get_currency_symbol();
208
209 // Default data
210 $script_data = array(
211 'ajaxUrl' => admin_url('admin-ajax.php'),
212 'nonce' => wp_create_nonce('easy_invoice_nonce'),
213 'i18n' => array(
214 'confirm_delete' => __('Are you sure you want to delete this invoice?', 'easy-invoice'),
215 'invoice_deleted' => __('Invoice deleted successfully', 'easy-invoice'),
216 'client_deleted' => __('Client deleted successfully', 'easy-invoice'),
217 'confirm_delete_client' => __('Are you sure you want to delete this client?', 'easy-invoice'),
218 'edit_invoice' => __('Edit Invoice', 'easy-invoice'),
219 'create_invoice' => __('Create Invoice', 'easy-invoice'),
220 'save' => __('Save', 'easy-invoice'),
221 'cancel' => __('Cancel', 'easy-invoice'),
222 'add_item' => __('Add Item', 'easy-invoice'),
223 'delete_item' => __('Delete Item', 'easy-invoice'),
224 'error' => __('An error occurred', 'easy-invoice'),
225 ),
226 'urls' => array(
227 'preview' => admin_url('admin.php?action=easy_invoice_preview&invoice_id='),
228 'edit' => admin_url('admin.php?page=easy-invoice-new&id='),
229 ),
230 'settings' => array(
231 'currency_symbol' => $currency_symbol,
232 'currency_position' => $currency_position,
233 'currency_code' => $currency_code,
234 'decimal_separator' => $settings['easy_invoice_decimal_separator'] ?? '.',
235 'thousand_separator' => $settings['easy_invoice_thousands_separator'] ?? ',',
236 'decimal_places' => $settings['easy_invoice_decimal_precision'] ?? 2,
237 'date_format' => get_option('date_format', 'F j, Y'),
238 ),
239 );
240
241 // Localize common scripts
242 wp_localize_script('easy-invoice-scripts', 'easyInvoice', $script_data);
243
244 // Conditionally localize client manager only when it's loaded
245 if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
246 wp_localize_script('easy-invoice-client-manager', 'easyInvoice', $script_data);
247 }
248
249 wp_localize_script('easy-invoice-payment-manager', 'easyInvoice', $script_data);
250 wp_localize_script('easy-invoice-tooltip', 'easyInvoice', $script_data);
251
252 // Conditionally localize invoice-specific scripts
253 if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
254 // Add invoice-specific field configuration
255 $form_manager = new \EasyInvoice\Forms\Invoice\InvoiceFormManager();
256 $script_data['fieldConfig'] = $form_manager->getFieldConfigForJavaScript();
257
258 // Add additional data for the invoice edit page
259 $invoice_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
260 $invoice_items_json = '';
261
262 if ($invoice_id > 0) {
263 // Existing invoice - get its data
264 $repository = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository();
265 $invoice = $repository->find($invoice_id);
266
267 if ($invoice) {
268 $script_data['invoice_id'] = $invoice_id;
269 $script_data['editMode'] = true;
270
271 // Get invoice data for JavaScript
272 $invoice_data = $invoice->toArray();
273 $script_data['invoiceData'] = $invoice_data;
274
275 // Get invoice items
276 $items = $invoice->getItems();
277 $items_data = [];
278 foreach ($items as $item) {
279 $items_data[] = [
280 'id' => $item->getId(),
281 'name' => $item->getName(),
282 'description' => $item->getDescription(),
283 'quantity' => $item->getQuantity(),
284 'price' => $item->getPrice(),
285 'taxable' => $item->isTaxable(),
286 'total' => $item->getAmount()
287 ];
288 }
289 $script_data['invoiceItems'] = $items_data;
290
291 // Get client data if available
292 if ($invoice->getClientId()) {
293 $client_repository = \EasyInvoice\Providers\ClientServiceProvider::getClientRepository();
294 $client = $client_repository->find($invoice->getClientId());
295 if ($client) {
296 $script_data['clientData'] = [
297 'id' => $client->getId(),
298 'name' => $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()),
299 'email' => $client->getEmail(),
300 'phone' => $client->getExtraInfo(),
301 'address' => $client->getAddress(),
302 'company' => $client->getBusinessClientName()
303 ];
304 }
305 }
306 }
307 } else {
308 // New invoice - set default items
309 $script_data['editMode'] = false;
310 $script_data['invoice_id'] = 0;
311 $default_items = [
312 [
313 'name' => '',
314 'description' => '',
315 'quantity' => 0,
316 'price' => 0,
317 'taxable' => true
318 ]
319 ];
320 $script_data['default_items'] = $default_items;
321 }
322
323 // Localize invoice-specific scripts
324 wp_localize_script('easy-invoice-builder', 'easyInvoice', $script_data);
325 wp_localize_script('easy-invoice-save', 'easyInvoice', $script_data);
326 }
327
328 // Conditionally localize quote-specific scripts
329 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quotes' && isset($_GET['action']) && $_GET['action'] === 'edit') {
330 // Quote-specific data is handled in the quote builder template
331 // to avoid conflicts with invoice scripts
332 }
333 }
334 }
335