-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.php
More file actions
178 lines (146 loc) · 3.7 KB
/
widget.php
File metadata and controls
178 lines (146 loc) · 3.7 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace BestProject\Wordpress;
defined('ABSPATH') or die;
use BestProject\Wordpress\Language,
BestProject\Wordpress\Widget\WidgetInterface;
/**
* Class that takes care of the all the functions required by Wordpress to create a widget.
*/
abstract class Widget extends \WP_Widget implements WidgetInterface
{
/**
* Name of this widget(id).
* @var String
*/
public $name;
/**
* Title of this widget.
*
* @var type
*/
public $title;
/**
* Short description of this widget.
*
* @var String
*/
public $description;
/**
* Widget constructor.
*/
public function __construct()
{
// Create widget name if not set
if (empty($this->name)) {
$className = explode('\\', get_class($this));
$className = strtolower(end($className));
$this->name = 'bp_'.$className;
}
// Create widget UI title if not set
if (empty($this->title)) {
$this->title = 'WIDGET_'.strtoupper($this->name).'_TITLE';
}
// Create widget UI description if not set
if (empty($this->description)) {
$this->description = 'WIDGET_'.strtoupper($this->name).'_DESC';
}
// Create new widget instance
parent::__construct(
$this->name, Language::_($this->title),
array('description' => Language::_($this->description))
);
}
/**
* Displays a widget instance on front-end.
*
* @param Array $args
* @param WP_Widget $instance
*/
public function widget($args, $instance)
{
$this->instance = $instance;
ob_start();
// before and after widget arguments are defined by themes
echo $args['before_widget'];
// If there is title field in this widget
if (isset($instance['title']) AND ! empty($instance['title'])) {
$title = apply_filters('widget_title', $instance['title']);
if (!empty($title)) {
echo $args['before_title'].$title.$args['after_title'];
}
}
$this->render($instance, $args);
echo $args['after_widget'];
echo ob_get_clean();
}
/**
* Render widget form.
*
* @param Array $instance
*/
public function form($instance)
{
$fields = $this->getFields();
$html = '';
/* @var $field Field */
foreach ($fields AS $field) {
$name = $field->get('name');
// Prepare the value
if (isset($instance[$name])) {
$field->set('value', $instance[$field->get('name')]);
}
// Set `name` attribute as provided by the system
$field->set('name', $this->get_field_name($name));
// Set `id` attribute as provided by the system
$field->set('id', $this->get_field_id($name));
$html .= $field->render();
}
echo $html;
}
/**
* Hook on updating this widget.
*
* @param Array $new_instance
* @param Array $old_instance
* @return Array
*/
public function update($new_instance, $old_instance)
{
return $new_instance;
}
/**
* Renders the widget.
*
* @param Array $instance An array of widget instance data.
* @param Array $args An array of widget/sidebar data.
*/
public function render($instance, $args)
{
// Get name of the widget
$reflection = new \ReflectionClass($this);
$filename = strtolower($reflection->getName());
$override_path = get_template_directory().'/template-parts/widgets/'.$filename.'.php';
// If there is html override for this widget in a template, render it
if (file_exists($override_path)) {
extract($instance);
require $override_path;
// If there is no override. Just print the data
} else {
ob_start();
foreach ($instance AS $key => $variable) {
if ($key !== 'title') {
?><div><h4><?php echo Language::_(strtoupper($key)) ?></h4><div><?php echo $variable ?></div></div><?php
}
}
}
}
/**
* Register this widget.
*
* @param String $className
*/
public static function register($className)
{
register_widget($className);
}
}