namespace, '/admin/settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'check_admin_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'update_settings' ), 'permission_callback' => array( $this, 'check_admin_permission' ), ), ) ); } /** * Get current settings and WooCommerce coupon list. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_settings( $request ) { $saved_settings = get_option( 'aca_settings', array() ); $defaults = array( 'primary_color' => '#5304c1', 'enable_dark_mode' => false, 'rewards_enabled' => true, 'rewards_type' => 'auto', 'rewards_target_orders' => 10, 'rewards_target_spend' => 250, 'rewards_discount_label' => '15% OFF', 'rewards_coupon_code' => 'VIP15', 'rewards_description' => __( 'Unlock our exclusive VIP Club discount with your next order!', 'advanced-customer-account' ), 'menu_items' => class_exists( 'ACA_Menu_API' ) ? ACA_Menu_API::get_merged_menu_items() : array(), ); $settings = wp_parse_args( $saved_settings, $defaults ); // Fetch published store coupons for selection. $coupons_query = get_posts( array( 'post_type' => 'shop_coupon', 'post_status' => 'publish', 'posts_per_page' => 100, 'orderby' => 'title', 'order' => 'ASC', ) ); $coupons = array(); foreach ( $coupons_query as $coupon_post ) { $coupon = new WC_Coupon( $coupon_post->ID ); $code = $coupon->get_code(); $amount = $coupon->get_amount(); $discount_type = $coupon->get_discount_type(); $min_spend = (float) $coupon->get_minimum_amount(); $label = $code; if ( 'percent' === $discount_type ) { $label .= ' (' . $amount . '% off)'; } elseif ( $amount > 0 ) { $label .= ' (' . $this->format_price( $amount ) . ' off)'; } if ( $min_spend > 0 ) { $label .= ' - Min Spend: ' . $this->format_price( $min_spend ); } $coupons[] = array( 'code' => $code, 'label' => $label, 'amount' => $amount, 'min_spend' => $min_spend, ); } return rest_ensure_response( array( 'success' => true, 'settings' => $settings, 'coupons' => $coupons, 'accountPageUrl' => function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'myaccount' ) : home_url( '/my-account/' ), 'currencySymbol' => html_entity_decode( get_woocommerce_currency_symbol() ), ) ); } /** * Update settings. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function update_settings( $request ) { $params = $request->get_json_params(); if ( empty( $params ) ) { $params = $request->get_params(); } $current = get_option( 'aca_settings', array() ); $sanitized = array(); // Primary Color. if ( isset( $params['primary_color'] ) ) { $color = sanitize_hex_color( $params['primary_color'] ); $sanitized['primary_color'] = $color ? $color : '#5304c1'; } else { $sanitized['primary_color'] = isset( $current['primary_color'] ) ? $current['primary_color'] : '#5304c1'; } // Dark Mode. $sanitized['enable_dark_mode'] = ! empty( $params['enable_dark_mode'] ); // Loyalty & Milestone Settings. $sanitized['rewards_enabled'] = ! empty( $params['rewards_enabled'] ); $valid_types = array( 'auto', 'orders', 'spend' ); $sanitized['rewards_type'] = isset( $params['rewards_type'] ) && in_array( $params['rewards_type'], $valid_types, true ) ? $params['rewards_type'] : 'auto'; $sanitized['rewards_target_orders'] = isset( $params['rewards_target_orders'] ) ? max( 1, absint( $params['rewards_target_orders'] ) ) : 10; $sanitized['rewards_target_spend'] = isset( $params['rewards_target_spend'] ) ? max( 1, floatval( $params['rewards_target_spend'] ) ) : 250; $sanitized['rewards_discount_label'] = isset( $params['rewards_discount_label'] ) ? sanitize_text_field( $params['rewards_discount_label'] ) : '15% OFF'; $sanitized['rewards_coupon_code'] = isset( $params['rewards_coupon_code'] ) ? strtoupper( sanitize_text_field( $params['rewards_coupon_code'] ) ) : 'VIP15'; $sanitized['rewards_description'] = isset( $params['rewards_description'] ) ? sanitize_text_field( $params['rewards_description'] ) : ''; // Menu item preferences. if ( isset( $params['menu_items'] ) && is_array( $params['menu_items'] ) ) { $sanitized['menu_items'] = array(); foreach ( $params['menu_items'] as $item ) { if ( ! empty( $item['id'] ) ) { $sanitized['menu_items'][] = array( 'id' => sanitize_key( $item['id'] ), 'title' => sanitize_text_field( $item['title'] ), 'enabled' => ! empty( $item['enabled'] ), 'endpoint' => isset( $item['endpoint'] ) ? sanitize_text_field( $item['endpoint'] ) : '', 'icon' => isset( $item['icon'] ) ? sanitize_text_field( $item['icon'] ) : 'dashboard', ); } } } elseif ( isset( $current['menu_items'] ) ) { $sanitized['menu_items'] = $current['menu_items']; } update_option( 'aca_settings', $sanitized ); return rest_ensure_response( array( 'success' => true, 'message' => __( 'Settings saved successfully!', 'advanced-customer-account' ), 'settings' => $sanitized, ) ); } }