diff --git a/cookie-notice.php b/cookie-notice.php index 2e76118..2e8ca3c 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,9 @@ class Cookie_Notice { private $effects = array(); private $times = array(); private $script_placements = array(); + private $is_network = false; + private $in_network_context = false; + private $is_network_values_forced = false; /** * @var $cookie, holds cookie name @@ -102,10 +105,35 @@ public function __construct() { $this->options = array( 'general' => array_merge( $this->defaults['general'], get_option( 'cookie_notice_options', $this->defaults['general'] ) ) ); + if(is_multisite()){ + if(! function_exists( 'is_plugin_active_for_network' ) ) { + require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); + } + if ( is_plugin_active_for_network( basename(__DIR__).'/'.basename(__FILE__) ) ) { + $this->is_network = true; + // Get network settings + $this->network_options = array( + 'general' => array_merge( $this->defaults['general'], get_site_option( 'cookie_notice_options', $this->defaults['general'] ) ) + ); + // If network settings are forced, override local settings + if(isset($this->network_options['general']['force_network_values']) && $this->network_options['general']['force_network_values']==='yes'){ + $this->options = $this->network_options; + $this->is_network_values_forced = true; + } + // Load local settings with network ones as default + else{ + $this->options = array( + 'general' => array_merge( $this->network_options['general'], get_option( 'cookie_notice_options', $this->network_options['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' ) ); @@ -135,7 +163,8 @@ public function load_defaults() { $this->links = array( 'custom' => __( 'Custom link', 'cookie-notice' ), - 'page' => __( 'Page link', 'cookie-notice' ) + 'page' => __( 'Page link', 'cookie-notice' ), + 'legacy' => __( 'Page for Privacy Policy', 'cookie-notice'), ); $this->link_target = array( @@ -179,6 +208,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 +270,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' ); - do_settings_sections( 'cookie_notice_options' ); - - echo ' -

'; - submit_button( '', 'primary', 'save_cookie_notice_options', false ); - echo ' '; - submit_button( __( 'Reset to defaults', 'cookie-notice' ), 'secondary', 'reset_cookie_notice_options', false ); - echo ' -

+ 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 '

+

'; + + } + + if($network || !$this->is_network_values_forced){ + do_settings_sections( 'cookie_notice_options' ); + echo ' +

'; + submit_button( '', 'primary', 'save_cookie_notice_options', false ); + echo ' '; + submit_button( __( 'Reset to defaults', 'cookie-notice' ), 'secondary', 'reset_cookie_notice_options', false ); + echo ' +

'; + } + else{ + echo '

'.__('Settings are managed on the whole network.', 'cookie-notice').'

'; + } + echo'
'; } + /** + * @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 +421,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 +442,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 +454,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 '
'; } /** * Validate options. - * + * * @param array $input * @return array */ @@ -584,7 +716,7 @@ public function validate_options( $input ) { if ( ! check_admin_referer( 'cookie_notice_options-options') ) return $input; - + if ( ! current_user_can( apply_filters( 'cn_manage_cookie_notice_cap', 'manage_options' ) ) ) return $input; @@ -618,10 +750,10 @@ public function validate_options( $input ) { // hide effect $input['hide_effect'] = sanitize_text_field( isset( $input['hide_effect'] ) && in_array( $input['hide_effect'], array_keys( $this->effects ) ) ? $input['hide_effect'] : $this->defaults['general']['hide_effect'] ); - + // on scroll $input['on_scroll'] = (bool) isset( $input['on_scroll'] ) ? 'yes' : 'no'; - + // on scroll offset $input['on_scroll_offset'] = absint( isset( $input['on_scroll_offset'] ) && $input['on_scroll_offset'] !== '' ? $input['on_scroll_offset'] : $this->defaults['general']['on_scroll_offset'] ); @@ -651,11 +783,12 @@ public function validate_options( $input ) { do_action( 'wpml_register_single_string', 'Cookie Notice', 'Custom link', $input['see_more_opt']['link'] ); } } elseif ( isset( $_POST['reset_cookie_notice_options'] ) ) { - - $input = $this->defaults['general']; + + $input = $this->is_network ? $this->network_options['general'] : $this->defaults['general']; + add_settings_error( 'reset_cookie_notice_options', 'reset_cookie_notice_options', __( 'Settings restored to defaults.', 'cookie-notice' ), 'updated' ); - + } return $input; @@ -663,7 +796,7 @@ public function validate_options( $input ) { /** * Cookie notice output. - * + * * @return mixed */ public function add_cookie_notice() { @@ -702,13 +835,21 @@ public function add_cookie_notice() { 'link_target' => $this->options['general']['link_target'], ) ); + $see_more_url = '#'; + if($options['see_more_opt']['link_type'] === 'custom'){ + $see_more_url = $options['see_more_opt']['link']; + } elseif($options['see_more_opt']['link_type'] === 'page'){ + $see_more_url = get_permalink( $options['see_more_opt']['id'] ); + } elseif($options['see_more_opt']['link_type'] === 'legacy'){ + $see_more_url = get_privacy_policy_url(); + } // message output $output = ' '; @@ -718,7 +859,7 @@ public function add_cookie_notice() { /** * Checks if cookie is setted - * + * * @return bool */ public function cookie_setted() { @@ -727,7 +868,7 @@ public function cookie_setted() { /** * Checks if third party non functional cookies are accepted - * + * * @return bool */ public static function cookies_accepted() { @@ -740,10 +881,10 @@ public static function cookies_accepted() { public function get_defaults() { return $this->defaults; } - + /** * Add links to Support Forum. - * + * * @param array $links * @param string $file * @return array @@ -762,7 +903,7 @@ public function plugin_extend_links( $links, $file ) { /** * Add links to settings page. - * + * * @param array $links * @param string $file * @return array @@ -805,7 +946,7 @@ public function admin_load_scripts_styles( $page ) { wp_enqueue_script( 'cookie-notice-admin', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery', 'wp-color-picker' ), $this->defaults['version'] ); - + wp_localize_script( 'cookie-notice-admin', 'cnArgs', array( 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'cookie-notice' ) @@ -842,15 +983,15 @@ public function front_load_scripts_styles() { wp_enqueue_style( 'cookie-notice-front', plugins_url( 'css/front.css', __FILE__ ) ); } } - + /** * Print non functional javascript. - * + * * @return mixed */ public function wp_print_footer_scripts() { $scripts = html_entity_decode( trim( wp_kses_post( $this->options['general']['refuse_code'] ) ) ); - + if ( $this->cookie_setted() && ! empty( $scripts ) ) { ?>