settings = $settings; $this->service = $service; $this->triggers = $triggers; } /** * Register routes. */ public function register_routes(): void { register_rest_route( $this->namespace, '/notifications', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/notifications/test', array( 'methods' => 'POST', 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'channel' => array( 'type' => 'string', 'required' => true, 'enum' => NotificationSettings::CHANNELS, ), ), ) ); register_rest_route( $this->namespace, '/notifications/digest', array( 'methods' => 'POST', 'callback' => array( $this, 'send_digest' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); } /** * GET /notifications — settings (secrets masked) and the delivery log. */ public function get_settings(): WP_REST_Response { return new WP_REST_Response( $this->payload( $this->settings->for_api() ), 200 ); } /** * PUT /notifications — save settings. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_settings( WP_REST_Request $request ) { $saved = $this->settings->update( (array) $request->get_json_params() ); if ( is_wp_error( $saved ) ) { return $saved; } return new WP_REST_Response( $this->payload( $saved ), 200 ); } /** * POST /notifications/test — send a test message to one channel now. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function send_test( WP_REST_Request $request ) { $result = $this->service->send_test( (string) $request->get_param( 'channel' ) ); if ( is_wp_error( $result ) ) { $data = $result->get_error_data(); return new WP_Error( $result->get_error_code(), $result->get_error_message(), array( 'status' => is_array( $data ) && isset( $data['status'] ) ? (int) $data['status'] : 502 ) ); } return new WP_REST_Response( array( 'sent' => true ), 200 ); } /** * POST /notifications/digest — queue today's digest now (a preview). * * @return WP_REST_Response|WP_Error */ public function send_digest() { if ( ! $this->settings->active_channels() ) { return new WP_Error( 'profit_engine_notification_no_channel', __( 'Turn on and save at least one channel first.', 'profit-engine' ), array( 'status' => 400 ) ); } $queued = $this->triggers->send_digest(); if ( 0 === $queued ) { return new WP_Error( 'profit_engine_notification_nothing', __( 'Nothing needs reordering right now, so there is no digest to send.', 'profit-engine' ), array( 'status' => 409 ) ); } return new WP_REST_Response( array( 'queued' => $queued ), 200 ); } /** * Settings plus the log, with store info the page needs. * * @param array $settings Masked settings. * @return array */ private function payload( array $settings ): array { return array( 'settings' => $settings, 'log' => $this->service->log_entries(), 'admin_email' => (string) get_option( 'admin_email' ), 'timezone' => wp_timezone_string(), ); } }