PluginProbe
Social Share Buttons / 1.3
Social Share Buttons v1.3
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.3, at classes/class-social-networks.php

637 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 //$fields .= print_r($options, true);
314
315 $output = array('status' => 'success',
316 'output' => $fields,
317 'title' => sprintf(__('Settings - %s', 'mbsocial'), $network->get_nice_name() ),
318 );
319
320 if ($echo)
321 {
322 echo json_encode($output);
323 exit();
324 }
325 else {
326 return $output;
327 }
328 }
329
330 public function ajax_savenetwork($post)
331 {
332
333 if (isset($post['form']))
334 {
335 $form = array();
336 parse_str($post['form'], $form);
337 }
338
339 $name = $form['network'];
340
341 $network = $this->get($name);
342 $network_settings = $this->getNetworkSettings();
343
344 if (! isset($network_settings[$name]))
345 {
346 $network_settings[$name] = array();
347 }
348
349 if ($network->get('is_native'))
350 {
351 $options = $network->get_all_options();
352 }
353 else {
354 $custom = $this->getCustomNetworkSettings(true);
355 $options = $network->get_all_options(); // for all is_xx fields
356 $custom = isset($custom[$name]) ? $custom[$name] : array();
357
358 $options = array_merge($options, $custom);
359
360 }
361
362 $store_options = array(); // only save options that are not the default options of this network.
363
364 $fields_replaced = 0;
365
366 foreach($options as $item_name => $item_val)
367 {
368 if (isset($form[$item_name]) && $form[$item_name] != $item_val)
369 {
370 $value = sanitize_text_field($form[$item_name]);
371
372 $store_options[$item_name] = $value;
373 $fields_replaced++;
374 }
375 elseif ( ($item_name == 'active' || $item_name == 'popup') && ! isset($form[$item_name])) // checkbox
376 {
377 $store_options[$item_name] = 0;
378 $fields_replaced++;
379 }
380
381 }
382
383
384 $network_settings[$name] = $store_options;
385
386 update_option('mbsocial_network_settings', $network_settings, false);
387 $this->network_settings = $network_settings;
388
389 $param = array('param' => $name);
390 $output = $this->ajax_networksettings($param, false);
391 $output = $output['output'];
392
393 echo json_encode(
394 array('success' => true,
395 'reload' => false,
396 'title' => __('Settings saved', 'mbsocial'),
397 'output' => $output,
398 'fields_replaced' => $fields_replaced,
399 ));
400 exit('');
401 }
402
403 public function ajax_removesettings($post)
404 {
405 if (isset($post['form']))
406 {
407 $form = array();
408 parse_str($post['form'], $form);
409 }
410
411 $network_name = $form['network'];
412
413 $network = $this->get($network_name);
414 $network_settings = $this->getNetworkSettings();
415
416 $reload = false;
417 $output = false;
418
419 if (! $network->get('is_native') )
420 {
421 $custom = $this->getCustomNetworkSettings();
422 if (isset($custom[$network_name]))
423 {
424 unset($custom[$network_name]);
425 $network_settings['custom'] = $custom;
426 update_option('mbsocial_network_settings', $network_settings, false);
427 }
428 if (isset($network_settings[$network_name])) // custom settings
429 {
430 unset($network_settings[$network_name]);
431 update_option('mbsocial_network_settings', $network_settings, false);
432 $this->network_settings = $network_settings; // new ones.
433 }
434 $reload = true;
435 }
436 else
437 {
438 if (isset($network_settings[$network_name]))
439 {
440 unset($network_settings[$network_name]);
441 update_option('mbsocial_network_settings', $network_settings, false);
442 $this->network_settings = $network_settings; // new ones.
443 }
444 $param = array('param' => $network_name);
445 $output = $this->ajax_networksettings($param, false);
446 $output = $output['output'];
447 }
448
449 echo json_encode(
450 array('success' => true,
451 'reload' => $reload,
452 'output' => $output,
453 'title' => __('Settings removed', 'mbsocial'),
454 ));
455 exit('');
456 }
457
458 public function ajax_showcustomnetworks()
459 {
460 $license = MB()->getClass('license');
461 if (! $license)
462 return false; // something wrong
463
464 $key = $license->get_key();
465
466 $post_args = array(
467 'url' => home_url(),
468 'license_key' => $key,
469
470 );
471
472 $remote_args = array(
473 'method' => 'POST',
474 'body' => array( 'license' => $key ),
475 );
476
477
478 $response = wp_remote_post($this->custom_api_url, $remote_args);
479
480 if ( is_wp_error($response))
481 {
482 // error
483 $output = array('status' => 'error',
484 'message' => __('Remote Error', 'mbsocial'),
485
486
487 );
488 echo json_encode($output);
489 exit();
490 }
491 else {
492 $result = wp_remote_retrieve_body($response);
493
494 $result = json_decode($result, true);
495
496 set_transient('mbsocial-social-networks', $result);
497
498 if (isset($result['status']) && $result['status'] == 'error')
499 {
500 $output = array('status' => 'error',
501 'message' => $result['message'],
502 );
503 echo json_encode($output);
504 exit();
505 }
506
507 $output = $this->parseCustomResult($result);
508
509 echo json_encode(
510 array(
511 'status' => 'success',
512 'output' => $output,
513
514
515 ));
516
517
518 exit();
519 }
520
521 /*
522 $output = array('status' => 'success',
523 'output' => 'output');
524
525 echo json_encode($output);
526 */
527 exit();
528
529 }
530
531 public function parseCustomResult($result)
532 {
533 $output = '
534 <div class= "customnetwork_select">
535 <p class="cns-toolbar"><span class="selected">0</span> ' . __('Selected', 'mbsocial') . '
536 <input type="hidden" name="selected_custom" id="selected_custom" value="">
537 <button type="button" class="mb-ajax-action button-primary" data-action="import-customnetworks" disabled data-param-input="#selected_custom">
538 ' . __('Import', 'mbsocial') . '
539 </button>
540 <ul>';
541
542 $customs = $this->getCustomNetworkSettings();
543
544 foreach($result as $item)
545 {
546 $name = ( isset($item['nice_name']) ) ? $item['nice_name'] : ucfirst($item['name']);
547 $network_name = $item['name'];
548 $icon = isset($item['icon']) ? $item['icon'] : '';
549 $icon_type = isset($item['icon_type']) ? $item['icon_type'] : '';
550
551 $is_imported = isset($customs[$network_name]) ? true : false;
552
553 $the_icon = MBSocial()->admin()->renderIcon(
554 array('icon' => $icon,
555 'icon_type' => $icon_type,
556 'title' => $name,
557 'icon_size' => 20)
558 );
559
560 $output .= '<li class="the_network" data-network="' . $network_name . '">' . $the_icon . $name;
561 if ($is_imported)
562 {
563 $output .= '<span class="is_imported">' . __('Already Imported.', 'mbsocial') . '</span>';
564 }
565 $output .= ' </li>';
566 }
567
568 $output .= '</ul>
569 </div>';
570
571 return $output;
572
573 }
574
575 public function ajax_importcustomnetworks($post)
576 {
577 $network_settings = $this->getNetworkSettings();
578 $custom_networks = get_transient('mbsocial-social-networks');
579
580 $to_import = isset($post['param']) ? $post['param'] : false;
581
582 if (! $to_import)
583 exit(); //error
584
585 $custom_active = isset($network_settings['custom']) ? $network_settings['custom'] : array();
586
587 $import_count = 0;
588 $to_import = explode(',', $to_import);
589
590 foreach($to_import as $item_name)
591 {
592 $network_name = trim($item_name);
593 $new_network = isset($custom_networks[$network_name]) ? $custom_networks[$network_name] : false;
594 //if ($new_network)
595 //{
596 $custom_active[$network_name] = $new_network;
597 $import_count++;
598 //}
599 }
600
601 if ($import_count > 0)
602 {
603 $network_settings['custom'] = $custom_active;
604 update_option('mbsocial_network_settings',$network_settings );
605 }
606
607 echo json_encode(
608 array('status' => 'success',
609 'import_count' => $import_count,
610 'title' => sprintf(__('%s Network(s) Imported','mbsocial'), $import_count),
611 )
612 );
613 exit();
614
615 }
616
617 public function checkPopup()
618 {
619 if (isset($this->network_data["popup"]) && $this->network_data["popup"] )
620 return true;
621 else
622 return false;
623 }
624
625 public function getPopupDimensions()
626 {
627 if (isset($this->network_data["popup_dimensions"]))
628 return $this->network_data["popup_dimensions"];
629 else
630 return array();
631 }
632
633
634
635
636 }
637