Cookiebot_Declaration_Widget.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\widgets; |
| 4 | |
| 5 | use cybot\cookiebot\lib\Supported_Languages; |
| 6 | use InvalidArgumentException; |
| 7 | use WP_Widget; |
| 8 | use cybot\cookiebot\lib\Cookiebot_WP; |
| 9 | use function cybot\cookiebot\lib\include_view; |
| 10 | |
| 11 | class Cookiebot_Declaration_Widget extends WP_Widget { |
| 12 | |
| 13 | // Main constructor |
| 14 | public function __construct() { |
| 15 | parent::__construct( |
| 16 | 'cookiebot_declaration_widget', |
| 17 | esc_html__( 'Cookiebot - Cookie Declaration', 'cookiebot' ), |
| 18 | array( |
| 19 | 'customize_selective_refresh' => true, |
| 20 | ) |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | // The widget form (for the backend ) |
| 25 | |
| 26 | /** |
| 27 | * @throws InvalidArgumentException |
| 28 | */ |
| 29 | public function form( $instance ) { |
| 30 | $defaults = array( |
| 31 | 'lang' => '', |
| 32 | 'title' => '', |
| 33 | ); |
| 34 | $fixed_args = array( |
| 35 | 'title_field_id' => $this->get_field_id( 'title' ), |
| 36 | 'title_field_name' => $this->get_field_name( 'title' ), |
| 37 | 'lang_field_id' => $this->get_field_id( 'lang' ), |
| 38 | 'lang_field_name' => $this->get_field_name( 'lang' ), |
| 39 | 'supported_languages' => Supported_Languages::get(), |
| 40 | ); |
| 41 | $view_args = wp_parse_args( (array) $instance, array_merge( $defaults, $fixed_args ) ); |
| 42 | include_view( 'admin/widgets/cookiebot-declaration-widget-form.php', $view_args ); |
| 43 | } |
| 44 | |
| 45 | // Update widget settings |
| 46 | public function update( $new_instance, $old_instance ) { |
| 47 | $instance = $old_instance; |
| 48 | $instance['lang'] = isset( $new_instance['lang'] ) ? wp_strip_all_tags( $new_instance['lang'] ) : ''; |
| 49 | $instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 50 | return $instance; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Display the widget |
| 55 | * |
| 56 | * @throws InvalidArgumentException |
| 57 | */ |
| 58 | public function widget( $args, $instance ) { |
| 59 | $before_widget_html = isset( $args['before_widget'] ) && is_string( $args['before_widget'] ) |
| 60 | ? $args['before_widget'] |
| 61 | : ''; |
| 62 | $after_widget_html = isset( $args['after_widget'] ) && is_string( $args['after_widget'] ) |
| 63 | ? $args['after_widget'] |
| 64 | : ''; |
| 65 | $has_before_title_html = isset( $args['before_title'] ) && is_string( $args['before_title'] ); |
| 66 | $has_after_title_html = isset( $args['after_title'] ) && is_string( $args['after_title'] ); |
| 67 | if ( $has_before_title_html && $has_after_title_html ) { |
| 68 | $before_title_html = $args['before_title']; |
| 69 | $after_title_html = $args['after_title']; |
| 70 | } else { |
| 71 | $before_title_html = '<h2>'; |
| 72 | $after_title_html = '</h2>'; |
| 73 | } |
| 74 | $has_widget_title = isset( $instance['title'] ) && is_string( $instance['title'] ); |
| 75 | $widget_title_html = $has_widget_title |
| 76 | ? $before_title_html . $instance['title'] . $after_title_html |
| 77 | : ''; |
| 78 | $tag_attribute_html = get_site_option( 'cookiebot-script-tag-cd-attribute', 'custom' ); |
| 79 | if ( ! is_multisite() || $tag_attribute_html === 'custom' ) { |
| 80 | $tag_attribute_html = get_option( 'cookiebot-script-tag-cd-attribute', 'async' ); |
| 81 | } |
| 82 | $view_args = array( |
| 83 | 'cookie_declaration_script_url' => 'https://consent.cookiebot.com/' . Cookiebot_WP::get_cbid() . '/cd.js', |
| 84 | 'culture' => isset( $lang ) && is_string( $lang ) ? $lang : null, |
| 85 | 'before_widget_html' => $before_widget_html, |
| 86 | 'after_widget_html' => $after_widget_html, |
| 87 | 'widget_title_html' => $widget_title_html, |
| 88 | 'tag_attribute_html' => $tag_attribute_html, |
| 89 | ); |
| 90 | include_view( 'frontend/widgets/cookiebot-declaration-widget.php', $view_args ); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 |