-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe2e-plugin.php
executable file
·129 lines (119 loc) · 3.05 KB
/
e2e-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Plugin Name: Newspack E2E Plugin
* Description: Special considerations for E2E testing.
* Version: 0.0.0
* Author: Automattic
* Author URI: https://newspack.com/
* License: GPL2
* Text Domain: newspack-e2e-plugin
* Domain Path: /languages/
*
* @package Newspack_E2E_Plugin
*/
defined( 'ABSPATH' ) || exit;
// Prevent the admin email confirmation screen
add_filter( 'admin_email_check_interval', '__return_false' );
// Register custom post type for email logs.
add_action(
'init',
function () {
$args = [
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'email_log' ],
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => [ 'title', 'editor', 'author', 'custom-fields' ],
];
$result = register_post_type( 'email_log', $args );
if ( is_wp_error( $result ) ) {
error_log( 'Failed to create the email_log CPT.' );
}
}
);
// Enable logout without nonce.
add_action(
'init',
function () {
if ( isset( $_GET['action'] ) && $_GET['action'] === 'logout_without_nonce' ) {
wp_logout();
wp_redirect( home_url() );
exit;
}
}
);
// Save outgoing emails as email_log CPT.
add_action(
'wp_mail',
function ( $attributes ) {
$recipient = $attributes['to'];
if ( empty( $recipient ) ) {
return;
}
// Only save emails sent to non-admin users.
$user = get_user_by( 'email', $recipient );
if ( $user && in_array( 'administrator', $user->roles ) ) {
return;
}
$attributes['message'] = preg_replace( '/<\/title>.*?<div/s', '</title><div', $attributes['message'] );
$post_data = [
'post_title' => $attributes['subject'] . ' (' . $recipient . ')',
'post_content' => $attributes['message'],
'post_status' => 'publish',
'post_type' => 'email_log',
];
wp_insert_post( $post_data );
}
);
// Display all sent emails.
add_action(
'init',
function () {
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '/_email' ) === 0 ) {
header( 'Content-Type: text/html' );
?>
<html><head><title>Email Sendbox</title></head><body>
<h1>Email Sendbox</h1>
<style>
.email-content{
border: 1px solid gray;
margin: 20px 0;
}
</style>
<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'email_log' ORDER BY post_date DESC", ARRAY_A );
if ( ! empty( $results ) ) {
foreach ( $results as $email ) {
?>
<br>
<div>
<details>
<summary>
<strong><?php echo esc_html( $email['post_title'] ); ?></strong> - <?php echo esc_html( $email['post_date'] ); ?>
</summary>
<div class="email-content">
<?php echo $email['post_content']; ?>
</div>
</details>
</div>
<?php
}
} else {
?>
<p>No emails found.</p>
<?php
}
?>
</body></html>
<?php
exit;
}
}
);