From 8665b65caa8dc9eb9649281794611e7e3f1ffa6f Mon Sep 17 00:00:00 2001 From: Bastien Ho Date: Thu, 24 May 2018 15:23:51 +0200 Subject: [PATCH 1/4] Enable network settings --- cookie-notice.php | 223 +++++++++++++++++++++++++++++++++------------- 1 file changed, 161 insertions(+), 62 deletions(-) diff --git a/cookie-notice.php b/cookie-notice.php index 2e76118..cd7a6c5 100644 --- a/cookie-notice.php +++ b/cookie-notice.php @@ -2,7 +2,7 @@ /* Plugin Name: Cookie Notice Description: Cookie Notice allows you to elegantly inform users that your site uses cookies and to comply with the EU cookie law regulations. -Version: 1.2.37 +Version: 1.3 Author: dFactory Author URI: http://www.dfactory.eu/ Plugin URI: http://www.dfactory.eu/plugins/cookie-notice/ @@ -70,7 +70,7 @@ class Cookie_Notice { 'translate' => true, 'deactivation_delete' => 'no' ), - 'version' => '1.2.37' + 'version' => '1.3' ); private $positions = array(); private $styles = array(); @@ -82,6 +82,7 @@ class Cookie_Notice { private $effects = array(); private $times = array(); private $script_placements = array(); + private $in_network_context = false; /** * @var $cookie, holds cookie name @@ -99,13 +100,23 @@ public function __construct() { register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) ); // settings - $this->options = array( - 'general' => array_merge( $this->defaults['general'], get_option( 'cookie_notice_options', $this->defaults['general'] ) ) + $this->network_options = array( + 'general' => array_merge( $this->defaults['general'], get_site_option( 'cookie_notice_options', $this->defaults['general'] ) ) ); + if(isset($this->network_options['general']['force_network_values']) && $this->network_options['general']['force_network_values']==='yes'){ + $this->options = $this->network_options; + } + else{ + $this->options = array( + 'general' => array_merge( $this->defaults['general'], get_option( 'cookie_notice_options', $this->defaults['general'] ) ) + ); + } // actions add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_menu', array( $this, 'admin_menu_options' ) ); + add_action( 'network_admin_menu', array(&$this,'network_admin_menu_options')); + add_action('admin_post_cn_save_network_settings', array(&$this, 'save_network_settings')); add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); add_action( 'after_setup_theme', array( $this, 'load_defaults' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_scripts_styles' ) ); @@ -179,6 +190,9 @@ public function load_defaults() { update_option( 'cookie_notice_options', $this->options['general'] ); } + if(!isset($this->network_options['general']['force_network_values'])){ + $this->network_options['general']['force_network_values'] = 'no'; + } // WPML >= 3.2 if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, '3.2', '>=' ) ) { @@ -238,16 +252,24 @@ public function admin_menu_options() { __( 'Cookie Notice', 'cookie-notice' ), __( 'Cookie Notice', 'cookie-notice' ), apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ), 'cookie-notice', array( $this, 'options_page' ) ); } + /** + * Add submenu in network + */ + public function network_admin_menu_options(){ + add_submenu_page( + 'settings.php', __( 'Cookie Notice (Network)', 'cookie-notice' ), __( 'Cookie Notice', 'cookie-notice' ), apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ), 'cookie-notice', array( $this, 'network_options_page' ) + ); + } /** * Options page output. - * + * * @return mixed */ - public function options_page() { + public function options_page($network=false) { echo '
' . screen_icon() . ' -

' . __( 'Cookie Notice', 'cookie-notice' ) . '

+

' . ($network ? __( 'Cookie Notice (Network)', 'cookie-notice' ) : __( 'Cookie Notice', 'cookie-notice' )) . '

-
'; - + '; settings_fields( 'cookie_notice_options' ); + if($network){ + $this->in_network_context = true; + echo ''; + wp_nonce_field('cn_network_settings', 'cn_network_settings'); + _e('The settings will be applied on the whole network. They can be overrided on each site.', 'cookie-notice'); + echo '

+

'; + + } do_settings_sections( 'cookie_notice_options' ); - + echo '

'; submit_button( '', 'primary', 'save_cookie_notice_options', false ); @@ -281,6 +313,53 @@ public function options_page() {

'; } + /** + * @since 1.3 + */ + public function network_options_page(){ + $this->options_page(true); + } + + /** + * Parse escaped posted settings + * @param mixte $value + * @return mixte $value + * @since 1.3 + */ + private function get_setting_post_value($value){ + + if(is_string($value)) + return esc_attr($value); + + if(is_array($value)){ + $values = array(); + foreach($value as $k=>$val){ + $values[$k] = $this->get_setting_post_value($val); + } + return $values; + } + + } + /** + * Save network settings + * @since 1.3 + */ + public function save_network_settings(){ + + if (!wp_verify_nonce(filter_input(INPUT_POST, 'cn_network_settings', FILTER_SANITIZE_STRING), 'cn_network_settings')) + wp_die(__('Cheating, uh?', 'cookie-notice')); + + $network_settings = array( + 'force_network_values' => isset($_POST['cookie_notice_options']['force_network_values']) ? esc_attr($_POST['cookie_notice_options']['force_network_values']) : 'no', + ); + foreach ($this->defaults['general'] as $setting => $value) { + $network_settings[$setting] = $this->get_setting_post_value($_POST['cookie_notice_options'][$setting]); + } + update_site_option( 'cookie_notice_options', $network_settings ); + + wp_safe_redirect(admin_url('network/settings.php?page=cookie-notice')); + exit; + } /** * Regiseter plugin settings. @@ -318,17 +397,19 @@ public function cn_section_design() {} * Delete plugin data on deactivation. */ public function cn_deactivation_delete() { + $options = $this->in_network_context ? $this->network_options : $this->options; echo ' - '; + '; } /** * Cookie message option. */ public function cn_message_text() { + $options = $this->in_network_context ? $this->network_options : $this->options; echo '
- +

' . __( 'Enter the cookie notice message.', 'cookie-notice' ) . '

'; } @@ -337,9 +418,10 @@ public function cn_message_text() { * Accept cookie label option. */ public function cn_accept_text() { + $options = $this->in_network_context ? $this->network_options : $this->options; echo '
- +

' . __( 'The text of the option to accept the usage of the cookies and make the notification disappear.', 'cookie-notice' ) . '

'; } @@ -348,18 +430,19 @@ public function cn_accept_text() { * Enable/Disable third party non functional cookies option. */ public function cn_refuse_opt() { + $options = $this->in_network_context ? $this->network_options : $this->options; echo '
- '; - echo '
options['general']['refuse_opt'] === 'no' ? ' style="display: none;"' : '') . '>'; + '; + echo '