templates = $templates; $this->renderer = $renderer; $this->mailer = $mailer; $this->runner = $runner; $this->vars = $vars; } /** * Registers the routes. * * @since 0.1.0 * * @return void */ public function register_routes() { $permission = Permissions::require_capability( Capabilities::MANAGE_SETTINGS ); $base = '/' . $this->rest_base; $key = '/(?P[a-z_]+)'; register_rest_route( $this->namespace, $base, array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => $permission, ) ); register_rest_route( $this->namespace, $base . $key, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => $permission, ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => $permission, ), ) ); foreach ( array( 'reset', 'preview', 'test' ) as $action ) { register_rest_route( $this->namespace, $base . $key . '/' . $action, array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, $action ), 'permission_callback' => $permission, ) ); } } /** * Lists every template. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { return rest_ensure_response( array_values( array_map( array( $this, 'present' ), $this->templates->all() ) ) ); } /** * Returns one template. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function get_item( $request ) { $template = $this->templates->get( (string) $request['key'] ); return null === $template ? $this->missing() : rest_ensure_response( $this->present( $template ) ); } /** * Saves the wording or the on/off switch of a template. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_item( $request ) { $result = $this->templates->save( (string) $request['key'], $this->request_body( $request ) ); if ( null === $result['template'] ) { return $this->error( 'hostkhata_invalid_input', __( 'Please check the highlighted fields.', 'hostkhata' ), 400, array( 'errors' => $result['errors'] ) ); } return rest_ensure_response( $this->present( $result['template'] ) ); } /** * Puts a template back to its default wording. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function reset( $request ) { $template = $this->templates->reset( (string) $request['key'] ); return null === $template ? $this->missing() : rest_ensure_response( $this->present( $template ) ); } /** * Renders a template with sample values; an unsaved `subject` / `body` can be previewed. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function preview( $request ) { $body = $this->request_body( $request ); $override = array( 'subject' => isset( $body['subject'] ) && is_string( $body['subject'] ) ? sanitize_text_field( $body['subject'] ) : '', 'body' => isset( $body['body'] ) && is_string( $body['body'] ) ? sanitize_textarea_field( $body['body'] ) : '', ); $rendered = $this->renderer->render( (string) $request['key'], $this->sample_vars(), $override ); return null === $rendered ? $this->missing() : rest_ensure_response( $rendered ); } /** * Sends a test of a template, with sample values, to the current user. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function test( $request ) { $user = wp_get_current_user(); $key = (string) $request['key']; if ( null === $this->templates->get( $key ) ) { return $this->missing(); } // Send even when the template is switched off: this is a test. $template = $this->templates->get( $key ); if ( ! $template['enabled'] ) { $this->templates->save( $key, array( 'enabled' => true ) ); $id = $this->mailer->queue( $key, (string) $user->user_email, (string) $user->display_name, $this->sample_vars(), 'test', 0 ); $this->templates->save( $key, array( 'enabled' => false ) ); } else { $id = $this->mailer->queue( $key, (string) $user->user_email, (string) $user->display_name, $this->sample_vars(), 'test', 0 ); } if ( 0 === $id ) { return $this->error( 'hostkhata_not_queued', __( 'The test email could not be queued.', 'hostkhata' ), 400 ); } $result = $this->runner->run(); return rest_ensure_response( array( 'to' => $user->user_email, 'queued' => true, 'sent' => $result['sent'] > 0, ) ); } /** * Shapes a template. * * @param array $template Template. * @return array */ private function present( array $template ): array { return array( 'key' => $template['key'], 'label' => $template['label'], 'audience' => $template['audience'], 'tags' => $template['tags'], 'subject' => $template['subject'], 'body' => $template['body'], 'enabled' => $template['enabled'], 'modified' => $template['modified'], ); } /** * Example values for previews and tests. * * @return array */ private function sample_vars(): array { return array_merge( $this->vars->base( null ), array( 'customer_name' => __( 'Asha', 'hostkhata' ), 'order_number' => 'ORD-00042', 'order_total' => $this->vars->money( 450000 ), 'invoice_number' => 'INV-00123', 'invoice_total' => $this->vars->money( 450000 ), 'invoice_balance' => $this->vars->money( 250000 ), 'payment_amount' => $this->vars->money( 200000 ), 'due_date' => $this->vars->date( gmdate( 'Y-m-d', time() + 7 * DAY_IN_SECONDS ) ), 'expires_on' => $this->vars->date( gmdate( 'Y-m-d', time() + 30 * DAY_IN_SECONDS ) ), 'invoice_url' => home_url( '/' ), 'domain' => 'example.com', 'days' => '7', 'amount' => $this->vars->money( 300000 ), 'details' => "Hosting: example.com ยท srv1\nDomain: example.com", 'notice_title' => __( 'Scheduled maintenance', 'hostkhata' ), 'notice_body' => __( 'Our servers will be updated on Friday night.', 'hostkhata' ), ) ); } /** * A not-found error. * * @return WP_Error */ private function missing(): WP_Error { return $this->error( 'hostkhata_not_found', __( 'Email template not found.', 'hostkhata' ), 404 ); } }