-
Notifications
You must be signed in to change notification settings - Fork 3
/
disable-theme-plugin-update-emails.php
212 lines (201 loc) · 7.14 KB
/
disable-theme-plugin-update-emails.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/*
* Plugin name: Disable Theme and Plugin Auto-Update Emails
* Plugin URI: https://wordpress.org/plugins/disable-theme-and-plugin-auto-update-emails/
* Description: Disables the default notification emails sent by a site after an automatic theme and/or plugin update. Simply activate the plugin to disable these email notifications (allows failure notices through unless setting is enabled to disable these as well).
* Author: KZeni
* Author URI: https://kzeni.com
* License: GPLv3
* Version: 2.0.5
* Requires at least: 5.5
* Tested up to: 6.4
*/
define(
"KZENI_DISABLE_THEME_PLUGIN_UPDATE_EMAILS_NAME",
plugin_basename(__FILE__)
);
// Per https://make.wordpress.org/core/2020/07/30/controlling-plugin-and-theme-auto-update-email-notifications-and-site-health-infos-in-wp-5-5/
// Also see https://gist.github.com/afragen/e2f40ed2e71e590a127e8adc1db05948 where it adds update failure detection
// Disable auto-update email notifications for plugins.
add_filter(
"auto_plugin_update_send_email",
"kzeni_plugin_theme_update_email_on_failure"
);
// Disable auto-update email notifications for themes.
add_filter(
"auto_theme_update_send_email",
"kzeni_plugin_theme_update_email_on_failure"
);
/**
* Sends plugin & theme update emails only when at least one plugin/theme update has failed.
*
* @param bool $enabled Whether the plugin update email is enabled.
* @param array $update_results An array of update results.
* @return bool
*/
function kzeni_plugin_theme_update_email_on_failure(
$enabled,
$update_results = []
) {
if (
get_option("kzeni_disable_theme_plugin_update_emails_disable_all") ===
true ||
get_option("kzeni_disable_theme_plugin_update_emails_disable_all") ===
"true"
) {
// If disable failures as well (disable all) is enabled (not the default so just seeing if it's set to true or not is enough to check)
return false;
} else {
foreach ($update_results as $update_result) {
if (true !== $update_result->result) {
return true;
}
}
return false;
}
}
// Add options to WordPress' General Settings page
add_action(
"admin_init",
"kzeni_disable_theme_plugin_update_emails_general_settings_section"
);
function kzeni_disable_theme_plugin_update_emails_general_settings_section()
{
add_settings_section(
"kzeni_disable_theme_plugin_update_emails_settings_section", // Section ID
__(
"Disable Theme and Plugin Auto-Update Emails",
"kzeni_disable_theme_plugin_update_emails"
), // Section Title
"kzeni_disable_theme_plugin_update_emails_options_callback", // Callback
"general" // What Page? This makes the section show up on the General Settings Page
);
add_settings_field(
// Option 1
"kzeni_disable_theme_plugin_update_emails_explanation", // Option ID
__(
"Disable successful update email notifications",
"kzeni_disable_theme_plugin_update_emails"
), // Label
"kzeni_disable_theme_plugin_update_emails_field_callback", // !important - This is where the args go!
"general", // Page it will be displayed (General Settings)
"kzeni_disable_theme_plugin_update_emails_settings_section", // Name of our section
[
// The $args
"kzeni_disable_theme_plugin_update_emails_explanation", // Should match Option ID
]
);
add_settings_field(
// Option 2
"kzeni_disable_theme_plugin_update_emails_disable_all", // Option ID
__(
"Disable failed update email notifications",
"kzeni_disable_theme_plugin_update_emails"
), // Label
"kzeni_disable_theme_plugin_update_emails_field_callback", // !important - This is where the args go!
"general", // Page it will be displayed (General Settings)
"kzeni_disable_theme_plugin_update_emails_settings_section", // Name of our section
[
// The $args
"kzeni_disable_theme_plugin_update_emails_disable_all", // Should match Option ID
]
);
register_setting(
"general",
"kzeni_disable_theme_plugin_update_emails_disable_all",
"_bool"
);
}
function kzeni_disable_theme_plugin_update_emails_options_callback()
{
// Section Callback
echo '<a id="disable-theme-plugin-update-emails"></a>';
}
function kzeni_disable_theme_plugin_update_emails_field_callback($args)
{
// Settings Field(s) Callback
if ($args[0] === "kzeni_disable_theme_plugin_update_emails_explanation") {
echo '<label><input type="checkbox" id="' .
$args[0] .
'" name="' .
$args[0] .
'" value="true" checked="checked" disabled="disabled" />';
echo __(
"Yes, disable <strong><em>successful update</em></strong> email notifications for themes & plugins.",
"kzeni_disable_theme_plugin_update_emails"
) . "</label>";
echo '<p class="description">' .
__(
'Successful theme & plugin updates won\'t send email notifications simply due to the <strong>Disable Theme and Plugin Auto-Update Emails</strong> plugin being active on this site. Deactivate this plugin if you want to have successful theme & plugin update emails resume sending.',
"kzeni_disable_theme_plugin_update_emails"
) .
"</p>";
}
if ($args[0] === "kzeni_disable_theme_plugin_update_emails_disable_all") {
$option = get_option($args[0]);
echo '<label><input type="checkbox" id="' .
$args[0] .
'" name="' .
$args[0] .
'" value="true" ';
echo checked($option, "true");
echo " /> ";
echo __(
"Yes, also disable <strong><em>failed update</em></strong> email notifications for themes & plugins.",
"kzeni_disable_theme_plugin_update_emails"
) . "</label>";
echo '<p class="description">' .
__(
'A theme and/or plugin may fail (or otherwise encounter an issue) when attempting an update, and it\'s typically more important to receieve email notifications for these compared to successful update email notifications. As such, the default is to allow these through while one can choose to disable these as well.',
"kzeni_disable_theme_plugin_update_emails"
) .
"</p>";
}
}
function kzeni_disable_theme_plugin_update_emails_plugin_extra_links(
$links,
$plugin_name
) {
if ($plugin_name != KZENI_DISABLE_THEME_PLUGIN_UPDATE_EMAILS_NAME) {
return $links;
}
$links[] =
'<a href="https://github.com/KZeni/Disable-WordPress-Theme-and-Plugin-Auto-Update-Emails" target="_blank">' .
__("GitHub", "kzeni_disable_theme_plugin_update_emails") .
"</a>";
$links[] =
'<a href="https://wordpress.org/support/plugin/disable-theme-and-plugin-auto-update-emails/reviews/" target="_blank">' .
__("Reviews", "kzeni_disable_theme_plugin_update_emails") .
"</a>";
$links[] =
'<a href="https://wordpress.org/support/plugin/disable-theme-and-plugin-auto-update-emails/" target="_blank">' .
__("Support", "kzeni_disable_theme_plugin_update_emails") .
"</a>";
return $links;
}
add_filter(
"plugin_row_meta",
"kzeni_disable_theme_plugin_update_emails_plugin_extra_links",
10,
2
);
function kzeni_disable_theme_plugin_update_emails_settings_plugin_links(
$actions
) {
$actions = array_merge(
[
"settings" =>
'<a href="' .
admin_url("options-general.php#disable-theme-plugin-update-emails") .
'">' .
__("Settings", "kzeni_disable_theme_plugin_update_emails") .
"</a>",
],
$actions
);
return $actions;
}
add_action(
"plugin_action_links_" . KZENI_DISABLE_THEME_PLUGIN_UPDATE_EMAILS_NAME,
"kzeni_disable_theme_plugin_update_emails_settings_plugin_links"
);