` and talks to these routes * instead of the campaign ones. Each email is one row of the campaigns table * (see {@see Mailpuff_System_Emails}). */ class Mailpuff_Emails_Controller extends Mailpuff_REST_Controller { /** * Register routes. */ public function register_routes() { $types = implode( '|', Mailpuff_System_Emails::TYPES ); register_rest_route( $this->namespace, '/emails/(?P' . $types . ')', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); // A tag's own welcome email: the same reads, writes and test as the // others, and its switch (`enabled`) in the PUT, for the owner. register_rest_route( $this->namespace, '/emails/tag-(?P\\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/emails/tag-(?P\\d+)/test', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // One of an add-on's own emails (a type it registered as `system`), // opened in the editor as `doc=email-`. register_rest_route( $this->namespace, '/emails/email-(?P\\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/emails/email-(?P\\d+)/test', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // The confirmation email as fields (a look and its words), the // default's (no tag) or a tag's own: read, change, go back to the // default (DELETE, a tag's), and the screen's live preview. register_rest_route( $this->namespace, '/emails/confirmation/fields', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'confirmation_fields' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_confirmation_fields' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'reset_confirmation' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/emails/confirmation/preview', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'preview_confirmation' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // A tag's own confirmation email in the email editor (`doc=tag-confirm-`): // opening it there makes it, from the default's fields. register_rest_route( $this->namespace, '/emails/tag-confirm-(?P\\d+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'update_item' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/emails/tag-confirm-(?P\\d+)/test', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // A tag's own From, for its confirmation and welcome emails. register_rest_route( $this->namespace, '/emails/tag-(?P\\d+)/from', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'tag_from' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'set_tag_from' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/emails/tags', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'tag_emails' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // Settings, Automatic emails: who gets new posts, each email's last // send and the next digest. register_rest_route( $this->namespace, '/emails/automatic', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'automatic' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); // The post editor's panel: whoever can edit the post. register_rest_route( $this->namespace, '/posts/(?P\d+)/email', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'post_email' ), 'permission_callback' => static function ( $request ) { $post_id = (int) $request->get_param( 'id' ); return $post_id > 0 && current_user_can( 'edit_post', $post_id ); }, ) ); register_rest_route( $this->namespace, '/emails/(?P' . $types . ')/test', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'send_test' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); } /** * GET /emails/tags: every tag with an email or a From of its own: its * From, its confirmation email and its welcome email. * * @return WP_REST_Response */ public function tag_emails() { return $this->success( array( 'items' => Mailpuff_Confirmation_Email::tags(), 'default_from' => Mailpuff_Confirmation_Email::describe( 0 )['default_from'], ) ); } /** * The tag a confirmation request names: 0 for the default. * * @param WP_REST_Request $request Request. * @return int */ private function confirmation_tag( $request ) { return max( 0, (int) $request->get_param( 'tag' ) ); } /** * GET /emails/confirmation/fields?tag=: the look and words of the * default confirmation email or a tag's. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function confirmation_fields( $request ) { $out = Mailpuff_Confirmation_Email::describe( $this->confirmation_tag( $request ) ); return is_wp_error( $out ) ? $this->wp_error( $out ) : $this->success( $out ); } /** * PUT /emails/confirmation/fields { tag?, fields }: change them. A tag * with no email of its own gets one, made from the default's. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function save_confirmation_fields( $request ) { $tag = $this->confirmation_tag( $request ); $fields = $request->get_param( 'fields' ); $saved = Mailpuff_Confirmation_Email::save( $tag, is_array( $fields ) ? $fields : array() ); if ( is_wp_error( $saved ) ) { return $this->wp_error( $saved ); } return $this->success( Mailpuff_Confirmation_Email::describe( $tag ) ); } /** * DELETE /emails/confirmation/fields?tag=: a tag goes back to the * default confirmation email. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function reset_confirmation( $request ) { $tag = $this->confirmation_tag( $request ); if ( ! $tag || ! Mailpuff_Group::find( $tag ) ) { return $this->error( 'no_tag', __( 'Name the tag whose own confirmation email goes.', 'mailpuff' ), 400 ); } Mailpuff_Confirmation_Email::remove( $tag ); return $this->success( Mailpuff_Confirmation_Email::describe( $tag ) ); } /** * POST /emails/confirmation/preview { tag?, fields }: the email those * fields make, filled in for the person looking. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function preview_confirmation( $request ) { $fields = $request->get_param( 'fields' ); $out = Mailpuff_Confirmation_Email::preview( $this->confirmation_tag( $request ), is_array( $fields ) ? $fields : array() ); return is_wp_error( $out ) ? $this->wp_error( $out ) : $this->success( $out ); } /** * GET /emails/tag-:id/from. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function tag_from( $request ) { $tag = (int) $request->get_param( 'tag' ); if ( ! Mailpuff_Group::find( $tag ) ) { return $this->error( 'not_found', __( 'No tag has that id.', 'mailpuff' ), 404 ); } return $this->success( Mailpuff_Confirmation_Email::sender( $tag ) ); } /** * PUT /emails/tag-:id/from { name?, email?, reply_to? }: a tag's own * From; all empty goes back to Settings, Sending's. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function set_tag_from( $request ) { $input = array(); foreach ( array( 'name', 'email', 'reply_to' ) as $key ) { if ( null !== $request->get_param( $key ) ) { $input[ $key ] = (string) $request->get_param( $key ); } } $out = Mailpuff_Confirmation_Email::set_sender( (int) $request->get_param( 'tag' ), $input ); return is_wp_error( $out ) ? $this->wp_error( $out ) : $this->success( $out ); } /** * A WP_Error as this controller's error answer, with its status. * * @param WP_Error $error Error. * @return WP_Error */ private function wp_error( WP_Error $error ) { $data = (array) $error->get_error_data(); return $this->error( $error->get_error_code(), $error->get_error_message(), (int) ( $data['status'] ?? 400 ) ); } /** * The row a request names: a tag's email (`tag`) or a system email * (`type`). A tag's is made on first use. * * @param WP_REST_Request $request Request. * @param bool $make Make a tag's own confirmation email when it * has none; else read the default's. * @return object|null */ private function row_for( $request, $make = true ) { if ( null !== $request->get_param( 'tag' ) ) { return Mailpuff_Tag_Emails::row( (int) $request->get_param( 'tag' ) ); } if ( null !== $request->get_param( 'tagc' ) ) { $tag = (int) $request->get_param( 'tagc' ); if ( ! Mailpuff_Group::find( $tag ) ) { return null; } return $make ? Mailpuff_Confirmation_Email::ensure_tag_row( $tag ) : Mailpuff_Confirmation_Email::row_for( $tag ); } if ( null !== $request->get_param( 'row' ) ) { $row = Mailpuff_Campaign::find( (int) $request->get_param( 'row' ) ); return $row && in_array( (string) $row->type, Mailpuff_System_Emails::addon_types(), true ) ? $row : null; } return Mailpuff_System_Emails::row( (string) $request->get_param( 'type' ) ); } /** * GET /emails/automatic. * * @return WP_REST_Response */ public function automatic() { return $this->success( Mailpuff_Post_Emails::overview() ); } /** * GET /posts/:id/email. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function post_email( $request ) { $post = get_post( (int) $request->get_param( 'id' ) ); if ( ! $post || ! in_array( $post->post_type, Mailpuff_Post_Emails::post_types(), true ) ) { return $this->error( 'not_found', __( 'Post not found.', 'mailpuff' ), 404 ); } return $this->success( Mailpuff_Post_Emails::for_post( $post ) ); } /** * GET /emails/:type. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function get_item( $request ) { $row = $this->row_for( $request ); if ( ! $row ) { return $this->error( 'not_found', __( 'Email not found.', 'mailpuff' ), 404 ); } return $this->success( Mailpuff_System_Emails::to_array( $row ) ); } /** * PUT /emails/:type. Changes the subject, preheader, body and, for the * welcome email and a tag's email, whether it is sent. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function update_item( $request ) { $row = $this->row_for( $request ); $type = $row ? (string) $row->type : ''; if ( ! $row ) { return $this->error( 'not_found', __( 'Email not found.', 'mailpuff' ), 404 ); } $data = array(); foreach ( array( 'title', 'subject', 'preheader', 'sender_name', 'reply_to' ) as $field ) { if ( null !== $request->get_param( $field ) ) { $data[ $field ] = sanitize_text_field( (string) $request->get_param( $field ) ); } } if ( null !== $request->get_param( 'sender_email' ) ) { $data['sender_email'] = sanitize_email( (string) $request->get_param( 'sender_email' ) ); } if ( null !== $request->get_param( 'content_html' ) ) { $data['content_html'] = Mailpuff_Email_Renderer::sanitize_html( $request->get_param( 'content_html' ) ); } $confirm = in_array( $type, array( 'confirmation', Mailpuff_Confirmation_Email::TYPE ), true ); if ( null !== $request->get_param( 'content_blocks' ) ) { $blocks = (string) $request->get_param( 'content_blocks' ); $decoded = json_decode( $blocks, true ); if ( '' !== $blocks && ! is_array( $decoded ) ) { return $this->error( 'invalid_content', __( 'The email content is not valid.', 'mailpuff' ), 400 ); } // Designed in the email editor now: no longer one of the looks. if ( $confirm && is_array( $decoded ) && isset( $decoded['settings']['confirm'] ) ) { unset( $decoded['settings']['confirm'] ); $blocks = (string) wp_json_encode( $decoded ); } $data['content_blocks'] = $blocks; } // Without its link nobody can confirm. if ( $confirm && isset( $data['content_html'] ) && false === strpos( $data['content_html'], '{{confirm_url}}' ) ) { return $this->error( 'missing_confirm_link', __( 'The confirmation email needs the confirm link. Add a link or button that uses {{confirm_url}}.', 'mailpuff' ), 422 ); } if ( $data ) { Mailpuff_Campaign::update( (int) $row->id, $data ); } if ( 'welcome' === $type && null !== $request->get_param( 'enabled' ) ) { $settings = (array) get_option( 'mailpuff_email_settings', array() ); $settings['welcome_enabled'] = rest_sanitize_boolean( $request->get_param( 'enabled' ) ) ? '1' : '0'; update_option( 'mailpuff_email_settings', $settings ); } if ( Mailpuff_Tag_Emails::TYPE === $type && null !== $request->get_param( 'enabled' ) ) { Mailpuff_Tag_Emails::set_enabled( (int) $row->audience_id, rest_sanitize_boolean( $request->get_param( 'enabled' ) ) ); } return $this->success( Mailpuff_System_Emails::to_array( Mailpuff_Campaign::find( (int) $row->id ) ) ); } /** * POST /emails/:type/test. Sends a test with the preview links. A new * post's email and the digest are filled with the newest posts, the owner * notice with a sample subscriber. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function send_test( $request ) { // A test never makes a tag's own confirmation email: without one, the // default its signups get is what goes. $row = $this->row_for( $request, false ); $type = $row ? (string) $row->type : ''; if ( ! $row ) { return $this->error( 'not_found', __( 'Email not found.', 'mailpuff' ), 404 ); } $to = sanitize_email( (string) $request->get_param( 'email' ) ); if ( '' === $to ) { $to = (string) wp_get_current_user()->user_email; } if ( ! is_email( $to ) ) { return $this->error( 'no_email', __( 'No recipient email provided.', 'mailpuff' ), 400 ); } $args = array( 'type' => 'test' ); $sample = null; // A confirmation's look is drawn now, as the real one will be. $fresh = Mailpuff_Confirmation_Email::fresh_body( $row ); if ( '' !== $fresh ) { $args['body'] = $fresh; } if ( in_array( $type, array( 'post', 'digest' ), true ) ) { $sample = Mailpuff_Post_Emails::sample( $type ); } elseif ( 'owner_notice' === $type ) { $sample = Mailpuff_Owner_Notice::sample(); } if ( ! empty( $sample ) ) { $args['subject'] = '[Test] ' . $sample['subject']; $args['body'] = $sample['body']; } $result = Mailpuff_Outbox::send_now( (int) $row->id, $to, $args ); if ( is_wp_error( $result ) ) { return $this->error( $result->get_error_code(), $result->get_error_message(), 'send_failed' === $result->get_error_code() ? 500 : 422 ); } $mailer = Mailpuff_Mailer::get_instance(); $mailer->record_test_sent(); $status = $mailer->status(); return $this->success( array( /* translators: %s: an email address. */ 'message' => sprintf( __( 'Test sent to %s', 'mailpuff' ), $to ), 'mode' => $status['mode'], 'warning' => $status['warning'], ) ); } }