namespace, '/menu', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_menu' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); // Update menu items (admin only). register_rest_route( $this->namespace, '/menu', array( array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_menu' ), 'permission_callback' => array( $this, 'check_admin_permission' ), ), ) ); // Get 3rd-party or custom endpoint content. register_rest_route( $this->namespace, '/endpoint-content/(?P[a-zA-Z0-9_-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_endpoint_content' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'endpoint' => array( 'required' => true, 'sanitize_callback' => 'sanitize_key', ), ), ), ) ); } /** * Get menu items. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_menu( $request ) { $items = self::get_merged_menu_items(); // Filter visible items only for frontend. $visible_items = array_filter( $items, function ( $item ) { return isset( $item['visible'] ) && $item['visible']; } ); // Sort by order. usort( $visible_items, function ( $a, $b ) { return ( $a['order'] ?? 0 ) - ( $b['order'] ?? 0 ); } ); return rest_ensure_response( array_values( $visible_items ) ); } /** * Get dynamic merged menu items (WooCommerce native + 3rd party plugins + ACA items). * * @return array */ public static function get_merged_menu_items() { // Native React routes handled by ACA. $native_routes = array( 'dashboard' => 'dashboard', 'orders' => 'orders', 'downloads' => 'downloads', 'addresses' => 'addresses', 'profile' => 'profile', 'coupons' => 'coupons', ); // Get all registered WooCommerce menu items (includes 3rd-party plugins). $wc_items = function_exists( 'wc_get_account_menu_items' ) ? wc_get_account_menu_items() : array(); // Ensure Coupons tab is available in ACA. if ( ! isset( $wc_items['coupons'] ) ) { $wc_items['coupons'] = __( 'My Coupons', 'advanced-customer-account' ); } // Icon map for common endpoints. $icon_map = array( 'dashboard' => 'dashboard', 'orders' => 'cart', 'downloads' => 'download', 'edit-address' => 'location', 'addresses' => 'location', 'payment-methods' => 'money-alt', 'edit-account' => 'admin-users', 'profile' => 'admin-users', 'customer-logout' => 'exit', 'coupons' => 'tickets-alt', 'subscriptions' => 'update', 'subscription' => 'update', 'wishlist' => 'heart', 'my-wishlist' => 'heart', 'tinv_wishlist' => 'heart', 'yith-wishlist' => 'heart', 'points-and-rewards' => 'awards', 'points' => 'awards', 'affiliate' => 'groups', 'booking' => 'calendar-alt', 'bookings' => 'calendar-alt', 'support' => 'sos', 'tickets' => 'sos', ); // Normalize WC endpoints to ACA route IDs. $merged = array(); $order = 1; foreach ( $wc_items as $endpoint => $label ) { // Skip logout from regular tabs (it is placed in the footer). if ( 'customer-logout' === $endpoint ) { continue; } // Map WC endpoint slugs to ACA internal IDs. $id = $endpoint; if ( 'edit-address' === $endpoint ) { $id = 'addresses'; } elseif ( 'edit-account' === $endpoint ) { $id = 'profile'; } $is_native = isset( $native_routes[ $id ] ); $url = function_exists( 'wc_get_account_endpoint_url' ) ? wc_get_account_endpoint_url( $endpoint ) : '#'; $icon = isset( $icon_map[ $endpoint ] ) ? $icon_map[ $endpoint ] : ( isset( $icon_map[ $id ] ) ? $icon_map[ $id ] : 'admin-generic' ); $merged[ $id ] = array( 'id' => $id, 'label' => $label, 'icon' => $icon, 'endpoint' => $endpoint, 'url' => esc_url( $url ), 'is_native' => $is_native, 'is_external' => false, 'visible' => true, 'order' => $order++, ); } // Check saved admin customizations. $settings = get_option( 'aca_settings', array() ); $saved_items = isset( $settings['menu_items'] ) && is_array( $settings['menu_items'] ) ? $settings['menu_items'] : array(); if ( ! empty( $saved_items ) ) { foreach ( $saved_items as $saved ) { $saved_id = $saved['id'] ?? ''; if ( ! empty( $saved_id ) && isset( $merged[ $saved_id ] ) ) { $merged[ $saved_id ]['label'] = ! empty( $saved['label'] ) ? sanitize_text_field( $saved['label'] ) : $merged[ $saved_id ]['label']; $merged[ $saved_id ]['icon'] = ! empty( $saved['icon'] ) ? sanitize_text_field( $saved['icon'] ) : $merged[ $saved_id ]['icon']; $merged[ $saved_id ]['visible'] = isset( $saved['visible'] ) ? (bool) $saved['visible'] : true; $merged[ $saved_id ]['order'] = isset( $saved['order'] ) ? absint( $saved['order'] ) : $merged[ $saved_id ]['order']; } elseif ( ! empty( $saved_id ) && ! empty( $saved['is_custom'] ) ) { // Custom added tabs (e.g. custom links or shortcodes). $merged[ $saved_id ] = array( 'id' => sanitize_key( $saved_id ), 'label' => sanitize_text_field( $saved['label'] ?? '' ), 'icon' => sanitize_text_field( $saved['icon'] ?? 'admin-generic' ), 'endpoint' => sanitize_key( $saved['endpoint'] ?? $saved_id ), 'url' => esc_url( $saved['url'] ?? '#' ), 'is_native' => false, 'is_external' => ! empty( $saved['url'] && '#' !== $saved['url'] ), 'visible' => (bool) ( $saved['visible'] ?? true ), 'order' => absint( $saved['order'] ?? $order++ ), ); } } } return array_values( $merged ); } /** * Update menu items. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function update_menu( $request ) { $menu_items = $request->get_param( 'menu_items' ); if ( ! is_array( $menu_items ) ) { return new WP_Error( 'rest_invalid_param', esc_html__( 'Invalid menu items.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Sanitize menu items. $sanitized_items = array(); foreach ( $menu_items as $item ) { $sanitized_items[] = array( 'id' => sanitize_key( $item['id'] ?? '' ), 'label' => sanitize_text_field( $item['label'] ?? '' ), 'icon' => sanitize_text_field( $item['icon'] ?? 'admin-generic' ), 'endpoint' => sanitize_key( $item['endpoint'] ?? '' ), 'url' => esc_url_raw( $item['url'] ?? '' ), 'visible' => (bool) ( $item['visible'] ?? true ), 'order' => absint( $item['order'] ?? 0 ), 'is_custom' => ! empty( $item['is_custom'] ), ); } // Update settings. $settings = get_option( 'aca_settings', array() ); $settings['menu_items'] = $sanitized_items; update_option( 'aca_settings', $settings ); return rest_ensure_response( array( 'success' => true, 'message' => __( 'Menu updated successfully.', 'advanced-customer-account' ), 'items' => $sanitized_items, ) ); } /** * Get 3rd-party or custom endpoint content. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function get_endpoint_content( $request ) { $endpoint = sanitize_key( $request->get_param( 'endpoint' ) ); if ( empty( $endpoint ) ) { return new WP_Error( 'rest_invalid_endpoint', esc_html__( 'Endpoint parameter is required.', 'advanced-customer-account' ), array( 'status' => 400 ) ); } // Set global query variables so WooCommerce and plugins recognize the active endpoint. global $wp, $wp_query; $wp->query_vars[ $endpoint ] = ''; // Get tab title. $wc_items = function_exists( 'wc_get_account_menu_items' ) ? wc_get_account_menu_items() : array(); $title = isset( $wc_items[ $endpoint ] ) ? $wc_items[ $endpoint ] : ucwords( str_replace( array( '-', '_' ), ' ', $endpoint ) ); // Capture the endpoint content output. ob_start(); /** * 1. Check for specific WooCommerce core functions. */ if ( 'payment-methods' === $endpoint && function_exists( 'woocommerce_account_payment_methods' ) ) { woocommerce_account_payment_methods(); } elseif ( 'add-payment-method' === $endpoint && function_exists( 'woocommerce_account_add_payment_method' ) ) { woocommerce_account_add_payment_method(); } elseif ( has_action( 'woocommerce_account_' . $endpoint . '_endpoint' ) ) { /** * 2. Standard 3rd-party endpoint hook. * Most plugins hook into woocommerce_account_{endpoint}_endpoint. */ do_action( 'woocommerce_account_' . $endpoint . '_endpoint' ); } else { /** * 3. Fallback to general account content hook. */ do_action( 'woocommerce_account_content' ); } $content = ob_get_clean(); // If no output was captured, provide a graceful message. if ( empty( trim( $content ) ) ) { $url = function_exists( 'wc_get_account_endpoint_url' ) ? wc_get_account_endpoint_url( $endpoint ) : '#'; $content = sprintf( '

%s

%s

', esc_html__( 'No direct preview available for this section.', 'advanced-customer-account' ), esc_url( $url ), esc_html__( 'Open Section Page', 'advanced-customer-account' ) ); } return rest_ensure_response( array( 'success' => true, 'endpoint' => $endpoint, 'title' => $title, 'content' => $content, 'url' => function_exists( 'wc_get_account_endpoint_url' ) ? wc_get_account_endpoint_url( $endpoint ) : '', ) ); } }