PluginProbe
MaxButtons – Create buttons / 6.6
MaxButtons – Create buttons v6.6
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / classes / social-networks.php

social-networks.php in MaxButtons – Create buttons 6.6, at classes/social-networks.php

369 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class maxSN
4 {
5 protected $network = "";
6 protected $network_data = array();
7 protected $network_name = '';
8
9 public function __construct($network)
10 {
11 $this->network = $network;
12 $this->setNetWork($network);
13
14 }
15
16 public function setNetwork($network)
17 {
18
19 $networks = $this->getSupportedNetworks();
20 $this->network_name = $networks[$network];
21
22 $network_data = array(
23 "share_url" => '', // url to send share to (incl. var)
24 "count_api" => '', // request url
25 "count_function" => "jsonRequest", // which function to run.
26 "json_return_var" => '', // which var is the count number?
27 "count_check_time" => 4 * HOUR_IN_SECONDS, // how much time before checking again - in seconds
28 "popup" => true, // button opens share popup
29 "popup_dimensions" => array(400,300), // width - height of popup
30 );
31
32 switch($network)
33 {
34 case "bloglovin":
35 $network_data["popup"] = false;
36
37 break;
38 case "digg":
39 $network_data["share_url"] = "http://digg.com/submit?url={url}&title={title}";
40 $network_data["count_api"] = '';
41 $network_data["popup_dimensions"] = array(600,400);
42 break;
43 case "email":
44 $network_data["share_url"] = 'mailto:?subject={title}&body={url}';
45 $network_data["count_api"] = '';
46 $network_data["popup"] = false;
47 break;
48 case "facebook":
49 $network_data["share_url"] = 'https://www.facebook.com/sharer.php?u={url}';
50 $network_data["count_api"] = "https://graph.facebook.com/{url}";
51 $network_data["json_return_var"] = "shares";
52 break;
53 case "google-plus":
54 $network_data["share_url"] = 'https://plus.google.com/share?url={url}';
55 $network_data["count_api"] = "{url}";
56 $network_data["popup_dimensions"] = array(600,600);
57 $network_data["count_function"] = "googlePlus";
58
59 break;
60 case "linkedin":
61 $network_data["share_url"] = "https://www.linkedin.com/shareArticle?mini=true&url={url}";
62 $network_data["count_api"] = "https://www.linkedin.com/countserv/count/share?url={url}&format=json";
63 $network_data["json_return_var"] = "count";
64 $network_data["popup_dimensions"] = array(700,500);
65 break;
66 case "pinterest":
67 $network_data["share_url"] = "https://www.pinterest.com/pin/create/bookmarklet/?media={img}&url={url}&is_video=false&description={title}";
68 $network_data["count_api"] = "https://api.pinterest.com/v1/urls/count.json?url={url}";
69 $network_data["json_return_var"] = "count";
70 $network_data["count_function"] = "pinterestCount";
71 $network_data["popup_dimensions"] = array(750, 500);
72 break;
73 case "print":
74 $network_data["share_url"] = 'javascript:window.print()';
75 $network_data["count_api"] = '';
76 $network_data["popup"] = false;
77 break;
78 case "stumbleupon":
79 $network_data["share_url"] = "https://www.stumbleupon.com/submit?url={url}&title={title}";
80 $network_data["count_api"] = "https://www.stumbleupon.com/services/1.01/badge.getinfo?url={url}";
81 $network_data["json_return_var"] = "result|views";
82
83 break;
84 case "reddit":
85 $network_data["share_url"] = "https://reddit.com/submit?url={url}&title={title}";
86 $network_data["count_api"] = "https://buttons.reddit.com/button_info.json?url={url}";
87 $network_data["json_return_var"] = "data|children|0|data|score";
88 $network_data["popup_dimensions"] = array(800, 500);
89
90 break;
91 case "twitter":
92 $network_data["share_url"] = 'https://twitter.com/intent/tweet?url={url}';
93 $network_data["count_api"] = "";
94 $network_data["json_return_var"] = "";
95 $network_data["popup_dimensions"] = array(550, 420);
96 break;
97 case "twitter-follow":
98 $network_data["share_url"] = 'https://twitter.com/{user}';
99 $network_data['count_api'] = '';
100 $network_data['json_return_var'] = 'count';
101
102
103 break;
104 case "vkontakte":
105 $network_data["share_url"] = "https://vkontakte.ru/share.php?url={url}";
106 $network_data["count_api"] = "https://vk.com/share.php?act=count&url={url}";
107 $network_data["count_function"] = "vKontakteCount";
108 break;
109 case "whatsapp":
110 $network_data["share_url"] = "whatsapp://send?text={url} {title}";
111 $network_data["count_api"] = '';
112 break;
113 default:
114 $network_data['popup'] = false; // no network no popup.
115 break;
116
117 }
118
119 $network_data = apply_filters("mb-collection-network-data", $network_data);
120 $network_data = apply_filters("mb-collection-network-data-" . $network, $network_data);
121
122 $this->network_data = $network_data;
123 }
124
125 public function getNetworkName()
126 {
127 return $this->network_name;
128
129 }
130
131 // the url to send shares to, by network.
132 public function getShareURL()
133 {
134 return $this->network_data["share_url"];
135 }
136
137 public function checkPopup()
138 {
139 if (isset($this->network_data["popup"]) && $this->network_data["popup"] )
140 return true;
141 else
142 return false;
143 }
144
145 public function getPopupDimensions()
146 {
147 if (isset($this->network_data["popup_dimensions"]))
148 return $this->network_data["popup_dimensions"];
149 else
150 return array();
151 }
152
153 public function getShareCount($args = array())
154 {
155 if ( $this->network_data["count_api"] == '')
156 return 0; // no api - count always zero.
157
158 $defaults = array("url" => "",
159 "preview" => false,
160 "force_share_update" => false,
161 );
162
163 $args = wp_parse_args($args,$defaults);
164
165 $share_url = esc_url($args["url"]);
166
167 $network = $this->network;
168 //$count = get_transient('mb-col-' . $network . '-' . $share_url . '-shares');
169 $count = maxUtils::get_transient('mbcol-shares-' . $network . '-' . $share_url);
170
171 if ($args["force_share_update"])
172 $count = -1; // force an update
173
174 if ( ($count === false || $count == -1) && ! $args["preview"])
175 { // request from external - this is done via ajax on runtime.
176 return false;
177 }
178
179 return $count;
180
181 }
182
183 public function getRemoteShareCount($share_url)
184 {
185 $count_api = $this->network_data["count_api"];
186 if ($count_api == '') return false; // no api
187
188 $network = $this->network;
189 $timeout = 60; // prevent the same requests from running multiple times ( i.e. one page, many collections on same url ) .
190 $locked = maxUtils::get_transient('mbcol-shares-' . $network . '-' . $share_url. '-lock');
191
192 if ($locked == true)
193 return 'locked'; // try again on next refresh.
194
195 //lock out next request while this one is still running.
196 maxUtils::set_transient('mbcol-shares-' . $network . '-' . $share_url . '-lock', true, $timeout );
197
198 $count_api = str_replace("{url}", $share_url, $count_api);
199
200 $func = $this->network_data["count_function"];
201 $count = $this->$func($count_api);
202 if (defined('MAXBUTTONS_DEBUG') && MAXBUTTONS_DEBUG)
203 {
204 $admin = MB()->getClass("admin");
205
206 $admin->log("Get Remote Share", "Call: $count_api - Network : " . $this->network . " - Count: $count \n ");
207 }
208
209 if ($count !== false)
210 {
211 $network = $this->network;
212 $check_time = $this->network_data["count_check_time"];
213
214 // set count
215 maxUtils::set_transient('mbcol-shares-' . $network . '-' . $share_url, $count, $check_time );
216
217 }
218
219 // remove lock
220 maxUtils::delete_transient('mbcol-shares-' . $network . '-' . $share_url . '-lock');
221
222 return $count;
223 }
224
225 protected function jsonRequest($url)
226 {
227
228 $response = wp_remote_get($url);
229 $result_path = $this->network_data["json_return_var"];
230 $result_array = explode("|",$result_path);
231 if (count($result_array) == 0)
232 $result_array = array($result_path);
233
234
235 if (is_wp_error($response) || $response['response']['code'] != 200) {
236 return false;
237 }
238 else {
239 $result = wp_remote_retrieve_body($response);
240 }
241 $result = json_decode($result, true);
242
243 foreach($result_array as $result_val)
244 {
245 if (isset($result[$result_val]))
246 $result = $result[$result_val];
247
248 }
249 if (is_int($result))
250 return $result;
251
252 return 0; // some networks don't return the json return var. Only return false on network errors
253
254 }
255
256 protected function googlePlus($url)
257 {
258 $args = array(
259 'method' => 'POST',
260 'headers' => array(
261 // setup content type to JSON
262 'Content-Type' => 'application/json',
263 ),
264 // setup POST options to Google API
265 'body' => json_encode(array(
266 'method' => 'pos.plusones.get',
267 'id' => 'p',
268 'method' => 'pos.plusones.get',
269 'jsonrpc' => '2.0',
270 'key' => 'p',
271 'apiVersion' => 'v1',
272 'params' => array(
273 'nolog' => true,
274 'id' => $url,
275 'source' => 'widget',
276 'userId' => '@viewer',
277 'groupId' => '@self',
278 ),
279 )),
280
281 'sslverify' => false,
282 );
283 $response = wp_remote_post('https://clients6.google.com/rpc', $args);
284
285
286 if (is_wp_error($response) || $response['response']['code'] != 200) {
287 return false;
288 }
289 else {
290 $result = wp_remote_retrieve_body($response);
291 }
292
293 $result = json_decode($result, true);
294 $count = 0;
295 if (isset($result['result']['metadata']['globalCounts']['count']))
296 { $count = intval($result['result']['metadata']['globalCounts']['count']);
297 return $count;
298 }
299
300 return 0;
301
302 }
303
304 protected function vKontakteCount($url)
305 {
306 $response = wp_remote_get($url);
307
308 if (is_wp_error($response) || $response['response']['code'] != 200) {
309 return false;
310 }
311 else {
312 $result = wp_remote_retrieve_body($response);
313 }
314
315 preg_match('/(\d+),\s+(\d+)/i', $result, $matches);
316 if (isset($matches[2])) // 0 is both patterns, one is first number (not used?).
317 { $count = $matches[2];
318 return $count;
319 }
320 return 0;
321 }
322
323 protected function pinterestCount($url)
324 {
325 $response = wp_remote_get($url);
326
327 if (is_wp_error($response) || $response['response']['code'] != 200) {
328 return false;
329 }
330 else {
331 $result = wp_remote_retrieve_body($response);
332 }
333
334 // remove the callback wrapper.
335 $result = str_replace("receiveCount(","",$result);
336 $result = substr($result,0,(strlen($result) -1) ); // remove last char.
337 $json = json_decode($result, true);
338
339 if (isset($json["count"]))
340 return $json["count"];
341
342 return 0;
343 }
344
345 public static function getSupportedNetworks()
346 {
347 // alphabetic
348 $supported_networks = array(
349 "none" => __("Select a network","maxbuttons"),
350 "bloglovin" => __("Bloglovin","maxbuttons"),
351 "digg" => __("Digg","maxbuttons"),
352 "email" => __("Email","maxbuttons"),
353 "facebook" => __("Facebook","maxbuttons"),
354 "google-plus" => __("Google +","maxbuttons"),
355 "linkedin" => __("LinkedIn","maxbuttons"),
356 "pinterest" => __("Pinterest","maxbuttons"),
357 "print" => __("Print this page","maxbuttons"),
358 "reddit" => __("Reddit", "maxbuttons"),
359 "stumbleupon" => __("StumbleUpon","maxbuttons"),
360 "twitter" => __("Twitter","maxbuttons"),
361 "vkontakte" => __("Vkontakte","maxbuttons"),
362 "whatsapp" => __("Whatsapp", "maxbuttons"),
363 );
364 $supported_networks = apply_filters("mb-collection-supported-networks", $supported_networks);
365 return $supported_networks;
366 }
367
368 }
369