PluginProbe
MaxButtons – Create buttons / 7.13
MaxButtons – Create buttons v7.13
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.13, at blocks/basic.php

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