PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.1.21
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.1.21
2.4.3 2.4.4 2.4.2 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 All 60 releases
easy-invoice / includes / Admin / AdminAssets.php

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

402 lines 18.3 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 // Main plugin layout + header strip alignment (always on Easy Invoice admin screens).
62 wp_enqueue_style(
63 'easy-invoice-main',
64 EASY_INVOICE_PLUGIN_URL . 'assets/css/easy-invoice.css',
65 array(),
66 EASY_INVOICE_VERSION
67 );
68
69 if($page === 'easy-invoice-builder') {
70 // Main admin styles
71
72 wp_enqueue_style(
73 'easy-invoice-preview',
74 EASY_INVOICE_PLUGIN_URL . 'assets/css/invoice-preview.css',
75 array(),
76 EASY_INVOICE_VERSION
77 );
78 }
79
80 wp_enqueue_style(
81 'font-awesome',
82 EASY_INVOICE_PLUGIN_URL . 'assets/lib/font-awesome/css/all.min.css',
83 array(),
84 EASY_INVOICE_VERSION
85 );
86
87
88 // Add RTL support
89 wp_style_add_data('easy-invoice-admin', 'rtl', 'replace');
90 }
91
92 /**
93 * Enqueue scripts
94 *
95 * @param string $hook The current admin page
96 */
97 private function enqueueScripts($hook) {
98 // Only load on Easy Invoice pages
99 if (empty($hook) || strpos($hook, 'easy-invoice') === false) {
100 return;
101 }
102
103 // Common admin scripts
104 wp_enqueue_script('jquery');
105 wp_enqueue_script('jquery-ui-core');
106 wp_enqueue_script('jquery-ui-datepicker');
107 wp_enqueue_script('wp-util');
108
109 // Add WordPress core dependencies that provide the 'wp' object
110 wp_enqueue_script('wp-api-fetch');
111 wp_enqueue_script('wp-i18n');
112 wp_enqueue_script('wp-a11y');
113 wp_enqueue_script('wp-hooks');
114
115 // Settings page script
116 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-settings') {
117 wp_enqueue_script(
118 'easy-invoice-settings',
119 EASY_INVOICE_PLUGIN_URL . 'assets/js/settings.js',
120 array('jquery', 'wp-util', 'wp-api-fetch', 'wp-i18n'),
121 EASY_INVOICE_VERSION,
122 true
123 );
124
125 // Localize the script with necessary data
126 wp_localize_script('easy-invoice-settings', 'easyInvoiceSettings', array(
127 'nonce' => wp_create_nonce('easy_invoice_settings'),
128 'ajaxurl' => admin_url('admin-ajax.php')
129 ));
130 return; // Don't load other scripts on settings page
131 }
132
133 // Load dependencies
134 wp_enqueue_script('jquery-ui-sortable');
135
136 // Main admin script
137 wp_enqueue_script('jquery');
138
139 // Register and enqueue our scripts
140 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);
141 wp_enqueue_script('easy-invoice-scripts');
142
143 // Conditionally load client manager only on invoice pages (not quote pages or invoice builder)
144 if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
145 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);
146 wp_enqueue_script('easy-invoice-client-manager');
147 }
148
149 // Load clients.js on the clients page
150 if (isset($_GET['page']) && $_GET['page'] === PagesSlugs::CLIENTS) {
151 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);
152 wp_enqueue_script('easy-invoice-clients');
153 }
154
155 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);
156 wp_enqueue_script('easy-invoice-payment-manager');
157
158 // Tooltip manager - reusable across the plugin
159 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);
160 wp_enqueue_script('easy-invoice-tooltip');
161
162 // Confirmation modal - reusable across the plugin
163 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);
164 wp_enqueue_script('easy-invoice-confirmation-modal');
165
166 // Toast notification system - global across the plugin
167 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);
168 wp_enqueue_script('easy-invoice-toast');
169
170 // jsPDF for PDF generation - available on all Easy Invoice pages
171 wp_register_script('jspdf', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', true);
172 wp_enqueue_script('jspdf');
173
174
175 // Conditionally load invoice-specific scripts only on invoice pages
176 if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
177 // Invoice builder scripts
178 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);
179 wp_enqueue_script('easy-invoice-builder');
180
181 // Use invoice-save.js for comprehensive save functionality
182 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);
183 wp_enqueue_script('easy-invoice-save');
184 }
185
186 // Quote builder (full-screen editor)
187 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quote-builder') {
188 wp_register_script(
189 'easy-quote-save',
190 EASY_INVOICE_PLUGIN_URL . 'assets/js/quote-save.js',
191 array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'),
192 EASY_INVOICE_VERSION,
193 true
194 );
195 wp_enqueue_script('easy-quote-save');
196 }
197
198 if (isset($_GET['page']) && in_array($_GET['page'], array('easy-invoice-builder', 'easy-invoice-quote-builder'), true)) {
199 wp_enqueue_script(
200 'easy-invoice-builder-mobile-tabs',
201 EASY_INVOICE_PLUGIN_URL . 'assets/js/builder-mobile-tabs.js',
202 array('jquery'),
203 EASY_INVOICE_VERSION,
204 true
205 );
206 wp_enqueue_script(
207 'easy-invoice-builder-ux',
208 EASY_INVOICE_PLUGIN_URL . 'assets/js/builder-ux.js',
209 array('jquery', 'easy-invoice-scripts'),
210 EASY_INVOICE_VERSION,
211 true
212 );
213 }
214 }
215
216 /**
217 * Localize script data
218 *
219 * @param string $hook The current admin page
220 */
221 private function localizeScripts($hook) {
222 global $pagenow, $post;
223
224 // Get currency settings
225 $settings_controller = new \EasyInvoice\Controllers\SettingsController();
226 $settings = $settings_controller->getSettings();
227 $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD';
228 $currency_position = $settings['easy_invoice_currency_position'] ?? 'left';
229 $currency_symbol = easy_invoice_get_currency_symbol();
230
231 $current_admin_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
232
233 // Default data
234 $script_data = array(
235 'ajaxUrl' => admin_url('admin-ajax.php'),
236 'nonce' => wp_create_nonce('easy_invoice_nonce'),
237 'i18n' => array(
238 'confirm_delete' => __('Are you sure you want to delete this invoice?', 'easy-invoice'),
239 'invoice_deleted' => __('Invoice deleted successfully', 'easy-invoice'),
240 'client_deleted' => __('Client deleted successfully', 'easy-invoice'),
241 'confirm_delete_client' => __('Are you sure you want to delete this client?', 'easy-invoice'),
242 'edit_invoice' => __('Edit Invoice', 'easy-invoice'),
243 'create_invoice' => __('Create Invoice', 'easy-invoice'),
244 'save' => __('Save', 'easy-invoice'),
245 'cancel' => __('Cancel', 'easy-invoice'),
246 'add_item' => __('Add Item', 'easy-invoice'),
247 'delete_item' => __('Delete Item', 'easy-invoice'),
248 'error' => __('An error occurred', 'easy-invoice'),
249 ),
250 );
251
252 if (in_array($current_admin_page, array('easy-invoice-builder', 'easy-invoice-quote-builder'), true)) {
253 $script_data['i18n'] = array_merge(
254 $script_data['i18n'],
255 array(
256 'saving' => __('Saving…', 'easy-invoice'),
257 'sending' => __('Sending…', 'easy-invoice'),
258 'save_invoice' => __('Save Invoice', 'easy-invoice'),
259 'update_invoice' => __('Update Invoice', 'easy-invoice'),
260 'save_quote' => __('Save Quote', 'easy-invoice'),
261 'update_quote' => __('Update Quote', 'easy-invoice'),
262 'send_invoice' => __('Send Invoice', 'easy-invoice'),
263 'send_quote' => __('Send Quote', 'easy-invoice'),
264 'save_first_invoice' => __('Please save the invoice before sending.', 'easy-invoice'),
265 'save_first_quote' => __('Please save the quote before sending.', 'easy-invoice'),
266 'email_sent_invoice' => __('Invoice email sent successfully.', 'easy-invoice'),
267 'email_sent_quote' => __('Quote email sent successfully.', 'easy-invoice'),
268 'email_error' => __('Error sending email.', 'easy-invoice'),
269 'network_error' => __('Error connecting to server.', 'easy-invoice'),
270 'tab_editor' => __('Editor', 'easy-invoice'),
271 'tab_preview' => __('Preview', 'easy-invoice'),
272 'confirm_send_invoice_title' => __('Send invoice by email?', 'easy-invoice'),
273 'confirm_send_invoice_message' => __('This will email the invoice to the client using your configured template.', 'easy-invoice'),
274 'confirm_send_quote_title' => __('Send quote by email?', 'easy-invoice'),
275 'confirm_send_quote_message' => __('This will email the quote to the client using your configured template.', 'easy-invoice'),
276 'confirm_send_confirm' => __('Send', 'easy-invoice'),
277 'confirm_cancel' => __('Cancel', 'easy-invoice'),
278 'builder_unsaved_warning' => __('You have unsaved changes. If you leave this page, your changes may be lost.', 'easy-invoice'),
279 'builder_title_hint' => __('The title above matches the document title field in the editor.', 'easy-invoice'),
280 'builder_shell_hint' => __('On smaller screens, use the tabs to switch between the editor and the live preview.', 'easy-invoice'),
281 'builder_shell_hint_dismiss' => __('Got it', 'easy-invoice'),
282 'send_invoice_confirm_browser' => __('Send this invoice by email?', 'easy-invoice'),
283 'send_quote_confirm_browser' => __('Send this quote by email?', 'easy-invoice'),
284 )
285 );
286 }
287
288 $script_data = array_merge(
289 $script_data,
290 array(
291 'urls' => array(
292 'preview' => admin_url('admin.php?action=easy_invoice_preview&invoice_id='),
293 'edit' => admin_url('admin.php?page=easy-invoice-new&id='),
294 ),
295 'settings' => array(
296 'currency_symbol' => $currency_symbol,
297 'currency_position' => $currency_position,
298 'currency_code' => $currency_code,
299 'decimal_separator' => $settings['easy_invoice_decimal_separator'] ?? '.',
300 'thousand_separator' => $settings['easy_invoice_thousands_separator'] ?? ',',
301 'decimal_places' => $settings['easy_invoice_decimal_precision'] ?? 2,
302 'date_format' => get_option('date_format', 'F j, Y'),
303 ),
304 'showAdjustField' => \EasyInvoice\Controllers\SettingsController::shouldShowInvoiceAdjustField(),
305 )
306 );
307
308 // Localize common scripts
309 wp_localize_script('easy-invoice-scripts', 'easyInvoice', $script_data);
310
311 // Conditionally localize client manager only when it's loaded
312 if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
313 wp_localize_script('easy-invoice-client-manager', 'easyInvoice', $script_data);
314 }
315
316 wp_localize_script('easy-invoice-payment-manager', 'easyInvoice', $script_data);
317 wp_localize_script('easy-invoice-tooltip', 'easyInvoice', $script_data);
318
319 // Conditionally localize invoice-specific scripts
320 if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
321 // Add invoice-specific field configuration
322 $form_manager = new \EasyInvoice\Forms\Invoice\InvoiceFormManager();
323 $script_data['fieldConfig'] = $form_manager->getFieldConfigForJavaScript();
324
325 // Add additional data for the invoice edit page
326 $invoice_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
327 $invoice_items_json = '';
328
329 if ($invoice_id > 0) {
330 // Existing invoice - get its data
331 $repository = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository();
332 $invoice = $repository->find($invoice_id);
333
334 if ($invoice) {
335 $script_data['invoice_id'] = $invoice_id;
336 $script_data['editMode'] = true;
337
338 // Get invoice data for JavaScript
339 $invoice_data = $invoice->toArray();
340 $script_data['invoiceData'] = $invoice_data;
341
342 // Get invoice items
343 $items = $invoice->getItems();
344 $items_data = [];
345 foreach ($items as $item) {
346 $items_data[] = [
347 'id' => $item->getId(),
348 'name' => $item->getName(),
349 'description' => $item->getDescription(),
350 'quantity' => $item->getQuantity(),
351 'price' => $item->getPrice(),
352 'taxable' => $item->isTaxable(),
353 'total' => $item->getAmount()
354 ];
355 }
356 $script_data['invoiceItems'] = $items_data;
357
358 // Get client data if available
359 if ($invoice->getClientId()) {
360 $client_repository = \EasyInvoice\Providers\ClientServiceProvider::getClientRepository();
361 $client = $client_repository->find($invoice->getClientId());
362 if ($client) {
363 $script_data['clientData'] = [
364 'id' => $client->getId(),
365 'name' => $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()),
366 'email' => $client->getEmail(),
367 'phone' => $client->getExtraInfo(),
368 'address' => $client->getAddress(),
369 'company' => $client->getBusinessClientName()
370 ];
371 }
372 }
373 }
374 } else {
375 // New invoice - set default items
376 $script_data['editMode'] = false;
377 $script_data['invoice_id'] = 0;
378 $default_items = [
379 [
380 'name' => '',
381 'description' => '',
382 'quantity' => 0,
383 'price' => 0,
384 'taxable' => true
385 ]
386 ];
387 $script_data['default_items'] = $default_items;
388 }
389
390 // Localize invoice-specific scripts
391 wp_localize_script('easy-invoice-builder', 'easyInvoice', $script_data);
392 wp_localize_script('easy-invoice-save', 'easyInvoice', $script_data);
393 }
394
395 if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quote-builder') {
396 // Override showAdjustField for quote pages
397 $script_data['showAdjustField'] = \EasyInvoice\Controllers\SettingsController::shouldShowQuoteAdjustField();
398 wp_localize_script('easy-quote-save', 'easyInvoice', $script_data);
399 }
400 }
401 }
402