groups = class_exists( 'WC_EDM_Admin_Groups' ) ? new WC_EDM_Admin_Groups() : null; $this->emails = class_exists( 'WC_EDM_Admin_Emails' ) ? new WC_EDM_Admin_Emails() : null; $this->shipping = class_exists( 'WC_EDM_Admin_Shipping' ) ? new WC_EDM_Admin_Shipping() : null; $this->audit = class_exists( 'WC_EDM_Admin_Audit' ) ? new WC_EDM_Admin_Audit() : null; add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_init', array( $this, 'handle_actions' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Register the submenu page under WooCommerce. */ public function register_menu() { add_submenu_page( 'woocommerce', __( 'Email Discounts', 'wc-email-discount' ), __( 'Email Discounts', 'wc-email-discount' ), 'manage_woocommerce', self::PAGE_SLUG, array( $this, 'render_page' ) ); } /** * Enqueue admin CSS/JS on the plugin page only. * * @param string $hook Current admin page hook. */ public function enqueue_assets( $hook ) { if ( false === strpos( $hook, self::PAGE_SLUG ) ) { return; } wp_enqueue_style( 'wc-edm-admin', WC_EDM_PLUGIN_URL . 'assets/css/admin.css', array(), WC_EDM_VERSION ); wp_enqueue_script( 'wc-edm-admin', WC_EDM_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-ui-sortable' ), WC_EDM_VERSION, true ); wp_localize_script( 'wc-edm-admin', 'wcEdmAdmin', array( 'confirmDelete' => __( 'Are you sure you want to delete this item? This cannot be undone.', 'wc-email-discount' ), ) ); } /** * Get the currently active tab. * * @return string */ public function current_tab() { $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'dashboard'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tabs = array_keys( $this->get_tabs() ); return in_array( $tab, $tabs, true ) ? $tab : 'dashboard'; } /** * Tab definitions. * * @return array */ private function get_tabs() { $tabs = array( 'dashboard' => __( 'Dashboard', 'wc-email-discount' ) ); // Only offer tabs whose controller actually loaded. if ( $this->groups ) { $tabs['groups'] = __( 'Discount Groups', 'wc-email-discount' ); } if ( $this->emails ) { $tabs['emails'] = __( 'Email Assignments', 'wc-email-discount' ); } if ( $this->shipping ) { $tabs['shipping'] = __( 'Shipping Rules', 'wc-email-discount' ); } if ( $this->audit ) { $tabs['audit'] = __( 'Audit Log', 'wc-email-discount' ); } return $tabs; } /** * Handle POST/GET actions before any output (allows redirects). */ public function handle_actions() { if ( ! isset( $_GET['page'] ) || self::PAGE_SLUG !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } switch ( $this->current_tab() ) { case 'groups': $this->groups->handle_actions(); break; case 'emails': $this->emails->handle_actions(); break; case 'shipping': $this->shipping->handle_actions(); break; case 'audit': $this->audit->handle_actions(); break; } } /** * Render the admin page with tab navigation. */ public function render_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'wc-email-discount' ) ); } $current = $this->current_tab(); ?>

render_notices(); ?>
groups->render(); break; case 'emails': $this->emails->render(); break; case 'shipping': $this->shipping->render(); break; case 'audit': $this->audit->render(); break; default: $this->render_dashboard(); break; } ?>
array( 'success', __( 'Saved successfully.', 'wc-email-discount' ) ), 'deleted' => array( 'success', __( 'Deleted successfully.', 'wc-email-discount' ) ), 'error' => array( 'error', __( 'Something went wrong. Please try again.', 'wc-email-discount' ) ), ); if ( ! isset( $messages[ $message ] ) ) { return; } list( $type, $text ) = $messages[ $message ]; printf( '

%2$s

', esc_attr( $type ), esc_html( $text ) ); } /** * Render the dashboard tab: stats cards, quick actions, recent groups. */ private function render_dashboard() { $group_count = WC_EDM_DB::count_groups(); $email_count = WC_EDM_DB::count_emails(); $active_count = WC_EDM_DB::count_emails( true ); $recent = WC_EDM_DB::get_recent_groups( 5 ); ?>

name ); ?> discount_percent ) ); ?>% id ) ) ); ?> created_at ) ) ); ?>