10, 'class' => "networkBlock"); use \simple_html_dom as simple_html_dom; use \MaxButtons\maxField as maxField; use \MaxButtons\maxBlocks as maxBlocks; 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; public function parse($domObj, $args = array() ) { $output = ''; $blockdata = $this->data[$this->blockname]; $admin = MBSocial()->admin(); //$share_data = $this->getShareData(); $itemObj = $domObj->find('a', 0); $itemObj->href = $this->applyVars($itemObj->href); $network = $args['network']; $popup = $network->get('popup'); if ($popup) { $tag = 'data-popup'; $dimensions = $network->get('popup_dimensions'); $dimensions = array('width' => $dimensions[0], 'height' => $dimensions[1]); $itemObj->$tag = htmlentities(json_encode($dimensions)); } /* Not working like this. if ($network->forMobile() ) { $itemObj->class .= ' for-mobile '; } if ($network->forDesktop() ) { $itemObj->class .= ' for-desktop '; } */ /* $btn_display = ''; $buttons = array(); $active = $blockdata['active']; $networks = $admin->get_networks($active); //echo "
"; print_R($domObj); echo ""; foreach($networks as $index => $network) { } foreach($buttons as $button) { // $button->reloadData(); $button_id = $button->getID(); $document_id = $button->getDocumentID(); $default_args = array("mode" => "normal", "compile" => false, "load_type" => "footer", ); $button_args = wp_parse_args($args, $default_args); $button_args["load_css"] = $button_args["load_type"]; $button_args["echo"] = false; // non-optional. if (isset($args['preview']) && $args["preview"]) $button_args["mode"] = "preview"; // buttons work with mode. $btn_display .= ""; } $collectionObj->innertext = $btn_display; $domObj->load($domObj->save()); */ return $domObj; } /** Get data from the page to use in sharing. Like page title, url and image **/ public function getShareData() { return $this->setShareData(); } public function setShareData() { if (! is_null($this->share_data)) return $this->share_data; $url = ''; $title = ''; $img = ''; $hook = $this->collection->getHook(); $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'] : ''; } switch($share_setting) { case "home": $url = get_home_url(); $title = get_bloginfo('name'); $img = $this->getImage(null, true); break; case "custom-url"; $url = $custom; $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); } else // are there any other cases? Page / Archive should give back their main url like this. { $url = get_permalink(); $title = get_the_title(); $img = $this->getImage(); } } break; } $data = array("url" => $url, "title" => $title, "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 ''; } public function applyVars($string) { $vars = $this->getShareData(); if (isset($vars["url"])) $string = str_replace("{url}", urlencode(esc_url($vars["url"])), $string); if (isset($vars["count"])) { $count = $this->formatCount($vars["count"]); $string = str_replace("{count}", "", $string); $string = str_replace("{c}", "", $string); } if (isset($vars["title"])) $string = str_replace("{title}", $vars["title"], $string); if (isset($vars["network_name"])) $string = str_replace("{network_name}", $vars["network_name"], $string); if (isset($vars["img"])) $string = str_replace("{img}", $vars["img"], $string); return $string; } 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'] : null; // new user, give default networks $collection_id = $this->collection->getID(); if (count($network_active) == 0 && $collection_id == 0) $network_active = array('facebook', 'twitter', 'linkedin'); $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); $content['share'] = array('title' => __('Share Options', 'mbsocial'), 'icon' => 'alt-share', 'content' => $fields, ); return $content; } public function admin() { $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(); ?>
Drag and drop the networks you want to use into the 'active' box. You can find additional networks under the 'more networks' tab.
Hold the mouse pointer over the network icon to see the network's name