10, 'class' => "networkBlock"); use \MaxButtons\simple_html_dom as simple_html_dom; use \MaxButtons\maxField as maxField; use \MaxButtons\maxBlocks as maxBlocks; use \MaxButtons\maxInstall as maxInstall; class networkBlock extends block { protected $blockname = "network"; protected $fields = array( 'network_active' => array('default' => array() ) ); protected $meta_fields = array( 'share_url' => array('default' => ''), ); protected $share_data; protected $index_helper = -1; public function __construct() { parent::__construct(); MBSocial()->whistle()->listen('display/parse/network', array($this, 'setShareData'), 'tell'); } public function parse($domObj, $args = array() ) { $w = MBSocial()->whistle(); $output = ''; $blockdata = $this->data[$this->blockname]; $admin = MBSocial()->admin(); $network = $w->ask('display/parse/network'); $is_popup = $network->is_popup(); $itemObj = $domObj->find('a', 0); $url = $itemObj->href; // replace all {} variables with data $url = $admin->applyVars($url); // remove empty query parts from the URL $parts = parse_url($url); if(isset($parts['query']) > 0) { parse_str($parts['query'], $query); foreach($query as $query_arg => $query_val ) { if (strlen($query_val) == 0) $url = remove_query_arg($query_arg, $url); } } $itemObj->href = $url; if ($is_popup) { $tag = 'data-popup'; $dimensions = $network->get('popup_dimensions'); $dimensions = array('width' => $dimensions[0], 'height' => $dimensions[1]); $itemObj->$tag = htmlentities(json_encode($dimensions)); } else { if (! $network->get('forcesamewindow')) $itemObj->target = "_blank"; } $itemObj->rel = 'noopener nofollow'; $icon = $this->renderIcon(); if ($icon) { $obj = $domObj->find('.mb-social', 0); if (is_object($obj)) { $obj->innertext .= ""; $this->collection->getStyle()->addDisplay('icon'); } } return $domObj; } public function renderIcon($network = false) { $w= MBSocial()->whistle(); if (! $network) $network = $w->ask('display/parse/network'); $icon = $network->get('icon'); $icon_type = $network->get('icon_type'); $network_name = $network->get_nice_name(); $icon_size = false; if ($this->collection) { $layoutBlock = $this->collection->getBlock('layoutBlock'); $icon_size = $layoutBlock->getValue('font_icon_size'); } $args = array('icon' => $icon, 'icon_type' => $icon_type, 'title' => $network_name, 'icon_size' => $icon_size ); if ($icon_type == 'image') { $args['image_id'] = $network->get('icon_image_id'); $args['image_size'] = $network->get('icon_image_size'); $args['image_url'] = $network->get('icon_image_url'); } return MBSocial()->admin()->renderIcon($args); } /** Get data from the page to use in sharing. Like page title, url and image **/ public function setShareData() { if (! is_null($this->share_data)) return $this->share_data; $w = MBSocial()->whistle(); $url = ''; $title = ''; $img = ''; $hook = $w->ask('display/env/hook'); // $this->collection->getHook(); $post_id = $w->ask('display/env/post_id'); $share_setting = 'auto'; if ( isset($this->data['display'][$hook . '_options'])) { $options = $this->data['display'][$hook . '_options']; $share_setting = isset($options['share_setting']) ? $options['share_setting'] : 'auto'; $custom = isset($options['share_custom_url']) ? $options['share_custom_url'] : ''; $custom_title = isset($options['share_custom_title']) ? $options['share_custom_title'] : ''; } switch($share_setting) { case "home": $url = get_home_url(); $title = get_bloginfo('name'); $img = $this->getImage(null, true); break; case "custom-url"; $url = $custom; $title = $custom_title; $img = $this->getImage(); break; case "current-post": $url = get_permalink(); $title = get_the_title(); $img = $this->getImage(); break; case "auto": default: /* After / before being placed on the post data, therefore by default (auto) always use the POST URL Static is being placed once(1) on the page, so get the greated to-share url ( of the whole section ) */ if ($this->collection->is_post) { $url = get_permalink(); $title = get_the_title(); $img = $this->getImage(); } if ($this->collection->is_static) { if (is_front_page()) { $url = get_home_url(); $title = get_bloginfo('name'); $img = $this->getImage(null, true); } elseif (is_category() ) { $obj = get_queried_object(); $category_id = $obj->cat_ID; $url = get_category_link($category_id); $title = $obj->category_nicename; } else // are there any other cases? Page should give back their main url like this. { $url = get_permalink(); $title = get_the_title(); $img = $this->getImage(); } } break; } // Check for custom URL settings if ($post_id > 0) { $metadata = $this->get_block_meta_data($post_id); if (isset($metadata['share_url']) && strlen( trim($metadata['share_url']) ) > 0 ) { $url = $metadata['share_url']; } } $data = array("url" => $url, "title" => $title, "img" => $img); $w->tell('display/vars/url', $url); $w->tell('display/vars/title', $title); $w->tell('display/vars/img', $img); $this->share_data = $data; return $data; } protected function getImage($post_id = null, $home = false) { if (is_admin()) return ''; // in admin all this is not set. if ($home && get_option('show_on_front') === 'page') { $post_id = get_option('page_on_front'); } $thumb_id = get_post_thumbnail_id($post_id); // First try to find thumbnail on the content $image = false; if (is_numeric($thumb_id)) { $image = wp_get_attachment_url($thumb_id); } if ($image !== false) return $image; // In case of no thumbnails, tries to find first image from current content. // if (! is_null($post_id)) // { $post = get_post($post_id); $content = ''; if (! is_null($post)) $content = $post->post_content; /* } else { $content = get_the_content(); } */ $domObj = new simple_html_dom(); $domObj->load($content); $img = $domObj->find('img', 0); if ($img) { $image = $img->src; return $image; } else return ''; } function save_fields($data, $post) { $data = parent::save_fields($data, $post); $selection = array(); $button = MB()->getClass("button"); $network_active = isset($post['network_item_active']) ? $post['network_item_active'] : array(); // Dummy is to ensure conditionals work on network_active. $index = array_search('dummy', $network_active); if ($index !== false) unset($network_active[$index]); //mbcustom $mbcustom = isset($post['mbcustom_id']) ? $post['mbcustom_id'] : array(); $data[$this->blockname]['mbcustom'] = array(); if (is_array($mbcustom)) { $i = 0; foreach($mbcustom as $index => $button_id) { if ( intval($button_id) > 0) { $usenetwork = isset($post['mbcustom_usenetwork'][$index]) ? intval($post['mbcustom_usenetwork'][$index]) : 0; $network = isset($post['mbcustom_network'][$index]) ? sanitize_text_field($post['mbcustom_network'][$index]) : ''; $url = isset($post['mbcustom_url'][$index]) ? sanitize_text_field($post['mbcustom_url'][$index]) : ''; $text = isset($post['mbcustom_text'][$index]) ? sanitize_text_field($post['mbcustom_text'][$index]) : ''; $data[$this->blockname]['mbcustom'][$i] = array( 'button_id' => $button_id, 'use_network' => $usenetwork, 'network' => $network, 'url' => $url, 'text' => $text, ); $i++; // this is not index. Start saving from index 0. } } } // new user, give default networks $collection_id = $this->collection->getID(); if (count($network_active) == 0 && $collection_id == 0) $network_active = array('facebook', 'twitter'); // default options $data[$this->blockname]["network_active"] = $network_active; return $data; } public function do_meta_boxes($content, $post) { $admin = mbSocial()->admin(); $metadata = $this->get_block_meta_data($post->ID); $url = new maxField(); $url->id = 'share_url'; $url->name = $url->id; $url->label = __('URL to Share', 'mbsocial'); $url->note = __('By default the URL of the post is being shared. Leave empty for default', 'mbsocial'); $url->value = isset($metadata['share_url']) ? $metadata['share_url'] : ''; $url->placeholder = 'https://'; $admin->addField($url, 'start', 'end'); $fields = $admin->display_fields(true, true, false); $content['share'] = array('title' => __('Share Options', 'mbsocial'), 'icon' => 'alt-share', 'content' => $fields, ); return $content; } public function admin() { $w = mbSocial()->whistle(); $admin = mbSocial()->admin(); $blockdata = $this->data[$this->blockname]; $active_networks = isset($blockdata['network_active']) ? $blockdata['network_active'] : array(); $networks = $admin->get_networks($active_networks); $styleObj = $this->collection->getStyle(); /* No clue what this is doing here. foreach ($networks as $type => $data) { echo $type; var_dump($data); foreach ($data as $network_id => $network) { $label = $network->get_nice_name(); $icon = $network->get('icon'); $icon_type = $network->get('icon_type'); $color = $network->get('color'); $field = new maxField('switch'); $field->id = $network->get('name'); $field->label = $label; $admin->addField($field, 'start', 'end'); } } */ ?>
', ''); ?>
The Share Page networks will share your website's URL to other networks.
The Share Profile networks will link from the website to your social accounts