Files
WooNooW/includes/Api/EmailController.php
dwindown 5fb5eda9c3 feat: Tax route fix + Local Pickup + Email/Notifications settings
## 1. Fixed Tax Settings Route 
- Changed /settings/taxes → /settings/tax in nav tree
- Now matches App.tsx route
- Tax page now loads correctly

## 2. Advanced Local Pickup 
Frontend (LocalPickup.tsx):
- Add/edit/delete pickup locations
- Enable/disable locations
- Full address fields (street, city, state, postcode)
- Phone number and business hours
- Clean modal UI for adding locations

Backend (PickupLocationsController.php):
- GET /settings/pickup-locations
- POST /settings/pickup-locations (create)
- POST /settings/pickup-locations/:id (update)
- DELETE /settings/pickup-locations/:id
- POST /settings/pickup-locations/:id/toggle
- Stores in wp_options as array

## 3. Email/Notifications Settings 
Frontend (Notifications.tsx):
- List all WooCommerce emails
- Separate customer vs admin emails
- Enable/disable toggle for each email
- Show from name/email
- Link to WooCommerce for advanced config

Backend (EmailController.php):
- GET /settings/emails - List all emails
- POST /settings/emails/:id/toggle - Enable/disable
- Uses WC()->mailer()->get_emails()
- Auto-detects recipient type (customer/admin)

## Features:
 Simple, non-tech-savvy UI
 All CRUD operations
 Real-time updates
 Links to WooCommerce for advanced settings
 Mobile responsive

Next: Test all settings pages
2025-11-09 23:44:24 +07:00

145 lines
3.2 KiB
PHP

<?php
/**
* Email Settings REST API Controller
*
* @package WooNooW
*/
namespace WooNooW\Api;
use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
class EmailController extends WP_REST_Controller {
/**
* Register routes
*/
public function register_routes() {
$namespace = 'woonoow/v1';
// Get email settings
register_rest_route(
$namespace,
'/settings/emails',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_settings' ),
'permission_callback' => array( $this, 'check_permission' ),
)
);
// Toggle email
register_rest_route(
$namespace,
'/settings/emails/(?P<email_id>[a-zA-Z0-9_-]+)/toggle',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'toggle_email' ),
'permission_callback' => array( $this, 'check_permission' ),
)
);
}
/**
* Check permission
*/
public function check_permission() {
return current_user_can( 'manage_woocommerce' );
}
/**
* Get email settings
*/
public function get_settings( WP_REST_Request $request ) {
try {
$email_settings = array();
$emails = \WC()->mailer()->get_emails();
foreach ( $emails as $email ) {
$recipient = 'customer';
// Determine recipient type
if ( strpos( $email->id, 'admin' ) !== false ||
strpos( $email->id, 'new_order' ) !== false ||
strpos( $email->id, 'cancelled_order' ) !== false ||
strpos( $email->id, 'failed_order' ) !== false ) {
$recipient = 'admin';
}
$email_settings[] = array(
'id' => $email->id,
'title' => $email->get_title(),
'description' => $email->get_description(),
'enabled' => $email->is_enabled() ? 'yes' : 'no',
'recipient' => $recipient,
);
}
$settings = array(
'emails' => $email_settings,
'from_name' => get_option( 'woocommerce_email_from_name' ),
'from_email' => get_option( 'woocommerce_email_from_address' ),
);
return new WP_REST_Response( $settings, 200 );
} catch ( \Exception $e ) {
return new WP_REST_Response(
array(
'error' => 'fetch_failed',
'message' => $e->getMessage(),
),
500
);
}
}
/**
* Toggle email
*/
public function toggle_email( WP_REST_Request $request ) {
try {
$email_id = $request->get_param( 'email_id' );
$enabled = rest_sanitize_boolean( $request->get_param( 'enabled' ) );
$emails = \WC()->mailer()->get_emails();
if ( ! isset( $emails[ $email_id ] ) ) {
return new WP_REST_Response(
array(
'error' => 'not_found',
'message' => __( 'Email not found', 'woonoow' ),
),
404
);
}
$email = $emails[ $email_id ];
// Update email settings
$email->update_option( 'enabled', $enabled ? 'yes' : 'no' );
return new WP_REST_Response(
array(
'success' => true,
'enabled' => $enabled,
'message' => $enabled
? __( 'Email enabled', 'woonoow' )
: __( 'Email disabled', 'woonoow' ),
),
200
);
} catch ( \Exception $e ) {
return new WP_REST_Response(
array(
'error' => 'toggle_failed',
'message' => $e->getMessage(),
),
500
);
}
}
}