PluginProbe
Social Share Buttons / 1.30
Social Share Buttons v1.30
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.30, at classes/blocks/count_block.php

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