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

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