Skip to content
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

Merged
merged 18 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aspire-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: AspireUpdate
* Domain Path: /languages
* Network: true
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down
87 changes: 59 additions & 28 deletions includes/class-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Comment on lines +53 to +54
Copy link
Contributor

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?

Copy link
Contributor Author

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.

}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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>';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how the i18n and escaping got removed. Sorry.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this made it into the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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' );
}
}

Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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' )
);
Expand All @@ -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 );
Expand All @@ -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(),
)
Expand All @@ -265,18 +293,20 @@ 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' ); ?>">
<?php wp_nonce_field( 'aspireupdate-settings' ); ?>
<?php submit_button( '', 'primary', 'submit', false ); ?>

<a href="<?php echo esc_url( $reset_url ); ?>" class="button button-secondary" ><?php esc_html_e( 'Reset', 'AspireUpdate' ); ?></a>
</p>
</form>
Expand All @@ -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(
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 What change am I not seeing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'] ?? '' ); ?>
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 What change am I not seeing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand All @@ -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;
}
}