PluginProbe
Social Share Buttons / 1.16
Social Share Buttons v1.16
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 / class-network.php

class-network.php in Social Share Buttons 1.16, at classes/class-network.php

399 lines 10.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 use \MaxButtons\MaxUtils as MaxUtils;
6
7 abstract class mbNetwork
8 {
9
10 protected $network; // internal name of network
11 protected $priority = 'unselected'; // Default priority of network - selected / unselected / readmore / hidden
12
13 //protected $api_url; // URL of API to request information
14 protected $countable = false; // If network supports count of likes / shares / favs
15 protected $count_api = ''; // API URL for requesting counts
16 protected $count_check_time = 14400; // (PHP 5.3) 4 * HOUR_IN_SECONDS; // Transient time, how often to check network for new counts
17 protected $share_url = null; // URL to link to when sharing something
18 protected $alternate_url = null; // if another URL needs to be
19 protected $alternate_label = null; // Label for the other option (checkbox)
20 protected $is_popup = true; // Open in Popup or not
21 protected $popup_dimensions = array(400,300); // Default popup dimensions
22 protected $return_var; // Count API return var ( JSON format )
23
24 // mobile or not settings - not yet developed
25 protected $displayMobile = true; // network should be displayed on mobiles
26 protected $displayDesktop = true; // network should be displayed on desktop
27
28 /** Options for Share Icons */
29 protected $profile_url = null; // URL to the network's profiles.
30 protected $profile_placeholder; // Placeholder for profile input text field [aka 'user name' or so ]
31 protected $profile_label; // Not in use at the moment?
32
33 protected $nice_name; // Nice name for display in the Editor interface
34
35 protected $label; // Label (default) Text. This will be in the button, e.g. on hover
36
37 protected $icon_type = 'fab'; // Icon library set
38 protected $icon = 'fa-circle'; // Icon
39 protected $icon_image_id; // for svg type icons.
40 protected $icon_image_url;
41 protected $icon_image_size;
42 // protected $icon_image_alt = '';
43
44 protected $color = '#fff';
45
46 // extra options [internal]
47 protected $is_native = true; // is this network built-in, or imported? Relevant for network settings
48 protected $is_editable = true; // should this network be user-editable?
49 protected $is_active = true;
50 protected $is_limitedpro = false; // the network is only intended for MB PRO
51 protected $forcesamewindow = false; // never open this in a new window / i.e. email / print type networks
52
53 protected $default_options = null;
54
55 public function __construct()
56 {
57 if (is_null($this->profile_placeholder))
58 $this->profile_placeholder = __('User Name','mbsocial');
59 if (is_null($this->profile_label))
60 $this->profile_label = __('Follow', 'mbsocial');
61
62 $this->default_options = $this->get_all_options(); // init them as default. For save function
63 }
64
65 public function get($name)
66 {
67 if (isset($this->$name))
68 return $this->$name;
69 else
70 return null;
71 }
72
73 public function get_all_options()
74 {
75 $popup_width = isset($this->popup_dimensions[0]) ? $this->popup_dimensions[0] : 0;
76 $popup_height = isset($this->popup_dimensions[1]) ? $this->popup_dimensions[1] : 0;
77
78
79 // iterate on this next time perhaps.
80 //$attrs = get_object_vars($this);
81
82 $options = array(
83 'active' => $this->is_active, // no nw setting for this
84 'popup' => $this->is_popup,
85 'label' => $this->label,
86 'share_url' => $this->share_url,
87 'profile_url' => $this->profile_url,
88 'popup_width' => $popup_width,
89 'popup_height' => $popup_height,
90 'icon_type' => $this->icon_type,
91 'icon' => $this->icon,
92 'icon_image_id' => $this->icon_image_id,
93 'icon_image_url' => $this->icon_image_url,
94 // 'icon_image_alt' => $this->icon_image_alt,
95 'icon_image_size' => $this->icon_image_size,
96 'color' => $this->color,
97 'displayMobile' => $this->displayMobile,
98 'displayDesktop' => $this->displayDesktop,
99 );
100
101 return $options;
102 }
103
104 public function get_all_defaults()
105 {
106 return $this->default_options;
107 }
108
109 public function load_settings($settings)
110 {
111 if (! $settings)
112 return;
113 foreach($settings as $setting => $value)
114 {
115 if (is_null($value) || strlen($value) == 0)
116 continue;
117
118 switch($setting)
119 {
120 case 'active':
121 $this->is_active = ($value == 1) ? true : false;
122 break;
123 case 'popup':
124 $this->is_popup = ($value == 1) ? true : false;
125 break;
126 case 'popup_width':
127 $this->popup_dimensions[0] = $value;
128 break;
129 case 'popup_height':
130 $this->popup_dimensions[1] = $value;
131 break;
132 default:
133 $this->{$setting} = $value;
134 break;
135 }
136 }
137 }
138
139
140 public function forMobile()
141 {
142 return $this->displayMobile;
143 }
144
145 public function forDesktop()
146 {
147 return $this->displayDesktop;
148 }
149
150 /** If current running setting of network should display count
151 *
152 * Even if network supports displaying a counter, this should not be the case when showing the 'profile'
153 * @return boolean
154 */
155 public function isCountable()
156 {
157 $use_profile = MBSocial()->whistle()->ask('editor/profile/use');
158 if ($use_profile == 1)
159 return false;
160
161 return $this->countable;
162 }
163
164 // If network is capable of sharing profile
165 public function is_share_icon()
166 {
167 if (is_null($this->profile_url))
168 return false;
169
170 return true;
171 }
172
173 /** If network is capable of sharing site page */
174 public function is_social_share()
175 {
176 if (is_null($this->share_url))
177 return false;
178
179 return true;
180 }
181
182 public function has_alternate()
183 {
184 if (is_null($this->alternate_url))
185 return false;
186
187 return true;
188 }
189
190 /** Use this function to check if popup link should be used **/
191 public function is_popup()
192 {
193 $use_profile = MBSocial()->whistle()->ask('editor/profile/use');
194 if ($use_profile == 1)
195 return false;
196 else {
197 return $this->is_popup;
198 }
199 }
200
201 /** Return the label for this network and use **/
202 public function get_label()
203 {
204 $use_profile = MBSocial()->whistle()->ask('editor/profile/use');
205 if ($use_profile == 1 && strlen($this->profile_label) > 0)
206 return $this->profile_label;
207 else
208 return $this->label;
209 }
210
211 public function get_nice_name()
212 {
213 if (isset($this->nice_name))
214 return $this->nice_name;
215 else
216 return ucfirst($this->network);
217 }
218
219 /** Find and get the proper URL to link to.
220 * This can be the network link to sharing a page
221 * or the network link to linking the profile.
222 * Try to find out automatically or whistle ask */
223 public function get_url()
224 {
225 $is_profile = $this->is_share_icon();
226 $is_share = $this->is_social_share();
227
228 if ($is_profile && ! $is_share) // profile only network
229 {
230 return $this->profile_url;
231 }
232 elseif (! $is_profile && $is_share) // share page only network
233 {
234 return $this->share_url;
235 }
236 elseif ($is_profile && $is_share)
237 {
238 $use_profile = MBSocial()->whistle()->ask('editor/profile/use');
239 $use_alternate = MBSocial()->whistle()->ask('editor/profile/alternate');
240
241 if ($use_profile == 1)
242 {
243 if ($use_alternate == 1)
244 return $this->alternate_url;
245 else
246 return $this->profile_url;
247 }
248 else {
249 return $this->share_url;
250 }
251
252 }
253 }
254
255 /* Network dependent creating of a button. Called from collection class */
256 public function createButton($args = array())
257 {
258 $defaults = array('link' => 'javascript:void(0)',
259 'preview' => false,
260 'index' => -1,
261 'name' => '',
262 'data' => array(),
263 'collection_count' => 0,
264 );
265
266 $item_index = $args['index'];
267 if ($args['collection_count'] > 1)
268 {
269 $item_index = $args['collection_count'] . "_" . $item_index;
270 }
271
272 $args = wp_parse_args($args, $defaults);
273 $html = "<span class='mb-item item-" . $item_index . "'>
274 <a href='" . $args['link'] . "' class='mb-social '>
275 ";
276
277 $html .= "</a></span>";
278
279 $button = str_get_html($html);
280 return $button;
281 }
282
283 public function getRemoteShareCount($share_url)
284 {
285 if (! $this->countable)
286 return false;
287
288 $count_api = $this->count_api;
289
290 if ($count_api == '') return false; // no api
291
292 $network = $this->network;
293 $timeout = 60; // prevent the same requests from running multiple times ( i.e. one page, many collections on same url ) .
294 $locked = maxUtils::get_transient('shares-' . $network . '-' . $share_url. '-lock');
295
296 if ($locked == true)
297 return 'locked'; // try again on next refresh.
298
299 //lock out next request while this one is still running.
300 maxUtils::set_transient('shares-' . $network . '-' . $share_url . '-lock', true, $timeout );
301
302 $count_api = str_replace("{url}", $share_url, $count_api);
303
304 $count = $this->remoteRequest($count_api);
305
306 if (defined('MAXBUTTONS_DEBUG') && MAXBUTTONS_DEBUG)
307 {
308 $admin = MB()->getClass("admin");
309 $admin->log("Get Remote Share", "Call: $count_api - Network : " . $this->network . " - Count: $count \n ");
310 }
311
312 if ($count !== false)
313 {
314 $network = $this->network;
315 $check_time = $this->count_check_time;
316
317 // set count
318 maxUtils::set_transient('shares-' . $network . '-' . $share_url, $count, $check_time );
319
320 }
321
322 // remove lock
323 maxUtils::delete_transient('shares-' . $network . '-' . $share_url . '-lock');
324
325 return $count;
326 }
327
328 protected function remoteRequest($url)
329 {
330 $response = wp_remote_get($url);
331 $result_path = $this->return_var;
332 $result_array = explode("|",$result_path);
333 if (count($result_array) == 0)
334 $result_array = array($result_path);
335
336 if (is_wp_error($response) || $response['response']['code'] != 200) {
337 return false;
338 }
339 else {
340 $result = wp_remote_retrieve_body($response);
341 }
342 $result = json_decode($result, true);
343
344
345 foreach($result_array as $result_val)
346 {
347 if (isset($result[$result_val]))
348 $result = $result[$result_val];
349
350 }
351 if (is_int($result))
352 return $result;
353
354 return 0; // some networks don't return the json return var. Only return false on network errors
355
356 }
357
358 public function getShareCount($args = array())
359 {
360 if ( $this->count_api == '')
361 return 0; // no api - count always zero.
362
363 $defaults = array(
364 "url" => "",
365 "preview" => false,
366 "force_share_update" => false,
367
368 );
369
370 $args = wp_parse_args($args,$defaults);
371
372 $share_url = esc_url($args["url"]);
373 $network = $this->network;
374 //$count = get_transient('mb-col-' . $network . '-' . $share_url . '-shares');
375 $count = maxUtils::get_transient('shares-' . $network . '-' . $share_url);
376
377 if ($args["force_share_update"])
378 $count = -1; // force an update
379
380 if ( ($count === false || $count == -1) && ! $args["preview"])
381 { // request from external - this is done via ajax on runtime.
382 return false;
383 }
384
385 return $count;
386 }
387
388
389 /** Function to display additional network-specific options.
390 * @return String Option Output.
391 **/
392 public function admin() {
393 return '';
394
395 }
396
397
398 }
399