| 1 |
<?php |
| 2 |
|
| 3 |
use Automattic\Jetpack\Assets; |
| 4 |
|
| 5 |
/** |
| 6 |
* Disable direct access/execution to/of the widget code. |
| 7 |
*/ |
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! class_exists( 'Jetpack_EU_Cookie_Law_Widget' ) ) { |
| 13 |
/** |
| 14 |
* EU Cookie Law Widget |
| 15 |
* |
| 16 |
* Display the EU Cookie Law banner in the bottom part of the screen. |
| 17 |
*/ |
| 18 |
class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { |
| 19 |
/** |
| 20 |
* EU Cookie Law cookie name. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public static $cookie_name = 'eucookielaw'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Default hide options. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $hide_options = array( |
| 32 |
'button', |
| 33 |
'scroll', |
| 34 |
'time', |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Default text options. |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $text_options = array( |
| 43 |
'default', |
| 44 |
'custom', |
| 45 |
); |
| 46 |
|
| 47 |
/** |
| 48 |
* Default color scheme options. |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
private $color_scheme_options = array( |
| 53 |
'default', |
| 54 |
'negative', |
| 55 |
); |
| 56 |
|
| 57 |
/** |
| 58 |
* Default policy URL options. |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
private $policy_url_options = array( |
| 63 |
'default', |
| 64 |
'custom', |
| 65 |
); |
| 66 |
|
| 67 |
/** |
| 68 |
* Widget position options. |
| 69 |
* |
| 70 |
* @var array |
| 71 |
*/ |
| 72 |
private $position_options = array( |
| 73 |
'bottom', |
| 74 |
'top', |
| 75 |
); |
| 76 |
|
| 77 |
/** |
| 78 |
* Constructor. |
| 79 |
*/ |
| 80 |
function __construct() { |
| 81 |
parent::__construct( |
| 82 |
'eu_cookie_law_widget', |
| 83 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 84 |
apply_filters( 'jetpack_widget_name', esc_html__( 'Cookies & Consents Banner', 'jetpack' ) ), |
| 85 |
array( |
| 86 |
'description' => esc_html__( 'Display a banner for EU Cookie Law and GDPR compliance.', 'jetpack' ), |
| 87 |
'customize_selective_refresh' => true, |
| 88 |
), |
| 89 |
array() |
| 90 |
); |
| 91 |
|
| 92 |
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
| 93 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Enqueue scripts and styles. |
| 99 |
*/ |
| 100 |
function enqueue_frontend_scripts() { |
| 101 |
wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); |
| 102 |
wp_enqueue_script( |
| 103 |
'eu-cookie-law-script', |
| 104 |
Assets::get_file_url_for_environment( |
| 105 |
'_inc/build/widgets/eu-cookie-law/eu-cookie-law.min.js', |
| 106 |
'modules/widgets/eu-cookie-law/eu-cookie-law.js' |
| 107 |
), |
| 108 |
array( 'jquery' ), |
| 109 |
'20180522', |
| 110 |
true |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Return an associative array of default values. |
| 116 |
* |
| 117 |
* These values are used in new widgets. |
| 118 |
* |
| 119 |
* @return array Default values for the widget options. |
| 120 |
*/ |
| 121 |
public function defaults() { |
| 122 |
return array( |
| 123 |
'hide' => $this->hide_options[0], |
| 124 |
'hide-timeout' => 30, |
| 125 |
'consent-expiration' => 180, |
| 126 |
'text' => $this->text_options[0], |
| 127 |
'customtext' => '', |
| 128 |
'color-scheme' => $this->color_scheme_options[0], |
| 129 |
'policy-url' => get_option( 'wp_page_for_privacy_policy' ) ? $this->policy_url_options[1] : $this->policy_url_options[0], |
| 130 |
'default-policy-url' => 'https://automattic.com/cookies/', |
| 131 |
'custom-policy-url' => get_option( 'wp_page_for_privacy_policy' ) ? get_permalink( (int) get_option( 'wp_page_for_privacy_policy' ) ) : '', |
| 132 |
'position' => $this->position_options[0], |
| 133 |
'policy-link-text' => esc_html__( 'Cookie Policy', 'jetpack' ), |
| 134 |
'button' => esc_html__( 'Close and accept', 'jetpack' ), |
| 135 |
'default-text' => esc_html__( "Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. \r\nTo find out more, including how to control cookies, see here:", 'jetpack' ), |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Front-end display of the widget. |
| 141 |
* |
| 142 |
* @param array $args Widget arguments. |
| 143 |
* @param array $instance Saved values from database. |
| 144 |
*/ |
| 145 |
public function widget( $args, $instance ) { |
| 146 |
/** |
| 147 |
* Filters the display of the EU Cookie Law widget. |
| 148 |
* |
| 149 |
* @since 6.1.1 |
| 150 |
* |
| 151 |
* @param bool true Should the EU Cookie Law widget be disabled. Default to false. |
| 152 |
*/ |
| 153 |
if ( apply_filters( 'jetpack_disable_eu_cookie_law_widget', false ) ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 158 |
|
| 159 |
$classes = array(); |
| 160 |
$classes['hide'] = 'hide-on-' . esc_attr( $instance['hide'] ); |
| 161 |
if ( 'negative' === $instance['color-scheme'] ) { |
| 162 |
$classes['negative'] = 'negative'; |
| 163 |
} |
| 164 |
|
| 165 |
if ( 'top' === $instance['position'] ) { |
| 166 |
$classes['top'] = 'top'; |
| 167 |
} |
| 168 |
|
| 169 |
if ( Jetpack::is_module_active( 'wordads' ) ) { |
| 170 |
$classes['ads'] = 'ads-active'; |
| 171 |
$classes['hide'] = 'hide-on-button'; |
| 172 |
} |
| 173 |
|
| 174 |
echo $args['before_widget']; |
| 175 |
require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' ); |
| 176 |
echo $args['after_widget']; |
| 177 |
/** This action is already documented in modules/widgets/gravatar-profile.php */ |
| 178 |
do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Back-end widget form. |
| 183 |
* |
| 184 |
* @param array $instance Previously saved values from database. |
| 185 |
*/ |
| 186 |
public function form( $instance ) { |
| 187 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 188 |
if ( Jetpack::is_module_active( 'wordads' ) ) { |
| 189 |
$instance['hide'] = 'button'; |
| 190 |
} |
| 191 |
|
| 192 |
wp_enqueue_script( |
| 193 |
'eu-cookie-law-widget-admin', |
| 194 |
Assets::get_file_url_for_environment( |
| 195 |
'_inc/build/widgets/eu-cookie-law/eu-cookie-law-admin.min.js', |
| 196 |
'modules/widgets/eu-cookie-law/eu-cookie-law-admin.js' |
| 197 |
), |
| 198 |
array( 'jquery' ), |
| 199 |
20180417 |
| 200 |
); |
| 201 |
|
| 202 |
require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Sanitize widget form values as they are saved. |
| 207 |
* |
| 208 |
* @param array $new_instance Values just sent to be saved. |
| 209 |
* @param array $old_instance Previously saved values from database. |
| 210 |
* @return array Updated safe values to be saved. |
| 211 |
*/ |
| 212 |
public function update( $new_instance, $old_instance ) { |
| 213 |
$instance = array(); |
| 214 |
$defaults = $this->defaults(); |
| 215 |
|
| 216 |
$instance['hide'] = $this->filter_value( isset( $new_instance['hide'] ) ? $new_instance['hide'] : '', $this->hide_options ); |
| 217 |
$instance['text'] = $this->filter_value( isset( $new_instance['text'] ) ? $new_instance['text'] : '', $this->text_options ); |
| 218 |
$instance['color-scheme'] = $this->filter_value( isset( $new_instance['color-scheme'] ) ? $new_instance['color-scheme'] : '', $this->color_scheme_options ); |
| 219 |
$instance['policy-url'] = $this->filter_value( isset( $new_instance['policy-url'] ) ? $new_instance['policy-url'] : '', $this->policy_url_options ); |
| 220 |
$instance['position'] = $this->filter_value( isset( $new_instance['position'] ) ? $new_instance['position'] : '', $this->position_options ); |
| 221 |
|
| 222 |
if ( isset( $new_instance['hide-timeout'] ) ) { |
| 223 |
// Time can be a value between 3 and 1000 seconds. |
| 224 |
$instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
| 225 |
} |
| 226 |
|
| 227 |
if ( isset( $new_instance['consent-expiration'] ) ) { |
| 228 |
// Time can be a value between 1 and 365 days. |
| 229 |
$instance['consent-expiration'] = min( 365, max( 1, intval( $new_instance['consent-expiration'] ) ) ); |
| 230 |
} |
| 231 |
|
| 232 |
if ( isset( $new_instance['customtext'] ) ) { |
| 233 |
$instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 ); |
| 234 |
} else { |
| 235 |
$instance['text'] = $this->text_options[0]; |
| 236 |
} |
| 237 |
|
| 238 |
if ( isset( $new_instance['policy-url'] ) ) { |
| 239 |
$instance['policy-url'] = 'custom' === $new_instance['policy-url'] |
| 240 |
? 'custom' |
| 241 |
: 'default'; |
| 242 |
} else { |
| 243 |
$instance['policy-url'] = $this->policy_url_options[0]; |
| 244 |
} |
| 245 |
|
| 246 |
if ( 'custom' === $instance['policy-url'] && isset( $new_instance['custom-policy-url'] ) ) { |
| 247 |
$instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) ); |
| 248 |
|
| 249 |
if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
| 250 |
unset( $instance['custom-policy-url'] ); |
| 251 |
global $wp_customize; |
| 252 |
if ( ! isset( $wp_customize ) ) { |
| 253 |
$instance['policy-url'] = $this->policy_url_options[0]; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if ( isset( $new_instance['policy-link-text'] ) ) { |
| 259 |
$instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { |
| 263 |
unset( $instance['policy-link-text'] ); |
| 264 |
} |
| 265 |
|
| 266 |
if ( isset( $new_instance['button'] ) ) { |
| 267 |
$instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); |
| 268 |
} |
| 269 |
|
| 270 |
if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { |
| 271 |
unset( $instance['button'] ); |
| 272 |
} |
| 273 |
|
| 274 |
// Show the banner again if a setting has been changed. |
| 275 |
setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
| 276 |
|
| 277 |
return $instance; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Check if the value is allowed and not empty. |
| 282 |
* |
| 283 |
* @param string $value Value to check. |
| 284 |
* @param array $allowed Array of allowed values. |
| 285 |
* |
| 286 |
* @return string $value if pass the check or first value from allowed values. |
| 287 |
*/ |
| 288 |
function filter_value( $value, $allowed = array() ) { |
| 289 |
$allowed = (array) $allowed; |
| 290 |
if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { |
| 291 |
$value = $allowed[0]; |
| 292 |
} |
| 293 |
return $value; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// Register Jetpack_EU_Cookie_Law_Widget widget. |
| 298 |
function jetpack_register_eu_cookie_law_widget() { |
| 299 |
register_widget( 'Jetpack_EU_Cookie_Law_Widget' ); |
| 300 |
}; |
| 301 |
|
| 302 |
add_action( 'widgets_init', 'jetpack_register_eu_cookie_law_widget' ); |
| 303 |
} |
| 304 |
|