Skip to content

Commit 1c7a6ec

Browse files
committed
moving the code that sends email into admin tools
1 parent 5ab8ec5 commit 1c7a6ec

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

admin/CF7_AntiSpam_Admin_Tools.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace CF7_AntiSpam\Admin;
44

5-
use CF7_AntiSpam\Core\CF7_AntiSpam;
6-
use CF7_AntiSpam\Core\CF7_AntiSpam_Filters;
7-
use CF7_AntiSpam\Core\CF7_AntiSpam_Flamingo;
8-
use CF7_AntiSpam\Engine\CF7_AntiSpam_Uninstaller;
9-
105
/**
116
* The plugin admin tools
127
*
@@ -23,17 +18,20 @@ class CF7_AntiSpam_Admin_Tools {
2318
/**
2419
* It sets a transient with the name of `cf7a_notice` and the value of the notice
2520
*
26-
* @param string $message The message you want to display.
27-
* @param string $type error, warning, success, info.
21+
* @param string $message The message you want to display.
22+
* @param string $type error, warning, success, info.
2823
* @param boolean $dismissible when the notice needs the close button.
2924
*/
30-
public static function cf7a_push_notice( $message = 'generic', $type = 'error', $dismissible = true ) {
25+
public static function cf7a_push_notice( string $message = 'generic', string $type = 'error', bool $dismissible = true ) {
3126
$class = "notice notice-$type";
3227
$class .= $dismissible ? ' is-dismissible' : '';
3328
$notice = sprintf( '<div class="%s"><p>%s</p></div>', esc_attr( $class ), esc_html( $message ) );
3429
set_transient( 'cf7a_notice', $notice );
3530
}
3631

32+
/**
33+
* It exports the blacklist
34+
*/
3735
public static function cf7a_export_blacklist() {
3836
global $wpdb;
3937
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
@@ -67,4 +65,38 @@ public function cf7a_handle_actions() {
6765
exit();
6866
}
6967
}
68+
69+
/**
70+
* It sends an email to the admin
71+
*
72+
* @param string $subject the mail message subject
73+
* @param string $recipient the mail recipient
74+
* @param string $body the mail message content
75+
* @param string $sender the mail message sender
76+
*/
77+
public function send_email_to_admin( string $subject, string $recipient, string $body, string $sender ) {
78+
/**
79+
* Filter cf7-antispam before resend an email who was spammed
80+
*
81+
* @param string $body the mail message content
82+
* @param string $sender the mail message sender
83+
* @param string $subject the mail message subject
84+
* @param string $recipient the mail recipient
85+
*
86+
* @returns string the mail body content
87+
*/
88+
$body = apply_filters( 'cf7a_before_resend_email', $body, $sender, $subject, $recipient );
89+
90+
// Set up headers correctly
91+
$site_name = get_bloginfo( 'name' );
92+
$from_email = get_option( 'admin_email' );
93+
94+
$headers = "From: {$site_name} <{$from_email}>\n";
95+
$headers .= "Content-Type: text/html\n";
96+
$headers .= "X-WPCF7-Content-Type: text/html\n";
97+
$headers .= "Reply-To: {$sender}\n";
98+
99+
/* send the email */
100+
return wp_mail( $recipient, $subject, $body, $headers );
101+
}
70102
}

core/CF7_AntiSpam_Flamingo.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CF7_AntiSpam\Core;
44

5+
use CF7_AntiSpam\Admin\CF7_AntiSpam_Admin_Tools;
56
use WP_Query;
67
use WPCF7_ContactForm;
78
use WPCF7_Submission;
@@ -208,7 +209,7 @@ private static function cf7a_get_mail_field( $flamingo_post, $field ) {
208209
*
209210
* @return array { success: boolean, message: string }
210211
*/
211-
public function cf7a_resend_mail( $mail_id ) {
212+
public function cf7a_resend_mail( int $mail_id ): array {
212213
$flamingo_data = new Flamingo_Inbound_Message( $mail_id );
213214
$message = self::cf7a_get_mail_field( $flamingo_data, 'message' );
214215

@@ -264,29 +265,9 @@ public function cf7a_resend_mail( $mail_id ) {
264265
}
265266
}
266267

267-
/**
268-
* Filter cf7-antispam before resend an email who was spammed
269-
*
270-
* @param string $body the mail message content
271-
* @param string $sender the mail message sender
272-
* @param string $subject the mail message subject
273-
* @param string $recipient the mail recipient
274-
*
275-
* @returns string the mail body content
276-
*/
277-
$body = apply_filters( 'cf7a_before_resend_email', $body, $sender, $subject, $recipient );
278-
279-
// Set up headers correctly
280-
$site_name = get_bloginfo( 'name' );
281-
$from_email = get_option( 'admin_email' );
282-
283-
$headers = "From: {$site_name} <{$from_email}>\n";
284-
$headers .= "Content-Type: text/html\n";
285-
$headers .= "X-WPCF7-Content-Type: text/html\n";
286-
$headers .= "Reply-To: {$sender}\n";
287-
288-
/* send the email */
289-
$result = wp_mail( $recipient, $subject, $body, $headers );
268+
$tools = new CF7_AntiSpam_Admin_Tools();
269+
$result = $tools->send_email_to_admin( $subject, $recipient, $body, $sender );
270+
290271
if ( $result ) {
291272
return array( 'success'=> true, 'message' => __( 'Email sent with success', 'cf7-antispam' ) );
292273
}
@@ -295,7 +276,7 @@ public function cf7a_resend_mail( $mail_id ) {
295276
}
296277

297278
/**
298-
* Parse CF7 mail tags in recipient field
279+
* Parse CF7 mail tags in the recipient field
299280
*
300281
* @param string $recipient The recipient string that may contain CF7 tags
301282
* @param Flamingo_Inbound_Message $flamingo_data The flamingo message data

0 commit comments

Comments
 (0)