PluginProbe
MaxButtons – Create buttons / 7.11
MaxButtons – Create buttons v7.11
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 / blocks / basic.php

basic.php in MaxButtons – Create buttons 7.11, at blocks/basic.php

553 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 $blockClass["basic"] = "basicBlock";
6 $blockOrder[0][] = "basic";
7
8
9 class basicBlock extends maxBlock
10 {
11 protected $blockname = "basic";
12 protected $fields = array("name" => array("default" => ''),
13 "status" => array("default" => "publish"),
14 "description" => array("default" => ''),
15 "url" => array("default" => ''),
16 'link_title' => array('default' => ''),
17 // "text" => array("default" => ''),
18 "new_window" => array("default" => 0),
19 "nofollow" => array("default" => 0)
20 );
21 protected $protocols = array("http","https",'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'sms', 'callto', 'fax', 'xmpp', "javascript", 'file', 'ms-windows-store', 'steam', 'webcal'); // allowed url protocols for esc_url functions
22
23
24 function __construct()
25 {
26 parent::__construct();
27 $extra_protocols = get_option('maxbuttons_protocol');
28 $extra_protocols = array_map('trim', array_filter(explode(',', $extra_protocols)));
29
30 if (is_array($extra_protocols) && count($extra_protocols) > 0)
31 {
32 $this->protocols = array_merge($this->protocols, $extra_protocols);
33 }
34 }
35
36 public function parse_css($css, $mode = 'normal')
37 {
38 // emtpy string init is not like by PHP 7.1
39 if (! is_array($css))
40 $css = array();
41
42 $data = $this->data[$this->blockname];
43
44 $css["maxbutton"]["normal"]["position"] = "relative";
45 $css["maxbutton"]["normal"]["text-decoration"] = "none";
46 // $css["maxbutton"]["normal"]["white-space"] = "nowrap"; // hinders correct rendering of oneline-multilines
47 $css["maxbutton"]["normal"]["display"] = "inline-block";
48 $css["maxbutton"]["normal"]["vertical-align"] = 'middle';
49
50 // option to show border boxed buttons in preview area.
51 $border_box = get_option('maxbuttons_borderbox');
52
53 if ($border_box == 1)
54 {
55 $css['maxbutton']['normal']['box-sizing'] = 'border-box';
56 }
57
58 $css = parent::parse_css($css, $mode);
59
60 /*if (isset($data["url"]) && $data["url"] == '') // don't show clickable anchor if there is no URL.
61 {
62 $css["maxbutton"]["normal"]["cursor"] = 'default';
63 // $css[":hover"]["cursor"] = 'default';
64 } */
65
66 return $css;
67
68 }
69
70 public function save_fields($data, $post)
71 {
72 // Possible solution:
73 // $post["url"] = isset($post["url"]) ? urldecode(urldecode($post["url"])) : '';
74
75 $description = false;
76
77 if (isset($post["description"]) && $post["description"] != '')
78 {
79 $description = str_replace("\n", '-nwline-', $post["description"]);
80 $description = sanitize_text_field($description);
81 $description = str_replace('-nwline-', "\n", $description);
82
83 }
84
85 $data = parent::save_fields($data, $post);
86
87 // bypass sanitize for description - causing the end of line-breaks
88 if ($description)
89 $data["basic"]["description"] = $description;
90
91 // bypassing sanitize text field - causes problems with URLs and spaces
92 $url = isset($post["url"]) ? trim($post["url"]) : '';
93
94 $parsed_url = parse_url($url);
95 $rawEncode = array("query","fragment");
96 foreach($rawEncode as $item)
97 {
98 if (isset($parsed_url[$item]))
99 {
100 $parsed_url[$item] = rawurlencode($parsed_url[$item]);
101 }
102 }
103
104 $url = $this->unParseURL($parsed_url);
105
106 $url = str_replace(" ", "%20", trim($url) );
107
108 if (! $this->checkRelative($parsed_url))
109 $url = esc_url_raw($url, $this->protocols); // str replace - known WP issue with spaces
110
111 $data[$this->blockname]["url"] = $url;
112
113 if (isset($post["name"]))
114 $data["name"] = sanitize_text_field($post["name"]);
115 if (isset($post["status"]))
116 $data["status"] = sanitize_text_field($post["status"]); // for conversion old - new.
117 return $data;
118 }
119
120 protected function unparseURL($parsed_url)
121 {
122 // Don't add // to these schemes
123 $noslash_schemes = array('javascript', 'mailto', 'tel', 'sms');
124 if (isset($parsed_url['scheme']) && in_array($parsed_url['scheme'], $noslash_schemes) )
125 $scheme = $parsed_url["scheme"] . ":";
126 else
127 $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
128
129
130 $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
131 $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
132 $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
133 $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
134 $pass = ($user || $pass) ? "$pass@" : '';
135 $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
136 $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
137 $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
138 return "$scheme$user$pass$host$port$path$query$fragment";
139 }
140
141 /* Check for a relative URL that gets killed by esc_url ( if there is no / first ) */
142 protected function checkRelative($parsed_url)
143 {
144 if (! isset($parsed_url['host']) && ! isset($parsed_url['scheme']) )
145 {
146 if (isset($parsed_url['path']) && $parsed_url['path'] !== '' && substr($parsed_url['path'], 0,1) !== '/')
147 {
148 return true;
149 }
150 }
151 return false;
152 }
153
154 public function parse_button($domObj, $mode = 'normal')
155 {
156
157 $data = $this->data[$this->blockname];
158 $button_id = $this->data["id"];
159 $rels = array();
160
161 $anchor = $domObj->find("a",0);
162
163 if (isset($data["nofollow"]) && $data["nofollow"] == 1)
164 {
165 $rels[] = 'nofollow';
166 $rels[] = 'noopener';
167 }
168 // $anchor->rel = "nofollow";
169 // $buttonAttrs[] = "rel=nofollow";
170
171 if (isset($data["new_window"]) && $data["new_window"] == 1)
172 {
173 $anchor->target = "_blank";
174 if (! in_array('noopener', $rels))
175 $rels[] = 'noopener';
176 }
177 if (isset($data['link_title']) && strlen($data['link_title']) > 0)
178 $anchor->title = $data['link_title'];
179
180 $rels = apply_filters('mb/button/rel', $rels);
181
182 if (count($rels) > 0)
183 {
184 $anchor->rel = implode(' ', $rels);
185 }
186
187 if (isset($data["url"]) && $data["url"] != '')
188 {
189 $url = $data["url"];
190 $parsed_url = parse_url($url);
191
192 if (! $this->checkRelative($parsed_url))
193 $url = esc_url($url, $this->protocols);
194
195 $url = rawurldecode($url); // removes the + from a URL part.
196 $url = apply_filters('mb-url', $url, $data['url']); // passes processed url / raw url.
197 $url = apply_filters('mb-url-' . $button_id, $url, $data['url']);
198
199
200 $anchor->href = $url;
201 //do_shortcode( esc_url($url, $this->protocols) );
202
203 }
204 else // fixing an iOS problem which renders anchors without URL wrongly.
205 {
206 $anchor->href = 'javascript:void(0);';
207 }
208
209
210 return $domObj;
211
212 }
213
214 public function map_fields($map)
215 {
216 $map = parent::map_fields($map);
217 $map["url"]["attr"] = "href";
218 $map["link_title"]["attr"] = "title";
219
220 // $map["text"]["func"] = "updateAnchorText";
221
222 return $map;
223 }
224
225 public function check_unique_name($name)
226 {
227 global $wpdb;
228 $table = maxUtils::get_table_name();
229
230 $button_id = $this->data['id'];
231
232 if (strlen($name) == 0 || $name == '')
233 return false;
234
235 if ($button_id <= 0)
236 return false;
237
238 $sql = $wpdb->prepare("SELECT id from $table where name = %s and status = 'publish' and id <> %d ", $name, $button_id);
239
240 $results = $wpdb->get_col($sql);
241
242 if (count($results) > 0)
243 {
244 $message = __('Button name already used. Using non-unique names with the shortcode can cause issues', 'maxbuttons');
245 $message .= ' ' . __('Already used in : ');
246 foreach($results as $id)
247 {
248 $url = admin_url() . 'admin.php?page=maxbuttons-controller&action=button&id=' . $id;
249 $message .= ' <a href="' . $url . '" target="_blank">' . $id . '</a> ';
250 }
251
252 return $message;
253 }
254 }
255
256 public function admin_fields()
257 {
258
259 $icon_url = MB()->get_plugin_url() . 'images/icons/';
260 $admin = MB()->getClass('admin');
261
262
263 $start_block = new maxField('block_start');
264 $start_block->name = __('basic', 'maxbuttons');
265 $start_block->label = __('Basics', 'maxbuttons');
266 $admin->addField($start_block);
267
268 $color_copy_self = __("Replace color from other field", "maxbuttons");
269 $color_copy_move = __("Copy Color to other field", "maxbuttons");
270
271 $check_name = $this->check_unique_name(maxBlocks::getValue('name'));
272
273 // Name
274 $field_name = new maxField() ;
275 $field_name->label = __('Button Name', 'maxbuttons');
276 $field_name->value = maxBlocks::getValue('name');
277 $field_name->id = 'name';
278 $field_name->name = $field_name->id;
279 $field_name->placeholder = __("Button Name","maxbuttons");
280
281 if ($check_name)
282 {
283 $field_name->warning = $check_name;
284 //$field_name->error = $check_name;
285 }
286 $admin->addField($field_name, 'start', 'end');
287
288 // URL
289 $field_url = new maxField();
290 $field_url->label = __('URL', 'maxbuttons');
291 // $field_url->note = __('The link when the button is clicked.', 'maxbuttons');
292 $field_url->value = rawurldecode(maxBlocks::getValue('url') );
293 $field_url->id = 'url';
294 $field_url->placeholder = __("http://","maxbuttons");
295 $field_url->name = $field_url->id;
296 $field_url->help = "<p>Enter any URL you wish to link to. </p>
297 <p>Examples: <br><ul class='nowrap'><li> https://example.com/ </li>
298 <li> /local-page/ </li>
299 <li> javascript:window.history.back(); </li></ul></p>
300 <p class='shortcode'> Shortcode attribute : url </p> ";
301 $admin->addField($field_url,'start');
302
303 $url_button = new maxField('button');
304 $url_button->id = 'url_button';
305 $url_button->name = $url_button->id;
306 $url_button->button_label = __('Select Site Content', 'maxbuttons');
307 $admin->addField($url_button, '', 'end');
308
309 $url_nonce = new maxField('hidden');
310 $url_nonce->id = '_ajax_linking_nonce';
311 $url_nonce->name = $url_nonce->id;
312 $url_nonce->value = wp_create_nonce('internal-linking');
313
314 $admin->addField($url_nonce);
315
316 // Spacer
317 $fspacer = new maxField('spacer');
318 $fspacer->name = 'url_options';
319 $fspacer->label = '&nbsp;';
320 $admin->addField($fspacer, 'start');
321
322 // New Window
323 $fwindow = new maxField('checkbox');
324 $fwindow->label = __('Open in New Window', 'maxbuttons');
325 $fwindow->name = 'new_window';
326 $fwindow->id = $fwindow->name;
327 $fwindow->value = 1;
328 $fwindow->checked = checked( maxBlocks::getValue('new_window'), 1, false);
329 $admin->addField($fwindow);
330
331 // NoRel
332 $ffollow = new maxField('checkbox');
333 $ffollow->label = __('Use rel="nofollow"', 'maxbuttons');
334 $ffollow->value = 1;
335 $ffollow->name = 'nofollow';
336 $ffollow->id = $ffollow->name;
337 $ffollow->checked = checked( maxBlocks::getValue('nofollow'), 1, false);
338 $admin->addField($ffollow, '','end');
339 // TITLE
340
341 $field_title = new maxField();
342 $field_title->label = __('Button Tooltip', 'maxbuttons');
343 $field_title->name = 'link_title'; // title is too generic
344 $field_title->id = $field_title->name;
345 $field_title->value = maxBlocks::getValue('link_title');
346 $field_title->help = __('<p>This text will appear when hovering over the button</p>
347 <p class="shortcode">Shortcode attribute : linktitle</p>', 'maxbuttons');
348 $admin->addField($field_title, 'start', 'end');
349
350 // TEXT
351 $field_text = new maxField();
352 $field_text->label = __('Text','maxbuttons');
353 $field_text->name = 'text';
354 $field_text->id = 'text';
355 $field_text->value = maxBlocks::getValue('text') ;
356 $admin->addField($field_text, 'start', 'end');
357
358 // FONTS
359 $fonts = MB()->getClass('admin')->loadFonts();
360
361 $field_font = new maxField('option_select');
362 $field_font->label = __('Font','maxbuttons');
363 $field_font->name = 'font';
364 $field_font->id = $field_font->name;
365 $field_font->selected = maxBlocks::getValue('font');
366 $field_font->options = $fonts;
367
368 $admin->addField($field_font,'start');
369 ?>
370
371 <?php
372 // FONT SIZE
373 //global $maxbuttons_font_sizes;
374 $sizes = apply_filters('mb/editor/fontsizes', maxUtils::generate_font_sizes(10,50) );
375
376
377 $field_size = new maxField('number');
378 $field_size->name = 'font_size';
379 $field_size->id= $field_size->name;
380 $field_size->inputclass = 'tiny';
381 $field_size->min = 8;
382 $field_size->after_input = __('px', 'maxbuttons');
383 $field_size->value = maxUtils::strip_px(maxBlocks::getValue('font_size'));
384 $admin->addField($field_size);
385
386 // Font style checkboxes
387 $fweight = new maxField('checkbox');
388 $fweight->icon = 'dashicons-editor-bold';
389 $fweight->title = __("Bold",'maxbuttons');
390 $fweight->id = 'check_fweight';
391 $fweight->name = 'font_weight';
392 $fweight->value = 'bold';
393 $fweight->inputclass = 'check_button icon';
394 $fweight->checked = checked( maxBlocks::getValue('font_weight'), 'bold', false);
395 $admin->addField($fweight, 'group_start');
396
397 $fstyle = new maxField('checkbox');
398 $fstyle->icon = 'dashicons-editor-italic';
399 $fstyle->title = __("Italic",'maxbuttons');
400 $fstyle->id = 'check_fstyle';
401 $fstyle->name = 'font_style';
402 $fstyle->value = 'italic';
403 $fstyle->inputclass = 'check_button icon';
404 $fstyle->checked = checked( maxBlocks::getValue('font_style'), 'italic', false);
405 $admin->addField($fstyle, '', 'group_end');
406
407 $falign_left = new maxField('radio');
408 $falign_left->icon = 'dashicons-editor-alignleft';
409 $falign_left->title = __('Align left','maxbuttons');
410 $falign_left->id = 'radio_talign_left';
411 $falign_left->name = 'text_align';
412 $falign_left->value = 'left';
413 $falign_left->inputclass = 'check_button icon';
414 $falign_left->checked = checked ( maxblocks::getValue('text_align'), 'left', false);
415 $admin->addField($falign_left, 'group_start');
416
417 $falign_center = new maxField('radio');
418 $falign_center->icon = 'dashicons-editor-aligncenter';
419 $falign_center->title = __('Align center','maxbuttons');
420 $falign_center->id = 'radio_talign_center';
421 $falign_center->name = 'text_align';
422 $falign_center->value = 'center';
423 $falign_center->inputclass = 'check_button icon';
424 $falign_center->checked = checked( maxblocks::getValue('text_align'), 'center', false);
425 $admin->addField($falign_center);
426
427 $falign_right = new maxField('radio');
428 $falign_right->icon = 'dashicons-editor-alignright';
429 $falign_right->title = __('Align right','maxbuttons');
430 $falign_right->id = 'radio_talign_right';
431 $falign_right->name = 'text_align';
432 $falign_right->value = 'right';
433 $falign_right->inputclass = 'check_button icon';
434 $falign_right->checked = checked( maxblocks::getValue('text_align'), 'right', false);
435 $admin->addField($falign_right, '', array('group_end','end') );
436
437 // Padding - trouble
438 $ptop = new maxField('number');
439 $ptop->label = __('Padding', 'maxbuttons');
440 $ptop->id = 'padding_top';
441 $ptop->name = $ptop->id;
442 $ptop->min = 0;
443 $ptop->inputclass = 'tiny';
444 $ptop->before_input = '<img src="' . $icon_url . 'p_top.png" title="' . __("Padding Top","maxbuttons") . '" >';
445 $ptop->value = maxUtils::strip_px(maxBlocks::getValue('padding_top'));
446 $admin->addField($ptop,'start');
447
448 $pright = new maxField('number');
449 $pright->id = 'padding_right';
450 $pright->name = $pright->id;
451 $pright->min = 0;
452 $pright->inputclass = 'tiny';
453 $pright->before_input = '<img src="' . $icon_url . 'p_right.png" class="icon padding" title="' . __("Padding Right","maxbuttons") . '" >';
454 $pright->value = maxUtils::strip_px(maxBlocks::getValue('padding_right'));
455 $admin->addField($pright);
456
457 $pbottom = new maxField('number');
458 $pbottom->id = 'padding_bottom';
459 $pbottom->name = $pbottom->id;
460 $pbottom->min = 0;
461 $pbottom->inputclass = 'tiny';
462 $pbottom->before_input = '<img src="' . $icon_url . 'p_bottom.png" class="icon padding" title="' . __("Padding Bottom","maxbuttons") . '" >';
463 $pbottom->value = maxUtils::strip_px(maxBlocks::getValue('padding_bottom'));
464 $admin->addField($pbottom);
465
466 $pleft = new maxField('number');
467 $pleft->id = 'padding_left';
468 $pleft->name = $pleft->id;
469 $pleft->min = 0;
470 $pleft->inputclass = 'tiny';
471 $pleft->before_input = '<img src="' . $icon_url . 'p_left.png" class="icon padding" title="' . __("Padding Left","maxbuttons") . '" >';
472 $pleft->value = maxUtils::strip_px(maxBlocks::getValue('padding_left'));
473 $admin->addField($pleft,'', 'end');
474
475
476 // Text Color
477 $fcolor = new maxField('color');
478 $fcolor->id = 'text_color';
479 $fcolor->name = $fcolor->id;
480 $fcolor->value = maxBlocks::getColorValue('text_color');
481 $fcolor->label = __('Text Color','maxbuttons');
482 $fcolor->copycolor = true;
483 $fcolor->bindto = 'text_color_hover';
484 $fcolor->copypos = 'right';
485 $fcolor->right_title = $color_copy_move;
486 $fcolor->left_title = $color_copy_self;
487 $admin->addField($fcolor, 'start');
488
489 // Text Color Hover
490 $fcolor_hover = new maxField('color');
491 $fcolor_hover->id = 'text_color_hover';
492 $fcolor_hover->name = $fcolor_hover->id;
493 $fcolor_hover->value = maxBlocks::getColorValue('text_color_hover');
494 $fcolor_hover->label = __('Text Color Hover','maxbuttons');
495 $fcolor_hover->copycolor = true;
496 $fcolor_hover->bindto = $fcolor->id;
497 $fcolor_hover->copypos = 'left';
498 $fcolor_hover->right_title = $color_copy_self;
499 $fcolor_hover->left_title = $color_copy_move;
500
501 $admin->addField($fcolor_hover, '','end');
502
503 // Dimension : width
504 $field_width = new maxField('number');
505 $field_width->label = __('Button Width','maxbuttons');
506 $field_width->name = 'button_width';
507 $field_width->id = $field_width->name;
508 $field_width->inputclass = 'small';
509 $field_width->min = 0;
510 $field_width->after_input = __('px', 'maxbuttons');
511 $field_width->value= maxUtils::strip_px(maxBlocks::getValue('button_width')); // strippx?
512
513 $admin->addField($field_width, 'start');
514
515 // Dimension : height
516 $field_height = new maxField('number');
517 $field_height->label = __('Button Height','maxbuttons');
518 $field_height->name = 'button_height';
519 $field_height->id = $field_height->name;
520 $field_height->inputclass = 'small';
521 $field_height->min = 0;
522 $field_height->after_input = __('px', 'maxbuttons');
523 $field_height->help = __('Width and Height are optional. When set to 0,button size will be determined by text size plus padding', 'maxbuttons');
524 $field_height->value= maxUtils::strip_px(maxBlocks::getValue('button_height')); // strippx?
525
526 $admin->addField($field_height, '', 'end');
527
528 // Description
529 $description_hide = get_option('maxbuttons_hidedescription');
530 if ($description_hide == 1)
531 $field_desc = new maxField('hidden');
532 else
533 $field_desc = new maxField('textarea');
534
535 $field_desc->label = __('Description', 'maxbuttons');
536 $field_desc->name = 'description';
537 $field_desc->id = $field_desc->name;
538 $field_desc->esc_function = 'esc_textarea';
539 $field_desc->value = maxBlocks::getValue('description') ;
540 $field_desc->placeholder = __('Brief explanation about how and where the button is used.','maxbuttons');
541
542 $admin->addField($field_desc, 'start', 'end');
543
544 $this->sidebar();
545 $endblock = new maxField('block_end');
546 $admin->addField($endblock);
547
548 } // admin_display
549
550 } // class
551
552 ?>
553