eu-cookie-law.php 9.1 KB

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