Jetpack_EU_Cookie_Law_Widget

EU Cookie Law Widget.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/eu-cookie-law.php  
  1. class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { 
  2. /** 
  3. * EU Cookie Law cookie name. 
  4. * @var string 
  5. */ 
  6. public static $cookie_name = 'eucookielaw'; 
  7.  
  8. /** 
  9. * EU Cookie Law cookie validity (30 days). 
  10. * @var int 
  11. */ 
  12. public static $cookie_validity = 2592000; 
  13.  
  14. /** 
  15. * Default hide options. 
  16. * @var array 
  17. */ 
  18. private $hide_options = array( 
  19. 'button',  
  20. 'scroll',  
  21. 'time',  
  22. ); 
  23.  
  24. /** 
  25. * Default text options. 
  26. * @var array 
  27. */ 
  28. private $text_options = array( 
  29. 'default',  
  30. 'custom',  
  31. ); 
  32.  
  33. /** 
  34. * Default color scheme options. 
  35. * @var array 
  36. */ 
  37. private $color_scheme_options = array( 
  38. 'default',  
  39. 'negative',  
  40. ); 
  41.  
  42. /** 
  43. * Default policy URL options. 
  44. * @var array 
  45. */ 
  46. private $policy_url_options = array( 
  47. 'default',  
  48. 'custom',  
  49. ); 
  50.  
  51. /** 
  52. * Constructor. 
  53. */ 
  54. function __construct() { 
  55. parent::__construct( 
  56. 'eu_cookie_law_widget',  
  57. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  58. apply_filters( 'jetpack_widget_name', esc_html__( 'EU Cookie Law Banner', 'jetpack' ) ),  
  59. array( 
  60. 'description' => esc_html__( 'Display a banner for compliance with the EU Cookie Law.', 'jetpack' ),  
  61. 'customize_selective_refresh' => true,  
  62. ),  
  63. array() 
  64. ); 
  65.  
  66. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { 
  67. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); 
  68.  
  69. /** 
  70. * Enqueue scripts and styles. 
  71. */ 
  72. function enqueue_frontend_scripts() { 
  73. wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' ); 
  74. wp_enqueue_script( 'eu-cookie-law-script', plugins_url( 'eu-cookie-law/eu-cookie-law.js', __FILE__ ), array( 'jquery' ), '20170404', true ); 
  75.  
  76. /** 
  77. * Return an associative array of default values. 
  78. * These values are used in new widgets. 
  79. * @return array Default values for the widget options. 
  80. */ 
  81. public function defaults() { 
  82. return array( 
  83. 'hide' => $this->hide_options[0],  
  84. 'hide-timeout' => 30,  
  85. 'text' => $this->text_options[0],  
  86. 'customtext' => '',  
  87. 'color-scheme' => $this->color_scheme_options[0],  
  88. 'policy-url' => $this->policy_url_options[0],  
  89. 'default-policy-url' => 'https://jetpack.com/support/cookies/',  
  90. 'custom-policy-url' => '',  
  91. 'policy-link-text' => esc_html__( 'Our Cookie Policy', 'jetpack' ),  
  92. 'button' => esc_html__( 'Close and accept', 'jetpack' ),  
  93. 'default-text' => esc_html__( 'Privacy & Cookies: This site uses cookies.', 'jetpack' ),  
  94. ); 
  95.  
  96. /** 
  97. * Front-end display of the widget. 
  98. * @param array $args Widget arguments. 
  99. * @param array $instance Saved values from database. 
  100. */ 
  101. public function widget( $args, $instance ) { 
  102. $instance = wp_parse_args( $instance, $this->defaults() ); 
  103. echo $args['before_widget']; 
  104. require( dirname( __FILE__ ) . '/eu-cookie-law/widget.php' ); 
  105. echo $args['after_widget']; 
  106. /** This action is already documented in modules/widgets/gravatar-profile.php */ 
  107. do_action( 'jetpack_stats_extra', 'widget_view', 'eu_cookie_law' ); 
  108.  
  109. /** 
  110. * Back-end widget form. 
  111. * @param array $instance Previously saved values from database. 
  112. */ 
  113. public function form( $instance ) { 
  114. $instance = wp_parse_args( $instance, $this->defaults() ); 
  115. require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' ); 
  116.  
  117. /** 
  118. * Sanitize widget form values as they are saved. 
  119. * @param array $new_instance Values just sent to be saved. 
  120. * @param array $old_instance Previously saved values from database. 
  121. * @return array Updated safe values to be saved. 
  122. */ 
  123. public function update( $new_instance, $old_instance ) { 
  124. $instance = array(); 
  125. $defaults = $this->defaults(); 
  126.  
  127. $instance['hide'] = $this->filter_value( $new_instance['hide'], $this->hide_options ); 
  128. $instance['text'] = $this->filter_value( $new_instance['text'], $this->text_options ); 
  129. $instance['color-scheme'] = $this->filter_value( $new_instance['color-scheme'], $this->color_scheme_options ); 
  130. $instance['policy-url'] = $this->filter_value( $new_instance['policy-url'], $this->policy_url_options ); 
  131.  
  132. if ( isset( $new_instance['hide-timeout'] ) ) { 
  133. // Time can be a value between 3 and 1000 seconds. 
  134. $instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); 
  135.  
  136. if ( isset( $new_instance['customtext'] ) ) { 
  137. $instance['customtext'] = mb_substr( wp_kses( $new_instance['customtext'], array() ), 0, 4096 ); 
  138. } else { 
  139. $instance['text'] = $this->text_options[0]; 
  140.  
  141. if ( isset( $new_instance['custom-policy-url'] ) ) { 
  142. $instance['custom-policy-url'] = esc_url( $new_instance['custom-policy-url'], array( 'http', 'https' ) ); 
  143.  
  144. if ( strlen( $instance['custom-policy-url'] ) < 10 ) { 
  145. unset( $instance['custom-policy-url'] ); 
  146. $instance['policy-url'] = $this->policy_url_options[0]; 
  147. } else { 
  148. $instance['policy-url'] = $this->policy_url_options[0]; 
  149.  
  150. if ( isset( $new_instance['policy-link-text'] ) ) { 
  151. $instance['policy-link-text'] = trim( mb_substr( wp_kses( $new_instance['policy-link-text'], array() ), 0, 100 ) ); 
  152.  
  153. if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $defaults['policy-link-text'] ) { 
  154. unset( $instance['policy-link-text'] ); 
  155.  
  156. if ( isset( $new_instance['button'] ) ) { 
  157. $instance['button'] = trim( mb_substr( wp_kses( $new_instance['button'], array() ), 0, 100 ) ); 
  158.  
  159. if ( empty( $instance['button'] ) || $instance['button'] == $defaults['button'] ) { 
  160. unset( $instance['button'] ); 
  161.  
  162. // Show the banner again if a setting has been changed. 
  163. setcookie( self::$cookie_name, '', time() - 86400, '/' ); 
  164.  
  165. return $instance; 
  166.  
  167. /** 
  168. * Set the EU Cookie Law cookie. 
  169. */ 
  170. public static function add_consent_cookie() { 
  171. if ( ! isset( $_POST['eucookielaw'] ) || 'accept' !== $_POST['eucookielaw'] ) { 
  172. return; 
  173.  
  174. if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'eucookielaw' ) ) { 
  175. return; 
  176.  
  177. // Cookie is valid for 30 days, so the user will be shown the banner again after 30 days. 
  178. setcookie( self::$cookie_name, current_time( 'timestamp' ), time() + self::$cookie_validity, '/' ); 
  179.  
  180. wp_safe_redirect( $_POST['redirect_url'] ); 
  181.  
  182. /** 
  183. * Check if the value is allowed and not empty. 
  184. * @param string $value Value to check. 
  185. * @param array $allowed Array of allowed values. 
  186. * @return string $value if pass the check or first value from allowed values. 
  187. */ 
  188. function filter_value( $value, $allowed = array() ) { 
  189. $allowed = (array) $allowed; 
  190. if ( empty( $value ) || ( ! empty( $allowed ) && ! in_array( $value, $allowed ) ) ) { 
  191. $value = $allowed[0]; 
  192. return $value;