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

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