PluginProbe
MaxButtons – Create buttons / 6.24
MaxButtons – Create buttons v6.24
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.24, at classes/social-networks.php

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