PluginProbe
MaxButtons – Create buttons / 9.8.6
MaxButtons – Create buttons v9.8.6
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / classes / screen.php

screen.php in MaxButtons – Create buttons 9.8.6, at classes/screen.php

497 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace MaxButtons;
5 // instance of screen, to keep all data from one screen together.
6
7 class Screen
8 {
9 protected $fields;
10 protected $defined_fields = array(); // fields defined in admin
11 protected $named_fields = array(); // fields that have an unique ID, but data field is based on name ( checkbox, radio etc )
12 protected $mapped_fields = array();
13 protected $templates_by_name = array('radio', 'checkbox', 'switch');
14
15 protected $data;
16 protected static $screens;
17
18 public $id;
19 public $name;
20
21 private $prefix = '';
22 private $is_responsive = false;
23 private $is_new = false;
24 private $is_default = false;
25
26
27 public function __construct($id, $args = array())
28 {
29 $this->id = $id;
30
31 if ($id == 'default')
32 {
33 $this->is_default = true;
34 }
35 elseif ($id == 'new')
36 {
37 $this->is_new = true;
38 }
39 else
40 {
41 $this->is_responsive = true;
42 }
43
44 if ($id !== 'default')
45 {
46 $this->prefix = $id . '_';
47 }
48
49 $this->name = isset($args['name']) ? $args['name'] : 'placeholder';
50 }
51
52 protected function setData($data)
53 {
54
55 $new_data = array(); //egalite
56 if (! is_array($data) || count($data) == 0) // no data
57 return false;
58
59 foreach($data as $block => $fields)
60 {
61 if (is_array($fields))
62 $new_data = array_merge($new_data, $fields);
63 }
64
65 $this->data = $new_data;
66 }
67
68
69 public static function setupScreens($data)
70 {
71 $responsive = isset($data['responsive']) ? $data['responsive'] : array();
72 $screens['default'] = array('name' => __('Main', 'maxbuttons'));
73
74 if (is_array($responsive) && isset($responsive['screens']))
75 {
76 $responsive_screens = array_filter($responsive['screens']); // remove anything empty.
77
78 foreach($responsive_screens as $screen_id)
79 {
80 $name = isset($responsive[$screen_id . '_screen_name']) ? $responsive[$screen_id . '_screen_name'] : false;
81 $screens[$screen_id] = array('name' => $name);
82 }
83 }
84
85 $screens['new'] = array('name' => __('Add', 'maxbuttons'));
86
87 $screenObjs = array();
88
89 foreach($screens as $screen_name => $this_screen_data)
90 {
91 $screen = new Screen($screen_name, $this_screen_data);
92 $screen->setData($data);
93 $screensObjs[$screen_name] = $screen;
94 }
95
96 self::$screens = $screensObjs;
97 return $screensObjs;
98 }
99
100 public static function hasResponsive()
101 {
102 foreach(self::$screens as $screen)
103 {
104 if ($screen->is_responsive())
105 return true;
106 }
107
108 return false;
109 }
110
111 public static function getScreens()
112 {
113 return self::$screens;
114 }
115
116 public static function countScreens()
117 {
118 return count(self::getScreens()) -1; // don't count new screen
119 }
120
121 // function to get field id, eventually converting it for responsive.
122 public function getFieldID($name)
123 {
124 return $this->prefix . $name;
125 }
126
127 public function is_responsive()
128 {
129 return $this->is_responsive;
130 }
131
132 public function is_new()
133 {
134 return $this->is_new;
135 }
136
137 public function is_default()
138 {
139 return $this->is_default;
140 }
141
142
143 // add a maxfield to be displayed on the admin.
144 public function addfield($field, $start = '', $end = '')
145 {
146 $field_id = isset($field->id) ? $field->id : $field->template . \wp_rand(0,1000);
147
148 // don't add if it's not our screen.
149 if (! $this->isFieldThisScreen($field))
150 {
151 return;
152 }
153
154 $this->fields[$field_id] = array('field' => $field,
155 'start' => $start,
156 'end' => $end);
157 $this->fields = apply_filters('mb/editor/addfield', $this->fields, $field, $this);
158
159 $this->defined_fields[] = $field_id;
160 // Radio and checkboxes more define as field_name ( same fieldname for all) as field_id, instead of an unique id. If not added here, the mapfields will skip it, because not in defined fields.
161 if (in_array($field->template, $this->templates_by_name) )
162 {
163 $this->named_fields[$field_id] = $field->name;
164 }
165 do_action('mb/editor/afterfield/'. $field_id, $field, $this);
166 }
167
168 /** Insert a field before or after a existing field
169 *
170 * @param String $insert_field Existing field to perform operation on
171 * @param Object $field The new field to insert
172 * @param String $start Start Template
173 * @param String $end End template
174 * @param String $op Insert before of after specified insert field
175 */
176 public function insertField($insert_field, $field, $start = 'start', $end = 'end', $op = 'before')
177 {
178 $insert_pos = 0;
179
180 // don't add if it's not our screen.
181 if (! $this->isFieldThisScreen($field))
182 return;
183
184 $this->addField($field, $start, $end); // add the field to the array
185
186 $added_field_index = array_search($field->id, array_keys($this->fields), true);
187
188 $insert_field_index = array_search($insert_field, array_keys($this->fields), true);
189 if ($insert_field_index === false)
190 {
191 $insert_field_index = count($this->fields) -2; // after the start block field, in the begin of block
192 // $op = 'after';
193 }
194
195 if ($op == 'after')
196 {
197 $insert_field_index++;
198 }
199
200 $this->fields = array_slice($this->fields, 0, $insert_field_index, true) + array_slice($this->fields, $added_field_index, 1, true) +
201 array_slice($this->fields, ($insert_field_index), null, true);
202
203 return ;
204
205 // old, @todo can go if no bugs arise.
206 foreach($this->fields as $field_id => $array)
207 {
208 if ($field_id == $insert_field)
209 {
210
211 break;
212 }
213 $insert_pos++;
214 }
215
216 // Find inserted field and remove it from array.
217 $new_field_id = $field->id;
218 $new_field_ar = $this->fields[$new_field_id];
219 unset($this->fields[$new_field_id]);
220
221 // Find position to insert new field
222 // Yes this could be more efficient.
223 $new_fields = array(); //$this->fields;
224 $inserted = false;
225 $i = 0;
226
227 foreach($this->fields as $field_id => $array)
228 {
229 if ($i == $insert_pos && $op == 'before')
230 {
231 $new_fields[$new_field_id] = $new_field_ar;
232 $inserted = true;
233 }
234 $new_fields[$field_id] = $array;
235 if ($i == $insert_pos && $op == 'after')
236 {
237 $new_fields[$new_field_id] = $new_field_ar;
238 $inserted = true;
239 }
240 $i++;
241 }
242 // This can happen if inserted example is not in responsive screen
243 if ( $inserted === false)
244 {
245 $new_fields[$new_field_id] = $new_field_ar;
246 }
247
248 $this->fields = $new_fields;
249 }
250
251 public function isFieldThisScreen($field)
252 {
253 if ($this->is_responsive && ! $field->is_responsive)
254 return false;
255 if ($this->is_default && ! $field->is_default)
256 return false;
257 if ($this->is_new && ! $field->is_new)
258 return false;
259
260 return true;
261 }
262
263 public function getFields()
264 {
265 return $this->fields;
266 }
267
268 // Maps fields to available fields, this is connecting the JS live Preview.
269 public function mapFields($map)
270 {
271 $newmap = array();
272
273 foreach($map as $name => $data) // make conversions for the screens.
274 {
275 $field_id = $this->getFieldID($name);
276
277 if (in_array($field_id, $this->defined_fields))
278 $newmap[$field_id] = $data;
279
280 if (in_array($field_id, $this->named_fields))
281 {
282 //$matches = array_keys($this->named_fields, $field_id);
283 // $fid == form field id, fname is form field.,
284 foreach($this->named_fields as $fid => $fname)
285 {
286 if ($fname == $field_id)
287 {
288 $newmap[$fid] = $data;
289 }
290
291 }
292
293 $newmap[$field_id] = $data;
294 }
295
296
297 }
298 $this->mapped_fields = $newmap;
299 return $newmap;
300 }
301
302 /** This is all a bit hacky, map can be delivered by param, or take the main thing */
303 public function displayFieldMap($map = null)
304 {
305 if (is_null($map))
306 $map = $this->mapped_fields;
307
308 // JS expects a fieldmap always.
309 echo "<span class='fieldmap'>";
310 echo json_encode($map);
311 echo "</span>";
312 }
313
314 public function display_fields($clean = true, $return = false)
315 {
316 $fields = apply_filters('mb/display_fields', $this->fields);
317 $output = '';
318
319 if (! is_array($fields))
320 return;
321
322 foreach($fields as $id => $item)
323 {
324 $field = $item['field'];
325
326 if ($field->publish == false) // don't publish this via screen, this is something manual.
327 continue;
328
329 $output .= $field->output($item['start'], $item['end']);
330 }
331
332 if ($clean)
333 {
334 $this->fields = array();
335 }
336
337 if (! $return)
338 echo $output;
339 else
340 return $output;
341
342 }
343
344 protected function getParentFieldName($fieldname)
345 {
346 return $prefixless = str_replace($this->prefix, '', $fieldname);
347 }
348
349 public function getValue($fieldname)
350 {
351 $value = false;
352
353 if(isset($this->data[$fieldname]))
354 {
355 $value = $this->data[$fieldname];
356 }
357 elseif($this->is_responsive && strpos($fieldname, $this->prefix) >= 0) // try to retrieve original value of the responsive field.
358 {
359 $prefixless = $this->getParentFieldName($fieldname);
360
361 if (isset($this->data[$prefixless]))
362 {
363 $value = $this->data[$prefixless];
364 }
365 }
366 elseif ($this->getDefault($fieldname))
367 {
368 $value = $this->getDefault($fieldname);
369 }
370
371 return $value; // dunno.
372 }
373
374 public function getColorValue($fieldname)
375 {
376 $value = $this->getValue($fieldname);
377 if (maxUtils::isrgba($value))
378 return $value;
379
380 if (! $value )
381 return false;
382 if (substr($value,0,1) !== '#')
383 {
384 $value = '#' . $value;
385 }
386 return $value;
387 }
388
389 public function getScreenIcon()
390 {
391 $screen_type = $this->getScreenType();
392 $min_width = $this->getValue($this->getFieldID('min_width'));
393
394 switch ($screen_type)
395 {
396 case 'new':
397 $icon = 'dashicons-plus';
398 break;
399 case 'responsive':
400 if ($min_width >= 1024)
401 $icon = 'dashicons-laptop';
402 else
403 $icon = 'dashicons-smartphone';
404 break;
405 case 'default':
406 default:
407 $icon = 'dashicons-desktop';
408 break;
409 }
410
411 return $icon;
412 }
413
414 public function getScreenTitle()
415 {
416 $min_width = $this->getValue($this->getFieldID('min_width'));
417 $max_width = $this->getValue($this->getFieldID('max_width'));
418
419 $display = $title = '';
420
421 if ($this->is_default())
422 {
423 $title = __('Your main button for all screens. ', 'maxbuttons');
424
425 }
426 elseif ($this->is_new())
427 {
428 $title = __('Add a new responsive screen for mobile devices', 'maxbuttons');
429 $display = __('for mobile', 'maxbuttons');
430 }
431 elseif ($min_width && $max_width)
432 {
433 $display = $min_width . __('px', 'maxbuttons') . ' - ' . $max_width . __('px', 'maxbuttons');
434 $title = sprintf(__('Shows at screen size from %s to %s', 'maxbuttons'), $min_width . __('px', 'maxbuttons'), $max_width . __('px', 'maxbuttons'));
435 }
436 elseif (! $min_width && $max_width)
437 {
438 $display = '< ' . $max_width . __('px', 'maxbuttons');
439 $title = sprintf(__('Shows at screen size smaller than %s', 'maxbuttons'), $max_width . __('px', 'maxbuttons'));
440
441 }
442 elseif (! $max_width && $min_width)
443 {
444 $display = '> ' . $min_width . __('px', 'maxbuttons');
445 $title = sprintf(__('Shows at screen size bigger than %s', 'maxbuttons'), $min_width . __('px', 'maxbuttons'));
446 }
447 elseif (! $min_width && ! $max_width) // if somebody does something like this :/
448 {
449 $display = '';
450 $title = sprintf(__('Set width and height to use this responsive screen', 'maxbuttons'));
451 }
452
453 return array($display, $title);
454 }
455
456 public function getScreenType()
457 {
458 $screen_type = ($this->is_responsive()) ? 'responsive' : 'default';
459 $screen_type = ($this->is_new()) ? 'new' : $screen_type;
460
461 return $screen_type;
462 }
463
464 public function getDefault($fieldname)
465 {
466 $fieldDef = maxBlocks::getFieldDefinition($fieldname) ;
467
468 if ($fieldDef && isset($fieldDef['default']) )
469 {
470 return $fieldDef['default'];
471 }
472
473
474 return false; // doesn't know
475 }
476
477 public function removeScreen($data)
478 {
479 foreach($data as $block_name => $blockdata)
480 {
481 if (is_array($blockdata)) // document_id block has a non-array blockdata
482 {
483 foreach($blockdata as $field_name => $field_data)
484 {
485 $has_id = strpos($field_name, $this->id);
486 if ($has_id !== false && $has_id === 0) // strict checks to fail-safe deletion.
487 {
488 unset($data[$block_name][$field_name]);
489 }
490 }
491 }
492 }
493 return $data;
494 }
495
496 } // class
497