sessions = $sessions; $this->emails = $emails; $this->events = $events; $this->reminders = $reminders; } /** * {@inheritdoc} */ public function register_routes(): void { $permission = Permissions::manage(); $list = array_merge( $this->list_params( array( 'id' ), 'id' ), array( 'session_id' => array( 'type' => 'integer', 'minimum' => 0, 'default' => 0, 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ), ) ); foreach ( array( '/admin/emails' => 'emails', '/admin/activity' => 'activity', ) as $path => $callback ) { register_rest_route( $this->namespace, $path, array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, $callback ), 'permission_callback' => $permission, 'args' => $list, ) ); } register_rest_route( $this->namespace, '/admin/emails/(?P\d+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'email' ), 'permission_callback' => $permission, 'args' => array( 'id' => $this->id_arg() ), ) ); register_rest_route( $this->namespace, '/admin/sessions/(?P\d+)/test-email', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'test_email' ), 'permission_callback' => $permission, 'args' => array( 'id' => $this->id_arg(), 'recipient' => array( 'type' => 'string', 'required' => true, 'maxLength' => 190, 'validate_callback' => 'rest_validate_request_arg', ), ), ) ); } /** * GET /admin/emails * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response */ public function emails( WP_REST_Request $request ) { $page = $this->emails->query( (int) $request->get_param( 'session_id' ), $this->paging( $request ) ); return $this->collection_response( array_map( array( Presenter::class, 'email' ), $page['items'] ), $page['total'], $request ); } /** * GET /admin/emails/{id} * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function email( WP_REST_Request $request ) { $mail = $this->emails->find( $this->path_id( $request ) ); if ( ! $mail ) { return $this->error( 'email_missing', __( 'Email log not found.', 'interviewnea' ), 404 ); } return rest_ensure_response( array_merge( Presenter::email( $mail ), array( 'body_html' => $mail->body_html ) ) ); } /** * GET /admin/activity * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response */ public function activity( WP_REST_Request $request ) { $page = $this->events->query( (int) $request->get_param( 'session_id' ), $this->paging( $request ) ); return $this->collection_response( array_map( array( Presenter::class, 'event' ), $page['items'] ), $page['total'], $request ); } /** * POST /admin/sessions/{id}/test-email * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function test_email( WP_REST_Request $request ) { $session = $this->sessions->find( $this->path_id( $request ) ); $recipient = sanitize_email( (string) $request->get_param( 'recipient' ) ); if ( ! $session ) { return $this->error( 'session_missing', __( 'Session not found.', 'interviewnea' ), 404 ); } if ( '' === $recipient || ! is_email( $recipient ) ) { return $this->error( 'invalid', __( 'Enter a valid test email address.', 'interviewnea' ), 400, array( 'fields' => array( 'recipient' => __( 'Enter a valid email address.', 'interviewnea' ) ) ) ); } $throttled = $this->throttle( 'email' ); if ( $throttled ) { return $throttled; } $accepted = $this->reminders->send_test_email( $session, $recipient ); return rest_ensure_response( array( 'ok' => $accepted, 'accepted' => $accepted, 'message' => $accepted ? __( 'Accepted by the WordPress mailer. Inbox delivery is not guaranteed.', 'interviewnea' ) : __( 'The WordPress mailer reported a failure. Check your SMTP settings and the email log.', 'interviewnea' ), ) ); } /** * Paging arguments of a list request. * * @param WP_REST_Request $request Request. * @return array */ private function paging( WP_REST_Request $request ): array { return array( 'page' => (int) $request->get_param( 'page' ), 'per_page' => (int) $request->get_param( 'per_page' ), ); } /** * Allows one test send per user every 30 seconds. * * @param string $kind email. * @return \WP_Error|null */ private function throttle( string $kind ): ?\WP_Error { $key = 'interviewnea_test_' . $kind . '_' . get_current_user_id(); if ( get_transient( $key ) ) { return $this->error( 'rate_limited', sprintf( /* translators: %d: seconds. */ __( 'Please wait %d seconds before sending another test.', 'interviewnea' ), self::TEST_COOLDOWN ), 429 ); } set_transient( $key, 1, self::TEST_COOLDOWN ); return null; } }