PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / libraries / adapter / dashboard / widget.php

widget.php in VikBooking Hotel Booking Engine & PMS trunk, at libraries/adapter/dashboard/widget.php

274 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.dashboard
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Class used to encapsulate the information of
16 * a widget that can be published within the
17 * dashboard of WordPress.
18 *
19 * @since 10.1.31
20 */
21 abstract class JDashboardWidget
22 {
23 /**
24 * Returns the name of the widget.
25 *
26 * @return string
27 */
28 abstract public function getName();
29
30 /**
31 * Returns the string that will be used within
32 * the ID and CLASS attributes of the widget.
33 *
34 * @return string
35 */
36 public function getID()
37 {
38 // by default generate the ID starting from the class name
39 $id = get_class($this);
40
41 // remove base class name, if specified
42 $id = preg_replace("/^JDashboardWidget/i", '', $id);
43
44 // add an underscore before every camel case
45 $id = preg_replace("/([a-z])([A-Z])/", '$1_$2', $id);
46
47 // always use lower-case letters
48 return strtolower($id);
49 }
50
51 /**
52 * Returns an optional description of the widget.
53 * Implement this method in order to avoid forcing
54 * its declaration in subclasses.
55 *
56 * @return string
57 */
58 public function getDescription()
59 {
60 return '';
61 }
62
63 /**
64 * Checks whether the specified user is able to access
65 * this widget. Implement this method in order to avoid
66 * forcing its declaration in subclasses.
67 *
68 * @param mixed $user The user to check.
69 *
70 * @return boolean
71 */
72 public function canAccess($user = null)
73 {
74 return true;
75 }
76
77 /**
78 * Returns the HTML that will be used to display the
79 * contents of the widget.
80 *
81 * @param mixed $object Gets passed to the meta box callback function as the first parameter.
82 * Often this is the object that's the focus of the current screen, for
83 * example a `WP_Post` or `WP_Comment` object.
84 * @param array $widget The arguments passed to the `wp_add_dashboard_widget` function.
85 *
86 * @return void
87 */
88 public function getHtml($object, $widget)
89 {
90 // render contents by passing the widget settings, if any
91 echo $this->renderWidget($widget['args']);
92 }
93
94 /**
95 * Renders the HTML to display within the contents of the widget.
96 *
97 * @param mixed $args A registry of settings.
98 *
99 * @return string The HTML to display.
100 */
101 abstract protected function renderWidget($args);
102
103 /**
104 * Returns an instance of the form, if supported.
105 * Implement this method in order to avoid forcing
106 * its declaration in subclasses.
107 *
108 * @return null|JForm
109 */
110 public function getForm()
111 {
112 return null;
113 }
114
115 /**
116 * Returns the configuration of the widget.
117 *
118 * @return JRegistry A registry of the configuration.
119 */
120 public function getConfig()
121 {
122 // fetch all dashboard widget options stored within the database
123 $options = get_option('dashboard_widget_options', array());
124
125 // get widget ID
126 $widget_id = $this->getID();
127
128 $config = new JRegistry();
129
130 // make sure the configuration of the widget exists
131 if (isset($options[$widget_id]))
132 {
133 // set configuration settings within registry
134 $config->setProperties($options[$widget_id]);
135 }
136
137 return $config;
138 }
139
140 /**
141 * Returns the HTML that will be used to display the
142 * configuration form of the widget.
143 * Dispatches also the save method when the form
144 * is submitted.
145 *
146 * @param mixed $object Gets passed to the meta box callback function as the first parameter.
147 * Often this is the object that's the focus of the current screen, for
148 * example a `WP_Post` or `WP_Comment` object.
149 * @param array $widget The arguments passed to the `wp_add_dashboard_widget` function.
150 *
151 * @return void
152 */
153 public function config($object, $widget)
154 {
155 $input = JFactory::getApplication()->input;
156
157 if ($input->getBool('submit'))
158 {
159 // save the widget settings
160 $this->save();
161 }
162 else
163 {
164 // display the widget configuration
165 echo $this->renderForm($this->getConfig());
166 }
167 }
168
169 /**
170 * Renders the HTML to display within the configuration of the widget.
171 * Implement this method in order to avoid forcing its declaration in subclasses.
172 *
173 * @param mixed $args A registry of settings.
174 *
175 * @return string The HTML to display.
176 */
177 protected function renderForm($args)
178 {
179 // try to get XML form
180 $form = $this->getForm();
181
182 $html = '';
183
184 if ($form)
185 {
186 // render form
187 $html = $form->renderForm($args ? (object) $args->getProperties() : array());
188 }
189
190 return $html;
191 }
192
193 /**
194 * Saves the configuration of the widget.
195 *
196 * @return boolean True on success, false otherwise.
197 */
198 public function save()
199 {
200 // fetch all dashboard widget options stored within the database
201 $options = get_option('dashboard_widget_options', array());
202
203 // get widget ID
204 $widget_id = $this->getID();
205
206 // bind settings
207 $options[$widget_id] = $this->bind();
208
209 // update db option
210 update_option('dashboard_widget_options', $options);
211
212 return true;
213 }
214
215 /**
216 * Binds the configuration settings that will be saved.
217 * Implement this method in order to avoid forcing its declaration in subclasses.
218 *
219 * @return array The associative array to save.
220 */
221 protected function bind()
222 {
223 // try to get XML form
224 $form = $this->getForm();
225
226 $data = array();
227
228 if ($form)
229 {
230 $input = JFactory::getApplication()->input;
231
232 // get form data
233 $formData = $input->get('jform', array(), 'array');
234
235 // get input filter
236 $inputFilter = JInputFilter::getInstance();
237
238 // iterate the layout fields
239 foreach ($form->getFields() as $field)
240 {
241 $attrs = $field->attributes();
242
243 $name = (string) $attrs->name;
244 $filter = (string) $attrs->filter;
245
246 // use string if no filter
247 if (empty($filter))
248 {
249 $filter = 'string';
250 }
251
252 if (isset($formData[$name]))
253 {
254 // clean filter in request
255 $value = $inputFilter->clean($formData[$name], $filter);
256 }
257 else
258 {
259 // use NULL
260 $value = null;
261 }
262
263 // save value only if not NULL
264 if ($value !== null)
265 {
266 $data[$name] = $value;
267 }
268 }
269 }
270
271 return $data;
272 }
273 }
274