forked from creativecommons/wp-plugin-creativecommons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreativecommons-widget.php
130 lines (114 loc) · 4.05 KB
/
creativecommons-widget.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Class: Creative Commons Widget
*
* User-specified Creative Commons License will display in the page footer by default.
* User will be able to drag this widget to a sidebar as well.
*
* @package CC_WordPress_Plugin
* @subpackage Widget Class
*/
if ( ! class_exists( 'CreativeCommons_Widget' ) ) {
/**
* Widget class, extends the default WP_Widget.
*/
class CreativeCommons_Widget extends WP_Widget {
/**
* This method is used to assign an id, name, class name, and description to the widget
* to show in admin area.
*/
public function __construct() {
// Widget settings.
$widget_ops = array(
'classname' => 'cc-license-widget',
'description' => __( 'By default, user-specified Creative Commons License will display in the page footer. Alternatively, drag this widget to a sidebar or any other widget area. The license will appear there instead.', 'CreativeCommons' ),
);
// Widget control settings.
$control_ops = array(
'id_base' => 'cc-license-widget',
);
// Create the widget.
parent::__construct(
'cc-license-widget',
__( 'CC License', 'CreativeCommons' ),
$widget_ops,
$control_ops
);
/*
* if the widget is not active, (i.e. the plugin is installed but the widget has not been
* dragged to a sidebar), then display the license in the footer as a default.
*/
add_action( 'wp_footer', array( &$this, 'print_license_footer' ) );
}
/**
* Instantiates CreativeCommons class and prints the license as widget
*/
public function print_license_widget() {
$ccl = CreativeCommons::get_instance();
$license = $ccl->get_license( $license = 'site' );
if ( $license['display_as_widget'] && !empty( $license['deed'] ) ) {
$ccl->print_license_html();
}
}
/**
* Instantiates CreativeCommons class and prints the license as footer
*/
public function print_license_footer() {
$ccl = CreativeCommons::get_instance();
$license = $ccl->get_license( $license = 'site' );
if ( $license['display_as_footer'] && !empty( $license['deed'] ) ) {
$ccl->print_license_html();
}
}
/**
* Defines the widget output that will be displayed on the site front end.
*
* @param mixed $args This variable loads an array of arguments which
* can be used when building widget output.
* @param mixed $instance Current values of this particular instance.
*/
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
/*
* To prevent rendering an empty heading tag (and related margin)
* only render title when it exists (and isn't an empty string)
*/
$title_exists = isset($title) && !empty(trim($title));
if ($title_exists) {
echo $args['before_title'] . $title . $args['after_title'];
}
$this->print_license_widget( );
echo $args['after_widget'];
}
/**
* Widget Backend
* Adds setting fields to the widget which will be displayed in the WordPress admin area.
*
* @param mixed $instance Current values of this particular instance.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>Leave it empty to remove the title.</p>
<?php
}
/**
* Validates the new settings as appropriate and then assigns
* them to the current instance.
*
* @param mixed $new_instance Contains the values added to the widget settings form.
* @param mixed $old_instance Contains the existing settings — if any exist.
*
* @return $instance Returns updated instance.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = wp_strip_all_tags( $new_instance['title'] ); // Strips all unwanted tags.
return $instance;
}
}
}