defineConstants();
// Initialize payment gateways first.
$this->initPaymentGateways();
// Note: Post types are now registered separately with proper timing
// Initialize template loader
$template_loader = new TemplateLoader();
$template_loader->init();
// Flush rewrite rules on first load to ensure public quote URLs work
if (get_option('easy_invoice_flush_rewrite_rules', false) === false) {
add_action('init', function() {
flush_rewrite_rules();
update_option('easy_invoice_flush_rewrite_rules', true);
}, 20);
}
// Also flush rewrite rules when post types are registered
add_action('init', function() {
if (get_option('easy_invoice_post_types_registered', false) === false) {
flush_rewrite_rules();
update_option('easy_invoice_post_types_registered', true);
}
}, 25);
// Initialize admin functionality if in admin area.
if ( is_admin() ) {
$this->initAdmin();
}
// Initialize background services
\EasyInvoice\Services\QuoteExpirationService::init();
}
/**
* Define plugin constants.
*
* @since 1.0.0
* @access private
* @return void
*/
private function defineConstants(): void {
if ( ! defined( 'EASY_INVOICE_VERSION' ) ) {
$fallback = '2.1.18';
if ( defined( 'EASY_INVOICE_PLUGIN_FILE' ) && function_exists( 'get_file_data' ) ) {
$headers = get_file_data(
EASY_INVOICE_PLUGIN_FILE,
array( 'version' => 'Version' ),
'plugin'
);
$fallback = ! empty( $headers['version'] ) ? $headers['version'] : $fallback;
}
define( 'EASY_INVOICE_VERSION', $fallback );
}
if ( ! defined( 'EASY_INVOICE_FILE' ) ) {
define( 'EASY_INVOICE_FILE', dirname( dirname( __FILE__ ) ) . '/easy-invoice.php' );
}
if ( ! defined( 'EASY_INVOICE_PATH' ) ) {
define( 'EASY_INVOICE_PATH', plugin_dir_path( EASY_INVOICE_FILE ) );
}
if ( ! defined( 'EASY_INVOICE_URL' ) ) {
define( 'EASY_INVOICE_URL', plugin_dir_url( EASY_INVOICE_FILE ) );
}
if ( ! defined( 'EASY_INVOICE_ASSETS_URL' ) ) {
define( 'EASY_INVOICE_ASSETS_URL', EASY_INVOICE_URL . 'assets/' );
}
}
/**
* Initialize admin functionality.
*
* @since 1.0.0
* @access private
* @return void
*/
private function initAdmin() {
// Load admin class if we're in the admin area.
$admin = new Admin\EasyInvoiceAdmin();
$admin->init();
// Initialize AJAX handlers.
$ajax = new Admin\EasyInvoiceAjax();
$ajax->init();
// Show draft invoices in the main list.
add_action( 'pre_get_posts', [ $this, 'modifyInvoiceAdminQuery' ] );
// Add custom columns to invoice list.
add_filter( 'manage_easy_invoice_posts_columns', [ $this, 'addInvoiceColumns' ] );
add_action( 'manage_easy_invoice_posts_custom_column', [ $this, 'populateInvoiceColumns' ], 10, 2 );
// Add custom columns to payment list.
add_filter( 'manage_easy_invoice_payment_posts_columns', [ $this, 'addPaymentColumns' ] );
add_action( 'manage_easy_invoice_payment_posts_custom_column', [ $this, 'populatePaymentColumns' ], 10, 2 );
// Add admin action to flush rewrite rules
add_action('admin_post_flush_easy_invoice_rewrite_rules', [$this, 'handleFlushRewriteRules']);
// Add admin action to fix quote slugs
add_action('admin_post_fix_easy_invoice_quote_slugs', [$this, 'handleFixQuoteSlugs']);
// Add admin action to manually register post types
add_action('admin_post_register_easy_invoice_post_types', [$this, 'handleRegisterPostTypes']);
// Disable admin notices on Easy Invoice pages for clean UI
add_action( 'admin_notices', [ $this, 'disableAdminNoticesOnEasyInvoicePages' ], 1 );
}
/**
* Modify the main query for easy_invoice post type in admin to include drafts.
*
* @since 1.0.0
* @param \WP_Query $query The WP_Query instance (passed by reference).
* @return void
*/
public function modifyInvoiceAdminQuery( \WP_Query $query ) {
// Check if we are on the main query for the 'easy_invoice' post type admin page.
if ( ! is_admin() || ! $query->is_main_query() || $query->get( 'post_type' ) !== 'easy_invoice' ) {
return;
}
// Check if a specific post_status is already set in the query.
$current_status = $query->get( 'post_status' );
if ( empty( $current_status ) || $current_status === '' ||
( is_array( $current_status ) && count( $current_status ) === 1 && $current_status[0] === 'any' ) ) {
// Also check for explicit views like 'all'.
if ( isset( $_GET['post_status'] ) && $_GET['post_status'] !== 'all' ) {
return;
}
// Include all needed statuses.
$query->set( 'post_status', [
'publish',
'private',
'draft',
'pending',
'future',
'pending-bank',
'pending-cheque'
] );
}
}
/**
* Initialize payment gateways.
*
* @since 1.0.0
* @access private
* @return void
*/
private function initPaymentGateways() {
if ( ! $this->gateway_manager ) {
$this->gateway_manager = new PaymentGatewayManager();
// Register payment gateways.
$gateways = [
new Gateways\PayPalGateway(),
new Gateways\ManualGateway(),
];
foreach ( $gateways as $gateway ) {
$this->gateway_manager->registerGateway( $gateway );
}
// Initialize all gateways.
$this->gateway_manager->initGateways();
// Set default payment methods if not already set.
$this->setDefaultPaymentMethods();
}
}
/**
* Set default payment methods and their details.
*
* @since 1.0.0
* @access private
* @return void
*/
private function setDefaultPaymentMethods() {
$payment_methods = get_option( 'easy_invoice_payment_methods', [] );
$defaults_updated = false;
// Bank Transfer, Cheque, and Cash payment defaults moved to Pro plugin
if ( $defaults_updated ) {
update_option( 'easy_invoice_payment_methods', array_unique( $payment_methods ) );
}
}
/**
* Get payment gateway manager.
*
* @since 1.0.0
* @return PaymentGatewayManager
*/
public function getGatewayManager(): PaymentGatewayManager {
if ( ! $this->gateway_manager ) {
$this->initPaymentGateways();
}
return $this->gateway_manager;
}
/**
* Register custom post types.
*
* @since 1.0.0
* @return void
*/
public function registerPostTypes() {
// Register Invoice post type.
register_post_type( \EasyInvoice\Constants\PostTypes::EASY_INVOICE_POST_TYPE, [
'labels' => [
'name' => _x( 'Invoices', 'post type general name', 'easy-invoice' ),
'singular_name' => _x( 'Invoice', 'post type singular name', 'easy-invoice' ),
'menu_name' => _x( 'Invoices', 'admin menu', 'easy-invoice' ),
'name_admin_bar' => _x( 'Invoice', 'add new on admin bar', 'easy-invoice' ),
'add_new' => _x( 'Add New', 'invoice', 'easy-invoice' ),
'add_new_item' => __( 'Add New Invoice', 'easy-invoice' ),
'new_item' => __( 'New Invoice', 'easy-invoice' ),
'edit_item' => __( 'Edit Invoice', 'easy-invoice' ),
'view_item' => __( 'View Invoice', 'easy-invoice' ),
'all_items' => __( 'All Invoices', 'easy-invoice' ),
'search_items' => __( 'Search Invoices', 'easy-invoice' ),
'parent_item_colon' => __( 'Parent Invoices:', 'easy-invoice' ),
'not_found' => __( 'No invoices found.', 'easy-invoice' ),
'not_found_in_trash' => __( 'No invoices found in Trash.', 'easy-invoice' )
],
'description' => __( 'Invoices for Easy Invoice plugin.', 'easy-invoice' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => true,
'rewrite' => [ 'slug' => 'invoice' ],
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => [ 'title', 'editor', 'custom-fields' ]
]);
// Register Quote post type.
$quote_post_type = \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE;
$result = register_post_type( $quote_post_type, [
'labels' => [
'name' => _x( 'Quotes', 'post type general name', 'easy-invoice' ),
'singular_name' => _x( 'Quote', 'post type singular name', 'easy-invoice' ),
'menu_name' => _x( 'Quotes', 'admin menu', 'easy-invoice' ),
'name_admin_bar' => _x( 'Quote', 'add new on admin bar', 'easy-invoice' ),
'add_new' => _x( 'Add New', 'quote', 'easy-invoice' ),
'add_new_item' => __( 'Add New Quote', 'easy-invoice' ),
'new_item' => __( 'New Quote', 'easy-invoice' ),
'edit_item' => __( 'Edit Quote', 'easy-invoice' ),
'view_item' => __( 'View Quote', 'easy-invoice' ),
'all_items' => __( 'All Quotes', 'easy-invoice' ),
'search_items' => __( 'Search Quotes', 'easy-invoice' ),
'parent_item_colon' => __( 'Parent Quotes:', 'easy-invoice' ),
'not_found' => __( 'No quotes found.', 'easy-invoice' ),
'not_found_in_trash' => __( 'No quotes found in Trash.', 'easy-invoice' )
],
'description' => __( 'Quotes for Easy Invoice plugin.', 'easy-invoice' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => true,
'rewrite' => [ 'slug' => 'easy-invoice-quote', 'with_front' => false ],
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => [ 'title', 'editor', 'custom-fields' ]
]);
// Register Payment post type.
register_post_type( 'easy_invoice_payment', [
'labels' => [
'name' => _x( 'Payments', 'post type general name', 'easy-invoice' ),
'singular_name' => _x( 'Payment', 'post type singular name', 'easy-invoice' ),
'menu_name' => _x( 'Payments', 'admin menu', 'easy-invoice' ),
'name_admin_bar' => _x( 'Payment', 'add new on admin bar', 'easy-invoice' ),
'add_new' => _x( 'Add New', 'payment', 'easy-invoice' ),
'add_new_item' => __( 'Add New Payment', 'easy-invoice' ),
'new_item' => __( 'New Payment', 'easy-invoice' ),
'edit_item' => __( 'Edit Payment', 'easy-invoice' ),
'view_item' => __( 'View Payment', 'easy-invoice' ),
'all_items' => __( 'All Payments', 'easy-invoice' ),
'search_items' => __( 'Search Payments', 'easy-invoice' ),
'parent_item_colon' => __( 'Parent Payments:', 'easy-invoice' ),
'not_found' => __( 'No payments found.', 'easy-invoice' ),
'not_found_in_trash' => __( 'No payments found in Trash.', 'easy-invoice' )
],
'description' => __( 'Payments for Easy Invoice plugin.', 'easy-invoice' ),
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=easy_invoice',
'query_var' => true,
'rewrite' => [ 'slug' => 'payment' ],
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => [ 'title', 'author', 'custom-fields' ],
'show_in_rest' => false,
] );
// Force flush rewrite rules after post type registration
$this->flushRewriteRules();
// Force an immediate rewrite rules flush
flush_rewrite_rules(true);
}
/**
* Flush rewrite rules to ensure custom post type URLs work
*/
private function flushRewriteRules() {
// Only flush if we haven't done it recently
$last_flush = get_option('easy_invoice_last_rewrite_flush', 0);
$current_time = time();
if ($current_time - $last_flush > 300) { // 5 minutes
flush_rewrite_rules();
update_option('easy_invoice_last_rewrite_flush', $current_time);
}
}
/**
* Manually flush rewrite rules (public method for admin use)
*/
public function forceFlushRewriteRules() {
flush_rewrite_rules();
update_option('easy_invoice_last_rewrite_flush', time());
update_option('easy_invoice_flush_rewrite_rules', false);
update_option('easy_invoice_post_types_registered', false);
}
/**
* Add columns to invoice list table.
*
* @since 1.0.0
* @param array $columns List of columns.
* @return array Modified list of columns.
*/
public function addInvoiceColumns( $columns ) {
$date_column = isset( $columns['date'] ) ? $columns['date'] : '';
unset( $columns['date'] );
$columns['invoice_number'] = __( 'Invoice #', 'easy-invoice' );
$columns['client'] = __( 'Client', 'easy-invoice' );
$columns['amount'] = __( 'Amount', 'easy-invoice' );
$columns['status'] = __( 'Status', 'easy-invoice' );
$columns['issue_date'] = __( 'Issue Date', 'easy-invoice' );
$columns['due_date'] = __( 'Due Date', 'easy-invoice' );
if ( $date_column ) {
$columns['date'] = $date_column;
}
return $columns;
}
/**
* Populate custom columns for invoice list table.
*
* @since 1.0.0
* @param string $column Column name.
* @param int $post_id Post ID.
* @return void
*/
public function populateInvoiceColumns( $column, $post_id ) {
switch ( $column ) {
case 'invoice_number':
$invoice_number = get_post_meta( $post_id, '_invoice_number', true );
echo esc_html( ! empty( $invoice_number ) ? $invoice_number : "INV-{$post_id}" );
break;
case 'client':
$client_id = get_post_meta( $post_id, '_client_id', true );
if ( $client_id ) {
$client = get_post( $client_id );
if ( $client ) {
echo esc_html( $client->post_title );
} else {
echo esc_html__('—', 'easy-invoice');
}
} else {
echo esc_html__('—', 'easy-invoice');
}
break;
case 'amount':
$total = get_post_meta( $post_id, '_invoice_total', true );
if ( ! empty( $total ) ) {
echo esc_html( '$' . number_format( $total, 2 ) );
} else {
echo esc_html__('—', 'easy-invoice');
}
break;
case 'status':
$status = get_post_meta( $post_id, '_payment_status', true );
if ( ! empty( $status ) ) {
echo '' . esc_html( ucfirst( $status ) ) . '';
} else {
echo esc_html__('—', 'easy-invoice');
}
break;
case 'issue_date':
$issue_date = get_post_meta( $post_id, '_issue_date', true );
if ( ! empty( $issue_date ) ) {
echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $issue_date ) ) );
} else {
echo esc_html__('—', 'easy-invoice');
}
break;
case 'due_date':
$due_date = get_post_meta( $post_id, '_due_date', true );
if ( ! empty( $due_date ) ) {
echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $due_date ) ) );
} else {
echo '—';
}
break;
}
}
/**
* Add columns to payment list table.
*
* @since 1.0.0
* @param array $columns List of columns.
* @return array Modified list of columns.
*/
public function addPaymentColumns( $columns ) {
$date_column = isset( $columns['date'] ) ? $columns['date'] : '';
unset( $columns['date'] );
$columns['invoice'] = __( 'Invoice', 'easy-invoice' );
$columns['amount'] = __( 'Amount', 'easy-invoice' );
$columns['method'] = __( 'Method', 'easy-invoice' );
$columns['status'] = __( 'Status', 'easy-invoice' );
$columns['transaction_id'] = __( 'Transaction ID', 'easy-invoice' );
if ( $date_column ) {
$columns['date'] = $date_column;
}
return $columns;
}
/**
* Populate custom columns for payment list table.
*
* @since 1.0.0
* @param string $column Column name.
* @param int $post_id Post ID.
* @return void
*/
public function populatePaymentColumns( $column, $post_id ) {
switch ( $column ) {
case 'invoice':
$invoice_id = get_post_meta( $post_id, '_invoice_id', true );
if ( $invoice_id ) {
$invoice = get_post( $invoice_id );
if ( $invoice ) {
$invoice_number = get_post_meta( $invoice_id, '_invoice_number', true );
if ( ! $invoice_number ) {
$invoice_number = "INV-{$invoice_id}";
}
echo '' . esc_html( $invoice_number ) . '';
} else {
echo '—';
}
} else {
echo '—';
}
break;
case 'amount':
$amount = get_post_meta( $post_id, '_amount', true );
$currency = get_post_meta( $post_id, '_currency_symbol', true ) ?: '$';
if ( ! empty( $amount ) ) {
echo esc_html( $currency . number_format( $amount, 2 ) );
} else {
echo '—';
}
break;
case 'method':
$method = get_post_meta( $post_id, '_payment_method', true );
if ( ! empty( $method ) ) {
echo esc_html( ucfirst( $method ) );
} else {
echo '—';
}
break;
case 'status':
$status = get_post_meta( $post_id, '_status', true );
if ( ! empty( $status ) ) {
echo '' . esc_html( ucfirst( $status ) ) . '';
} else {
echo '—';
}
break;
case 'transaction_id':
$transaction_id = get_post_meta( $post_id, '_transaction_id', true );
if ( ! empty( $transaction_id ) ) {
echo esc_html( $transaction_id );
} else {
echo '—';
}
break;
}
}
/**
* Register custom post statuses.
*
* @since 1.0.0
* @return void
*/
public function registerCustomStatuses() {
// Register custom post statuses for invoices and payments.
$statuses = [
'pending-bank' => [
'label' => _x( 'Pending Bank Transfer', 'Invoice status', 'easy-invoice' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop(
'Pending Bank Transfer (%s)',
'Pending Bank Transfer (%s)',
'easy-invoice'
),
],
'pending-cheque' => [
'label' => _x( 'Pending Cheque', 'Invoice status', 'easy-invoice' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop(
'Pending Cheque (%s)',
'Pending Cheque (%s)',
'easy-invoice'
),
],
];
foreach ( $statuses as $status => $args ) {
register_post_status( $status, $args );
}
}
/**
* Handle admin action to flush rewrite rules
*/
public function handleFlushRewriteRules() {
if (!current_user_can('manage_options')) {
wp_die('Unauthorized');
}
check_admin_referer('flush_easy_invoice_rewrite_rules');
$this->forceFlushRewriteRules();
// Get the referer to determine which page to redirect back to
$referer = wp_get_referer();
$redirect_url = admin_url('admin.php?page=easy-quote-all&rewrite_flushed=1'); // Default fallback
if ($referer) {
// Check if user was on invoice page
if (strpos($referer, 'page=easy-invoice-all') !== false) {
$redirect_url = admin_url('admin.php?page=easy-invoice-all&rewrite_flushed=1');
} elseif (strpos($referer, 'page=easy-quote-all') !== false) {
$redirect_url = admin_url('admin.php?page=easy-quote-all&rewrite_flushed=1');
}
}
wp_redirect($redirect_url);
exit;
}
/**
* Handle admin action to manually register post types
*/
public function handleRegisterPostTypes() {
if (!current_user_can('manage_options')) {
wp_die('Unauthorized');
}
check_admin_referer('register_easy_invoice_post_types');
$this->registerPostTypes();
$this->forceFlushRewriteRules();
// Get the referer to determine which page to redirect back to
$referer = wp_get_referer();
$redirect_url = admin_url('admin.php?page=easy-quote-all&post_types_registered=1'); // Default fallback
if ($referer) {
// Check if user was on invoice page
if (strpos($referer, 'page=easy-invoice-all') !== false) {
$redirect_url = admin_url('admin.php?page=easy-invoice-all&post_types_registered=1');
} elseif (strpos($referer, 'page=easy-quote-all') !== false) {
$redirect_url = admin_url('admin.php?page=easy-quote-all&post_types_registered=1');
}
}
wp_redirect($redirect_url);
exit;
}
/**
* Disable admin notices on Easy Invoice pages for clean UI.
*
* @since 1.0.0
* @return void
*/
public function disableAdminNoticesOnEasyInvoicePages() {
// Get current page
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
// Static list of core Easy Invoice page slugs. Addon-owned page
// slugs (Item Library, Template Builder, enterprise addon pages,
// export manager, etc.) are appended dynamically below from
// AddonRegistry so new addons automatically suppress WP notices
// on their pages without needing to edit this list.
$easy_invoice_pages = [
'easy-invoice', // Dashboard
'easy-invoice-all', // All invoices
'easy-invoice-builder', // Invoice builder
'easy-invoice-preview', // Invoice preview
'easy-quote-all', // All quotes
'easy-invoice-quote-builder', // Quote builder
'easy-quote-preview', // Quote preview
'easy-invoice-payments', // All payments
'easy-invoice-payment-new', // Add new payment
'easy-invoice-clients', // All clients
'easy-invoice-client-edit', // Edit client
'easy-invoice-client-view', // View client
'easy-invoice-reports', // Reports
'easy-invoice-settings', // Main settings
'easy-invoice-email-settings', // Email settings
'easy-invoice-email-settings-general',
'easy-invoice-email-settings-invoice',
'easy-invoice-email-settings-quote',
'easy-invoice-email-settings-payment',
'easy-invoice-pro-settings', // Pro settings
'easy-invoice-pro-translations',// Pro translations
'easy-invoice-pro-item-library',// Item Library (Pro)
'easy-invoice-templates', // Template Builder listing (Pro)
'easy-invoice-templates-new', // Template Builder "Create New" (Pro)
'easy-invoice-template-builder',// Template Builder canvas (Pro)
'easy-invoice-export', // Export Data (bulk_operations addon)
'easy-invoice-addons', // Addons grid
'easy-invoice-migration', // Migration page
'easy-invoice-license', // License page
'easy-invoice-free-vs-pro',
'easy-invoice-join-community',
];
// Dynamically include every addon's `settings_url` page slug.
// This means enterprise addons (time-tracking, dunning, white-label,
// team-roles, webhooks) and any future addon automatically get
// notice-suppression without needing to update this array.
if ( class_exists( '\\EasyInvoice\\Addons\\AddonRegistry' ) ) {
foreach ( \EasyInvoice\Addons\AddonRegistry::all() as $ei_addon ) {
if ( empty( $ei_addon['settings_url'] ) ) {
continue;
}
$parts = wp_parse_url( $ei_addon['settings_url'] );
if ( empty( $parts['query'] ) ) {
continue;
}
parse_str( $parts['query'], $qs );
if ( ! empty( $qs['page'] ) ) {
$easy_invoice_pages[] = $qs['page'];
}
}
$easy_invoice_pages = array_values( array_unique( $easy_invoice_pages ) );
}
// Check if current page is an Easy Invoice page
if ( in_array( $page, $easy_invoice_pages ) ) {
// Don't remove our review notice - keep it for free users
// Store our notice callback temporarily
$review_notice_callback = false;
$review_notice_priority = false;
// Find our review notice hook
global $wp_filter;
if (isset($wp_filter['admin_notices']->callbacks)) {
foreach ($wp_filter['admin_notices']->callbacks as $priority => $callbacks) {
foreach ($callbacks as $callback) {
if (is_array($callback['function']) &&
is_object($callback['function'][0]) &&
$callback['function'][0] instanceof \EasyInvoice\Services\ReviewNoticeService &&
$callback['function'][1] === 'displayAdminNotice') {
$review_notice_callback = $callback['function'];
$review_notice_priority = $priority;
break 2;
}
}
}
}
// Remove all admin notices by removing the action
remove_all_actions( 'admin_notices' );
// Also remove network and user admin notices
remove_all_actions( 'network_admin_notices' );
remove_all_actions( 'user_admin_notices' );
remove_all_actions( 'all_admin_notices' );
// Re-add our review notice if it was found
if ($review_notice_callback !== false && $review_notice_priority !== false) {
add_action('admin_notices', $review_notice_callback, $review_notice_priority);
}
}
}
}