PluginProbe
Social Share Buttons / 0.9
Social Share Buttons v0.9
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 0.9, at classes/blocks/count_block.php

260 lines 6.7 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 );
22
23 protected static $total = 0;
24
25
26 public function parse($itemObj, $args = array() )
27 {
28 $blockdata = $this->data[$this->blockname];
29
30 $active = isset($blockdata['count_active']) ? $blockdata['count_active'] : false;
31 $share_min_count = isset($blockdata['share_min_count']) ? $blockdata['share_min_count'] : 0;
32
33 $network = $args['network'];
34
35 // this can use some streamlining
36 $network_block = $this->collection->getBlock('networkBlock');
37 $share_data = $network_block->getShareData();
38 $share_url = esc_url($share_data['url']);
39
40 if ( ( $active == 1 || $this->showTotal() ) && $network->isCountable() )
41 {
42
43 if ($args['preview'] == true)
44 {
45 $share_min_count = 0;
46 $share_count = round(rand(1,9));
47
48 }
49
50 else
51 $share_count = $network->getShareCount(array(
52 'url' => $share_url));
53
54 if ($active && $share_count > $share_min_count)
55 {
56 $anchor = $itemObj->find('a', 0);
57 $anchor->class .= ' with-count';
58
59 //$icon = $itemObj->find('.mb-icon-wrapper', 0);
60
61 $anchor->innertext .= "<span class='mb-share-count'>" . $this->format_count($share_count) . "</span>";
62 }
63 elseif ( $share_count === false) // request remote count
64 {
65 //$network_block = $this->collection->getBlock('networkBlock');
66
67 //$share_url = $network->get('share_url');
68 $share_url = '';
69
70 if ($network_block)
71 {
72 $share_data = $network_block->getShareData();
73 $page_url = $share_data['url'];
74 }
75
76 $tag = "data-onload";
77
78 $json = array("network" => $network->get('network') ,
79 "share_url" => esc_url($page_url),
80 "count_threshold" => $share_min_count,
81 );
82 $item = $itemObj->find('.collection-item', 0);
83 $item->$tag = htmlentities(json_encode($json), ENT_QUOTES, 'UTF-8');
84
85 }
86
87 if (is_numeric($share_count) && $share_count > 0)
88 {
89
90 self::$total += $share_count;
91 }
92 }
93
94 $itemObj->load($itemObj->save());
95 return $itemObj;
96 }
97
98 public function get_total($format = true)
99 {
100 if ($format)
101 {
102 return $this->format_count(self::$total);
103 }
104 else
105 return self::$total;
106 }
107
108 public function showTotal()
109 {
110
111 $show_total = isset($this->data[$this->blockname]['show_total_count']) ? $this->data[$this->blockname]['show_total_count'] : false;
112 if ($show_total == 1)
113 return true;
114
115 return false;
116 }
117
118 public function set($data)
119 {
120 $data = parent::set($data);
121
122 $this->resetTotal(); // reset total counter
123
124 return true;
125
126 }
127
128 /* Format share counts to proper values for display
129 * About 10,000 => 10K
130 */
131 public function format_count($count)
132 {
133 // must be a number
134 if ( ! is_numeric($count) )
135 return $count;
136
137 // must be significant
138 if ($count < 10000)
139 return $count;
140
141
142 if ($count >= 1000000)
143 $count = round($count / 1000000) . __("M",'mbsocial');
144 elseif ($count >= 10000)
145 $count = round($count/1000) . __("K", 'mbsocial');
146
147 $count = apply_filters('mbsocial/count/format', $count);
148 return $count;
149 }
150
151 public function resetTotal()
152 {
153 self::$total = 0;
154
155 }
156
157 public function ajax_get_count($result, $data)
158 {
159 $share_url = sanitize_text_field($data['share_url']);
160 $network_name = sanitize_text_field($data['network']);
161 $network = MBSocial()->networks()->get($network_name);
162
163 // check if share count appeared in the cache. If this is the case it's possible another process put it there.
164 $do_remote = true;
165 $share_count = $network->getShareCount(array("url" => $share_url));
166 if ($share_count !== false)
167 {
168 $count = $share_count;
169 $do_remote = false; // stop annoying other servers
170 }
171
172 if ($do_remote)
173 {
174 // returns false, a number or 'locked' in case of locked.
175 $count = $network->getRemoteShareCount($share_url);
176
177 /* If the request is lock, another request is probably asking for this and shouldn't take long.
178 Try a maximum of 5 times to prevent server process hanging until php times out ( we want to prevent that ) */
179 if ($count == 'locked')
180 {
181 if ($this->ajax_remote_count < 5)
182 {
183 sleep(1); // in seconds please.
184 // after retry result here will at some point have the count data, extract and return at the end.
185 $result = $this->ajax_get_count($result,$data);
186 $count = $result["data"]["count"];
187 }
188 else
189 $count = "TIMEOUT";
190 $this->ajax_remote_count++;
191 }
192 }
193
194 $count = $this->format_count($count);
195 $result["data"] = array('count' => intval($count) );
196
197 return $result;
198
199 }
200
201 public function admin()
202 {
203 ?>
204 <div id='countBlock' class='options option-container count' data-refresh='styleBlock'>
205 <div class='title'><?php _e('Share Count Options'); ?></div>
206 <div class='inside'>
207 <?php
208 $blockdata = $this->data[$this->blockname];
209 $admin = MBSocial()->admin();
210
211 $active = new maxField('switch');
212 $active->id = 'count_active';
213 $active->name = $active->id;
214 $active->value = 1;
215 $active->label = __('Show Share Counts', 'mbsocial');
216 $active->checked = checked( maxBlocks::getValue('count_active'), 1, false);
217 $active->publish = false;
218
219 $admin->addField( $active, 'start','end');
220
221 $min_count = new maxField('number');
222 $min_count->id = 'share_min_count';
223 $min_count->min = 0;
224 $min_count->name = $min_count->id;
225 $min_count->inputclass = 'tiny';
226 $min_count->value = maxBlocks::getValue('share_min_count');
227 $min_count->label = __('Minimum count to show shares','mbsocial');
228
229 $admin->addField( $min_count, 'start', 'end');
230
231
232 $total_count = new maxField('switch');
233 $total_count->id = 'show_total_count';
234 $total_count->name = $total_count->id;
235 $total_count->label = __('Show total count', 'mbsocial');
236 $total_count->value = 1;
237 $total_count->checked = checked (maxBlocks::getValue('show_total_count'), 1, false);
238
239 $admin->addField($total_count, 'start', 'end');
240
241 $tcount_label = new maxField();
242 $tcount_label->id = 'total_count_label';
243 $tcount_label->name = $tcount_label->id;
244 $tcount_label->label = __('Total Count Label', 'mbsocial');
245 $tcount_label->inputclass = 'medium';
246 $tcount_label->value = maxBlocks::getValue('total_count_label');
247
248 $admin->addField($tcount_label, 'start', 'end');
249
250 $admin->display_fields();
251
252 ?>
253 </div> <!-- inside -->
254 </div> <!-- option container -->
255 <?php
256
257 }
258
259 } // class
260