# share-button/0.9/classes/blocks/network-block.php

Social Share Buttons, version 0.9. 426 lines.

- Page: https://pluginprobe.com/plugins/share-button/0.9/code/classes/blocks/network-block.php
- Raw: https://pluginprobe.com/plugins/share-button/0.9/raw/classes/blocks/network-block.php
- Modified: 2017-11-22T15:53:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/share-button/0.9/code/classes/blocks/network-block.php#L10-L20`.

```php
<?php
namespace MBSocial;
defined('ABSPATH') or die('No direct access permitted');

$collectionBlock["network"] = array('order' => 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 "<PRE>"; print_R($domObj); echo "</PRE>";

		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 .= "<span class='mb-collection-item item-$button_id ' data-doc-id='" . $document_id . "'>" . $button->display($button_args) . "</span>";


		}

		$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}", "<span class='share_count'>" . $count . "</span>", $string);
	 		$string = str_replace("{c}", "<span class='share_count'>" . $count . "</span>", $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();

	?>
		<div class='help-side'>
			<h3><?php _e('Social Networks','mbsocial'); ?></h3>
			<p>Drag and drop the networks you want to use into the 'active' box. You can find additional networks under the 'more networks' tab.</p>
			<p>Hold the mouse pointer over the network icon to see the network's name</p>
		</div>
		<div class='options networks option-container' id='networkBlock' data-refresh='styleBlock'>
		<?php
			$field_obj = new maxField('hidden');
			$field_obj->id = 'network_trigger_change';
			$field_obj->name = $field_obj->id;
			$field_obj->value = '';
			$field_obj->placeholder = '';

			$admin->addField($field_obj);
			$admin->addUpdate($field_obj);
			$admin->display_fields();

		?>
			<div class='title'><?php _e('Social Networks','mbsocial') ?> </div>
			<div class='inside'>
				<div class='option active_network'>
					<label><?php _e('Active','mbsocial'); ?></label>
					<div class='drag-area input' data-area='active'>
						<?php if (isset($networks['selected'])): foreach($networks['selected'] as $index => $network): ?>

						<?php $network_name = $network->get('network');
							$icon_output = $styleObj->renderIcon($network);

						?>
				<span class="item <?php echo $network_name ?>">
				<?php echo $icon_output ?>

					<input type='hidden' name='network_item_active[]' value='<?php echo $network_name ?>'>

				</span>

						<?php endforeach; endif; ?>
					</div>
				</div>


				<div class='option inactive_network'>
					<label><?php _e('Inactive', 'mbsocial'); ?></label>
					<div class='drag-area input' data-area='inactive'>
						<?php if (isset($networks['unselected'])): foreach($networks['unselected'] as $index => $network): ?>

						<?php $network_name = $network->get('network');
							$icon_output = $styleObj->renderIcon($network);
						?>
						<span class="item <?php echo $network_name ?>">
							<?php echo $icon_output ?>
							<input type='hidden' name='network_item[]' value='<?php echo $network_name ?>'>
						</i>
						</span>

						<?php endforeach; endif; ?>
					</div>
				</div>

				<div class='option'>
					<label>&nbsp;</label>
					<div class='see-more toggle input'><span class='title'><?php _e('more networks', 'mbsocial'); ?></span>
						<span class='see-more-wrap option toggle-target'>

							<div class='drag-area'>
								<?php if (isset($networks['readmore'])): foreach($networks['readmore'] as $index => $network): ?>

								<?php $network_name = $network->get('network');
									$icon_output = $styleObj->renderIcon($network);
								?>

								<span class="item <?php echo $network_name ?>" title="<?php echo $network_name ?>">
								<?php echo $icon_output ?>
								<input type='hidden' name='network_item[]' value='<?php echo $network_name ?>'>


								</span>

								<?php endforeach; endif; ?>
							</div>
							<p><?php _e("Need another network?","mbsocial"); ?>
							   <a href='mailto:support@maxfoundry.com'><?php _e('Email us','mbsocial'); ?> </a></p>
						</span>

					</div>

				</div>
			</div> <!-- inside -->
		</div> <!-- option-container -->
	<?php
	}

}

?>

```
