forked from afragen/wordpress-beta-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWPBT_Extras.php
202 lines (185 loc) · 5.09 KB
/
WPBT_Extras.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
<?php
/**
* WordPress Beta Tester
*
* @package WordPress_Beta_Tester
* @author Andy Fragen, original author Peter Westwood.
* @license GPLv2+
* @copyright 2009-2016 Peter Westwood (email : peter.westwood@ftwr.co.uk)
*/
/**
* WPBT_Extras
*/
class WPBT_Extras {
/**
* Placeholder for saved options.
*
* @var array
*/
protected static $options;
/**
* Holds the WP_Beta_Tester instance.
*
* @var WP_Beta_Tester
*/
protected $wp_beta_tester;
/**
* Constructor.
*
* @param WP_Beta_Tester $wp_beta_tester Instance of class WP_Beta_Tester.
* @param array $options Site options.
* @return void
*/
public function __construct( WP_Beta_Tester $wp_beta_tester, $options ) {
$this->wp_beta_tester = $wp_beta_tester;
self::$options = $options;
}
/**
* Load hooks.
*
* @return void
*/
public function load_hooks() {
add_filter( 'wp_beta_tester_add_settings_tabs', array( $this, 'add_settings_tab' ) );
add_action( 'wp_beta_tester_add_settings', array( $this, 'add_settings' ) );
add_action( 'wp_beta_tester_add_admin_page', array( $this, 'add_admin_page' ), 10, 2 );
add_action( 'wp_beta_tester_update_settings', array( $this, 'save_settings' ) );
}
/**
* Add class settings tab.
*
* @param array $tabs Settings tabs.
* @return array
*/
public function add_settings_tab( $tabs ) {
return array_merge( $tabs, array( 'wp_beta_tester_extras' => esc_html__( 'Extra Settings', 'wordpress-beta-tester' ) ) );
}
/**
* Setup Settings API.
*
* @return void
*/
public function add_settings() {
register_setting(
'wp_beta_tester',
'wp_beta_tester_extras',
array( 'WPBT_Settings', 'sanitize' )
);
add_settings_section(
'wp_beta_tester_email',
null,
null,
'wp_beta_tester_extras'
);
add_settings_field(
'skip_autoupdate_email',
null,
array( 'WPBT_Settings', 'checkbox_setting' ),
'wp_beta_tester_extras',
'wp_beta_tester_email',
array(
'id' => 'skip_autoupdate_email',
'title' => esc_html__( 'Skip successful autoupdate emails.', 'wordpress-beta-tester' ),
'description' => esc_html__( 'Disable sending emails to the admin user for successful autoupdates. Only emails indicating failures of the autoupdate process are sent.', 'wordpress-beta-tester' ),
)
);
add_settings_field(
'hide_report_a_bug',
null,
array( 'WPBT_Settings', 'checkbox_setting' ),
'wp_beta_tester_extras',
'wp_beta_tester_email',
array(
'id' => 'hide_report_a_bug',
'title' => esc_html__( 'Hide Report a Bug feature.', 'wordpress-beta-tester' ),
'class' => ! apply_filters( 'wpbt_hide_report_a_bug', false ) || isset( self::$options['hide_report_a_bug'] ) ? '' : 'hidden',
)
);
}
/**
* Save settings.
*
* @param mixed $post_data $_POST data.
* @return void
*/
public function save_settings( $post_data ) {
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wp_beta_tester_extras-options' )
) {
return;
}
if ( isset( $post_data['option_page'] )
&& 'wp_beta_tester_extras' === $post_data['option_page']
) {
$options = isset( $post_data['wp-beta-tester'] )
? $post_data['wp-beta-tester']
: array();
$options = WPBT_Settings::sanitize( $options );
$filtered_options = array_filter( self::$options, array( $this, 'get_unchecked_options' ) );
$options = array_merge( $filtered_options, $options );
update_site_option( 'wp_beta_tester', (array) $options );
add_filter( 'wp_beta_tester_save_redirect', array( $this, 'save_redirect_page' ) );
}
}
/**
* Filter saved setting to remove unchecked checkboxes.
*
* @param array $checked Options.
* @return bool
*/
private function get_unchecked_options( $checked ) {
return '1' !== $checked;
}
/**
* Redirect page/tab after saving options.
*
* @param mixed $option_page Settings page.
* @return array
*/
public function save_redirect_page( $option_page ) {
return array_merge( $option_page, array( 'wp_beta_tester_extras' ) );
}
/**
* Create core settings page.
*
* @param array $tab Settings tab.
* @param string $action Form action.
* @return void
*/
public function add_admin_page( $tab, $action ) {
?>
<div>
<?php if ( 'wp_beta_tester_extras' === $tab ) : ?>
<form method="post" action="<?php echo esc_attr( $action ); ?>">
<?php settings_fields( 'wp_beta_tester_extras' ); ?>
<?php do_settings_sections( 'wp_beta_tester_extras' ); ?>
<?php submit_button(); ?>
</form>
<?php endif; ?>
</div>
<?php
}
/**
* Skip successful autoupdate emails.
*
* @since 2.1.0
*
* @return void
*/
public function skip_autoupdate_email() {
if ( ! isset( self::$options['skip_autoupdate_email'] ) ) {
return;
}
// Disable update emails on success.
add_filter(
'auto_core_update_send_email',
static function ( $send, $type ) {
$send = 'success' === $type ? false : $send;
return $send;
},
10,
2
);
// Disable sending debug email.
add_filter( 'automatic_updates_send_debug_email', '__return_false', 10, 2 );
}
}