Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 33 additions & 12 deletions AGENTS.md

Large diffs are not rendered by default.

54 changes: 48 additions & 6 deletions README.md

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,72 @@ input[type="checkbox"].safety-net-pause-renewal-actions-toggle:checked:after {
content: 'PAUSED';
}

input[type="checkbox"].safety-net-send-emails-toggle:checked:after {
background-color: #d63638;
}

.form-table td p.description.safety-net-warning {
color: #b32d2e;
font-weight: 600;
}

.safety-net-status {
margin: 20px 0;
}

.safety-net-status-table th,
.safety-net-status-table td {
overflow-wrap: anywhere;
}

.safety-net-status-table tbody th {
width: 22%;
font-weight: 600;
}

.safety-net-status-table .safety-net-status-state {
width: 28%;
}

.safety-net-status-table .dashicons {
margin-right: 4px;
vertical-align: text-bottom;
}

.safety-net-status-ok .dashicons {
color: #008a20;
}

.safety-net-status-warning .dashicons {
color: #b26200;
}

.safety-net-status-danger .dashicons {
color: #d63638;
}

.safety-net-status-danger .safety-net-status-state {
color: #b32d2e;
font-weight: 600;
}

@media screen and (max-width: 782px) {
.safety-net-status-table thead {
display: none;
}

.safety-net-status-table tbody th,
.safety-net-status-table tbody td,
.safety-net-status-table .safety-net-status-state {
display: block;
width: auto;
}

.safety-net-status-table tbody th {
padding-bottom: 0;
}
}

button.safety-net-save {
margin-top: 10px;
}
Expand Down
14 changes: 14 additions & 0 deletions assets/js/safety-net-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@
}
}

// Only switching on is confirmed; switching off is always safe.
function confirmSwitchOn( event ) {
const input = event.target;

if ( ! ( input instanceof HTMLInputElement ) || 'checkbox' !== input.type || ! input.checked || ! input.dataset.safetyNetConfirm ) {
return;
}

if ( ! confirm( input.dataset.safetyNetConfirm ) ) {
input.checked = false;
}
}

function initEmailLogTabs( root ) {
for ( const tablist of root.querySelectorAll( '.safety-net-email-log-tabs' ) ) {
const selected = tablist.querySelector( '[role="tab"][aria-selected="true"]' ) || tablist.querySelector( '[role="tab"]' );
Expand Down Expand Up @@ -398,6 +411,7 @@

// Delegated, so that the forms of an entry loaded into the dialog are handled too.
document.addEventListener( 'submit', confirmSubmit );
document.addEventListener( 'change', confirmSwitchOn );
document.addEventListener( 'click', clickEmailLogTab );
document.addEventListener( 'keydown', moveEmailLogTab );
document.addEventListener( 'click', openEmailLogEntry );
Expand Down
96 changes: 85 additions & 11 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use function SafetyNet\EmailLog\page_hooks;
use function SafetyNet\EmailLog\page_url;
use function SafetyNet\EmailLog\sending_copy;
use function SafetyNet\Status\render_status_panel;
use function SafetyNet\Utilities\are_emails_enabled;
use function SafetyNet\Utilities\get_admin_emails;
use function SafetyNet\Utilities\get_admin_user_ids;
use function SafetyNet\Utilities\get_environment_type;
Expand All @@ -20,6 +22,8 @@
use function SafetyNet\DisableWebhooks\disable_webhooks;
use function SafetyNet\Integrations\plugin_patterns;
use function SafetyNet\Utilities\is_denylisted_plugin;
use const SafetyNet\EmailLog\SETTINGS_SLUG;
use const SafetyNet\Utilities\SEND_EMAILS_OPTION;

add_filter( 'init', __NAMESPACE__ . '\add_admin_hooks' );

Expand Down Expand Up @@ -50,6 +54,8 @@ function add_admin_hooks() {
add_filter( 'plugin_action_links_' . SAFETY_NET_BASENAME, __NAMESPACE__ . '\add_action_links' );
}
add_action( 'admin_notices', __NAMESPACE__ . '\show_warning' );
add_action( 'admin_notices', __NAMESPACE__ . '\show_emails_enabled_notice' );
add_action( 'network_admin_notices', __NAMESPACE__ . '\show_emails_enabled_notice' );
add_action( 'admin_notices', __NAMESPACE__ . '\show_keep_notice' );
add_action( 'admin_notices', __NAMESPACE__ . '\show_active_denylisted_warning' );
add_action( 'network_admin_notices', __NAMESPACE__ . '\show_active_denylisted_warning' );
Expand All @@ -67,7 +73,7 @@ function enqueue_scripts( string $hook_suffix ) {
return;
}

wp_enqueue_script( 'safety-net-admin', SAFETY_NET_URL . 'assets/js/safety-net-admin.js', array( 'jquery', 'wp-a11y' ), '1.2', true );
wp_enqueue_script( 'safety-net-admin', SAFETY_NET_URL . 'assets/js/safety-net-admin.js', array( 'jquery', 'wp-a11y' ), '1.3', true );

wp_localize_script(
'safety-net-admin',
Expand All @@ -77,7 +83,7 @@ function enqueue_scripts( string $hook_suffix ) {
)
);

wp_enqueue_style( 'safety-net-admin-style', SAFETY_NET_URL . 'assets/css/admin.css', array(), '0.2' );
wp_enqueue_style( 'safety-net-admin-style', SAFETY_NET_URL . 'assets/css/admin.css', array(), '0.3' );
}

/**
Expand Down Expand Up @@ -213,7 +219,7 @@ function settings_init() {
/**
* Renders the HTML for the settings.
*
* @param array $args Arguments passed to the fields.
* @param array $args Arguments passed to the fields; a checkbox's 'confirm' text is asked before it is switched on, and a disabled one keeps its 'hidden_value', or else its stored value.
*
* @return void
*/
Expand All @@ -224,17 +230,18 @@ function render_field( array $args = array() ) {

if ( 'checkbox' === $args['type'] ) {
printf(
'<input id="%s" class="%s" name="%s" type="checkbox"%s%s />',
'<input id="%s" class="%s" name="%s" type="checkbox"%s%s%s />',
esc_attr( $args['name'] ),
esc_attr( $args['class'] ),
esc_attr( $args['name'] ),
checked( 'on', $args['value'] ?? get_option( $args['name'] ), false ),
disabled( ! empty( $args['disabled'] ), true, false )
disabled( ! empty( $args['disabled'] ), true, false ),
isset( $args['confirm'] ) ? ' data-safety-net-confirm="' . esc_attr( $args['confirm'] ) . '"' : ''
);

// A disabled checkbox is not submitted, so saving the form would otherwise store it as off.
if ( ! empty( $args['disabled'] ) ) {
printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $args['name'] ), esc_attr( (string) get_option( $args['name'] ) ) );
printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $args['name'] ), esc_attr( (string) ( $args['hidden_value'] ?? get_option( $args['name'] ) ) ) );
}
}

Expand All @@ -249,7 +256,8 @@ function render_field( array $args = array() ) {

if ( isset( $args['description'] ) ) {
printf(
'<p class="description" id="tagline-description">%s</p>',
'<p class="%s" id="tagline-description">%s</p>',
esc_attr( isset( $args['description_class'] ) ? 'description ' . $args['description_class'] : 'description' ),
esc_html( $args['description'] )
);
}
Expand Down Expand Up @@ -451,6 +459,7 @@ function render_options_html() {
<a href="https://github.com/a8cteam51/safety-net/#plugin-not-running">More info in the README</a>.
</p>
<?php else : ?>
<?php render_status_panel(); ?>
<h3>Tools</h3>
<form action="options.php" method="post">
<?php
Expand Down Expand Up @@ -691,15 +700,67 @@ function show_warning() {
echo '</strong>';
esc_html_e( 'The Safety Net plugin is currently active', 'safety-net' );
echo '<br>';
// A site that drops the woocommerce-subscriptions integration keeps the toggle but loses the pause.
if ( ( is_data_deletion_disabled() || 'on' === get_option( 'safety_net_pause_renewal_actions_toggle' ) ) && false !== has_filter( 'action_scheduler_store_class', 'SafetyNet\Integrations\WooCommerceSubscriptions\paused_store_class' ) ) {
if ( are_renewals_paused() ) {
esc_html_e( 'WooCommerce Subscriptions scheduled actions are currently paused.', 'safety-net' );
echo '<br>';
}
echo 'This site\'s environment type is set to "' . esc_html( get_environment_type() ) . '".';
echo '</p></div>';
}

/**
* Whether Action Scheduler skips WooCommerce Subscriptions' renewal, payment retry and end of prepaid term actions in this request.
*
* @return bool
*/
function are_renewals_paused(): bool {
// A site that drops the woocommerce-subscriptions integration keeps the toggle but loses the pause.
return ( is_data_deletion_disabled() || 'on' === get_option( 'safety_net_pause_renewal_actions_toggle' ) ) && false !== has_filter( 'action_scheduler_store_class', 'SafetyNet\Integrations\WooCommerceSubscriptions\paused_store_class' );
}

/**
* Warns on every wp-admin page, for users who can manage options, that emails are switched on, and says how to switch them off; in the network admin, for the main site, whose switch the network admin's emails follow.
*
* @return void
*/
function show_emails_enabled_notice() {
if ( ! are_emails_enabled() || ! current_user_can( 'manage_options' ) ) {
return;
}

$command = 'wp option update ' . SEND_EMAILS_OPTION . ' off';
if ( is_multisite() ) {
$command .= ' --url=' . home_url( '/' );
}
$command = '<code>' . esc_html( $command ) . '</code>';

if ( true === apply_filters( 'safety_net_hide_admin', false ) ) {
/* translators: %s: the WP-CLI command that switches emails off */
$how = sprintf( esc_html__( 'To stop it, run: %s', 'safety-net' ), $command );
} else {
$how = sprintf(
/* translators: 1: link to the Email Log Settings page, 2: the WP-CLI command that switches emails off */
esc_html__( 'To stop it, turn off Send emails in %1$s, or run: %2$s', 'safety-net' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=' . SETTINGS_SLUG ) ) . '">' . esc_html__( 'Email Log > Settings', 'safety-net' ) . '</a>',
$command
);
}

if ( is_network_admin() ) {
/* translators: %s: the main site's URL */
$title = sprintf( __( 'Safety Net: emails are switched on for %s.', 'safety-net' ), home_url() );
} else {
$title = __( 'Safety Net: emails are switched on.', 'safety-net' );
}

printf(
'<div class="notice notice-error safety-net-emails-enabled"><p><strong>%1$s</strong> %2$s</p><p>%3$s</p></div>',
esc_html( $title ),
esc_html__( 'This site sends every email WordPress sends to real addresses.', 'safety-net' ),
$how // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped above.
);
}

/**
* Returns the lines of the notice about SAFETY_NET_DELETE_DATA and SAFETY_NET_KEEP_UNTIL, as plain text.
*
Expand Down Expand Up @@ -772,7 +833,7 @@ function show_keep_notice() {
}

/**
* Stop all emails except password resets, which only go to administrators while SAFETY_NET_DELETE_DATA keeps data, and record each decision in the email log
* Stop all emails, unless emails are switched on, except password resets, which only go to administrators while SAFETY_NET_DELETE_DATA keeps data, and record each decision in the email log
*
* @param boolean|null $return WP_Mail short-circuit return value.
* @param array $args The wp_mail() arguments.
Expand All @@ -781,7 +842,8 @@ function show_keep_notice() {
*/
function stop_emails( $return, $args ) {
// One-shot, so emails sent from the copy's own hooks still get the rules below.
if ( sending_copy() ) {
$copy = sending_copy();
if ( $copy ) {
sending_copy( false );
if ( is_sent_only_to_admins( $args ) ) {
error_log( 'Email sent: ' . $args['subject'] . ' (copy of a logged email)' ); // phpcs:ignore -- Logging is okay here.
Expand All @@ -790,6 +852,18 @@ function stop_emails( $return, $args ) {
}
}

// Send to me promises a copy for administrators only, so the switch never lets one reach anyone else.
if ( ! $copy && are_emails_enabled() ) {
// The switch lifts only Safety Net's own block: another plugin's block, or a mailer that already sent, stands.
if ( null !== $return ) {
return $return;
}

error_log( 'Email sent: ' . $args['subject'] . ' (emails are switched on)' ); // phpcs:ignore -- Logging is okay here.
log_email( 'allowed_switch', $args );
return null;
}

if ( ! strstr( $args['subject'], 'Password Reset' ) ) {
error_log( "Email blocked: " . $args['subject'] ); // phpcs:ignore -- Logging is okay here.
// returning false says "short-circuit the wp_mail() function and indicate we did not send the email"
Expand Down
73 changes: 71 additions & 2 deletions includes/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,82 @@

namespace SafetyNet\Common;

add_filter( 'send_password_change_email', '__return_false' );
add_filter( 'send_email_change_email', '__return_false' );
use function SafetyNet\Utilities\are_emails_enabled;
use function SafetyNet\Utilities\bind_send_emails_switch;
use const SafetyNet\Utilities\SEND_EMAILS_OPTION;

add_filter( 'send_password_change_email', __NAMESPACE__ . '\follow_send_emails_switch' );
add_filter( 'send_email_change_email', __NAMESPACE__ . '\follow_send_emails_switch' );

add_action( 'add_option_' . SEND_EMAILS_OPTION, __NAMESPACE__ . '\bind_added_send_emails_switch', 10, 2 );
add_action( 'update_option_' . SEND_EMAILS_OPTION, __NAMESPACE__ . '\bind_updated_send_emails_switch', 10, 2 );
add_filter( 'pre_update_option_' . SEND_EMAILS_OPTION, __NAMESPACE__ . '\bind_resaved_send_emails_switch', 10, 2 );
add_action( 'delete_option_' . SEND_EMAILS_OPTION, __NAMESPACE__ . '\bind_send_emails_switch_off' );

// Discourage search engines from indexing the site and disallow the entire site in robots.txt.
add_filter( 'option_blog_public', '__return_zero' );
add_filter( 'robots_txt', __NAMESPACE__ . '\disallow_all_user_agents' );

/**
* Stops core's password change and email change notifications, unless emails are switched on.
*
* @param bool $send Whether core sends the notification.
*
* @return bool
*/
function follow_send_emails_switch( $send ) {
return are_emails_enabled() ? $send : false;
}

/**
* Binds the Send emails switch to this site's address when it is added.
*
* @param string $option The option's name.
* @param mixed $value Its value.
*
* @return void
*/
function bind_added_send_emails_switch( $option, $value ) {
bind_send_emails_switch( $value );
}

/**
* Binds the Send emails switch to this site's address when it changes to 'on', and unbinds it when it changes to anything else.
*
* @param mixed $old_value The previous value.
* @param mixed $value The new value.
*
* @return void
*/
function bind_updated_send_emails_switch( $old_value, $value ) {
bind_send_emails_switch( $value );
}

/**
* Binds the Send emails switch to this site's address when 'on' is saved over a stored 'on', which update_option() then skips, as on a copy where it was on for another address.
*
* @param mixed $value The new value.
* @param mixed $old_value The stored value.
*
* @return mixed
*/
function bind_resaved_send_emails_switch( $value, $old_value ) {
if ( 'on' === $value && $value === $old_value ) {
bind_send_emails_switch( $value );
}

return $value;
}

/**
* Forgets the address the Send emails switch was bound to when the switch is deleted.
*
* @return void
*/
function bind_send_emails_switch_off() {
bind_send_emails_switch( 'off' );
}

function disallow_all_user_agents( $output ) {
return "User-agent: *\nDisallow: /\n";
}
Loading
Loading