PluginProbe
Social Share Buttons / 1.1.1
Social Share Buttons v1.1.1
trunk 0.9 1.0 1.1 1.1.1 1.1.2 1.10 1.11 1.12 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.3 1.30 1.4 1.5 1.6 1.7 1.8 1.9
share-button / classes / blocks / count_block.php

count_block.php in Social Share Buttons 1.1.1, at classes/blocks/count_block.php

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