PluginProbe
Social Share Buttons / 1.5
Social Share Buttons v1.5
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-social-networks.php

class-social-networks.php in Social Share Buttons 1.5, at classes/class-social-networks.php

635 lines 15.4 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\maxField as maxField;
6 use \MaxButtons\maxBlocks as maxBlocks;
7
8 class mbSocialNetworks
9 {
10 protected $network = "";
11 protected $network_data = array();
12 protected $network_name = '';
13
14 protected $networks = array();
15 protected $network_settings;
16
17
18 private $custom_api_url = 'https://maxbuttons.com/getsocialnetworks/';
19
20
21 public function __construct()
22 {
23 $this->setNetworkClasses();
24 $this->getNetworkSettings();
25
26 //$this->setNetWorks();
27
28 }
29
30 public function get($name = null)
31 {
32 if (! is_null($name))
33 {
34 foreach($this->networks as $class => $network)
35 {
36 if ($name == $class || $name == $network->get('network') )
37 {
38 return $network;
39 }
40 }
41 return false; // not found
42
43 }
44 else
45 {
46 return $this->networks;
47 }
48 }
49
50
51 //public function
52
53 /** Get all available subclasses of main mbNetwork class **/
54 protected function setNetworkClasses()
55 {
56
57 foreach (get_declared_classes() as $class) {
58
59 if (is_subclass_of($class, 'MBSocial\mbNetwork'))
60 {
61 if ($class != 'MBSocial\customNetwork')
62 {
63 $newObj = new $class;
64 $name = $newObj->get('network');
65 $newObj->load_settings( $this->getNetworkSettings($name) );
66 $this->networks[$class] = $newObj;
67
68 }
69 }
70 }
71
72 // network settings that are imported
73 $custom = $this->getCustomNetworkSettings();
74 if ($custom)
75 {
76 foreach($custom as $network)
77 {
78 $name = $network['name'];
79 $network['is_limitedpro'] = true;
80 $new = new CustomNetwork();
81 $new->load_settings($network);
82
83 $this->networks[$name] = $new;
84 }
85 }
86
87 if (! Install::isPRO() )
88 {
89 foreach($this->networks as $index => $class)
90 {
91 if ($class->get('is_limitedpro') === true)
92 {
93 unset($this->networks[$index]);
94 }
95 }
96 }
97 }
98
99 public function getNetworkSettings($name = false)
100 {
101 if ( is_null($this->network_settings))
102 {
103 $this->network_settings = get_option('mbsocial_network_settings');
104 }
105
106 if ($name)
107 {
108 if (isset($this->network_settings[$name]))
109 {
110 return $this->network_settings[$name];
111 }
112 else {
113 return false;
114 }
115 }
116 else {
117 return $this->network_settings;
118 }
119 }
120
121 // orginal - take only the imported settings, not the network editor ( used in network editor )
122 public function getCustomNetworkSettings($original = false)
123 {
124 $settings = $this->getNetworkSettings();
125
126 if (is_array($settings))
127 {
128 $custom = isset($settings['custom']) ? $settings['custom'] : false;
129
130 if (! $custom || ! is_array($custom))
131 return false; // no settings.
132
133 foreach($custom as $name => $network)
134 {
135 // merge settings from network editor if any
136 $custom_settings = isset( $settings[$name] ) ? $settings[$name] : false;
137 if ( (! $original) && $custom_settings)
138 {
139 $custom[$name] = array_merge($network, $custom_settings);
140 }
141 }
142 return $custom;
143
144 }
145 return false;
146 }
147
148 public function ajax_networksettings($post, $echo = true)
149 {
150 $network_name = sanitize_text_field($post['param']);
151
152 $network = $this->get($network_name);
153 $network_settings = $this->getNetworkSettings();
154
155 if (! $network)
156 {
157 exit('network not found - ' . $network_name);
158 }
159
160 $admin = MBSocial()->admin();
161 maxBlocks::initBlocks();
162
163 $default_options = $network->get_all_options(); // network should also load from WP
164 if (isset($network_settings[$network_name]))
165 {
166 $options = $network_settings[$network_name];
167 // per manual
168 $options = $options + $default_options;
169 }
170 else
171 {
172 $options = $default_options;
173 }
174
175 $network_field = new maxField('hidden');
176 $network_field->name = 'network';
177 $network_field->id = $network_field->name;
178 $network_field->value = $network_name;
179
180 $admin->addField($network_field);
181
182 $active = new maxField('switch');
183 $active->id = 'active';
184 $active->name = $active->id;
185 $active->label = __('Active', 'mbsocial');
186 $active->value = 1;
187 $active->checked = checked($options['active'], 1, false);
188
189 $admin->addField($active, 'start','end');
190
191 $label = new maxField('text');
192 $label->id = 'label';
193 $label->name = $label->id;
194 $label->value = $options['label'];
195 $label->label = __('Label', 'mbsocial');
196
197 $admin->addField($label, 'start', 'end');
198
199 $shareurl = new maxField('text');
200 $shareurl->id = 'share_url';
201 $shareurl->name = $shareurl->id;
202 $shareurl->value = $options['share_url'];
203 $shareurl->disabled = true;
204 $shareurl->label = __('Share URL', 'mbsocial');
205
206 $admin->addField($shareurl, 'start', 'end');
207
208 $profileurl = new maxField('text');
209 $profileurl->id = 'profile_url';
210 $profileurl->name = $profileurl->id;
211 $profileurl->value = $options['profile_url'];
212 $profileurl->disabled = true;
213 $profileurl->label = __('Profile URL', 'mbsocial');
214
215 $admin->addField($profileurl,'start', 'end');
216
217 $popup = new maxField('switch');
218 $popup->id = 'popup';
219 $popup->name = $popup->id;
220 $popup->checked = checked($options['popup'], 1, false);
221 $popup->value = 1;
222 $popup->label = __('Open in Popup','mbsocial');
223
224 $admin->addField($popup, 'start', 'end');
225
226 $popupwidth = new maxField('text');
227 $popupwidth->id = 'popup_width';
228 $popupwidth->name = $popupwidth->id;
229 $popupwidth->value = $options['popup_width'];
230 $popupwidth->inputclass ='tiny';
231 $popupwidth->label = __('Width', 'mbsocial');
232
233 $admin->addField($popupwidth, 'start');
234
235 $popupheight = new maxField('text');
236 $popupheight->id = 'popup_height';
237 $popupheight->name = $popupheight->id;
238 $popupheight->value = $options['popup_height'];
239 $popupheight->inputclass = 'tiny';
240 $popupheight->label = __('Height', 'mbsocial');
241
242 $admin->addField($popupheight, '', 'end');
243
244 $icon_type = new maxField('text');
245 $icon_type->id = 'icon_type';
246 $icon_type->name = $icon_type->id;
247 $icon_type->value= $options['icon_type'];
248 $icon_type->label = __('Icon Type', 'mbsocial');
249 $icon_type->inputclass = 'medium';
250
251 $admin->addField($icon_type, 'start', 'end');
252
253 $icon = new maxField('text');
254 $icon->id = 'icon';
255 $icon->name = $icon->id;
256 $icon->value= $options['icon'];
257 //$icon->inputclass = 'medium';
258 $icon->label = __('Icon', 'mbsocial');
259
260 $admin->addField($icon, 'start', 'end');
261
262 $color = new maxField('color');
263 $color->id = 'color';
264 $color->name = $color->id;
265 $color->value = $options['color'];
266 $color->label = __("Network Color", 'mbsocial');
267
268 $admin->addField($color, 'start', 'end');
269
270 $show_mobile = new maxField('switch');
271 $show_mobile->id = 'displayMobile';
272 $show_mobile->name = $show_mobile->id;
273 $show_mobile->value = 1;
274 $show_mobile->checked = checked($options['displayMobile'], 1, false);
275 $show_mobile->label = __('Show on Mobile', 'mbsocial');
276
277 $admin->addField($show_mobile, 'start', 'end');
278
279 $show_desktop = new maxField('switch');
280 $show_desktop->id = 'displayDesktop';
281 $show_desktop->name = $show_desktop->id;
282 $show_desktop->value = 1;
283 $show_desktop->checked = checked($options['displayDesktop'], 1, false);
284 $show_desktop->label = __('Show on Desktop', 'mbsocial');
285
286 $admin->addField($show_desktop, 'start', 'end');
287
288 $saveB = new maxField('button');
289 $saveB->id = 'save_network';
290 $saveB->name = $saveB->id;
291 $saveB->button_label = __('Save Network', 'mbsocial');
292 $saveB->dataaction = 'save-network';
293 $saveB->inputclass = 'mb-ajax-submit button-primary';
294
295 $admin->addField($saveB, 'start', '');
296
297 if (! $network->get('is_native'))
298 {
299 $label = __('Remove Imported Network', 'mbsocial');
300 }
301 else
302 $label = __('Return to defaults', 'mbsocial');
303
304 $default = new maxField('generic');
305 $default->name = 'return_default';
306 $default->id = $default->name;
307 $default->content = '<a class="mb-ajax-submit" data-action="remove-networksettings">' . $label . '</a>';
308
309 $admin->addField($default, '', 'end');
310
311 $fields = $admin->display_fields(true, true);
312
313 $output = array('status' => 'success',
314 'output' => $fields,
315 'title' => sprintf(__('Settings - %s', 'mbsocial'), $network->get_nice_name() ),
316 );
317
318 if ($echo)
319 {
320 echo json_encode($output);
321 exit();
322 }
323 else {
324 return $output;
325 }
326 }
327
328 public function ajax_savenetwork($post)
329 {
330
331 if (isset($post['form']))
332 {
333 $form = array();
334 parse_str($post['form'], $form);
335 }
336
337 $name = $form['network'];
338
339 $network = $this->get($name);
340 $network_settings = $this->getNetworkSettings();
341
342 if (! isset($network_settings[$name]))
343 {
344 $network_settings[$name] = array();
345 }
346
347 if ($network->get('is_native'))
348 {
349 $options = $network->get_all_options();
350 }
351 else {
352 $custom = $this->getCustomNetworkSettings(true);
353 $options = $network->get_all_options(); // for all is_xx fields
354 $custom = isset($custom[$name]) ? $custom[$name] : array();
355
356 $options = array_merge($options, $custom);
357
358 }
359
360 $store_options = array(); // only save options that are not the default options of this network.
361
362 $fields_replaced = 0;
363
364 foreach($options as $item_name => $item_val)
365 {
366 if (isset($form[$item_name]) && $form[$item_name] != $item_val)
367 {
368 $value = sanitize_text_field($form[$item_name]);
369
370 $store_options[$item_name] = $value;
371 $fields_replaced++;
372 }
373 elseif ( ($item_name == 'active' || $item_name == 'popup') && ! isset($form[$item_name])) // checkbox
374 {
375 $store_options[$item_name] = 0;
376 $fields_replaced++;
377 }
378
379 }
380
381
382 $network_settings[$name] = $store_options;
383
384 update_option('mbsocial_network_settings', $network_settings, false);
385 $this->network_settings = $network_settings;
386
387 $param = array('param' => $name);
388 $output = $this->ajax_networksettings($param, false);
389 $output = $output['output'];
390
391 echo json_encode(
392 array('success' => true,
393 'reload' => false,
394 'title' => __('Settings saved', 'mbsocial'),
395 'output' => $output,
396 'fields_replaced' => $fields_replaced,
397 ));
398 exit('');
399 }
400
401 public function ajax_removesettings($post)
402 {
403 if (isset($post['form']))
404 {
405 $form = array();
406 parse_str($post['form'], $form);
407 }
408
409 $network_name = $form['network'];
410
411 $network = $this->get($network_name);
412 $network_settings = $this->getNetworkSettings();
413
414 $reload = false;
415 $output = false;
416
417 if (! $network->get('is_native') )
418 {
419 $custom = $this->getCustomNetworkSettings();
420 if (isset($custom[$network_name]))
421 {
422 unset($custom[$network_name]);
423 $network_settings['custom'] = $custom;
424 update_option('mbsocial_network_settings', $network_settings, false);
425 }
426 if (isset($network_settings[$network_name])) // custom settings
427 {
428 unset($network_settings[$network_name]);
429 update_option('mbsocial_network_settings', $network_settings, false);
430 $this->network_settings = $network_settings; // new ones.
431 }
432 $reload = true;
433 }
434 else
435 {
436 if (isset($network_settings[$network_name]))
437 {
438 unset($network_settings[$network_name]);
439 update_option('mbsocial_network_settings', $network_settings, false);
440 $this->network_settings = $network_settings; // new ones.
441 }
442 $param = array('param' => $network_name);
443 $output = $this->ajax_networksettings($param, false);
444 $output = $output['output'];
445 }
446
447 echo json_encode(
448 array('success' => true,
449 'reload' => $reload,
450 'output' => $output,
451 'title' => __('Settings removed', 'mbsocial'),
452 ));
453 exit('');
454 }
455
456 public function ajax_showcustomnetworks()
457 {
458 $license = MB()->getClass('license');
459 if (! $license)
460 return false; // something wrong
461
462 $key = $license->get_key();
463
464 $post_args = array(
465 'url' => home_url(),
466 'license_key' => $key,
467
468 );
469
470 $remote_args = array(
471 'method' => 'POST',
472 'body' => array( 'license' => $key ),
473 );
474
475
476 $response = wp_remote_post($this->custom_api_url, $remote_args);
477
478 if ( is_wp_error($response))
479 {
480 // error
481 $output = array('status' => 'error',
482 'message' => __('Remote Error', 'mbsocial'),
483
484
485 );
486 echo json_encode($output);
487 exit();
488 }
489 else {
490 $result = wp_remote_retrieve_body($response);
491
492 $result = json_decode($result, true);
493
494 set_transient('mbsocial-social-networks', $result);
495
496 if (isset($result['status']) && $result['status'] == 'error')
497 {
498 $output = array('status' => 'error',
499 'message' => $result['message'],
500 );
501 echo json_encode($output);
502 exit();
503 }
504
505 $output = $this->parseCustomResult($result);
506
507 echo json_encode(
508 array(
509 'status' => 'success',
510 'output' => $output,
511
512
513 ));
514
515
516 exit();
517 }
518
519 /*
520 $output = array('status' => 'success',
521 'output' => 'output');
522
523 echo json_encode($output);
524 */
525 exit();
526
527 }
528
529 public function parseCustomResult($result)
530 {
531 $output = '
532 <div class= "customnetwork_select">
533 <p class="cns-toolbar"><span class="selected">0</span> ' . __('Selected', 'mbsocial') . '
534 <input type="hidden" name="selected_custom" id="selected_custom" value="">
535 <button type="button" class="mb-ajax-action button-primary" data-action="import-customnetworks" disabled data-param-input="#selected_custom">
536 ' . __('Import', 'mbsocial') . '
537 </button>
538 <ul>';
539
540 $customs = $this->getCustomNetworkSettings();
541
542 foreach($result as $item)
543 {
544 $name = ( isset($item['nice_name']) ) ? $item['nice_name'] : ucfirst($item['name']);
545 $network_name = $item['name'];
546 $icon = isset($item['icon']) ? $item['icon'] : '';
547 $icon_type = isset($item['icon_type']) ? $item['icon_type'] : '';
548
549 $is_imported = isset($customs[$network_name]) ? true : false;
550
551 $the_icon = MBSocial()->admin()->renderIcon(
552 array('icon' => $icon,
553 'icon_type' => $icon_type,
554 'title' => $name,
555 'icon_size' => 20)
556 );
557
558 $output .= '<li class="the_network" data-network="' . $network_name . '">' . $the_icon . $name;
559 if ($is_imported)
560 {
561 $output .= '<span class="is_imported">' . __('Already Imported.', 'mbsocial') . '</span>';
562 }
563 $output .= ' </li>';
564 }
565
566 $output .= '</ul>
567 </div>';
568
569 return $output;
570
571 }
572
573 public function ajax_importcustomnetworks($post)
574 {
575 $network_settings = $this->getNetworkSettings();
576 $custom_networks = get_transient('mbsocial-social-networks');
577
578 $to_import = isset($post['param']) ? $post['param'] : false;
579
580 if (! $to_import)
581 exit(); //error
582
583 $custom_active = isset($network_settings['custom']) ? $network_settings['custom'] : array();
584
585 $import_count = 0;
586 $to_import = explode(',', $to_import);
587
588 foreach($to_import as $item_name)
589 {
590 $network_name = trim($item_name);
591 $new_network = isset($custom_networks[$network_name]) ? $custom_networks[$network_name] : false;
592 //if ($new_network)
593 //{
594 $custom_active[$network_name] = $new_network;
595 $import_count++;
596 //}
597 }
598
599 if ($import_count > 0)
600 {
601 $network_settings['custom'] = $custom_active;
602 update_option('mbsocial_network_settings',$network_settings );
603 }
604
605 echo json_encode(
606 array('status' => 'success',
607 'import_count' => $import_count,
608 'title' => sprintf(__('%s Network(s) Imported','mbsocial'), $import_count),
609 )
610 );
611 exit();
612
613 }
614
615 public function checkPopup()
616 {
617 if (isset($this->network_data["popup"]) && $this->network_data["popup"] )
618 return true;
619 else
620 return false;
621 }
622
623 public function getPopupDimensions()
624 {
625 if (isset($this->network_data["popup_dimensions"]))
626 return $this->network_data["popup_dimensions"];
627 else
628 return array();
629 }
630
631
632
633
634 }
635