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 / EasyInvoice.php

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

757 lines 28.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main EasyInvoice Plugin Class
4 *
5 * @package EasyInvoice
6 * @author Your Name
7 * @copyright Copyright (c) 2023, Your Company
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 */
11
12 namespace EasyInvoice;
13
14 use EasyInvoice\PostTypes\InvoicePostType;
15 use EasyInvoice\PostTypes\PaymentPostType;
16
17 /**
18 * EasyInvoice main plugin class.
19 *
20 * @since 1.0.0
21 */
22 class EasyInvoice {
23 /**
24 * Plugin instance.
25 *
26 * @since 1.0.0
27 * @access private
28 * @var EasyInvoice
29 */
30 private static $instance = null;
31
32 /**
33 * Payment gateway manager instance.
34 *
35 * @since 1.0.0
36 * @access private
37 * @var PaymentGatewayManager
38 */
39 private $gateway_manager;
40
41 /**
42 * Get plugin instance.
43 *
44 * @since 1.0.0
45 * @return EasyInvoice
46 */
47 public static function getInstance(): EasyInvoice {
48 if ( null === self::$instance ) {
49 self::$instance = new self();
50 }
51 return self::$instance;
52 }
53
54 /**
55 * Initialize the plugin.
56 *
57 * @since 1.0.0
58 * @return void
59 */
60 public function init() {
61 // Define plugin constants.
62 $this->defineConstants();
63
64 // Initialize payment gateways first.
65 $this->initPaymentGateways();
66
67 // Note: Post types are now registered separately with proper timing
68
69 // Initialize template loader
70 $template_loader = new TemplateLoader();
71 $template_loader->init();
72
73 // Flush rewrite rules on first load to ensure public quote URLs work
74 if (get_option('easy_invoice_flush_rewrite_rules', false) === false) {
75 add_action('init', function() {
76 flush_rewrite_rules();
77 update_option('easy_invoice_flush_rewrite_rules', true);
78 }, 20);
79 }
80
81 // Also flush rewrite rules when post types are registered
82 add_action('init', function() {
83 if (get_option('easy_invoice_post_types_registered', false) === false) {
84 flush_rewrite_rules();
85 update_option('easy_invoice_post_types_registered', true);
86 }
87 }, 25);
88
89 // Initialize admin functionality if in admin area.
90 if ( is_admin() ) {
91 $this->initAdmin();
92 }
93
94
95
96 // Initialize background services
97 \EasyInvoice\Services\QuoteExpirationService::init();
98 }
99
100 /**
101 * Define plugin constants.
102 *
103 * @since 1.0.0
104 * @access private
105 * @return void
106 */
107 private function defineConstants(): void {
108 if ( ! defined( 'EASY_INVOICE_VERSION' ) ) {
109 define( 'EASY_INVOICE_VERSION', '1.0.0' );
110 }
111
112 if ( ! defined( 'EASY_INVOICE_FILE' ) ) {
113 define( 'EASY_INVOICE_FILE', dirname( dirname( __FILE__ ) ) . '/easy-invoice.php' );
114 }
115
116 if ( ! defined( 'EASY_INVOICE_PATH' ) ) {
117 define( 'EASY_INVOICE_PATH', plugin_dir_path( EASY_INVOICE_FILE ) );
118 }
119
120 if ( ! defined( 'EASY_INVOICE_URL' ) ) {
121 define( 'EASY_INVOICE_URL', plugin_dir_url( EASY_INVOICE_FILE ) );
122 }
123
124 if ( ! defined( 'EASY_INVOICE_ASSETS_URL' ) ) {
125 define( 'EASY_INVOICE_ASSETS_URL', EASY_INVOICE_URL . 'assets/' );
126 }
127 }
128
129 /**
130 * Initialize admin functionality.
131 *
132 * @since 1.0.0
133 * @access private
134 * @return void
135 */
136 private function initAdmin() {
137 // Load admin class if we're in the admin area.
138 $admin = new Admin\EasyInvoiceAdmin();
139 $admin->init();
140
141 // Initialize AJAX handlers.
142 $ajax = new Admin\EasyInvoiceAjax();
143 $ajax->init();
144
145 // Show draft invoices in the main list.
146 add_action( 'pre_get_posts', [ $this, 'modifyInvoiceAdminQuery' ] );
147
148 // Add custom columns to invoice list.
149 add_filter( 'manage_easy_invoice_posts_columns', [ $this, 'addInvoiceColumns' ] );
150 add_action( 'manage_easy_invoice_posts_custom_column', [ $this, 'populateInvoiceColumns' ], 10, 2 );
151
152 // Add custom columns to payment list.
153 add_filter( 'manage_easy_invoice_payment_posts_columns', [ $this, 'addPaymentColumns' ] );
154 add_action( 'manage_easy_invoice_payment_posts_custom_column', [ $this, 'populatePaymentColumns' ], 10, 2 );
155
156 // Add admin action to flush rewrite rules
157 add_action('admin_post_flush_easy_invoice_rewrite_rules', [$this, 'handleFlushRewriteRules']);
158
159 // Add admin action to fix quote slugs
160 add_action('admin_post_fix_easy_invoice_quote_slugs', [$this, 'handleFixQuoteSlugs']);
161
162 // Add admin action to manually register post types
163 add_action('admin_post_register_easy_invoice_post_types', [$this, 'handleRegisterPostTypes']);
164
165 // Disable admin notices on Easy Invoice pages for clean UI
166 add_action( 'admin_notices', [ $this, 'disableAdminNoticesOnEasyInvoicePages' ], 1 );
167 }
168
169 /**
170 * Modify the main query for easy_invoice post type in admin to include drafts.
171 *
172 * @since 1.0.0
173 * @param \WP_Query $query The WP_Query instance (passed by reference).
174 * @return void
175 */
176 public function modifyInvoiceAdminQuery( \WP_Query $query ) {
177 // Check if we are on the main query for the 'easy_invoice' post type admin page.
178 if ( ! is_admin() || ! $query->is_main_query() || $query->get( 'post_type' ) !== 'easy_invoice' ) {
179 return;
180 }
181
182 // Check if a specific post_status is already set in the query.
183 $current_status = $query->get( 'post_status' );
184 if ( empty( $current_status ) || $current_status === '' ||
185 ( is_array( $current_status ) && count( $current_status ) === 1 && $current_status[0] === 'any' ) ) {
186
187 // Also check for explicit views like 'all'.
188 if ( isset( $_GET['post_status'] ) && $_GET['post_status'] !== 'all' ) {
189 return;
190 }
191
192 // Include all needed statuses.
193 $query->set( 'post_status', [
194 'publish',
195 'private',
196 'draft',
197 'pending',
198 'future',
199 'pending-bank',
200 'pending-cheque'
201 ] );
202 }
203 }
204
205 /**
206 * Initialize payment gateways.
207 *
208 * @since 1.0.0
209 * @access private
210 * @return void
211 */
212 private function initPaymentGateways() {
213 if ( ! $this->gateway_manager ) {
214 $this->gateway_manager = new PaymentGatewayManager();
215
216 // Register payment gateways.
217 $gateways = [
218 new Gateways\PayPalGateway(),
219 ];
220
221 foreach ( $gateways as $gateway ) {
222 $this->gateway_manager->registerGateway( $gateway );
223 }
224
225 // Initialize all gateways.
226 $this->gateway_manager->initGateways();
227
228 // Set default payment methods if not already set.
229 $this->setDefaultPaymentMethods();
230 }
231 }
232
233 /**
234 * Set default payment methods and their details.
235 *
236 * @since 1.0.0
237 * @access private
238 * @return void
239 */
240 private function setDefaultPaymentMethods() {
241 $payment_methods = get_option( 'easy_invoice_payment_methods', [] );
242
243 $defaults_updated = false;
244
245 // Bank Transfer, Cheque, and Cash payment defaults moved to Pro plugin
246
247 if ( $defaults_updated ) {
248 update_option( 'easy_invoice_payment_methods', array_unique( $payment_methods ) );
249 }
250 }
251
252 /**
253 * Get payment gateway manager.
254 *
255 * @since 1.0.0
256 * @return PaymentGatewayManager
257 */
258 public function getGatewayManager(): PaymentGatewayManager {
259 if ( ! $this->gateway_manager ) {
260 $this->initPaymentGateways();
261 }
262 return $this->gateway_manager;
263 }
264
265 /**
266 * Register custom post types.
267 *
268 * @since 1.0.0
269 * @return void
270 */
271 public function registerPostTypes() {
272 // Register Invoice post type.
273 register_post_type( \EasyInvoice\Constants\PostTypes::EASY_INVOICE_POST_TYPE, [
274 'labels' => [
275 'name' => _x( 'Invoices', 'post type general name', 'easy-invoice' ),
276 'singular_name' => _x( 'Invoice', 'post type singular name', 'easy-invoice' ),
277 'menu_name' => _x( 'Invoices', 'admin menu', 'easy-invoice' ),
278 'name_admin_bar' => _x( 'Invoice', 'add new on admin bar', 'easy-invoice' ),
279 'add_new' => _x( 'Add New', 'invoice', 'easy-invoice' ),
280 'add_new_item' => __( 'Add New Invoice', 'easy-invoice' ),
281 'new_item' => __( 'New Invoice', 'easy-invoice' ),
282 'edit_item' => __( 'Edit Invoice', 'easy-invoice' ),
283 'view_item' => __( 'View Invoice', 'easy-invoice' ),
284 'all_items' => __( 'All Invoices', 'easy-invoice' ),
285 'search_items' => __( 'Search Invoices', 'easy-invoice' ),
286 'parent_item_colon' => __( 'Parent Invoices:', 'easy-invoice' ),
287 'not_found' => __( 'No invoices found.', 'easy-invoice' ),
288 'not_found_in_trash' => __( 'No invoices found in Trash.', 'easy-invoice' )
289 ],
290 'description' => __( 'Invoices for Easy Invoice plugin.', 'easy-invoice' ),
291 'public' => true,
292 'publicly_queryable' => true,
293 'show_ui' => false,
294 'show_in_menu' => false,
295 'query_var' => true,
296 'rewrite' => [ 'slug' => 'invoice' ],
297 'capability_type' => 'post',
298 'has_archive' => false,
299 'hierarchical' => false,
300 'menu_position' => null,
301 'supports' => [ 'title', 'editor', 'custom-fields' ]
302 ]);
303
304 // Register Quote post type.
305 $quote_post_type = \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE;
306
307 $result = register_post_type( $quote_post_type, [
308 'labels' => [
309 'name' => _x( 'Quotes', 'post type general name', 'easy-invoice' ),
310 'singular_name' => _x( 'Quote', 'post type singular name', 'easy-invoice' ),
311 'menu_name' => _x( 'Quotes', 'admin menu', 'easy-invoice' ),
312 'name_admin_bar' => _x( 'Quote', 'add new on admin bar', 'easy-invoice' ),
313 'add_new' => _x( 'Add New', 'quote', 'easy-invoice' ),
314 'add_new_item' => __( 'Add New Quote', 'easy-invoice' ),
315 'new_item' => __( 'New Quote', 'easy-invoice' ),
316 'edit_item' => __( 'Edit Quote', 'easy-invoice' ),
317 'view_item' => __( 'View Quote', 'easy-invoice' ),
318 'all_items' => __( 'All Quotes', 'easy-invoice' ),
319 'search_items' => __( 'Search Quotes', 'easy-invoice' ),
320 'parent_item_colon' => __( 'Parent Quotes:', 'easy-invoice' ),
321 'not_found' => __( 'No quotes found.', 'easy-invoice' ),
322 'not_found_in_trash' => __( 'No quotes found in Trash.', 'easy-invoice' )
323 ],
324 'description' => __( 'Quotes for Easy Invoice plugin.', 'easy-invoice' ),
325 'public' => true,
326 'publicly_queryable' => true,
327 'show_ui' => false,
328 'show_in_menu' => false,
329 'query_var' => true,
330 'rewrite' => [ 'slug' => 'easy-invoice-quote', 'with_front' => false ],
331 'capability_type' => 'post',
332 'has_archive' => false,
333 'hierarchical' => false,
334 'menu_position' => null,
335 'supports' => [ 'title', 'editor', 'custom-fields' ]
336 ]);
337
338
339
340 // Register Payment post type.
341 register_post_type( 'easy_invoice_payment', [
342 'labels' => [
343 'name' => _x( 'Payments', 'post type general name', 'easy-invoice' ),
344 'singular_name' => _x( 'Payment', 'post type singular name', 'easy-invoice' ),
345 'menu_name' => _x( 'Payments', 'admin menu', 'easy-invoice' ),
346 'name_admin_bar' => _x( 'Payment', 'add new on admin bar', 'easy-invoice' ),
347 'add_new' => _x( 'Add New', 'payment', 'easy-invoice' ),
348 'add_new_item' => __( 'Add New Payment', 'easy-invoice' ),
349 'new_item' => __( 'New Payment', 'easy-invoice' ),
350 'edit_item' => __( 'Edit Payment', 'easy-invoice' ),
351 'view_item' => __( 'View Payment', 'easy-invoice' ),
352 'all_items' => __( 'All Payments', 'easy-invoice' ),
353 'search_items' => __( 'Search Payments', 'easy-invoice' ),
354 'parent_item_colon' => __( 'Parent Payments:', 'easy-invoice' ),
355 'not_found' => __( 'No payments found.', 'easy-invoice' ),
356 'not_found_in_trash' => __( 'No payments found in Trash.', 'easy-invoice' )
357 ],
358 'description' => __( 'Payments for Easy Invoice plugin.', 'easy-invoice' ),
359 'public' => false,
360 'publicly_queryable' => false,
361 'show_ui' => true,
362 'show_in_menu' => 'edit.php?post_type=easy_invoice',
363 'query_var' => true,
364 'rewrite' => [ 'slug' => 'payment' ],
365 'capability_type' => 'post',
366 'has_archive' => false,
367 'hierarchical' => false,
368 'menu_position' => null,
369 'supports' => [ 'title', 'author', 'custom-fields' ],
370 'show_in_rest' => false,
371 ] );
372
373
374 // Force flush rewrite rules after post type registration
375 $this->flushRewriteRules();
376
377 // Force an immediate rewrite rules flush
378 flush_rewrite_rules(true);
379 }
380
381 /**
382 * Flush rewrite rules to ensure custom post type URLs work
383 */
384 private function flushRewriteRules() {
385 // Only flush if we haven't done it recently
386 $last_flush = get_option('easy_invoice_last_rewrite_flush', 0);
387 $current_time = time();
388
389 if ($current_time - $last_flush > 300) { // 5 minutes
390 flush_rewrite_rules();
391 update_option('easy_invoice_last_rewrite_flush', $current_time);
392 }
393 }
394
395 /**
396 * Manually flush rewrite rules (public method for admin use)
397 */
398 public function forceFlushRewriteRules() {
399 flush_rewrite_rules();
400 update_option('easy_invoice_last_rewrite_flush', time());
401 update_option('easy_invoice_flush_rewrite_rules', false);
402 update_option('easy_invoice_post_types_registered', false);
403 }
404
405 /**
406 * Add columns to invoice list table.
407 *
408 * @since 1.0.0
409 * @param array $columns List of columns.
410 * @return array Modified list of columns.
411 */
412 public function addInvoiceColumns( $columns ) {
413 $date_column = isset( $columns['date'] ) ? $columns['date'] : '';
414 unset( $columns['date'] );
415
416 $columns['invoice_number'] = __( 'Invoice #', 'easy-invoice' );
417 $columns['client'] = __( 'Client', 'easy-invoice' );
418 $columns['amount'] = __( 'Amount', 'easy-invoice' );
419 $columns['status'] = __( 'Status', 'easy-invoice' );
420 $columns['issue_date'] = __( 'Issue Date', 'easy-invoice' );
421 $columns['due_date'] = __( 'Due Date', 'easy-invoice' );
422
423 if ( $date_column ) {
424 $columns['date'] = $date_column;
425 }
426
427 return $columns;
428 }
429
430 /**
431 * Populate custom columns for invoice list table.
432 *
433 * @since 1.0.0
434 * @param string $column Column name.
435 * @param int $post_id Post ID.
436 * @return void
437 */
438 public function populateInvoiceColumns( $column, $post_id ) {
439 switch ( $column ) {
440 case 'invoice_number':
441 $invoice_number = get_post_meta( $post_id, '_invoice_number', true );
442 echo esc_html( ! empty( $invoice_number ) ? $invoice_number : "INV-{$post_id}" );
443 break;
444 case 'client':
445 $client_id = get_post_meta( $post_id, '_client_id', true );
446 if ( $client_id ) {
447 $client = get_post( $client_id );
448 if ( $client ) {
449 echo esc_html( $client->post_title );
450 } else {
451 echo esc_html__('', 'easy-invoice');
452 }
453 } else {
454 echo esc_html__('', 'easy-invoice');
455 }
456 break;
457 case 'amount':
458 $total = get_post_meta( $post_id, '_invoice_total', true );
459 if ( ! empty( $total ) ) {
460 echo esc_html( '$' . number_format( $total, 2 ) );
461 } else {
462 echo esc_html__('', 'easy-invoice');
463 }
464 break;
465 case 'status':
466 $status = get_post_meta( $post_id, '_payment_status', true );
467 if ( ! empty( $status ) ) {
468 echo '<span class="invoice-status status-' . esc_attr( $status ) . '">' . esc_html( ucfirst( $status ) ) . '</span>';
469 } else {
470 echo esc_html__('', 'easy-invoice');
471 }
472 break;
473 case 'issue_date':
474 $issue_date = get_post_meta( $post_id, '_issue_date', true );
475 if ( ! empty( $issue_date ) ) {
476 echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $issue_date ) ) );
477 } else {
478 echo esc_html__('', 'easy-invoice');
479 }
480 break;
481 case 'due_date':
482 $due_date = get_post_meta( $post_id, '_due_date', true );
483 if ( ! empty( $due_date ) ) {
484 echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $due_date ) ) );
485 } else {
486 echo '';
487 }
488 break;
489 }
490 }
491
492 /**
493 * Add columns to payment list table.
494 *
495 * @since 1.0.0
496 * @param array $columns List of columns.
497 * @return array Modified list of columns.
498 */
499 public function addPaymentColumns( $columns ) {
500 $date_column = isset( $columns['date'] ) ? $columns['date'] : '';
501 unset( $columns['date'] );
502
503 $columns['invoice'] = __( 'Invoice', 'easy-invoice' );
504 $columns['amount'] = __( 'Amount', 'easy-invoice' );
505 $columns['method'] = __( 'Method', 'easy-invoice' );
506 $columns['status'] = __( 'Status', 'easy-invoice' );
507 $columns['transaction_id'] = __( 'Transaction ID', 'easy-invoice' );
508
509 if ( $date_column ) {
510 $columns['date'] = $date_column;
511 }
512
513 return $columns;
514 }
515
516 /**
517 * Populate custom columns for payment list table.
518 *
519 * @since 1.0.0
520 * @param string $column Column name.
521 * @param int $post_id Post ID.
522 * @return void
523 */
524 public function populatePaymentColumns( $column, $post_id ) {
525 switch ( $column ) {
526 case 'invoice':
527 $invoice_id = get_post_meta( $post_id, '_invoice_id', true );
528 if ( $invoice_id ) {
529 $invoice = get_post( $invoice_id );
530 if ( $invoice ) {
531 $invoice_number = get_post_meta( $invoice_id, '_invoice_number', true );
532 if ( ! $invoice_number ) {
533 $invoice_number = "INV-{$invoice_id}";
534 }
535 echo '<a href="' . esc_url( admin_url( 'post.php?post=' . $invoice_id . '&action=edit' ) ) . '">' . esc_html( $invoice_number ) . '</a>';
536 } else {
537 echo '';
538 }
539 } else {
540 echo '';
541 }
542 break;
543 case 'amount':
544 $amount = get_post_meta( $post_id, '_amount', true );
545 $currency = get_post_meta( $post_id, '_currency_symbol', true ) ?: '$';
546 if ( ! empty( $amount ) ) {
547 echo esc_html( $currency . number_format( $amount, 2 ) );
548 } else {
549 echo '';
550 }
551 break;
552 case 'method':
553 $method = get_post_meta( $post_id, '_payment_method', true );
554 if ( ! empty( $method ) ) {
555 echo esc_html( ucfirst( $method ) );
556 } else {
557 echo '';
558 }
559 break;
560 case 'status':
561 $status = get_post_meta( $post_id, '_status', true );
562 if ( ! empty( $status ) ) {
563 echo '<span class="payment-status status-' . esc_attr( $status ) . '">' . esc_html( ucfirst( $status ) ) . '</span>';
564 } else {
565 echo '';
566 }
567 break;
568 case 'transaction_id':
569 $transaction_id = get_post_meta( $post_id, '_transaction_id', true );
570 if ( ! empty( $transaction_id ) ) {
571 echo esc_html( $transaction_id );
572 } else {
573 echo '';
574 }
575 break;
576 }
577 }
578
579 /**
580 * Register custom post statuses.
581 *
582 * @since 1.0.0
583 * @return void
584 */
585 public function registerCustomStatuses() {
586 // Register custom post statuses for invoices and payments.
587 $statuses = [
588 'pending-bank' => [
589 'label' => _x( 'Pending Bank Transfer', 'Invoice status', 'easy-invoice' ),
590 'public' => true,
591 'exclude_from_search' => false,
592 'show_in_admin_all_list' => true,
593 'show_in_admin_status_list' => true,
594 'label_count' => _n_noop(
595 'Pending Bank Transfer <span class="count">(%s)</span>',
596 'Pending Bank Transfer <span class="count">(%s)</span>',
597 'easy-invoice'
598 ),
599 ],
600 'pending-cheque' => [
601 'label' => _x( 'Pending Cheque', 'Invoice status', 'easy-invoice' ),
602 'public' => true,
603 'exclude_from_search' => false,
604 'show_in_admin_all_list' => true,
605 'show_in_admin_status_list' => true,
606 'label_count' => _n_noop(
607 'Pending Cheque <span class="count">(%s)</span>',
608 'Pending Cheque <span class="count">(%s)</span>',
609 'easy-invoice'
610 ),
611 ],
612 ];
613
614 foreach ( $statuses as $status => $args ) {
615 register_post_status( $status, $args );
616 }
617 }
618
619 /**
620 * Handle admin action to flush rewrite rules
621 */
622 public function handleFlushRewriteRules() {
623 if (!current_user_can('manage_options')) {
624 wp_die('Unauthorized');
625 }
626
627 check_admin_referer('flush_easy_invoice_rewrite_rules');
628
629 $this->forceFlushRewriteRules();
630
631 // Get the referer to determine which page to redirect back to
632 $referer = wp_get_referer();
633 $redirect_url = admin_url('admin.php?page=easy-quote-all&rewrite_flushed=1'); // Default fallback
634
635 if ($referer) {
636 // Check if user was on invoice page
637 if (strpos($referer, 'page=easy-invoice-all') !== false) {
638 $redirect_url = admin_url('admin.php?page=easy-invoice-all&rewrite_flushed=1');
639 } elseif (strpos($referer, 'page=easy-quote-all') !== false) {
640 $redirect_url = admin_url('admin.php?page=easy-quote-all&rewrite_flushed=1');
641 }
642 }
643
644 wp_redirect($redirect_url);
645 exit;
646 }
647
648 /**
649 * Handle admin action to manually register post types
650 */
651 public function handleRegisterPostTypes() {
652 if (!current_user_can('manage_options')) {
653 wp_die('Unauthorized');
654 }
655
656 check_admin_referer('register_easy_invoice_post_types');
657
658 $this->registerPostTypes();
659 $this->forceFlushRewriteRules();
660
661 // Get the referer to determine which page to redirect back to
662 $referer = wp_get_referer();
663 $redirect_url = admin_url('admin.php?page=easy-quote-all&post_types_registered=1'); // Default fallback
664
665 if ($referer) {
666 // Check if user was on invoice page
667 if (strpos($referer, 'page=easy-invoice-all') !== false) {
668 $redirect_url = admin_url('admin.php?page=easy-invoice-all&post_types_registered=1');
669 } elseif (strpos($referer, 'page=easy-quote-all') !== false) {
670 $redirect_url = admin_url('admin.php?page=easy-quote-all&post_types_registered=1');
671 }
672 }
673
674 wp_redirect($redirect_url);
675 exit;
676 }
677
678 /**
679 * Disable admin notices on Easy Invoice pages for clean UI.
680 *
681 * @since 1.0.0
682 * @return void
683 */
684 public function disableAdminNoticesOnEasyInvoicePages() {
685 // Get current page
686 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
687
688 // Check if we're on an Easy Invoice page
689 $easy_invoice_pages = [
690 'easy-invoice', // Dashboard
691 'easy-invoice-all', // All invoices
692 'easy-invoice-builder', // Invoice builder
693 'easy-invoice-preview', // Invoice preview
694 'easy-quote-all', // All quotes
695 'easy-invoice-quote-builder', // Quote builder
696 'easy-quote-preview', // Quote preview
697 'easy-invoice-payments', // All payments
698 'easy-invoice-payment-new', // Add new payment
699 'easy-invoice-clients', // All clients
700 'easy-invoice-client-edit', // Edit client
701 'easy-invoice-client-view', // View client
702 'easy-invoice-reports', // Reports
703 'easy-invoice-settings', // Main settings
704 'easy-invoice-email-settings', // Email settings
705 'easy-invoice-email-settings-general',
706 'easy-invoice-email-settings-invoice',
707 'easy-invoice-email-settings-quote',
708 'easy-invoice-email-settings-payment',
709 'easy-invoice-pro-settings', // Pro settings
710 'easy-invoice-pro-translations', // Pro translations
711 'easy-invoice-pro-item-library', // Item Library (Pro)
712 'easy-invoice-templates', // Template Builder (Pro)
713 'easy-invoice-migration', // Migration page
714 'easy-invoice-license', // License page
715 'easy-invoice-free-vs-pro'
716 ];
717
718 // Check if current page is an Easy Invoice page
719 if ( in_array( $page, $easy_invoice_pages ) ) {
720 // Don't remove our review notice - keep it for free users
721 // Store our notice callback temporarily
722 $review_notice_callback = false;
723 $review_notice_priority = false;
724
725 // Find our review notice hook
726 global $wp_filter;
727 if (isset($wp_filter['admin_notices']->callbacks)) {
728 foreach ($wp_filter['admin_notices']->callbacks as $priority => $callbacks) {
729 foreach ($callbacks as $callback) {
730 if (is_array($callback['function']) &&
731 is_object($callback['function'][0]) &&
732 $callback['function'][0] instanceof \EasyInvoice\Services\ReviewNoticeService &&
733 $callback['function'][1] === 'displayAdminNotice') {
734 $review_notice_callback = $callback['function'];
735 $review_notice_priority = $priority;
736 break 2;
737 }
738 }
739 }
740 }
741
742 // Remove all admin notices by removing the action
743 remove_all_actions( 'admin_notices' );
744
745 // Also remove network and user admin notices
746 remove_all_actions( 'network_admin_notices' );
747 remove_all_actions( 'user_admin_notices' );
748 remove_all_actions( 'all_admin_notices' );
749
750 // Re-add our review notice if it was found
751 if ($review_notice_callback !== false && $review_notice_priority !== false) {
752 add_action('admin_notices', $review_notice_callback, $review_notice_priority);
753 }
754 }
755 }
756 }
757