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

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