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

Tweak - Catch mail send error #1316

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions includes/admin/class-evf-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'survey' => 'survey_notice',
'allow_usage' => 'allow_usage_notice',
'php_deprecation' => 'php_deprecation_notice',
'email_failed' => 'email_failed_notice',
);

/**
Expand Down Expand Up @@ -84,6 +85,7 @@
self::add_notice( 'survey' );
self::add_notice( 'allow_usage' );
self::add_notice( 'php_deprecation' );
self::add_notice( 'email_failed' );
}

/**
Expand Down Expand Up @@ -245,7 +247,7 @@
'dismissed' => false,
);
update_option( 'everest_forms_review', $review );
} else {

Check failure on line 250 in includes/admin/class-evf-admin-notices.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

If control structure block found as the only statement within an "else" block. Use elseif instead.
// Check if it has been dismissed or not.
if ( ( isset( $review['dismissed'] ) && ! $review['dismissed'] ) && ( isset( $review['time'] ) && ( ( $review['time'] + DAY_IN_SECONDS ) <= $time ) ) ) {
$load = true;
Expand All @@ -260,7 +262,7 @@
if ( empty( $entries_count ) || $entries_count < 50 ) {
return;
}
} else {

Check failure on line 265 in includes/admin/class-evf-admin-notices.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

If control structure block found as the only statement within an "else" block. Use elseif instead.
// Only continue if plugin has been installed for at least 14 days.
if ( ( $activated + ( WEEK_IN_SECONDS * 2 ) ) > $time ) {
return;
Expand Down Expand Up @@ -301,7 +303,7 @@

return $status;

}

Check failure on line 306 in includes/admin/class-evf-admin-notices.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Function closing brace must go on the next line following the body; found 1 blank lines before brace

/**
* Include allow usage & discount notice.
Expand Down Expand Up @@ -341,6 +343,23 @@
}
}

/**
* Email Failed Notice
*/
public static function email_failed_notice() {
$failed_data = get_transient( 'everest_forms_mail_send_failed_count' );
$failed_count = isset( $failed_data['failed_count'] ) ? $failed_data['failed_count'] : 0;
$error_message = isset( $failed_data['error_message'] ) ? $failed_data['error_message'] : '';
$show_notice = $failed_count > 5 ? true : false;
if ( $show_notice ) {
$is_dismissed = get_option( 'everest_forms_email_send_notice_dismiss', false );
}
if ( $show_notice && ! $is_dismissed ) {
include 'views/html-notice-email-failed-notice.php';
}
}


/**
* Remove non-EverestForms notices from EverestForms pages.
*
Expand Down
43 changes: 43 additions & 0 deletions includes/admin/views/html-notice-email-failed-notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Admin View: Notice - Email Failed Notice.
*
* @package Everest Forms
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
?>

<div class="notice notice-warning is-dismissible" id="everest-forms-email-failed-notice">
<p>
<strong><?php esc_html_e( 'Everest Forms Email Send Error', 'everest-forms' ); ?></strong><br/>
<?php esc_html_e( 'The last email sent from the User Registration Plugin was not delivered to the user.', 'everest-forms' ); ?>
</p>
<p style="border-left: 2px solid #72aee6; background: #F0FFFF; padding: 10px;">
<?php esc_html_e( ''.$error_message.'', 'everest-forms' ); ?>

Check failure on line 19 in includes/admin/views/html-notice-email-failed-notice.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

The $text parameter must be a single text string literal. Found: ''.$error_message.''

Check failure on line 19 in includes/admin/views/html-notice-email-failed-notice.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Concat operator must be surrounded by a single space

Check failure on line 19 in includes/admin/views/html-notice-email-failed-notice.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Concat operator must be surrounded by a single space
</p>
<br/>
<p>
<a href="https://docs.wpeverest.com/everest-forms/docs/emails-are-not-being-delivered/" target="_blank">
<?php esc_html_e( 'Learn More', 'everest-forms' ); ?>
</a>
</p>
</div>

<script>
jQuery(function($) {
$(document).ready(function() {
var noticeContainer = $('#everest-forms-email-failed-notice');
noticeContainer.find('.notice-dismiss').on('click', function(e) {
e.preventDefault();

$.post(ajaxurl, {
action: 'everest_forms_email_failed_notice_dismiss',
_wpnonce: '<?php echo esc_js( wp_create_nonce( 'email_failed_nonce' ) ); ?>'
});
});
});
});
</script>
11 changes: 11 additions & 0 deletions includes/class-evf-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'survey_dismiss' => false,
'allow_usage_dismiss' => false,
'php_notice_dismiss' => false,
'email_failed_notice_dismiss' => false,
'enabled_form' => false,
'import_form_action' => false,
'template_licence_check' => false,
Expand Down Expand Up @@ -830,6 +831,16 @@
wp_die();
}

public static function email_failed_notice_dismiss() {

Check failure on line 834 in includes/class-evf-ajax.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Missing doc comment for function email_failed_notice_dismiss()
check_ajax_referer( 'email_failed_nonce', '_wpnonce' );

if ( ! current_user_can( 'manage_everest_forms' ) ) {
wp_die( -1 );
}
update_option( 'everest_forms_email_send_notice_dismiss', true );
wp_die();
}

/**
* Triggered when clicking the form toggle.
*/
Expand Down Expand Up @@ -941,7 +952,7 @@
$message = '<div class="everest-forms-message-text">';
$message .= '<h3 style="text-align:center; color: #ffc107;">' . esc_html( 'PS. This is just the sample data' ) . '</h3>';
$message .= '<p><strong>' . esc_html__( 'Hi there!', 'everest-forms' ) . ' 👋</strong></p>';
$message .= '<p>' . esc_html__( 'Let\'s see how your forms performed ' . $evf_summary_duration . '.', 'everest-forms' ) . '</p>';

Check failure on line 955 in includes/class-evf-ajax.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

The $text parameter must be a single text string literal. Found: 'Let\'s see how your forms performed ' . $evf_summary_duration . '.'
$message .= '<br/>';
$message .= '<p><strong>' . esc_html__( 'Forms Stats', 'everest-forms' ) . '</strong></p>';
$message .= '<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="solid #dddddd; display:block;min-width: 100%;border-collapse: collapse;width:100%; display:table; padding-bottom:2rem" class="evf_entries_summary_table">';
Expand Down Expand Up @@ -1173,7 +1184,7 @@
$is_imported = false;
$imported_text = esc_html__( 'No', 'everest-forms' );
}
$forms_list_table .= '<tr id="evf-fm-row-' . esc_attr( $row ) . '" class="evf-fm-row ' . esc_attr( $hidden ) . '"><td><input class="evf-fm-select-single" type="checkbox" name="fm_select_single_form_' . esc_attr( $form_id ) . '" data-form-id="' . esc_attr( $form_id ) . '" /></td><td>' . esc_html__( $form_name, 'everest-forms' ) . '</td><td><p class="evf-fm-imported" data-form-id="' . esc_attr( $form_id ) . '">' . esc_attr( $imported_text ) . '<p></td>';

Check failure on line 1187 in includes/class-evf-ajax.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

The $text parameter must be a single text string literal. Found: $form_name
$forms_list_table .= '<td>';
$forms_list_table .= '<div class="evf-fm-import-actions"><button class="evf-fm-import-single" data-form-id="' . esc_attr( $form_id ) . '">' . esc_html( 'Import Form' ) . '</button>';
if ( 'contact-form-7' !== $form_slug ) {
Expand Down
13 changes: 13 additions & 0 deletions includes/class-evf-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ public function send( $to, $subject, $message, $attachments = '', $connection_id
// Let's do this.
$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $this->attachments );

if ( ! $sent ) {
$error_message = apply_filters( 'everest_forms_email_send_failed_message', '' );
$failed_data = get_transient( 'everest_forms_mail_send_failed_count' );
$failed_count = $failed_data && isset( $failed_data['failed_count'] ) ? $failed_data['failed_count'] : 0;
++$failed_count;
set_transient(
'everest_forms_mail_send_failed_count',
array(
'failed_count' => $failed_count,
'error_message' => $error_message,
)
);
}
// Hooks after the email is sent.
do_action( 'everest_forms_email_send_after', $this );

Expand Down
37 changes: 37 additions & 0 deletions includes/evf-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@
* @return array of form data.
*/
function evf_get_all_forms( $skip_disabled_entries = false, $check_disable_storing_entry_info = true ) {
if( is_null( evf()->form ) ) {

Check failure on line 1135 in includes/evf-core-functions.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Space after opening control structure is required
return array();
}
$forms = array();
Expand Down Expand Up @@ -5593,3 +5593,40 @@

return isset( $next_key ) ? $next_key : '' ;
}


add_action( 'wp_mail_failed', 'evf_email_send_failed_handler', 1 );

if ( ! function_exists( 'evf_email_send_failed_handler' ) ) {

/**
* Handle errors fetch mechanism when mail send failed.
*
* @param object $error_instance WP_Error message instance.
*/
function evf_email_send_failed_handler( $error_instance ) {
$error_message = '';
$decoded_message = json_decode( $error_instance->get_error_message() );

if ( json_last_error() === JSON_ERROR_NONE && ! empty( $decoded_message ) ) {
/* translators: %s: Status Log URL */
$error_message = wp_kses_post( sprintf( __( 'Please check the `evf_mail_errors` log under <a target="_blank" href="%s"> Logs </a> section.', 'everest-forms' ), admin_url( 'admin.php?page=evf-tools&tab=logs' ) ) );
} else {
$error_message = $error_instance->get_error_message();
}

evf_get_logger()->info(
$error_message,
array( 'source' => 'evf_mail_errors' )
);

if ( ! empty( $error_message ) ) {
add_filter(
'everest_forms_email_send_failed_message',
function ( $msg ) use ( $error_message ) {
return $error_message;
}
);
}
}
}
Loading