| 1 |
<?php |
| 2 |
namespace MBSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
$collectionBlock["count"] = array('class' => "countBlock", |
| 6 |
'order' => 50); |
| 7 |
|
| 8 |
use \MaxButtons\maxField as maxField; |
| 9 |
use \MaxButtons\maxBlocks as maxBlocks; |
| 10 |
use \MaxButtons\maxUtils as maxUtils; |
| 11 |
|
| 12 |
class countBlock extends block |
| 13 |
{ |
| 14 |
|
| 15 |
protected $blockname = "count"; |
| 16 |
protected $fields = array( 'count_active' => array('default' => 0), |
| 17 |
'share_min_count' => array('default' => '5'), |
| 18 |
'show_total_count' => array('default' => '0'), |
| 19 |
'total_count_label' => array('default' => 'Shares'), |
| 20 |
|
| 21 |
'font_count_size' => array('default' => '15px', |
| 22 |
'css' => 'font-size', |
| 23 |
'csspart' => 'mb-share-count', |
| 24 |
), |
| 25 |
"font_count_style" => array("default" => "normal", |
| 26 |
"css" => "font-style", |
| 27 |
"csspart" => 'mb-share-count'), |
| 28 |
"font_count_weight" => array("default" => "normal", |
| 29 |
"css" => "font-weight", |
| 30 |
"csspart" => 'mb-share-count'), |
| 31 |
"total_count_color" => array('default' => '', |
| 32 |
'css' => 'color', |
| 33 |
'csspart' => 'mb-icon-total-icon,mb-count-total,mb-label-total'), |
| 34 |
); |
| 35 |
|
| 36 |
protected static $total = 0; |
| 37 |
|
| 38 |
protected $ajax_remote_count = 0; |
| 39 |
|
| 40 |
|
| 41 |
public function parse($itemObj, $args = array() ) |
| 42 |
{ |
| 43 |
$blockdata = $this->data[$this->blockname]; |
| 44 |
$w = MBSocial()->whistle(); |
| 45 |
|
| 46 |
$active = isset($blockdata['count_active']) ? $blockdata['count_active'] : false; |
| 47 |
$share_min_count = isset($blockdata['share_min_count']) ? $blockdata['share_min_count'] : 0; |
| 48 |
|
| 49 |
// this can use some streamlining |
| 50 |
//$network_block = $this->collection->getBlock('networkBlock'); |
| 51 |
//$share_data = $network_block->getShareData(); |
| 52 |
$share_url = $w->ask('display/vars/url'); //esc_url($share_data['url']); |
| 53 |
|
| 54 |
if ( ( $active == 1 || $this->showTotal() ) && $this->network->isCountable() ) |
| 55 |
{ |
| 56 |
|
| 57 |
if ($args['preview'] == true) |
| 58 |
{ |
| 59 |
$share_min_count = 0; |
| 60 |
$share_count = round(rand(1,9)); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
else |
| 65 |
$share_count = $this->network->getShareCount(array( |
| 66 |
'url' => $share_url)); |
| 67 |
|
| 68 |
if ($active && $share_count > $share_min_count) |
| 69 |
{ |
| 70 |
$anchor = $itemObj->find('.mb-social',0); |
| 71 |
|
| 72 |
$anchor->class .= ' '; |
| 73 |
$anchor->innertext .= "<span class='mb-share-count'>" . $this->format_count($share_count) . "</span>"; |
| 74 |
$this->collection->getStyle()->addDisplay('count'); |
| 75 |
|
| 76 |
} |
| 77 |
elseif ( $share_count === false) // request remote count |
| 78 |
{ |
| 79 |
$tag = "data-onload"; |
| 80 |
|
| 81 |
$json = array("network" => $this->network->get('network') , |
| 82 |
"share_url" => esc_url($share_url), |
| 83 |
"count_threshold" => $share_min_count, |
| 84 |
); |
| 85 |
$item = $itemObj->find('.mb-item', 0); |
| 86 |
$item->$tag = htmlentities(json_encode($json), ENT_QUOTES, 'UTF-8'); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
if (is_numeric($share_count) && $share_count > 0) |
| 91 |
{ |
| 92 |
|
| 93 |
self::$total += $share_count; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$itemObj->load($itemObj->save()); |
| 98 |
return $itemObj; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// create a block for total shares |
| 103 |
public function createTotal($collectionObj, $args = array() ) |
| 104 |
{ |
| 105 |
$w = MBSocial()->whistle(); |
| 106 |
|
| 107 |
if (! $this->showTotal() ) |
| 108 |
{ |
| 109 |
$w->tell('display/env/has_totals', false); |
| 110 |
return; |
| 111 |
} |
| 112 |
$total = $this->get_total(); |
| 113 |
$min_count = $this->getValue('share_min_count'); |
| 114 |
|
| 115 |
if ($total < $min_count) |
| 116 |
{ |
| 117 |
return $collectionObj; |
| 118 |
} |
| 119 |
|
| 120 |
$total_label = $this->getValue('total_count_label'); //$count_block->get_total_label(); |
| 121 |
|
| 122 |
$total_html = '<span class="mb-total-container"> |
| 123 |
<div class="maxbutton-social-total"> |
| 124 |
<div class="mb-icon-total"><i class="mb-icon-total-icon dashicons dashicons-share"></i></div> |
| 125 |
<div class="mb-totals"> |
| 126 |
<div class="mb-count-total">' . $total . '</div> |
| 127 |
<div class="mb-label-total">' . $total_label .'</div> |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
</span>'; |
| 131 |
|
| 132 |
$w->tell('display/env/has_totals', true); |
| 133 |
|
| 134 |
$collectionObj->find('.maxsocial',0)->innertext = $total_html; |
| 135 |
$collectionObj->load($collectionObj->save()); |
| 136 |
return $collectionObj; |
| 137 |
} |
| 138 |
|
| 139 |
public function get_total($format = true) |
| 140 |
{ |
| 141 |
if ($format) |
| 142 |
{ |
| 143 |
return $this->format_count(self::$total); |
| 144 |
} |
| 145 |
else |
| 146 |
return self::$total; |
| 147 |
} |
| 148 |
|
| 149 |
public function showTotal() |
| 150 |
{ |
| 151 |
|
| 152 |
$show_total = isset($this->data[$this->blockname]['show_total_count']) ? $this->data[$this->blockname]['show_total_count'] : false; |
| 153 |
if ($show_total == 1) |
| 154 |
return true; |
| 155 |
|
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
public function set($data) |
| 160 |
{ |
| 161 |
$data = parent::set($data); |
| 162 |
|
| 163 |
$this->resetTotal(); // reset total counter |
| 164 |
|
| 165 |
return true; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
/* Format share counts to proper values for display |
| 170 |
* About 10,000 => 10K |
| 171 |
*/ |
| 172 |
public function format_count($count) |
| 173 |
{ |
| 174 |
// must be a number |
| 175 |
if ( ! is_numeric($count) ) |
| 176 |
return $count; |
| 177 |
|
| 178 |
// must be significant |
| 179 |
if ($count < 10000) |
| 180 |
return $count; |
| 181 |
|
| 182 |
|
| 183 |
if ($count >= 1000000) |
| 184 |
$count = round($count / 1000000) . __("M",'mbsocial'); |
| 185 |
elseif ($count >= 10000) |
| 186 |
$count = round($count/1000) . __("K", 'mbsocial'); |
| 187 |
|
| 188 |
$count = apply_filters('mbsocial/count/format', $count); |
| 189 |
return $count; |
| 190 |
} |
| 191 |
|
| 192 |
public function resetTotal() |
| 193 |
{ |
| 194 |
self::$total = 0; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
public function ajax_get_count($result, $data) |
| 199 |
{ |
| 200 |
$share_url = sanitize_text_field($data['share_url']); |
| 201 |
$network_name = sanitize_text_field($data['network']); |
| 202 |
$network = MBSocial()->networks()->get($network_name); |
| 203 |
|
| 204 |
// check if share count appeared in the cache. If this is the case it's possible another process put it there. |
| 205 |
$do_remote = true; |
| 206 |
$share_count = $network->getShareCount(array("url" => $share_url)); |
| 207 |
if ($share_count !== false) |
| 208 |
{ |
| 209 |
$count = $share_count; |
| 210 |
$do_remote = false; // stop annoying other servers |
| 211 |
} |
| 212 |
|
| 213 |
if ($do_remote) |
| 214 |
{ |
| 215 |
// returns false, a number or 'locked' in case of locked. |
| 216 |
$count = $network->getRemoteShareCount($share_url); |
| 217 |
|
| 218 |
/* If the request is lock, another request is probably asking for this and shouldn't take long. |
| 219 |
Try a maximum of 5 times to prevent server process hanging until php times out ( we want to prevent that ) */ |
| 220 |
if ($count == 'locked') |
| 221 |
{ |
| 222 |
if ($this->ajax_remote_count < 5) |
| 223 |
{ |
| 224 |
sleep(1); // in seconds please. |
| 225 |
// after retry result here will at some point have the count data, extract and return at the end. |
| 226 |
$result = $this->ajax_get_count($result,$data); |
| 227 |
$count = $result["data"]["count"]; |
| 228 |
} |
| 229 |
else |
| 230 |
$count = "TIMEOUT"; |
| 231 |
$this->ajax_remote_count++; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
$count = $this->format_count($count); |
| 236 |
$result["data"] = array('count' => intval($count) ); |
| 237 |
|
| 238 |
return $result; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
public function admin() |
| 243 |
{ |
| 244 |
?> |
| 245 |
<div id='countBlock' class='options option-container count' data-refresh='previewBlock'> |
| 246 |
<div class='title'><?php _e('Share Count Options'); ?></div> |
| 247 |
<div class='inside'> |
| 248 |
<?php |
| 249 |
$blockdata = $this->data[$this->blockname]; |
| 250 |
$admin = MBSocial()->admin(); |
| 251 |
|
| 252 |
$active = new maxField('switch'); |
| 253 |
$active->id = 'count_active'; |
| 254 |
$active->name = $active->id; |
| 255 |
$active->value = 1; |
| 256 |
$active->label = __('Show Share Counts', 'mbsocial'); |
| 257 |
$active->checked = checked( $this->getValue('count_active'), 1, false); |
| 258 |
$active->publish = false; |
| 259 |
|
| 260 |
$admin->addField( $active, 'start','end'); |
| 261 |
|
| 262 |
$min_count = new maxField('number'); |
| 263 |
$min_count->id = 'share_min_count'; |
| 264 |
$min_count->min = 0; |
| 265 |
$min_count->name = $min_count->id; |
| 266 |
$min_count->inputclass = 'tiny'; |
| 267 |
$min_count->value = $this->getValue('share_min_count'); |
| 268 |
$min_count->label = __('Minimum count to show shares','mbsocial'); |
| 269 |
|
| 270 |
$admin->addField( $min_count, 'start', 'end', false); |
| 271 |
|
| 272 |
// *** COUNT SIZE *** // |
| 273 |
$field_size = new maxField('number'); |
| 274 |
$field_size->label = __('Number Size', 'mbsocial'); |
| 275 |
$field_size->name = 'font_count_size'; |
| 276 |
$field_size->id= $field_size->name; |
| 277 |
$field_size->inputclass = 'tiny'; |
| 278 |
$field_size->min = 8; |
| 279 |
$field_size->value = maxUtils::strip_px($this->getValue('font_count_size')); |
| 280 |
$admin->addField($field_size, 'start'); |
| 281 |
|
| 282 |
// Font style checkboxes |
| 283 |
$fweight = new maxField('checkbox'); |
| 284 |
$fweight->icon = 'dashicons-editor-bold'; |
| 285 |
$fweight->title = __("Bold",'mbsocial'); |
| 286 |
$fweight->id = 'check_count_fweight'; |
| 287 |
$fweight->name = 'font_count_weight'; |
| 288 |
$fweight->value = 'bold'; |
| 289 |
$fweight->inputclass = 'check_button icon'; |
| 290 |
$fweight->checked = checked( $this->getValue('font_count_weight'), 'bold', false); |
| 291 |
$admin->addField($fweight, 'group_start'); |
| 292 |
|
| 293 |
$fstyle = new maxField('checkbox'); |
| 294 |
$fstyle->icon = 'dashicons-editor-italic'; |
| 295 |
$fstyle->title = __("Italic",'mbsocial'); |
| 296 |
$fstyle->id = 'check_count_fstyle'; |
| 297 |
$fstyle->name = 'font_count_style'; |
| 298 |
$fstyle->value = 'italic'; |
| 299 |
$fstyle->inputclass = 'check_button icon'; |
| 300 |
$fstyle->checked = checked( $this->getValue('font_count_style'), 'italic', false); |
| 301 |
$admin->addField($fstyle, '', array('group_end', 'end')); |
| 302 |
|
| 303 |
$total_count = new maxField('switch'); |
| 304 |
$total_count->id = 'show_total_count'; |
| 305 |
$total_count->name = $total_count->id; |
| 306 |
$total_count->label = __('Show total count', 'mbsocial'); |
| 307 |
$total_count->value = 1; |
| 308 |
$total_count->checked = checked ($this->getValue('show_total_count'), 1, false); |
| 309 |
$admin->addField($total_count, 'start', 'end'); |
| 310 |
|
| 311 |
$tcount_label = new maxField(); |
| 312 |
$tcount_label->id = 'total_count_label'; |
| 313 |
$tcount_label->name = $tcount_label->id; |
| 314 |
$tcount_label->label = __('Total Count Label', 'mbsocial'); |
| 315 |
$tcount_label->inputclass = 'medium'; |
| 316 |
$tcount_label->value = $this->getValue('total_count_label'); |
| 317 |
$admin->addField($tcount_label, 'start', 'end'); |
| 318 |
|
| 319 |
$total_color = new maxField('color'); |
| 320 |
$total_color->id = 'total_count_color'; |
| 321 |
$total_color->name = $total_color->id; |
| 322 |
$total_color->label = __('Total Count Color', 'mbsocial'); |
| 323 |
$total_color->value = ''; |
| 324 |
|
| 325 |
$admin->addField($total_color, 'start', 'end'); |
| 326 |
|
| 327 |
$admin->display_fields(); |
| 328 |
?> |
| 329 |
</div> <!-- inside --> |
| 330 |
</div> <!-- option container --> |
| 331 |
<?php |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
} // class |
| 336 |
|