enqueueStyles(); // Enqueue scripts $this->enqueueScripts($hook); // Localize script data $this->localizeScripts($hook); } /** * Enqueue stylesheets */ private function enqueueStyles() { wp_enqueue_style( 'easy-invoice-tailwind', 'https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css', array(), EASY_INVOICE_VERSION ); // Main admin styles wp_enqueue_style( 'easy-invoice-builder', EASY_INVOICE_PLUGIN_URL . 'assets/css/easy-invoice.css', array(), EASY_INVOICE_VERSION ); wp_enqueue_style( 'easy-invoice-preview', EASY_INVOICE_PLUGIN_URL . 'assets/css/invoice-preview.css', array(), EASY_INVOICE_VERSION ); // Add additional admin styles based on screen // Add Font Awesome for icons wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css', array(), '6.0.0' ); // Add RTL support wp_style_add_data('easy-invoice-admin', 'rtl', 'replace'); } /** * Enqueue scripts * * @param string $hook The current admin page */ private function enqueueScripts($hook) { // Only load on Easy Invoice pages if (empty($hook) || strpos($hook, 'easy-invoice') === false) { return; } // Common admin scripts wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-core'); wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_script('wp-util'); // Add WordPress core dependencies that provide the 'wp' object wp_enqueue_script('wp-api-fetch'); wp_enqueue_script('wp-i18n'); wp_enqueue_script('wp-a11y'); wp_enqueue_script('wp-hooks'); // Settings page script if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-settings') { wp_enqueue_script( 'easy-invoice-settings', EASY_INVOICE_PLUGIN_URL . 'assets/js/settings.js', array('jquery', 'wp-util', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true ); // Localize the script with necessary data wp_localize_script('easy-invoice-settings', 'easyInvoiceSettings', array( 'nonce' => wp_create_nonce('easy_invoice_settings'), 'ajaxurl' => admin_url('admin-ajax.php') )); return; // Don't load other scripts on settings page } // Load dependencies wp_enqueue_script('jquery-ui-sortable'); // Main admin script wp_enqueue_script('jquery'); // Register and enqueue our scripts 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); wp_enqueue_script('easy-invoice-scripts'); // Conditionally load client manager only on invoice pages (not quote pages or invoice builder) if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) { 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); wp_enqueue_script('easy-invoice-client-manager'); } // Load clients.js on the clients page if (isset($_GET['page']) && $_GET['page'] === PagesSlugs::CLIENTS) { 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); wp_enqueue_script('easy-invoice-clients'); } 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); wp_enqueue_script('easy-invoice-payment-manager'); // Tooltip manager - reusable across the plugin 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); wp_enqueue_script('easy-invoice-tooltip'); // Confirmation modal - reusable across the plugin 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); wp_enqueue_script('easy-invoice-confirmation-modal'); // Toast notification system - global across the plugin 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); wp_enqueue_script('easy-invoice-toast'); // jsPDF for PDF generation - available on all Easy Invoice pages wp_register_script('jspdf', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', true); wp_enqueue_script('jspdf'); // Conditionally load invoice-specific scripts only on invoice pages if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) { // Invoice builder scripts 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); wp_enqueue_script('easy-invoice-builder'); // Use invoice-save.js for comprehensive save functionality 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); wp_enqueue_script('easy-invoice-save'); } // Conditionally load quote-specific scripts only on quote pages if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quotes' && isset($_GET['action']) && $_GET['action'] === 'edit') { // Quote builder scripts - these are loaded directly in the quote builder template // to avoid conflicts with invoice scripts } } /** * Localize script data * * @param string $hook The current admin page */ private function localizeScripts($hook) { global $pagenow, $post; // Get currency settings $settings_controller = new \EasyInvoice\Controllers\SettingsController(); $settings = $settings_controller->getSettings(); $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD'; $currency_position = $settings['easy_invoice_currency_position'] ?? 'left'; $currency_symbol = easy_invoice_get_currency_symbol(); // Default data $script_data = array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('easy_invoice_nonce'), 'i18n' => array( 'confirm_delete' => __('Are you sure you want to delete this invoice?', 'easy-invoice'), 'invoice_deleted' => __('Invoice deleted successfully', 'easy-invoice'), 'client_deleted' => __('Client deleted successfully', 'easy-invoice'), 'confirm_delete_client' => __('Are you sure you want to delete this client?', 'easy-invoice'), 'edit_invoice' => __('Edit Invoice', 'easy-invoice'), 'create_invoice' => __('Create Invoice', 'easy-invoice'), 'save' => __('Save', 'easy-invoice'), 'cancel' => __('Cancel', 'easy-invoice'), 'add_item' => __('Add Item', 'easy-invoice'), 'delete_item' => __('Delete Item', 'easy-invoice'), 'error' => __('An error occurred', 'easy-invoice'), ), 'urls' => array( 'preview' => admin_url('admin.php?action=easy_invoice_preview&invoice_id='), 'edit' => admin_url('admin.php?page=easy-invoice-new&id='), ), 'settings' => array( 'currency_symbol' => $currency_symbol, 'currency_position' => $currency_position, 'currency_code' => $currency_code, 'decimal_separator' => $settings['easy_invoice_decimal_separator'] ?? '.', 'thousand_separator' => $settings['easy_invoice_thousands_separator'] ?? ',', 'decimal_places' => $settings['easy_invoice_decimal_precision'] ?? 2, 'date_format' => get_option('date_format', 'F j, Y'), ), ); // Localize common scripts wp_localize_script('easy-invoice-scripts', 'easyInvoice', $script_data); // Conditionally localize client manager only when it's loaded if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) { wp_localize_script('easy-invoice-client-manager', 'easyInvoice', $script_data); } wp_localize_script('easy-invoice-payment-manager', 'easyInvoice', $script_data); wp_localize_script('easy-invoice-tooltip', 'easyInvoice', $script_data); // Conditionally localize invoice-specific scripts if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) { // Add invoice-specific field configuration $form_manager = new \EasyInvoice\Forms\Invoice\InvoiceFormManager(); $script_data['fieldConfig'] = $form_manager->getFieldConfigForJavaScript(); // Add additional data for the invoice edit page $invoice_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $invoice_items_json = ''; if ($invoice_id > 0) { // Existing invoice - get its data $repository = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository(); $invoice = $repository->find($invoice_id); if ($invoice) { $script_data['invoice_id'] = $invoice_id; $script_data['editMode'] = true; // Get invoice data for JavaScript $invoice_data = $invoice->toArray(); $script_data['invoiceData'] = $invoice_data; // Get invoice items $items = $invoice->getItems(); $items_data = []; foreach ($items as $item) { $items_data[] = [ 'id' => $item->getId(), 'name' => $item->getName(), 'description' => $item->getDescription(), 'quantity' => $item->getQuantity(), 'price' => $item->getPrice(), 'taxable' => $item->isTaxable(), 'total' => $item->getAmount() ]; } $script_data['invoiceItems'] = $items_data; // Get client data if available if ($invoice->getClientId()) { $client_repository = \EasyInvoice\Providers\ClientServiceProvider::getClientRepository(); $client = $client_repository->find($invoice->getClientId()); if ($client) { $script_data['clientData'] = [ 'id' => $client->getId(), 'name' => $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()), 'email' => $client->getEmail(), 'phone' => $client->getExtraInfo(), 'address' => $client->getAddress(), 'company' => $client->getBusinessClientName() ]; } } } } else { // New invoice - set default items $script_data['editMode'] = false; $script_data['invoice_id'] = 0; $default_items = [ [ 'name' => '', 'description' => '', 'quantity' => 1, 'price' => 0, 'taxable' => true ] ]; $script_data['default_items'] = $default_items; } // Localize invoice-specific scripts wp_localize_script('easy-invoice-builder', 'easyInvoice', $script_data); wp_localize_script('easy-invoice-save', 'easyInvoice', $script_data); } // Conditionally localize quote-specific scripts if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quotes' && isset($_GET['action']) && $_GET['action'] === 'edit') { // Quote-specific data is handled in the quote builder template // to avoid conflicts with invoice scripts } } }