-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.dable-widget.php
More file actions
108 lines (96 loc) · 3.44 KB
/
class.dable-widget.php
File metadata and controls
108 lines (96 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
class DableWidget extends WP_Widget {
var $description = '???';
static function register() {
register_widget( 'DableWidget' );
}
function __construct() {
parent::__construct(
'dable_widget',
esc_html__( 'Dable', 'dable' ),
array(
'description' => esc_html__( 'Displays a Dable banner.', 'dable' )
)
);
}
function widget( $args, $instance ) {
if ( ! Dable::is_eligible_post_type() ) {
return;
}
if ( empty( $instance['widget_id']) ) {
echo '<!-- ' . __('Dable widget ID is not defined.', 'dable') . ' -->';
return;
}
$widget_id = $instance['widget_id'];
echo '<!-- ' . esc_html__( 'Begin Dable Script / For inquiries, support@dable.io', 'dable' ) . ' / generated by Dable for WordPress -->';
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) && empty( $instance['hide_title'] ) ) {
echo $args['before_title'];
echo apply_filters( 'widget_title', $instance['title'] );
echo $args['after_title'];
}
echo "<div id='dablewidget_{$widget_id}' data-widget_id='{$widget_id}'><script>";
echo "(function(d,a){d[a]=d[a]||function(){(d[a].q=d[a].q||[]).push(arguments)};}(window,'dable'));dable('renderWidget', 'dablewidget_{$widget_id}');";
echo '</script></div>';
echo $args['after_widget'];
echo '<!-- ' . esc_html__( 'End Dable Script / For inquiries, support@dable.io', 'dable' ) . ' -->';
}
function update( $new_instance, $old_instance ) {
$instance = array(
'title' => empty( $new_instance['title'] ) ? '' : sanitize_text_field( $new_instance['title'] ),
'widget_id' => empty( $new_instance['title'] ) ? '' : sanitize_text_field( $new_instance['widget_id'] ),
'hide_title' => empty( $new_instance['hide_title'] ) ? false : true
);
return $instance;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : esc_html__( 'Dable widget title' , 'dable' );
$widget_id = isset( $instance['widget_id'] ) ? $instance['widget_id'] : '';
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title' ); ?>:</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
value="<?php echo esc_attr( $title ); ?>"
>
</p>
<p>
<input
type="checkbox"
id="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'hide_title' ) ); ?>"
value="true"
<?php if ( isset( $instance['hide_title'] ) && $instance['hide_title'] ): ?>
checked
<?php endif; ?>
>
<label for="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>">
<?php esc_html_e( 'Do not show title.' ); ?>
</label>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'widget_id' ) ); ?>"><?php esc_html_e( 'Widget ID', 'dable' ); ?>:</label>
<input
type="text"
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'widget_id' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'widget_id' ) ); ?>"
value="<?php echo esc_attr( $widget_id ); ?>"
>
<span><?php
printf(
wp_kses(
__( 'If you are not sure about this, please contact <a href="mailto:%1$s">%1$s</a>', 'dable' ),
array( 'a' => array( 'href' => true ) )
),
'support@dable.io'
);
?></span>
</p>
<?php
}
}
add_action( 'widgets_init', 'DableWidget::register' );