array("default" => ''), "status" => array("default" => "publish"), "description" => array("default" => ''), "url" => array("default" => ''), 'link_title' => array('default' => ''), // "text" => array("default" => ''), "new_window" => array("default" => 0), "nofollow" => array("default" => 0) ); 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 function __construct() { parent::__construct(); $extra_protocols = get_option('maxbuttons_protocol'); $extra_protocols = array_map('trim', array_filter(explode(',', $extra_protocols))); if (is_array($extra_protocols) && count($extra_protocols) > 0) { $this->protocols = array_merge($this->protocols, $extra_protocols); } } public function parse_css($css, $mode = 'normal') { // emtpy string init is not like by PHP 7.1 if (! is_array($css)) $css = array(); $data = $this->data[$this->blockname]; $css["maxbutton"]["normal"]["position"] = "relative"; $css["maxbutton"]["normal"]["text-decoration"] = "none"; // $css["maxbutton"]["normal"]["white-space"] = "nowrap"; // hinders correct rendering of oneline-multilines $css["maxbutton"]["normal"]["display"] = "inline-block"; $css["maxbutton"]["normal"]["vertical-align"] = 'middle'; /*if (isset($data["url"]) && $data["url"] == '') // don't show clickable anchor if there is no URL. { $css["maxbutton"]["normal"]["cursor"] = 'default'; // $css[":hover"]["cursor"] = 'default'; } */ return $css; } public function save_fields($data, $post) { // Possible solution: // $post["url"] = isset($post["url"]) ? urldecode(urldecode($post["url"])) : ''; $description = false; if (isset($post["description"]) && $post["description"] != '') { $description = str_replace("\n", '-nwline-', $post["description"]); $description = sanitize_text_field($description); $description = str_replace('-nwline-', "\n", $description); } $data = parent::save_fields($data, $post); // bypass sanitize for description - causing the end of line-breaks if ($description) $data["basic"]["description"] = $description; // bypassing sanitize text field - causes problems with URLs and spaces $url = isset($post["url"]) ? trim($post["url"]) : ''; $parsed_url = parse_url($url); $rawEncode = array("query","fragment"); foreach($rawEncode as $item) { if (isset($parsed_url[$item])) { $parsed_url[$item] = rawurlencode($parsed_url[$item]); } } $url = $this->unParseURL($parsed_url); $url = str_replace(" ", "%20", trim($url) ); if (! $this->checkRelative($parsed_url)) $url = esc_url_raw($url, $this->protocols); // str replace - known WP issue with spaces $data[$this->blockname]["url"] = $url; if (isset($post["name"])) $data["name"] = sanitize_text_field($post["name"]); if (isset($post["status"])) $data["status"] = sanitize_text_field($post["status"]); // for conversion old - new. return $data; } protected function unparseURL($parsed_url) { // Don't add // to these schemes $noslash_schemes = array('javascript', 'mailto', 'tel', 'sms'); if (isset($parsed_url['scheme']) && in_array($parsed_url['scheme'], $noslash_schemes) ) $scheme = $parsed_url["scheme"] . ":"; else $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; $pass = ($user || $pass) ? "$pass@" : ''; $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; return "$scheme$user$pass$host$port$path$query$fragment"; } /* Check for a relative URL that gets killed by esc_url ( if there is no / first ) */ protected function checkRelative($parsed_url) { if (! isset($parsed_url['host']) && ! isset($parsed_url['scheme']) ) { if (isset($parsed_url['path']) && $parsed_url['path'] !== '' && substr($parsed_url['path'], 0,1) !== '/') { return true; } } return false; } public function parse_button($domObj, $mode = 'normal') { $data = $this->data[$this->blockname]; $button_id = $this->data["id"]; $rels = array(); $anchor = $domObj->find("a",0); if (isset($data["nofollow"]) && $data["nofollow"] == 1) { $rels[] = 'nofollow'; $rels[] = 'noopener'; } // $anchor->rel = "nofollow"; // $buttonAttrs[] = "rel=nofollow"; if (isset($data["new_window"]) && $data["new_window"] == 1) { $anchor->target = "_blank"; if (! in_array('noopener', $rels)) $rels[] = 'noopener'; } if (isset($data['link_title']) && strlen($data['link_title']) > 0) $anchor->title = $data['link_title']; $rels = apply_filters('mb/button/rel', $rels); if (count($rels) > 0) { $anchor->rel = implode(' ', $rels); } if (isset($data["url"]) && $data["url"] != '') { $url = $data["url"]; $parsed_url = parse_url($url); if (! $this->checkRelative($parsed_url)) $url = esc_url($url, $this->protocols); $url = rawurldecode($url); // removes the + from a URL part. $url = apply_filters('mb-url', $url, $data['url']); // passes processed url / raw url. $url = apply_filters('mb-url-' . $button_id, $url, $data['url']); $anchor->href = $url; //do_shortcode( esc_url($url, $this->protocols) ); } else // fixing an iOS problem which renders anchors without URL wrongly. { $anchor->href = 'javascript:void(0);'; } return $domObj; } public function map_fields($map) { $map["url"]["attr"] = "href"; $map["link_title"]["attr"] = "title"; // $map["text"]["func"] = "updateAnchorText"; return $map; } public function admin_fields() { $icon_url = MB()->get_plugin_url() . 'images/icons/'; $admin = MB()->getClass('admin'); ?>