| 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 |
esc_html__( 'Cookiebot - Cookie Declaration', 'cookiebot' ), |
| 11 |
array( |
| 12 |
'customize_selective_refresh' => true, |
| 13 |
) |
| 14 |
); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Outputs the settings update form. |
| 19 |
* |
| 20 |
* @since 2.8.0 |
| 21 |
* |
| 22 |
* @param array $instance Current settings. |
| 23 |
* @return string Default return is 'noform'. |
| 24 |
*/ |
| 25 |
public function form( $instance ) { |
| 26 |
$lang = isset( $instance['lang'] ) ? $instance['lang'] : ''; |
| 27 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 28 |
?> |
| 29 |
<p> |
| 30 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'Cookiebot' ); ?></label> |
| 31 |
<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 ); ?>" /> |
| 32 |
</p> |
| 33 |
<p> |
| 34 |
<label for="<?php echo esc_attr( $this->get_field_id( 'lang' ) ); ?>"><?php esc_html_e( 'Language', 'cookiebot' ); ?></label> |
| 35 |
<select name="<?php echo esc_attr( $this->get_field_name( 'lang' ) ); ?>" |
| 36 |
id="<?php echo esc_attr( $this->get_field_id( 'lang' ) ); ?>" |
| 37 |
class="widefat"> |
| 38 |
<option value=""><?php echo esc_html__( '- Default -', 'cookiebot' ); ?></option> |
| 39 |
<?php |
| 40 |
$options = Cookiebot_WP::get_supported_languages(); |
| 41 |
foreach ( $options as $key => $name ) { |
| 42 |
?> |
| 43 |
<option value="<?php echo esc_attr( $key ); ?>" |
| 44 |
id="<?php echo esc_attr( $key ); ?>" |
| 45 |
class="value" |
| 46 |
<?php selected( $lang, $key, false, true ); ?> |
| 47 |
><?php echo esc_html( $name ); ?></option> |
| 48 |
<?php |
| 49 |
} |
| 50 |
?> |
| 51 |
</select> |
| 52 |
</p> |
| 53 |
<?php |
| 54 |
|
| 55 |
// the base function expects a string to be returned |
| 56 |
return ''; |
| 57 |
} |
| 58 |
|
| 59 |
// Update widget settings |
| 60 |
public function update( $new_instance, $old_instance ) { |
| 61 |
$instance = $old_instance; |
| 62 |
$instance['lang'] = isset( $new_instance['lang'] ) ? wp_strip_all_tags( $new_instance['lang'] ) : ''; |
| 63 |
$instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 64 |
return $instance; |
| 65 |
} |
| 66 |
|
| 67 |
// Display the widget |
| 68 |
|
| 69 |
/** |
| 70 |
* Echoes the widget content. |
| 71 |
* |
| 72 |
* Subclasses should override this function to generate their widget code. |
| 73 |
* |
| 74 |
* @since 2.8.0 |
| 75 |
* |
| 76 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 77 |
* 'before_widget', and 'after_widget'. |
| 78 |
* @param array $instance The settings for the particular instance of the widget. |
| 79 |
*/ |
| 80 |
public function widget( $args, $instance ) { |
| 81 |
$before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : ''; |
| 82 |
$after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : ''; |
| 83 |
$before_title = isset( $args['before_title'] ) ? $args['before_title'] : ''; |
| 84 |
$after_title = isset( $args['after_title'] ) ? $args['after_title'] : ''; |
| 85 |
$lang = isset( $instance['lang'] ) ? $instance['lang'] : ''; |
| 86 |
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 87 |
|
| 88 |
// WordPress core before_widget hook |
| 89 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 90 |
echo $before_widget; |
| 91 |
|
| 92 |
echo '<div class="widget-text wp_widget_plugin_box cookiebot_cookie_declaration">'; |
| 93 |
|
| 94 |
// Display widget title if defined |
| 95 |
if ( $title ) { |
| 96 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 97 |
echo $before_title . esc_html( $title ) . $after_title; |
| 98 |
} |
| 99 |
|
| 100 |
$cbid = Cookiebot_WP::get_cbid(); |
| 101 |
if ( ! is_multisite() || get_site_option( 'cookiebot-script-tag-cd-attribute', 'custom' ) === 'custom' ) { |
| 102 |
$tag_attr = get_option( 'cookiebot-script-tag-cd-attribute', 'async' ); |
| 103 |
} else { |
| 104 |
$tag_attr = get_site_option( 'cookiebot-script-tag-cd-attribute' ); |
| 105 |
} |
| 106 |
|
| 107 |
?> |
| 108 |
<script type="text/javascript" |
| 109 |
id="CookieDeclaration" |
| 110 |
src="<?php echo esc_url( 'https://consent.cookiebot.com/' . $cbid . '/cd.js' ); ?>" |
| 111 |
<?php if ( ! empty( $lang ) ) : ?> |
| 112 |
data-culture="<?php echo esc_attr( $lang ); ?>" |
| 113 |
<?php endif; ?> |
| 114 |
<?php echo esc_attr( $tag_attr ); ?> |
| 115 |
/> |
| 116 |
<?php |
| 117 |
echo '</div>'; |
| 118 |
|
| 119 |
// WordPress core after_widget hook |
| 120 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 121 |
echo $after_widget; |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|