Sends an email invitation to a member.
Description
See also
Parameters
- $data
-
(array) (Required) AJAX data, including $userId and $formId arguments.
Return
(array|WP_Error) AJAX response array, or error object in case of failure.
Source
File: src/modules/access-controls/members.php
public function ajax_invite_member( $data ) { if ( empty( $data['userId'] ) || empty( $data['formId'] ) ) { return new WP_Error( 'missing_parameters', __( 'Missing request parameters.', 'torro-forms' ) ); } $user = get_user_by( 'id', $data['userId'] ); if ( ! $user || ! $user->exists() ) { return new WP_Error( 'cannot_find_user', __( 'Could not find user.', 'torro-forms' ) ); } $form = $this->module->manager()->forms()->get( $data['formId'] ); if ( ! $form ) { return new WP_Error( 'cannot_find_form', __( 'Could not find form.', 'torro-forms' ) ); } $mode = $this->is_user_invited( (int) $user->ID, $form->id ) ? 'reinvitation' : 'invitation'; $from_name = $this->get_option( $mode . '_from_name', '{sitetitle}' ); $from_email = $this->get_option( $mode . '_from_email', '{adminemail}' ); $subject = $this->get_option( $mode . '_subject', call_user_func( array( $this, 'get_default_' . $mode . '_subject' ) ) ); $message = $this->get_option( $mode . '_message', call_user_func( array( $this, 'get_default_' . $mode . '_message' ) ) ); $from_name = $this->template_tag_handler->process_content( $from_name, array( $form, $user ) ); $from_email = $this->template_tag_handler_email_only->process_content( $from_email, array( $form, $user ) ); $subject = $this->template_tag_handler->process_content( $subject, array( $form, $user ) ); $message = $this->template_tag_handler->process_content( $message, array( $form, $user ) ); $message = wpautop( $message ); add_filter( 'wp_mail_content_type', array( $this, 'override_content_type' ) ); add_filter( 'wp_mail_from_name', array( $this, 'override_from_name' ) ); add_filter( 'wp_mail_from', array( $this, 'override_from_email' ) ); $this->from_name = $from_name; $this->from_email = $from_email; $result = wp_mail( $user->user_email, $subject, $message ); $this->from_name = ''; $this->from_email = ''; remove_filter( 'wp_mail_content_type', array( $this, 'override_content_type' ) ); remove_filter( 'wp_mail_from_name', array( $this, 'override_from_name' ) ); remove_filter( 'wp_mail_from', array( $this, 'override_from_email' ) ); if ( ! $result ) { if ( 'reinvitation' === $mode ) { /* translators: %s: user display name */ return new WP_Error( 'cannot_reinvite_user', sprintf( __( 'User %s could not be reinvited.', 'torro-forms' ), $user->display_name ) ); } /* translators: %s: user display name */ return new WP_Error( 'cannot_invite_user', sprintf( __( 'User %s could not be invited.', 'torro-forms' ), $user->display_name ) ); } $this->set_user_invited( (int) $user->ID, $form->id ); if ( 'reinvitation' === $mode ) { return array( /* translators: %s: user display name */ 'message' => sprintf( __( 'User %s was successfully reinvited.', 'torro-forms' ), $user->display_name ), ); } return array( /* translators: %s: user display name */ 'message' => sprintf( __( 'User %s was successfully invited.', 'torro-forms' ), $user->display_name ), ); }
Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |