cookiebot-declaration-widget.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | // The widget class for Cookiebot D |
| 4 | class Cookiebot_Declaration_Widget extends WP_Widget { |
| 5 | |
| 6 | // Main constructor |
| 7 | public function __construct() { |
| 8 | parent::__construct( |
| 9 | 'cookiebot_declaration_widget', |
| 10 | __( 'Cookiebot - Cookie Declaration', 'cookiebot' ), |
| 11 | array( |
| 12 | 'customize_selective_refresh' => true, |
| 13 | ) |
| 14 | ); |
| 15 | } |
| 16 | |
| 17 | // The widget form (for the backend ) |
| 18 | public function form( $instance ) { |
| 19 | $defaults = array('lang'=>''); |
| 20 | extract( wp_parse_args( ( array ) $instance, $defaults ) ); |
| 21 | ?> |
| 22 | <p> |
| 23 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'Cookiebot' ); ?></label> |
| 24 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 25 | </p> |
| 26 | <p> |
| 27 | <label for="<?php echo $this->get_field_id( 'lang' ); ?>"><?php _e( 'Language', 'cookiebot' ); ?></label> |
| 28 | <select name="<?php echo $this->get_field_name( 'lang' ); ?>" id="<?php echo $this->get_field_id( 'lang' ); ?>" class="widefat"> |
| 29 | <option value=""><?php echo __('- Default -', 'cookiebot'); ?></option> |
| 30 | <?php |
| 31 | $options = Cookiebot_WP::get_supported_languages(); |
| 32 | foreach ( $options as $key => $name ) { |
| 33 | echo '<option value="' . esc_attr( $key ) . '" id="' . esc_attr( $key ) . '" '. selected( $lang, $key, false ) . '>'. $name . '</option>'; |
| 34 | } |
| 35 | ?> |
| 36 | </select> |
| 37 | |
| 38 | </p> |
| 39 | <?php |
| 40 | } |
| 41 | |
| 42 | // Update widget settings |
| 43 | public function update( $new_instance, $old_instance ) { |
| 44 | $instance = $old_instance; |
| 45 | $instance['lang'] = isset( $new_instance['lang'] ) ? wp_strip_all_tags( $new_instance['lang'] ) : ''; |
| 46 | $instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 47 | return $instance; |
| 48 | } |
| 49 | |
| 50 | // Display the widget |
| 51 | public function widget( $args, $instance ) { |
| 52 | extract( $args ); |
| 53 | extract( $instance ); |
| 54 | |
| 55 | // WordPress core before_widget hook |
| 56 | echo $before_widget; |
| 57 | |
| 58 | echo '<div class="widget-text wp_widget_plugin_box cookiebot_cookie_declaration">'; |
| 59 | |
| 60 | // Display widget title if defined |
| 61 | if ( $title ) { |
| 62 | echo $before_title . $title . $after_title; |
| 63 | } |
| 64 | |
| 65 | $cbid = Cookiebot_WP::get_cbid(); |
| 66 | if(!empty($lang)) { $lang = ' data-culture="'.$lang.'"'; } |
| 67 | if(!is_multisite() || get_site_option('cookiebot-script-tag-cd-attribute','custom') == 'custom') { |
| 68 | $tagAttr = get_option('cookiebot-script-tag-cd-attribute','async'); |
| 69 | } |
| 70 | else { |
| 71 | $tagAttr = get_site_option('cookiebot-script-tag-cd-attribute'); |
| 72 | } |
| 73 | |
| 74 | echo '<script id="CookieDeclaration" src="https://consent.cookiebot.com/'.$cbid.'/cd.js"'.$lang.' type="text/javascript" '.$tagAttr.'></script>'; |
| 75 | |
| 76 | echo '</div>'; |
| 77 | |
| 78 | // WordPress core after_widget hook |
| 79 | echo $after_widget; |
| 80 | } |
| 81 | |
| 82 | } |
| 83 |