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