-
Notifications
You must be signed in to change notification settings - Fork 21
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 7 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>Settings have been reset to default.</p></div>'; | ||
delete_option( 'aspireupdate-reset' ); | ||
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,12 +213,37 @@ 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
|
||
*/ | ||
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. | ||
* | ||
* @return void | ||
*/ | ||
public function register_admin_menu() { | ||
$capability = is_multisite() ? 'manage_network_options' : 'manage_options'; | ||
|
||
if ( ! defined( 'AP_REMOVE_UI' ) ) { | ||
define( 'AP_REMOVE_UI', false ); | ||
} | ||
|
@@ -224,7 +252,7 @@ public function register_admin_menu() { | |
'index.php', | ||
'AspireUpdate', | ||
'AspireUpdate', | ||
'manage_options', | ||
$capability, | ||
'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(), | ||
) | ||
|
@@ -260,23 +288,26 @@ public function admin_enqueue_scripts( $hook ) { | |
* @return void | ||
*/ | ||
public function the_settings_page() { | ||
$action = is_multisite() ? 'index.php?page=aspireupdate-settings' : 'options.php'; | ||
$reset_url = add_query_arg( | ||
array( | ||
'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="<?php echo esc_attr( $action ); ?>"> | ||
<?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="Save Changes"> | ||
<?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> | ||
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. Same clarification request about if this is necessary for this issue or if it's aprt of another? 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's necessary for sending a nonce with the submit button. I did change the link to use the 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. Works for me, thanks! |
||
</p> | ||
</form> | ||
|
@@ -291,13 +322,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 +533,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 +547,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 +570,18 @@ class="regular-text" | |
public function sanitize_settings( $input ) { | ||
$sanitized_input = array(); | ||
|
||
$sanitized_input['enable'] = ( isset( $input['enable'] ) && $input['enable'] ) ? 1 : 0; | ||
$sanitized_input['enable'] = ( ! empty( $input['enable'] ) && $input['enable'] ) ? 1 : 0; | ||
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. Why the change here for 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. Also, the second condition shouldn't be necessary here if using What about just 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. These values are unset by default. With multisite the sanitize function runs twice, I assume once from the 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. Let me test that. 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. Tested and working. 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. Further simplified, doesn't need ternary. |
||
$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'] = ! empty( $input['enable_debug'] ) ? 1 : 0; | ||
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'] = ( ! empty( $input['disable_ssl_verification'] ) && $input['disable_ssl_verification'] ) ? 1 : 0; | ||
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.