Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UR-830 Feature - Email preview #551

Merged
merged 17 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion includes/admin/class-ur-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,13 @@ public static function output_fields( $options ) {

if ( 'card' === $section['type'] ) {
$settings .= '<div class="user-registration-card ur-mt-4 ur-border-0">';
$settings .= '<div class="user-registration-card__header ur-border-0">';

$header_css = '';
if ( isset( $section['preview_link'] ) ) {
$header_css = 'display:flex; justify-content: space-between;';
}

$settings .= '<div class="user-registration-card__header ur-border-0" style="' . esc_attr( $header_css ) . '">';
if ( ! empty( $section['title'] ) ) {
$settings .= '<h3 class="user-registration-card__title">' . esc_html( strtoupper( $section['title'] ) );

Expand All @@ -281,6 +286,11 @@ public static function output_fields( $options ) {

$settings .= '</h3>';
}

if ( isset( $section['preview_link'] ) ) {
$settings .= wp_kses_post( $section['preview_link'] );
}

$settings .= '</div>';

if ( ! empty( $section['desc'] ) ) {
Expand Down
11 changes: 11 additions & 0 deletions includes/admin/settings/class-ur-settings-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public function email_notification_setting( $settings, $value ) {
array(
'name' => __( 'Email', 'user-registration' ),
'status' => __( 'Status', 'user-registration' ),
'preview' => __( 'Preview', 'user-registration' ),
'actions' => __( 'Configure', 'user-registration' ),
)
);
Expand Down Expand Up @@ -220,6 +221,16 @@ public function email_notification_setting( $settings, $value ) {
$settings .= '</label>';
$settings .= '</td>';
$settings .= '<td class="ur-email-settings-table">';
$settings .= '<a class="button tips user-registration-email-preview " target="__blank" data-tip="' . esc_attr__( 'Preview', 'user-registration' ) . '" href="' . esc_url(
add_query_arg(
array(
'ur_email_preview' => $email->id,
),
home_url()
)
) . '"><span class="dashicons dashicons-visibility"></span></a>';
$settings .= '</td>';
$settings .= '<td class="ur-email-settings-table">';
$settings .= '<a class="button tips" data-tip="' . esc_attr__( 'Configure', 'user-registration' ) . '" href="' . esc_url( admin_url( 'admin.php?page=user-registration-settings&tab=email&section=ur_settings_' . $email->id . '' ) ) . '"><span class="dashicons dashicons-admin-generic"></span> </a>';
$settings .= '</td>';
$settings .= '</tr>';
Expand Down
14 changes: 9 additions & 5 deletions includes/admin/settings/emails/class-ur-settings-admin-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'admin_email' => array(
'title' => __( 'Admin Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Admin Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to admin after successful user registration.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'awaiting_admin_approval_email' => array(
'title' => __( 'Awaiting Admin Approval Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Awaiting Admin Approval Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to user notifying the registration is awaiting admin approval.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'email_confirmation' => array(
'title' => __( 'Confirmation Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Confirmation Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Email Subject', 'user-registration' ),
'desc' => __( 'The email subject you want to customize.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'profile_details_changed_email' => array(
'title' => __( 'Profile Details Changed Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Profile Details Changed Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to the admin when a user changed profile information.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'registration_approved_email' => array(
'title' => __( 'Registration Approved Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Registration Approved Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to the user notifying the registration is approved by site admin.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'registration_denied_email' => array(
'title' => __( 'Registration Denied Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Registration Denied Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to the user notifying the registration is denied by the admin.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'registration_pending_email' => array(
'title' => __( 'Registration Pending Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Registration Pending Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to the user notifying the registration is pending.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'reset_password_email' => array(
'title' => __( 'Reset Password Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Reset Password Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this to send an email to the user when they request for a password reset.', 'user-registration' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public function get_settings() {
'title' => __( 'Emails', 'user-registration' ),
'sections' => array(
'successfully_registered_email' => array(
'title' => __( 'Successfully Registered Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'settings' => array(
'title' => __( 'Successfully Registered Email', 'user-registration' ),
'type' => 'card',
'desc' => '',
'back_link' => ur_back_link( __( 'Return to emails', 'user-registration' ), admin_url( 'admin.php?page=user-registration-settings&tab=email' ) ),
'preview_link' => ur_email_preview_link(
__( 'Preview', 'user-registration' ),
$this->id
),
'settings' => array(
array(
'title' => __( 'Enable this email', 'user-registration' ),
'desc' => __( 'Enable this email sent to the user after successful user registration.', 'user-registration' ),
Expand Down
6 changes: 1 addition & 5 deletions includes/class-ur-emailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,7 @@ public static function parse_smart_tags( $content = '', $values = array(), $name
* @return void
*/
public static function user_registration_process_and_send_email( $email, $subject, $message, $header, $attachment, $template_id ) {
// Check if email template is selected.
if ( '' !== $template_id && 'none' !== $template_id ) {
$message = apply_filters( 'user_registration_email_template_message', $message, $template_id );
}

$message = user_registration_process_email_content( $message, $template_id );
wp_mail( $email, $subject, $message, $header, $attachment, $template_id );
}
}
Expand Down
49 changes: 49 additions & 0 deletions includes/class-ur-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function init() {
add_filter( 'frontpage_template_hierarchy', array( $this, 'template_include' ) );
add_filter( 'astra_remove_entry_header_content', '__return_true' ); // Need to remove in next version, If astra release the patches.

} elseif ( isset( $_GET['ur_email_preview'] ) ) {
add_filter( 'template_include', array( $this, 'handle_email_preview' ), PHP_INT_MAX );
add_filter( 'astra_remove_entry_header_content', '__return_true' ); // Need to remove in next version, If astra release the patches.
}
}
}
Expand Down Expand Up @@ -196,6 +199,52 @@ public function login_form_preview_content( $content ) {
echo '</div>';
return ob_get_clean();
}

/**
* Displays content of email preview.
*
* @param string $content Page/Post content.
* @return string
*/
public function handle_email_preview() {
if ( ! is_user_logged_in() ) {
return;
}

$option_name = isset( $_GET['ur_email_preview'] ) ? sanitize_text_field( $_GET['ur_email_preview'] ) : '';
$email_template = isset( $_GET['ur_email_template'] ) ? sanitize_text_field( $_GET['ur_email_template'] ) : '';

$class_name = 'UR_Settings_' . str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $option_name ) ) );

$emails = apply_filters( 'user_registration_email_classes', array() );

if ( isset( $emails[ $class_name ] ) && ! class_exists( $class_name ) ) {
$class_name = get_class( $emails[ $class_name ] );
}

if ( ! class_exists( $class_name ) ) {
echo '<h3>' . esc_html_e( 'Something went wrong. Please verify if the email you want to preview exists or addon it is associated with is activated.' ) . '</h3>';
} else {
$class_instance = new $class_name();
$default_content = 'ur_get_' . $option_name;

if ( ! method_exists( $class_instance, $default_content ) ) {
$default_content = 'user_registration_get_' . $option_name;
}

$email_content = get_option( 'user_registration_' . $option_name, $class_instance->$default_content() );
$email_content = apply_filters( 'user_registration_process_smart_tags', $email_content );

ur_get_template(
'email-preview.php',
array(
'email_content' => $email_content,
'email_template' => $email_template,
)
);
}

}
}

new UR_Preview();
54 changes: 54 additions & 0 deletions includes/functions-ur-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -3746,6 +3746,60 @@ function ur_get_all_pages() {
}
}


if ( ! function_exists( 'user_registration_process_email_content' ) ) {
/**
* Returns email content wrapped in email template.
*
* @param string $email_content Email Content.
* @param string $template Email Template id.
*/
function user_registration_process_email_content( $email_content, $template = '' ) {
// Check if email template is selected.
if ( '' !== $template && 'none' !== $template ) {
$email_content = apply_filters( 'user_registration_email_template_message', $email_content, $template );
} else {
ob_start();
?>
<div class="user-registration-email-body" style="padding: 100px 0; background-color: #ebebeb;">
<table class="user-registration-email" border="0" cellpadding="0" cellspacing="0" style="width: 40%; margin: 0 auto; background: #ffffff; padding: 30px 30px 26px; border: 0.4px solid #d3d3d3; border-radius: 11px; font-family: 'Segoe UI', sans-serif; ">
<tbody>
<tr>
<td colspan="2" style="text-align: left;">
<?php echo wp_kses_post( $email_content ); ?>
</td>
</tr>
</tbody>
</table>
</div>
<?php
$email_content = wp_kses_post( ob_get_clean() );
}

return $email_content;
}
}

if ( ! function_exists( 'ur_email_preview_link' ) ) {

/**
* Get link for preview email button used on email settings.
*
* @param string $label Label.
* @param string $email_id Email id.
*/
function ur_email_preview_link( $label, $email_id ) {
$url = add_query_arg(
array(
'ur_email_preview' => $email_id,
),
home_url()
);

return '<a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( $label ) . '" class="button user-registration-email-preview " style="min-width:70px;">' . esc_html( $label ) . '</a>';
}
}

add_action( 'user_registration_after_user_meta_update', 'ur_parse_and_update_hidden_field', 10, 3 );

if ( ! function_exists( 'ur_parse_and_update_hidden_field' ) ) {
Expand Down
Loading
Loading