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

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