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