# share-button/0.9/classes/blocks/count_block.php

Social Share Buttons, version 0.9. 260 lines.

- Page: https://pluginprobe.com/plugins/share-button/0.9/code/classes/blocks/count_block.php
- Raw: https://pluginprobe.com/plugins/share-button/0.9/raw/classes/blocks/count_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/count_block.php#L10-L20`.

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

$collectionBlock["count"] = array('class' => "countBlock",
				 'order' => 50);

use \MaxButtons\maxField as maxField;
use \MaxButtons\maxBlocks as maxBlocks;
use \MaxButtons\maxUtils as maxUtils;

class countBlock extends block
{

	protected $blockname = "count";
	protected $fields = array( 'count_active' => array('default' => 0),
				   'share_min_count' => array('default' => '5'),
				   'show_total_count' => array('default' => '0'),
				   'total_count_label' => array('default' => 'Shares'),

							 );

	protected static $total = 0;


	public function parse($itemObj, $args = array() )
	{
		$blockdata = $this->data[$this->blockname];

		$active = isset($blockdata['count_active']) ? $blockdata['count_active'] : false;
		$share_min_count = isset($blockdata['share_min_count']) ? $blockdata['share_min_count'] : 0;

		$network = $args['network'];

		// this can use some streamlining
		$network_block = $this->collection->getBlock('networkBlock');
		$share_data = $network_block->getShareData();
		$share_url = esc_url($share_data['url']);

		if ( ( $active == 1 || $this->showTotal() )  && $network->isCountable() )
		{

			if ($args['preview'] == true)
			{
				$share_min_count = 0;
				$share_count = round(rand(1,9));

			}

			else
				$share_count = $network->getShareCount(array(
							'url' => $share_url));

			if ($active && $share_count > $share_min_count)
			{
				$anchor = $itemObj->find('a', 0);
				$anchor->class .= ' with-count';

				//$icon = $itemObj->find('.mb-icon-wrapper', 0);

				$anchor->innertext .= "<span class='mb-share-count'>" . $this->format_count($share_count) . "</span>";
			}
			elseif ( $share_count === false)  // request remote count
			{
				//$network_block = $this->collection->getBlock('networkBlock');

		 		//$share_url = $network->get('share_url');
		 		$share_url = '';

		 		if ($network_block)
		 		{
		 			$share_data = $network_block->getShareData();
		 			$page_url = $share_data['url'];
		 		}

				$tag = "data-onload";

				$json = array("network" => $network->get('network') ,
							  "share_url" => esc_url($page_url),
							  "count_threshold" => $share_min_count,
							 );
				$item = $itemObj->find('.collection-item', 0);
				$item->$tag =  htmlentities(json_encode($json), ENT_QUOTES, 'UTF-8');

			}

			if (is_numeric($share_count) && $share_count > 0)
			{

				self::$total += $share_count;
			}
		}

		$itemObj->load($itemObj->save());
		return $itemObj;
	}

	public function get_total($format = true)
	{
		if ($format)
		{
				return $this->format_count(self::$total);
		}
		else
			return self::$total;
	}

	public function showTotal()
	{

		$show_total = isset($this->data[$this->blockname]['show_total_count']) ? $this->data[$this->blockname]['show_total_count'] : false;
		if ($show_total == 1)
			return true;

		return false;
	}

	public function set($data)
	{
		$data = parent::set($data);

		$this->resetTotal(); // reset total counter

		return true;

	}

	/* Format share counts to proper values for display
	* About 10,000 => 10K
	*/
	public function format_count($count)
	{
			// must be a number
		if ( ! is_numeric($count) )
			return $count;

		// must be significant
		if ($count < 10000)
				return $count;


		if ($count >= 1000000)
			 	$count = round($count / 1000000) . __("M",'mbsocial');
		elseif ($count >= 10000)
		 	$count = round($count/1000) . __("K", 'mbsocial');

			$count = apply_filters('mbsocial/count/format', $count);
			return $count;
	}

	public function resetTotal()
	{
		self::$total = 0;

	}

	public function ajax_get_count($result, $data)
	{
		$share_url = sanitize_text_field($data['share_url']);
		$network_name = sanitize_text_field($data['network']);
		$network = MBSocial()->networks()->get($network_name);

 		// check if share count appeared in the cache. If this is the case it's possible another process put it there.
 		$do_remote = true;
 		$share_count = $network->getShareCount(array("url" => $share_url));
 		if ($share_count !== false)
 		{
 			$count = $share_count;
 			$do_remote = false; // stop annoying other servers
 		}

 		if ($do_remote)
 		{
	 		// returns false, a number or 'locked' in case of locked.
	 		$count = $network->getRemoteShareCount($share_url);

			/* If the request is lock, another request is probably asking for this and shouldn't take long.
				Try a maximum of 5 times to prevent server process hanging until php times out ( we want to prevent that ) */
	 		if ($count == 'locked')
	 		{
	 			if ($this->ajax_remote_count < 5)
	 			{
	 				sleep(1); // in seconds please.
	 				// after retry result here will at some point have the count data, extract and return at the end.
	 				$result = $this->ajax_get_count($result,$data);
	 				$count = $result["data"]["count"];
	 			}
	 			else
	 				$count = "TIMEOUT";
	 			$this->ajax_remote_count++;
	 		}
 		}

		$count = $this->format_count($count);
 		$result["data"] = array('count' => intval($count) );

 		return $result;

	}

	public function admin()
	{
?>
	<div id='countBlock' class='options option-container count' data-refresh='styleBlock'>
		<div class='title'><?php _e('Share Count Options'); ?></div>
		<div class='inside'>
	<?php
		$blockdata = $this->data[$this->blockname];
		$admin = MBSocial()->admin();

		$active = new maxField('switch');
		$active->id = 'count_active';
		$active->name = $active->id;
		$active->value = 1;
		$active->label = __('Show Share Counts', 'mbsocial');
		$active->checked = checked( maxBlocks::getValue('count_active'), 1, false);
		$active->publish = false;

		$admin->addField( $active, 'start','end');

		$min_count = new maxField('number');
		$min_count->id = 'share_min_count';
		$min_count->min = 0;
		$min_count->name = $min_count->id;
		$min_count->inputclass = 'tiny';
		$min_count->value = maxBlocks::getValue('share_min_count');
 		$min_count->label = __('Minimum count to show shares','mbsocial');

		$admin->addField( $min_count, 'start', 'end');


		$total_count = new maxField('switch');
		$total_count->id = 'show_total_count';
		$total_count->name = $total_count->id;
		$total_count->label = __('Show total count', 'mbsocial');
		$total_count->value = 1;
		$total_count->checked = checked (maxBlocks::getValue('show_total_count'), 1, false);

		$admin->addField($total_count, 'start', 'end');

		$tcount_label = new maxField();
		$tcount_label->id = 'total_count_label';
		$tcount_label->name = $tcount_label->id;
		$tcount_label->label = __('Total Count Label', 'mbsocial');
		$tcount_label->inputclass = 'medium';
		$tcount_label->value = maxBlocks::getValue('total_count_label');

		$admin->addField($tcount_label, 'start', 'end');

		$admin->display_fields();

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

	}

} // class

```
