on ) {
global $wp_filter;
if ( empty( $wp_filter[ $action ]->callbacks ) || ! is_array( $wp_filter[ $action ]->callbacks ) ) {
return;
}
foreach ( $wp_filter[ $action ]->callbacks as $priority => $hooks ) {
foreach ( $hooks as $name => $arr ) {
if ( strpos( strtolower( $name ), 'no3x\wpml' ) !== false ) {
continue;
}
// Handle the case when the callback is an array.
if (
is_array( $arr ) && ! empty( $arr['function'] ) && is_array( $arr['function'] )
&& ! empty( $arr['function'][0] ) && is_object( $arr['function'][0] )
&& ( strpos( strtolower( get_class( $arr['function'][0] ) ), 'no3x\wpml' ) !== false )
) {
continue;
}
unset( $wp_filter[ $action ]->callbacks[ $priority ][ $name ] );
}
}
}
/**
* Action to log errors for mails failed to send.
*
* @since 1.8.0
* @global $wpml_current_mail_id
* @param \WP_Error $wperror
*/
public function log_email_failed( $wperror ) {
global $wpml_current_mail_id;
if(!isset($wpml_current_mail_id)) return;
$failed_mail = Mail::find_one($wpml_current_mail_id);
if( !$failed_mail ) return;
$failed_mail->set_error($wperror->get_error_message())->save();
}
/**
* Logs mail to database.
*
* @global $wpml_current_mail_id
*
* @param array $mailArray
*
* @since 1.0
* @since 1.12.0 Short-circuit if $mailArray is not an array.
* @since 1.13.0 Trim the subject to < 200 characters and save the Content-Type header if not set.
* @since 1.13.1 Add a hook action `wp_mail_logging_log_email` to log email data before being sent.
*
* @return array $mailOriginal
*/
public function log_email( $mailArray ) {
/**
* Log email data before being sent.
*
* @since 1.13.1
*
* @param array $mailArray Array containing the mail data to be logged.
*/
do_action( 'wp_mail_logging_log_email', $mailArray );
return $mailArray;
}
/**
* Save the email logs to the database.
*
* @since 1.13.1
*
* @param array $mail_data Array containing the mail data to be logged.
*/
public function save_email_log( $mail_data ) {
/**
* Filters mail data before it is logged.
*
* @since 1.12.0
*
* @param array $mail_data Array containing the mail data to be logged.
*/
$mail_data = apply_filters( 'wp_mail_logging_before_log_email', $mail_data );
if ( ! is_array( $mail_data ) ) {
return $mail_data;
}
global $wpml_current_mail_id;
if ( ! empty( $mail_data['subject'] ) && mb_strlen( $mail_data['subject'] ) > 200 ) {
$mail_data['subject'] = mb_substr( $mail_data['subject'], 0, 195 ) . '...';
}
$mail_data['headers'] = $this->get_mail_headers( $mail_data );
$mail = (new WPML_MailExtractor())->extract( $mail_data );
$mail->set_plugin_version( $this->getVersionSaved() );
$mail->set_timestamp( current_time( 'mysql' ) );
$mail->set_host( isset( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '' );
$wpml_current_mail_id = $mail->save();
}
/**
* Get the headers of the mail to be logged.
*
* @since 1.13.0
*
* @param array $mail_array Array containing the mail data to be logged.
*
* @return string[]
*/
public function get_mail_headers( $mail_array ) {
$content_type = 'Content-Type: ' . apply_filters( 'wp_mail_content_type', 'text/html' );
if ( empty( $mail_array['headers'] ) ) {
return [ $content_type ];
}
$mail_headers = WPML_Utils::clean_headers( $mail_array['headers'] );
if ( empty( $mail_headers ) ) {
return [ $content_type ];
}
$should_force_add_content_type = true;
foreach ( $mail_headers as $mail_header ) {
$header_arr = explode( ":", $mail_header );
// If Content-Type header is already set, don't add it again.
if ( ! empty( $header_arr[0] ) && strtolower( $header_arr[0] ) === 'content-type' ) {
$should_force_add_content_type = false;
}
}
if ( $should_force_add_content_type ) {
$mail_headers[] = $content_type;
}
return $mail_headers;
}
public static function getClass() {
return __CLASS__;
}
/**
* When user is on a WP Mail Logging related admin page, display footer text
* that graciously asks them to rate us.
*
* @since 1.11.0
*
* @param string $text Footer text.
*
* @return string
*/
public function admin_footer( $text ) {
global $current_screen, $wp_logging_list_page;
if ( empty( $current_screen->id ) || $current_screen->id !== $wp_logging_list_page ) {
return $text;
}
$url = 'https://wordpress.org/support/plugin/wp-mail-logging/reviews/?filter=5#new-post';
return sprintf(
wp_kses( /* translators: $1$s - WP Mail Logging plugin name; $2$s - WP.org review link; $3$s - WP.org review link. */
__( 'Please rate %1$s ★★★★★ on WordPress.org to help us spread the word.', 'wp-mail-logging' ),
[
'a' => [
'href' => [],
'target' => [],
'rel' => [],
],
]
),
'WP Mail Logging',
$url,
$url
);
}
/**
* Plugin activation hook.
*
* @since 1.11.0
*
* @return void
*/
public function activate() {}
/**
* Get the assets URL.
*
* @since 1.11.0
*
* @return string
*/
public function get_assets_url() {
return untrailingslashit( WP_MAIL_LOGGING_PLUGIN_URL ) . '/assets';
}
}