From 46cd8f17bee61ccfa50a6bc5107a0d1d5ec8675c Mon Sep 17 00:00:00 2001 From: Andy Fragen Date: Mon, 4 Nov 2024 10:20:00 -0800 Subject: [PATCH] add multisite support (#102) --- aspire-update.php | 1 + includes/class-admin-settings.php | 87 +++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/aspire-update.php b/aspire-update.php index 4c49a089..da283719 100644 --- a/aspire-update.php +++ b/aspire-update.php @@ -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' ) ) { diff --git a/includes/class-admin-settings.php b/includes/class-admin-settings.php index b4114b66..0f237358 100644 --- a/includes/class-admin-settings.php +++ b/includes/class-admin-settings.php @@ -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,8 +90,8 @@ 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( @@ -96,7 +99,7 @@ public function reset_settings() { '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; @@ -119,14 +122,14 @@ public function delete_all_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 '

' . esc_html__( 'Settings have been reset to default.', 'AspireUpdate' ) . '

'; - delete_option( 'aspireupdate-reset' ); + echo '

Settings have been reset to default.

'; + delete_site_option( 'aspireupdate-reset' ); } } @@ -140,13 +143,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 ) ) { @@ -219,6 +222,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/ + * + * @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. * @@ -233,7 +261,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' ) ); @@ -247,7 +275,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 ); @@ -256,7 +284,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(), ) @@ -274,18 +302,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' ) ); ?>

-
+ option_group ); do_settings_sections( 'aspireupdate-settings' ); ?>

- + + +

@@ -366,13 +396,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( @@ -577,10 +607,10 @@ public function add_settings_field_callback( $args = array() ) { -