PluginProbe
Social Share Buttons / trunk
Social Share Buttons vtrunk
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 trunk, at classes/class-network.php

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