-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add multisite support #102
Changes from 15 commits
c6fe174
13f43b6
a584690
13e6731
904596a
f214c40
e55f7b6
47a8b21
2342f72
2f964da
ae88d99
6248b74
f6a7019
8841bf9
2851b3e
d154153
429ede3
69786dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,14 @@ class Admin_Settings { | |
* The Constructor. | ||
*/ | ||
public function __construct() { | ||
add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); | ||
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', array( $this, 'register_admin_menu' ) ); | ||
add_action( 'admin_init', array( $this, 'reset_settings' ) ); | ||
add_action( 'admin_init', array( $this, 'register_settings' ) ); | ||
add_action( 'admin_notices', array( $this, 'reset_admin_notice' ) ); | ||
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | ||
|
||
add_action( 'admin_init', array( $this, 'update_settings' ) ); | ||
add_action( 'network_admin_edit_aspireupdate-settings', array( $this, 'update_settings' ) ); | ||
} | ||
|
||
/** | ||
|
@@ -87,16 +90,16 @@ public function reset_settings() { | |
wp_verify_nonce( sanitize_key( $_GET['reset-nonce'] ), 'aspireupdate-reset-nonce' ) | ||
) { | ||
$options = $this->get_default_settings(); | ||
update_option( $this->option_name, $options ); | ||
update_option( 'aspireupdate-reset', 'true' ); | ||
update_site_option( $this->option_name, $options ); | ||
update_site_option( 'aspireupdate-reset', 'true' ); | ||
|
||
wp_safe_redirect( | ||
add_query_arg( | ||
array( | ||
'reset-success' => 'success', | ||
'reset-success-nonce' => wp_create_nonce( 'aspireupdate-reset-success-nonce' ), | ||
), | ||
admin_url( 'index.php?page=aspireupdate-settings' ) | ||
network_admin_url( 'index.php?page=aspireupdate-settings' ) | ||
) | ||
); | ||
exit; | ||
|
@@ -110,14 +113,14 @@ public function reset_settings() { | |
*/ | ||
public function reset_admin_notice() { | ||
if ( | ||
( 'true' === get_option( 'aspireupdate-reset' ) ) && | ||
( 'true' === get_site_option( 'aspireupdate-reset' ) ) && | ||
isset( $_GET['reset-success'] ) && | ||
( 'success' === $_GET['reset-success'] ) && | ||
isset( $_GET['reset-success-nonce'] ) && | ||
wp_verify_nonce( sanitize_key( $_GET['reset-success-nonce'] ), 'aspireupdate-reset-success-nonce' ) | ||
) { | ||
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings have been reset to default.', 'AspireUpdate' ) . '</p></div>'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how the i18n and escaping got removed. Sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this made it into the PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It didn't but I made another PR for it. |
||
delete_option( 'aspireupdate-reset' ); | ||
echo '<div class="notice notice-success is-dismissible"><p>Settings have been reset to default.</p></div>'; | ||
delete_site_option( 'aspireupdate-reset' ); | ||
} | ||
} | ||
|
||
|
@@ -131,13 +134,13 @@ public function reset_admin_notice() { | |
*/ | ||
public function get_setting( $setting_name, $default_value = false ) { | ||
if ( null === $this->options ) { | ||
$options = get_option( $this->option_name, false ); | ||
$options = get_site_option( $this->option_name, false ); | ||
/** | ||
* If the options are not set load defaults. | ||
*/ | ||
if ( false === $options ) { | ||
$options = $this->get_default_settings(); | ||
update_option( $this->option_name, $options ); | ||
update_site_option( $this->option_name, $options ); | ||
} | ||
$config_file_options = $this->get_settings_from_config_file(); | ||
if ( is_array( $options ) ) { | ||
|
@@ -210,6 +213,31 @@ private function get_settings_from_config_file() { | |
return $options; | ||
} | ||
|
||
/** | ||
* Update settings for single site or network activated. | ||
* | ||
* @link http://wordpress.stackexchange.com/questions/64968/settings-api-in-multisite-missing-update-message | ||
* @link http://benohead.com/wordpress-network-wide-plugin-settings/ | ||
afragen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @return void | ||
*/ | ||
public function update_settings() { | ||
// Exit if improper privileges. | ||
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'aspireupdate-settings' ) ) { | ||
return; | ||
} | ||
|
||
// Save settings and redirect. | ||
if ( ( isset( $_POST['option_page'] ) && 'aspireupdate_settings' === $_POST['option_page'] ) ) { | ||
update_site_option( $this->option_name, $this->sanitize_settings( wp_unslash( $_POST['aspireupdate_settings'] ) ) ); | ||
|
||
wp_safe_redirect( | ||
add_query_arg( array( network_admin_url( 'index.php?page=aspireupdate-settings' ) ) ) | ||
); | ||
exit; | ||
} | ||
} | ||
|
||
/** | ||
* Register the Admin Menu. | ||
* | ||
|
@@ -224,7 +252,7 @@ public function register_admin_menu() { | |
'index.php', | ||
'AspireUpdate', | ||
'AspireUpdate', | ||
'manage_options', | ||
is_multisite() ? 'manage_network_options' : 'manage_options', | ||
'aspireupdate-settings', | ||
array( $this, 'the_settings_page' ) | ||
); | ||
|
@@ -238,7 +266,7 @@ public function register_admin_menu() { | |
* @return void | ||
*/ | ||
public function admin_enqueue_scripts( $hook ) { | ||
if ( 'dashboard_page_aspireupdate-settings' !== $hook ) { | ||
if ( ! in_array( $hook, array( 'dashboard_page_aspireupdate-settings','index_page_aspireupdate-settings' ) ) ) { | ||
return; | ||
} | ||
wp_enqueue_style( 'aspire_update_settings_css', plugin_dir_url( __DIR__ ) . 'assets/css/aspire-update.css', array(), AP_VERSION ); | ||
|
@@ -247,7 +275,7 @@ public function admin_enqueue_scripts( $hook ) { | |
'aspire_update_settings_js', | ||
'aspireupdate', | ||
array( | ||
'ajax_url' => admin_url( 'admin-ajax.php' ), | ||
'ajax_url' => network_admin_url( 'admin-ajax.php' ), | ||
'nonce' => wp_create_nonce( 'aspireupdate-ajax' ), | ||
'domain' => Utilities::get_top_level_domain(), | ||
) | ||
|
@@ -265,19 +293,21 @@ public function the_settings_page() { | |
'reset' => 'reset', | ||
'reset-nonce' => wp_create_nonce( 'aspireupdate-reset-nonce' ), | ||
), | ||
admin_url( 'index.php?page=aspireupdate-settings' ) | ||
network_admin_url( 'index.php?page=aspireupdate-settings' ) | ||
); | ||
?> | ||
<div class="wrap"> | ||
<h1><?php esc_html_e( 'AspireUpdate Settings', 'AspireUpdate' ); ?></h1> | ||
<form id="aspireupdate-settings-form" method="post" action="options.php"> | ||
<form id="aspireupdate-settings-form" method="post" action="index.php?page=aspireupdate-settings"> | ||
<?php | ||
settings_fields( $this->option_group ); | ||
do_settings_sections( 'aspireupdate-settings' ); | ||
?> | ||
<p class="submit"> | ||
<input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Save Changes', 'AspireUpdate' ); ?>"> | ||
<a href="<?php echo esc_url( $reset_url ); ?>" class="button button-secondary" ><?php esc_html_e( 'Reset', 'AspireUpdate' ); ?></a> | ||
<?php wp_nonce_field( 'aspireupdate-settings' ); ?> | ||
<?php submit_button( esc_html__( 'Save Changes', 'aspireupdate-settings' ), 'primary', 'submit', false ); ?> | ||
|
||
<a href="<?php echo esc_url( $reset_url ); ?>" class="button button-secondary" >Reset</a> | ||
</p> | ||
</form> | ||
</div> | ||
|
@@ -291,13 +321,13 @@ public function the_settings_page() { | |
*/ | ||
public function register_settings() { | ||
$nonce = wp_create_nonce( 'aspireupdate-settings' ); | ||
$options = get_option( $this->option_name, false ); | ||
$options = get_site_option( $this->option_name, false ); | ||
/** | ||
* If the options are not set load defaults. | ||
*/ | ||
if ( false === $options ) { | ||
$options = $this->get_default_settings(); | ||
update_option( $this->option_name, $options ); | ||
update_site_option( $this->option_name, $options ); | ||
} | ||
|
||
register_setting( | ||
|
@@ -502,10 +532,10 @@ public function add_settings_field_callback( $args = array() ) { | |
<?php | ||
foreach ( $group_options as $group_option ) { | ||
?> | ||
<option | ||
data-api-key-url="<?php echo esc_html( $group_option['api-key-url'] ?? '' ); ?>" | ||
data-require-api-key="<?php echo esc_html( $group_option['require-api-key'] ?? 'false' ); ?>" | ||
value="<?php echo esc_attr( $group_option['value'] ?? '' ); ?>" | ||
<option | ||
data-api-key-url="<?php echo esc_html( $group_option['api-key-url'] ?? '' ); ?>" | ||
data-require-api-key="<?php echo esc_html( $group_option['require-api-key'] ?? 'false' ); ?>" | ||
value="<?php echo esc_attr( $group_option['value'] ?? '' ); ?>" | ||
Comment on lines
+535
to
+538
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 What change am I not seeing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. VSCode removed the extra space at the end of some of the lines automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly 😂 |
||
<?php selected( esc_attr( $group_option['value'] ?? '' ), esc_attr( $options[ $id ] ?? '' ) ); ?> | ||
> | ||
<?php echo esc_html( $group_option['label'] ?? '' ); ?> | ||
|
@@ -516,10 +546,10 @@ public function add_settings_field_callback( $args = array() ) { | |
</select> | ||
<p> | ||
<input | ||
type="text" | ||
id="aspireupdate-settings-field-<?php echo esc_attr( $id ); ?>_other" | ||
name="<?php echo esc_attr( $this->option_name ); ?>[<?php echo esc_attr( $id ); ?>_other]" | ||
value="<?php echo esc_attr( $options[ $id . '_other' ] ?? '' ); ?>" | ||
type="text" | ||
id="aspireupdate-settings-field-<?php echo esc_attr( $id ); ?>_other" | ||
name="<?php echo esc_attr( $this->option_name ); ?>[<?php echo esc_attr( $id ); ?>_other]" | ||
value="<?php echo esc_attr( $options[ $id . '_other' ] ?? '' ); ?>" | ||
Comment on lines
+549
to
+552
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 What change am I not seeing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, my VSCode settings removed the extra space at the end of the line automatically. |
||
class="regular-text" | ||
/> | ||
</p> | ||
|
@@ -539,18 +569,19 @@ class="regular-text" | |
public function sanitize_settings( $input ) { | ||
$sanitized_input = array(); | ||
|
||
$sanitized_input['enable'] = ( isset( $input['enable'] ) && $input['enable'] ) ? 1 : 0; | ||
$sanitized_input['enable'] = (int) ! empty( $input['enable'] ); | ||
$sanitized_input['api_key'] = sanitize_text_field( $input['api_key'] ?? '' ); | ||
$sanitized_input['api_host'] = sanitize_text_field( $input['api_host'] ?? '' ); | ||
$sanitized_input['api_host_other'] = sanitize_text_field( $input['api_host_other'] ?? '' ); | ||
|
||
$sanitized_input['enable_debug'] = isset( $input['enable_debug'] ) ? 1 : 0; | ||
$sanitized_input['enable_debug'] = (int) ! empty( $input['enable_debug'] ); | ||
if ( isset( $input['enable_debug_type'] ) && is_array( $input['enable_debug_type'] ) ) { | ||
$sanitized_input['enable_debug_type'] = array_map( 'sanitize_text_field', $input['enable_debug_type'] ); | ||
} else { | ||
$sanitized_input['enable_debug_type'] = array(); | ||
} | ||
$sanitized_input['disable_ssl_verification'] = ( isset( $input['disable_ssl_verification'] ) && $input['disable_ssl_verification'] ) ? 1 : 0; | ||
$sanitized_input['disable_ssl_verification'] = (int) ! empty( $input['disable_ssl_verification'] ); | ||
|
||
return $sanitized_input; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double-checking if this is necessary for this issue or if this fixes a different issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary for saving from multisite and provides a consistent manner of saving for both single and multisite.