| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress Beta Tester |
| 4 |
* |
| 5 |
* @package WordPress_Beta_Tester |
| 6 |
* @author Andy Fragen, original author Peter Westwood. |
| 7 |
* @license GPLv2+ |
| 8 |
* @copyright 2009-2016 Peter Westwood (email : peter.westwood@ftwr.co.uk) |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* WPBT_Extras |
| 13 |
*/ |
| 14 |
class WPBT_Extras { |
| 15 |
/** |
| 16 |
* Placeholder for saved options. |
| 17 |
* |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
protected static $options; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the WP_Beta_Tester instance. |
| 24 |
* |
| 25 |
* @var WP_Beta_Tester |
| 26 |
*/ |
| 27 |
protected $wp_beta_tester; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param WP_Beta_Tester $wp_beta_tester Instance of class WP_Beta_Tester. |
| 33 |
* @param array $options Site options. |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function __construct( WP_Beta_Tester $wp_beta_tester, $options ) { |
| 37 |
$this->wp_beta_tester = $wp_beta_tester; |
| 38 |
self::$options = $options; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize Extras. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function init() { |
| 47 |
$this->load_hooks(); |
| 48 |
$this->skip_autoupdate_email(); |
| 49 |
$this->skip_bundled_files_on_upgrade(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Load hooks. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function load_hooks() { |
| 58 |
add_filter( 'wp_beta_tester_add_settings_tabs', array( $this, 'add_settings_tab' ) ); |
| 59 |
add_action( 'wp_beta_tester_add_settings', array( $this, 'add_settings' ) ); |
| 60 |
add_action( 'wp_beta_tester_add_admin_page', array( $this, 'add_admin_page' ), 10, 2 ); |
| 61 |
add_action( 'wp_beta_tester_update_settings', array( $this, 'save_settings' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add class settings tab. |
| 66 |
* |
| 67 |
* @param array $tabs Settings tabs. |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
public function add_settings_tab( $tabs ) { |
| 71 |
return array_merge( $tabs, array( 'wp_beta_tester_extras' => esc_html__( 'Extra Settings', 'wordpress-beta-tester' ) ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Setup Settings API. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function add_settings() { |
| 80 |
register_setting( |
| 81 |
'wp_beta_tester', |
| 82 |
'wp_beta_tester_extras', |
| 83 |
array( 'WPBT_Settings', 'sanitize' ) |
| 84 |
); |
| 85 |
|
| 86 |
add_settings_section( |
| 87 |
'wp_beta_tester_email', |
| 88 |
null, |
| 89 |
null, |
| 90 |
'wp_beta_tester_extras' |
| 91 |
); |
| 92 |
|
| 93 |
add_settings_field( |
| 94 |
'skip_autoupdate_email', |
| 95 |
null, |
| 96 |
array( 'WPBT_Settings', 'checkbox_setting' ), |
| 97 |
'wp_beta_tester_extras', |
| 98 |
'wp_beta_tester_email', |
| 99 |
array( |
| 100 |
'id' => 'skip_autoupdate_email', |
| 101 |
'title' => esc_html__( 'Skip successful autoupdate emails.', 'wordpress-beta-tester' ), |
| 102 |
'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' ), |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
add_settings_section( |
| 107 |
'wp_beta_tester_bundled', |
| 108 |
null, |
| 109 |
null, |
| 110 |
'wp_beta_tester_extras' |
| 111 |
); |
| 112 |
|
| 113 |
add_settings_field( |
| 114 |
'skip_bundled_files_on_upgrade', |
| 115 |
null, |
| 116 |
array( 'WPBT_Settings', 'checkbox_setting' ), |
| 117 |
'wp_beta_tester_extras', |
| 118 |
'wp_beta_tester_bundled', |
| 119 |
array( |
| 120 |
'id' => 'skip_bundled_files_on_upgrade', |
| 121 |
'title' => esc_html__( 'Skip bundled plugins and themes on upgrade.', 'wordpress-beta-tester' ), |
| 122 |
'description' => esc_html__( 'Bundled plugins and themes are skipped on Core Upgrade.', 'wordpress-beta-tester' ), |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Save settings. |
| 129 |
* |
| 130 |
* @param mixed $post_data $_POST data. |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public function save_settings( $post_data ) { |
| 134 |
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'wp_beta_tester_extras-options' ) |
| 135 |
) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if ( isset( $post_data['option_page'] ) |
| 140 |
&& 'wp_beta_tester_extras' === $post_data['option_page'] |
| 141 |
) { |
| 142 |
$options = isset( $post_data['wp-beta-tester'] ) |
| 143 |
? $post_data['wp-beta-tester'] |
| 144 |
: array(); |
| 145 |
$options = WPBT_Settings::sanitize( $options ); |
| 146 |
$filtered_options = array_filter( self::$options, array( $this, 'get_unchecked_options' ) ); |
| 147 |
$options = array_merge( $filtered_options, $options ); |
| 148 |
update_site_option( 'wp_beta_tester', (array) $options ); |
| 149 |
add_filter( 'wp_beta_tester_save_redirect', array( $this, 'save_redirect_page' ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Filter saved setting to remove unchecked checkboxes. |
| 155 |
* |
| 156 |
* @param array $checked Options. |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
private function get_unchecked_options( $checked ) { |
| 160 |
return '1' !== $checked; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Redirect page/tab after saving options. |
| 165 |
* |
| 166 |
* @param mixed $option_page Settings page. |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
public function save_redirect_page( $option_page ) { |
| 170 |
return array_merge( $option_page, array( 'wp_beta_tester_extras' ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Create core settings page. |
| 175 |
* |
| 176 |
* @param array $tab Settings tab. |
| 177 |
* @param string $action Form action. |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public function add_admin_page( $tab, $action ) { |
| 181 |
?> |
| 182 |
<div> |
| 183 |
<?php if ( 'wp_beta_tester_extras' === $tab ) : ?> |
| 184 |
<form method="post" action="<?php echo esc_attr( $action ); ?>"> |
| 185 |
<?php settings_fields( 'wp_beta_tester_extras' ); ?> |
| 186 |
<?php do_settings_sections( 'wp_beta_tester_extras' ); ?> |
| 187 |
<?php submit_button(); ?> |
| 188 |
</form> |
| 189 |
<?php endif; ?> |
| 190 |
</div> |
| 191 |
<?php |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Skip successful autoupdate emails. |
| 196 |
* |
| 197 |
* @since 2.1.0 |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function skip_autoupdate_email() { |
| 202 |
if ( ! isset( self::$options['skip_autoupdate_email'] ) ) { |
| 203 |
return; |
| 204 |
} |
| 205 |
// Disable update emails on success. |
| 206 |
add_filter( |
| 207 |
'auto_core_update_send_email', |
| 208 |
static function ( $send, $type ) { |
| 209 |
$send = 'success' === $type ? false : $send; |
| 210 |
|
| 211 |
return $send; |
| 212 |
}, |
| 213 |
10, |
| 214 |
2 |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Skip bundled plugins and themes on Core Upgrade. |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public function skip_bundled_files_on_upgrade() { |
| 224 |
if ( ! isset( self::$options['skip_bundled_files_on_upgrade'] ) ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
if ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) ) { |
| 229 |
define( 'CORE_UPGRADE_SKIP_NEW_BUNDLED', true ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|