settings = Plugin::get_settings( 'updates-and-notifications' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
if( isset( $this->settings['disable_auto_update_core'] ) && true === (bool) $this->settings['disable_auto_update_core'] ) {
add_filter( 'allow_major_auto_core_updates', '__return_false', 9999 );
if( 'minor' !== ( $this->settings['core_auto_update_mode'] ?? 'off' ) ) {
add_filter( 'auto_update_core', '__return_false', 9999 );
add_filter( 'allow_minor_auto_core_updates', '__return_false', 9999 );
add_filter( 'allow_dev_auto_core_updates', '__return_false', 9999 );
}
}
if( isset( $this->settings['disable_auto_update_plugin'] ) && true === (bool) $this->settings['disable_auto_update_plugin'] ) {
add_filter( 'auto_update_plugin', '__return_false', 9999 );
add_filter( 'plugins_auto_update_enabled', '__return_false', 9999 );
}
if( isset( $this->settings['disable_auto_update_theme'] ) && true === (bool) $this->settings['disable_auto_update_theme'] ) {
add_filter( 'auto_update_theme', '__return_false', 9999 );
add_filter( 'themes_auto_update_enabled', '__return_false', 9999 );
}
if( isset( $this->settings['disable_auto_update_translation'] ) && true === (bool) $this->settings['disable_auto_update_translation'] ) {
add_filter( 'auto_update_translation', '__return_false', 9999 );
}
if( isset( $this->settings['disable_auto_core_update_send_email'] ) && true === (bool) $this->settings['disable_auto_core_update_send_email'] ) {
add_filter( 'auto_core_update_send_email', [ $this, 'filter_core_update_email' ], 10, 2 );
}
if( isset( $this->settings['disable_auto_theme_update_send_email'] ) && true === (bool) $this->settings['disable_auto_theme_update_send_email'] ) {
add_filter( 'auto_theme_update_send_email', [ $this, 'filter_update_email' ], 10, 2 );
}
if( isset( $this->settings['disable_auto_plugin_update_send_email'] ) && true === (bool) $this->settings['disable_auto_plugin_update_send_email'] ) {
add_filter( 'auto_plugin_update_send_email', [ $this, 'filter_update_email' ], 10, 2 );
}
if( isset( $this->settings['disable_new_user_admin_notification_email'] ) && true === (bool) $this->settings['disable_new_user_admin_notification_email'] ) {
$this->disable_new_user_admin_notification_email();
}
if( isset( $this->settings['disable_user_password_change_admin_notification_email'] ) && true === (bool) $this->settings['disable_user_password_change_admin_notification_email'] ) {
remove_action( 'after_password_reset', 'wp_password_change_notification' );
add_filter( 'wp_password_change_notification_email', [ $this, 'wp_password_change_notification_email' ], 10, 3 );
add_filter( 'woocommerce_disable_password_change_notification', '__return_true' );
add_filter( 'send_password_change_email', '__return_false' );
add_filter( 'send_email_change_email', '__return_false' );
}
if( isset( $this->settings['disable_comment_post_author_notification_email'] ) && true === (bool) $this->settings['disable_comment_post_author_notification_email'] ) {
add_filter( 'notify_post_author', '__return_false' );
}
if( isset( $this->settings['disable_comment_admin_notification_email'] ) && true === (bool) $this->settings['disable_comment_admin_notification_email'] ) {
add_filter( 'notify_moderator', '__return_false' );
}
if( isset( $this->settings['disable_recovery_mode_email_notification'] ) && true === (bool) $this->settings['disable_recovery_mode_email_notification'] ) {
add_filter( 'pre_option_recovery_mode_email_last_sent', [ $this, 'pre_option_recovery_mode_email_last_sent' ] );
}
if( empty( $this->settings['disable_recovery_mode_email_notification'] ) && isset( $this->settings['customize_recovery_mode_recipient_email'] ) && true === (bool) $this->settings['customize_recovery_mode_recipient_email'] ) {
add_filter( 'recovery_mode_email', [ $this, 'recovery_mode_email' ] );
}
}
/**
* Extends and modifies the array of settings fields for the admin interface.
*
* @param array $fields An array of existing settings fields to be extended. This array is structured hierarchically, containing categories and their respective fields.
*
* @return array The modified array of settings fields, including added fields for disabling automatic updates and related notifications for core, plugins, themes, and translations.
*/
public function adminease_settings_fields( array $fields ): array {
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-update-core',
'name' => 'adminease[updates-and-notifications][disable_auto_update_core]',
'value' => $this->settings['disable_auto_update_core'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input toggle-field',
'label' => __( 'Disable auto update core', 'adminease' ),
'description' => __( 'WordPress has a built-in feature that can automatically update itself to the latest version. This helps keep your website secure and up to date without needing to do anything manually. However, some people prefer to disable automatic updates so they can test new versions first or avoid unexpected changes. You can easily turn this option on or off depending on how much control you want over updates.', 'adminease' ),
'child_fields' => [
[
'type' => 'select',
'id' => 'core-auto-update-mode',
'name' => 'adminease[updates-and-notifications][core_auto_update_mode]',
'value' => $this->settings['core_auto_update_mode'] ?? 'off',
'options' => [
'off' => __( 'All automatic core updates', 'adminease' ),
'minor' => __( 'Major versions only (security and maintenance updates still install)', 'adminease' ),
],
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Automatic core updates to disable', 'adminease' ),
'attributes' => [
'data-parent' => 'disable-auto-update-core',
],
],
],
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-update-plugin',
'name' => 'adminease[updates-and-notifications][disable_auto_update_plugin]',
'value' => $this->settings['disable_auto_update_plugin'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto update plugins', 'adminease' ),
'description' => __( 'WordPress also lets you automatically update your plugins to keep your site secure and running smoothly. This means you don’t have to manually check for updates-WordPress will do it for you. It’s a great way to save time and protect your site from security risks. However, some users prefer to disable auto-updates for plugins so they can test each update first, especially on websites with custom setups or many plugins. You can turn plugin auto-updates on or off individually from your WordPress dashboard.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-update-theme',
'name' => 'adminease[updates-and-notifications][disable_auto_update_theme]',
'value' => $this->settings['disable_auto_update_theme'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto update themes', 'adminease' ),
'description' => __( "WordPress gives you the option to automatically update your themes, just like plugins. This helps ensure your site stays secure and compatible with the latest features. When auto-updates are enabled for a theme, WordPress will install new versions as they’re released, without requiring manual action. However, if you've made custom changes to your theme files, automatic updates could overwrite them. That’s why some users prefer to turn off auto-updates for themes and update them manually after testing. You can easily enable or disable theme auto-updates from the Appearance > Themes section in your dashboard.", 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-update-translation',
'name' => 'adminease[updates-and-notifications][disable_auto_update_translation]',
'value' => $this->settings['disable_auto_update_translation'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto update translations', 'adminease' ),
'description' => __( 'WordPress also supports automatic updates for translations, which include language files for the WordPress core, themes, and plugins. These updates help keep your site’s content accurate and fully translated in your chosen language. Translation files are usually small and safe to update, so WordPress enables these auto-updates by default. Most users leave this feature on, as it improves the user experience without affecting the site’s design or functionality. If needed, advanced users can disable translation auto-updates through code or a plugin, but in most cases, it’s best to keep them enabled.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-core-update-send-email',
'name' => 'adminease[updates-and-notifications][disable_auto_core_update_send_email]',
'value' => $this->settings['disable_auto_core_update_send_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto core update send email', 'adminease' ),
'description' => __( "When WordPress automatically updates its core software, it sends an email notification to the site administrator. This email lets you know that the update was successful and includes details about what was updated. It’s a helpful way to stay informed about changes to your site, especially if you're not logging in regularly. While you can disable these emails with custom code, they’re useful for keeping track of important updates and ensuring everything is working as expected.", 'adminease' ) . ' ' . __( 'Failure emails are still sent.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-theme-update-send-email',
'name' => 'adminease[updates-and-notifications][disable_auto_theme_update_send_email]',
'value' => $this->settings['disable_auto_theme_update_send_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto theme update send email', 'adminease' ),
'description' => __( "When a theme is automatically updated in WordPress, the site administrator receives an email notification with details about the update. This helps you stay informed about changes to your theme, especially if you’re using auto-updates. The email includes the theme name and version, so you know exactly what was updated. It’s a useful feature for tracking updates and making sure your site looks and functions as expected after the change.", 'adminease' ) . ' ' . __( 'Failure emails are still sent.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-auto-plugin-update-send-email',
'name' => 'adminease[updates-and-notifications][disable_auto_plugin_update_send_email]',
'value' => $this->settings['disable_auto_plugin_update_send_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable auto plugin update send email', 'adminease' ),
'description' => __( "When a plugin is automatically updated in WordPress, the site administrator gets an email notification with information about the update. This email lists which plugin was updated and its new version, helping you keep track of changes to your site. These notifications are especially useful for monitoring plugin activity, ensuring everything still works correctly, and spotting potential issues early.", 'adminease' ) . ' ' . __( 'Failure emails are still sent.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-new-user-admin-notification-email',
'name' => 'adminease[updates-and-notifications][disable_new_user_admin_notification_email]',
'value' => $this->settings['disable_new_user_admin_notification_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable new user admin notification email', 'adminease' ),
'description' => __( 'The New User Admin Notification Email is a message sent to the site administrator whenever a new user registers on the website. It includes the new user’s username and email address, helping you keep track of user activity and detect unwanted registrations. While useful, some site owners choose to disable this notification-especially on busy sites or when using custom user management tools-to reduce inbox clutter.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-user-password-change-admin-notification-email',
'name' => 'adminease[updates-and-notifications][disable_user_password_change_admin_notification_email]',
'value' => $this->settings['disable_user_password_change_admin_notification_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable user password change admin notification email', 'adminease' ),
'description' => __( 'The User Password Change Admin Notification Email is sent to the site administrator when any user changes their password. This alert helps you monitor account activity and quickly spot suspicious behavior-like unexpected password changes. While it adds a layer of security awareness, some admins may choose to disable it to avoid unnecessary notifications on high-traffic sites.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-comment-admin-notification-email',
'name' => 'adminease[updates-and-notifications][disable_comment_admin_notification_email]',
'value' => $this->settings['disable_comment_admin_notification_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable comment admin notification email', 'adminease' ),
'description' => __( 'The Comment Moderator Notification Email is sent to the site moderator whenever a new comment is posted on the site. It includes the comment content, author details, and a direct link to moderate or reply. This feature helps you stay on top of user engagement and quickly catch spam or inappropriate comments.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-comment-post-author-notification-email',
'name' => 'adminease[updates-and-notifications][disable_comment_post_author_notification_email]',
'value' => $this->settings['disable_comment_post_author_notification_email'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable comment post author notification email', 'adminease' ),
'description' => __( 'The Comment Post Author Notification Email is sent to the site administrator whenever a new comment is posted on the site. It includes the comment content, author details, and a direct link to moderate or reply. This feature helps you stay on top of user engagement and quickly catch spam or inappropriate comments.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'disable-recovery-mode-email-notification',
'name' => 'adminease[updates-and-notifications][disable_recovery_mode_email_notification]',
'value' => $this->settings['disable_recovery_mode_email_notification'] ?? false,
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable recovery mode email notification', 'adminease' ),
'description' => __( 'The Recovery Mode Email Notification is sent to the site administrator when recovery mode is activated. This email provides details about the activation and includes a link to deactivate recovery mode. This feature helps you monitor site security and take immediate action if necessary.', 'adminease' ),
];
$fields['updates-and-notifications']['fields'][] = [
'type' => 'switch',
'id' => 'customize-recovery-mode-recipient-email',
'name' => 'adminease[updates-and-notifications][customize_recovery_mode_recipient_email]',
'value' => $this->settings['customize_recovery_mode_recipient_email'] ?? '',
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input toggle-field',
'label' => __( 'Customize recovery mode recipient email', 'adminease' ),
'description' => __( 'By default, recovery mode notification emails are sent to the site administrator email address. You can specify a different email address here to receive these notifications instead.', 'adminease' ),
'child_fields' => [
[
'type' => 'text',
'id' => 'recovery-mode-recipient-email',
'name' => 'adminease[updates-and-notifications][recovery_mode_recipient_email]',
'value' => $this->settings['recovery_mode_recipient_email'] ?? '',
'label_class' => 'pwp-field-label',
'input_class' => 'pwp-field-input',
'wrapper_class' => 'form-group-child',
'label' => __( 'Recovery mode recipient email', 'adminease' ),
'description' => __( 'Enter the email address where recovery mode notifications should be sent.', 'adminease' ),
'field_description' => __( 'You can enter multiple email addresses separated by commas.', 'adminease' ),
'attributes' => [
'data-parent' => 'customize-recovery-mode-recipient-email',
],
],
],
];
return $fields;
}
/**
* Keeps the core update emails off, except the ones that say something went wrong.
*
* @param bool $send Whether the email is sent.
* @param string $type The email type: success, fail, manual or critical.
*
* @return bool Whether the email is sent.
*/
public function filter_core_update_email( $send, $type = '' ) {
if( in_array( $type, [ 'fail', 'critical' ], true ) ) {
return $send;
}
return false;
}
/**
* Keeps the plugin and theme update emails off, unless at least one update did not succeed.
* The second argument only exists from WordPress 5.5.1, so it has a default.
*
* @param bool $send Whether the email is sent.
* @param array $results The update results.
*
* @return bool Whether the email is sent.
*/
public function filter_update_email( $send, $results = [] ) {
foreach( (array) $results as $update ) {
$result = is_object( $update ) ? ( $update->result ?? null ) : ( is_array( $update ) ? ( $update['result'] ?? null ) : null );
if( true !== $result ) {
return $send;
}
}
return false;
}
/**
* Stops the email that tells the site administrator about a new user.
* WordPress 6.1 and newer have a filter for it. Older versions have none, so core's sender is
* swapped for one that only mails the new user. Must run before the user is created, so it is
* called from the constructor and not from plugins_loaded, which is already running by then.
*
* @return void
*/
public function disable_new_user_admin_notification_email(): void {
add_filter( 'wp_send_new_user_notification_to_admin', '__return_false', 10, 2 );
if( version_compare( get_bloginfo( 'version' ), '6.1', '<' ) ) {
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10 );
add_action( 'register_new_user', [ $this, 'send_new_user_notification_to_user_only' ] );
add_action( 'edit_user_created_user', [ $this, 'send_new_user_notification_to_user_only' ], 10, 2 );
}
}
/**
* Sends the new user their own email and skips the one to the administrator. Used only on
* WordPress before 6.1, in place of core's wp_send_new_user_notifications().
*
* @param int $user_id The new user's ID.
* @param string $notify Who to notify: 'admin', 'user' or 'both'.
*
* @return void
*/
public function send_new_user_notification_to_user_only( $user_id, $notify = 'both' ): void {
if( 'admin' === $notify ) {
return;
}
wp_new_user_notification( $user_id, null, 'user' );
}
/**
* Modifies the password change notification email for a user.
* This method updates the recipient of the password change notification email,
* effectively disabling the default behavior by setting the recipient to an empty value.
*
* @param array $wp_password_change_notification_email The email data for the password change notification.
* @param WP_User $user The user object for whom the password change notification is being processed.
* @param string $blogname The name of the blog where the password change occurred.
*
* @return array The filtered email data with updated recipient information.
*/
public function wp_password_change_notification_email( array $wp_password_change_notification_email, WP_User $user, string $blogname ): array {
$wp_password_change_notification_email['to'] = '';
return $wp_password_change_notification_email;
}
/**
* Tells core a recovery mode email was just sent, so it skips the next one quietly.
*
* @return int The current time.
*/
public function pre_option_recovery_mode_email_last_sent(): int {
return time();
}
/**
* Modifies the email recipient(s) for recovery mode notifications.
* This method updates the 'to' field in the email data to use the configured
* recovery mode recipient email address from the settings. If multiple email
* addresses are specified (comma-separated), they are sanitized and added as an array.
* If no recovery mode recipient email is configured, the original email data is returned.
*
* @param mixed $email_data The original email data array containing email details like 'to', 'subject', etc.
*
* @return mixed The modified email data array with an updated 'to' field for recovery mode notifications.
*/
public function recovery_mode_email( $email_data ) {
$recovery_mode_recipient_email = $this->settings['recovery_mode_recipient_email'] ?? '';
if( !is_array( $email_data ) || empty( $recovery_mode_recipient_email ) ) {
return $email_data;
}
if( false !== strpos( $recovery_mode_recipient_email, ',' ) ) {
$recovery_mode_recipient_email = array_map( 'trim', explode( ',', $recovery_mode_recipient_email ) );
}
if( is_array( $recovery_mode_recipient_email ) ) {
$recovery_mode_recipient_email = array_map( 'sanitize_email', $recovery_mode_recipient_email );
} else {
$recovery_mode_recipient_email = sanitize_email( $recovery_mode_recipient_email );
}
$email_data['to'] = $recovery_mode_recipient_email;
return $email_data;
}
}