Sin descripción

eu-cookie-law.php 9.4KB

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