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

744 lines 19.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 public function __construct()
21 {
22 $this->setNetworkClasses();
23 $this->getNetworkSettings();
24
25
26 if (is_admin())
27 add_filter( 'http_request_host_is_external', array($this, 'allow_custom_api_host'), 10, 3 );
28 //$this->setNetWorks();
29
30 }
31
32 public function allow_custom_api_host($allow, $host, $url)
33 {
34 $api = parse_url($this->custom_api_url);
35 if ($api['host'] == $host)
36 return true;
37
38 }
39
40 public function get($name = null)
41 {
42 if (! is_null($name))
43 {
44 foreach($this->networks as $class => $network)
45 {
46 if ($name == $class || $name == $network->get('network') )
47 {
48 return $network;
49 }
50 }
51 return false; // not found
52
53 }
54 else
55 {
56 return $this->networks;
57 }
58 }
59
60
61 //public function
62
63 /** Get all available subclasses of main mbNetwork class **/
64 protected function setNetworkClasses()
65 {
66
67 foreach (get_declared_classes() as $class) {
68
69 if (is_subclass_of($class, 'MBSocial\mbNetwork'))
70 {
71 if ($class != 'MBSocial\customNetwork')
72 {
73 $newObj = new $class;
74 $name = $newObj->get('network');
75 $newObj->load_settings( $this->getNetworkSettings($name) );
76 $this->networks[$class] = $newObj;
77
78 }
79 }
80 }
81
82 // network settings that are imported
83 $custom = $this->getCustomNetworkSettings();
84 if ($custom)
85 {
86 foreach($custom as $network)
87 {
88 $name = $network['name'];
89 $network['is_limitedpro'] = true;
90 $new = new CustomNetwork();
91 $new->load_settings($network);
92
93 $this->networks[$name] = $new;
94 }
95 }
96
97 if (! Install::isPRO() )
98 {
99 foreach($this->networks as $index => $class)
100 {
101 if ($class->get('is_limitedpro') === true)
102 {
103 unset($this->networks[$index]);
104 }
105 }
106 }
107 }
108
109 public function getNetworkSettings($name = false)
110 {
111 if (is_null($this->network_settings))
112 $this->network_settings = get_option('mbsocial_network_settings', array());
113
114 if ($name)
115 {
116 if (isset($this->network_settings[$name]))
117 {
118 return $this->network_settings[$name];
119 }
120 else {
121 return array();
122 }
123 }
124 else {
125 return $this->network_settings;
126 }
127 }
128
129 // orginal - take only the imported settings, not the network editor ( used in network editor )
130 public function getCustomNetworkSettings($original = false)
131 {
132 $settings = $this->getNetworkSettings();
133
134 if (is_array($settings))
135 {
136 $custom = isset($settings['custom']) ? $settings['custom'] : false;
137
138 if (! $custom || ! is_array($custom))
139 return false; // no settings.
140
141 foreach($custom as $name => $network)
142 {
143 // merge settings from network editor if any
144 $custom_settings = isset( $settings[$name] ) ? $settings[$name] : false;
145 if ( (! $original) && $custom_settings)
146 {
147 $custom[$name] = array_merge($network, $custom_settings);
148 }
149 }
150 return $custom;
151
152 }
153 return false;
154 }
155
156 public function ajax_networksettings($post, $echo = true)
157 {
158 $network_name = sanitize_text_field($post['param']);
159
160 $network = $this->get($network_name);
161
162 if (! $network)
163 {
164 exit('network not found - ' . $network_name);
165 }
166
167 $network_settings = $this->getNetworkSettings();
168
169 $admin = MBSocial()->admin();
170 maxBlocks::initBlocks();
171
172 $default_options = $network->get_all_defaults(); // network should also load from WP
173 if (isset($network_settings[$network_name]))
174 {
175 $options = $network_settings[$network_name];
176 // per manual
177 $options = $options + $default_options;
178 }
179 else
180 {
181 $options = $default_options;
182 }
183
184 $icon_types = array(
185 'fas' => __('Font Awesome Solid (fas)', 'mbsocial'),
186 'fab' => __("Font Awesome Brand (fab)", 'mbsocial'),
187 'far' => __('Font Awesome Regular (far)', 'mbsocial'),
188 'nucleo' => __('Nucleo', 'mbsocial'),
189 'image' => __('Image', 'mbsocial'),
190 );
191
192 $network_field = new maxField('hidden');
193 $network_field->name = 'network';
194 $network_field->id = $network_field->name;
195 $network_field->value = $network_name;
196
197 $admin->addField($network_field);
198
199 $active = new maxField('switch');
200 $active->id = 'active';
201 $active->name = $active->id;
202 $active->label = __('Active', 'mbsocial');
203 $active->value = 1;
204 $active->checked = checked($options['active'], 1, false);
205
206 $admin->addField($active, 'start','end');
207
208 $label = new maxField('text');
209 $label->id = 'label';
210 $label->name = $label->id;
211 $label->value = $options['label'];
212 $label->label = __('Label', 'mbsocial');
213
214 $admin->addField($label, 'start', 'end');
215
216 $shareurl = new maxField('text');
217 $shareurl->id = 'share_url';
218 $shareurl->name = $shareurl->id;
219 $shareurl->value = $options['share_url'];
220 $shareurl->disabled = true;
221 $shareurl->label = __('Share URL', 'mbsocial');
222
223 $admin->addField($shareurl, 'start', 'end');
224
225 $profileurl = new maxField('text');
226 $profileurl->id = 'profile_url';
227 $profileurl->name = $profileurl->id;
228 $profileurl->value = $options['profile_url'];
229 $profileurl->disabled = true;
230 $profileurl->label = __('Profile URL', 'mbsocial');
231
232 $admin->addField($profileurl,'start', 'end');
233
234 $popup = new maxField('switch');
235 $popup->id = 'popup';
236 $popup->name = $popup->id;
237 $popup->checked = checked($options['popup'], 1, false);
238 $popup->value = 1;
239 $popup->label = __('Open in Popup','mbsocial');
240
241 $admin->addField($popup, 'start', 'end');
242
243 $popupwidth = new maxField('text');
244 $popupwidth->id = 'popup_width';
245 $popupwidth->name = $popupwidth->id;
246 $popupwidth->value = $options['popup_width'];
247 $popupwidth->inputclass ='tiny';
248 $popupwidth->after_input = 'px';
249 $popupwidth->label = __('Popup Width', 'mbsocial');
250
251 $admin->addField($popupwidth, 'start');
252
253 $popupheight = new maxField('text');
254 $popupheight->id = 'popup_height';
255 $popupheight->name = $popupheight->id;
256 $popupheight->value = $options['popup_height'];
257 $popupheight->inputclass = 'tiny';
258 $popupheight->after_input = 'px';
259 $popupheight->label = __('Popup Height', 'mbsocial');
260
261 $admin->addField($popupheight, '', 'end');
262
263 $icon_type = new maxField('option_select');
264 $icon_type->id = 'icon_type';
265 $icon_type->name = $icon_type->id;
266 $icon_type->label = __('Icon Type', 'mbsocial');
267 $icon_type->options = $icon_types;
268 $icon_type->selected = $options['icon_type'];
269
270 $admin->addField($icon_type, 'start', 'end');
271
272 $icon = new maxField('text');
273 $icon->id = 'icon';
274 $icon->name = $icon->id;
275 $icon->value= $options['icon'];
276 $icon->inputclass = 'large';
277 $icon->label = __('Icon', 'mbsocial');
278 $icon->start_conditional = htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('fas','far','fab', 'nucleo') )));
279
280 $admin->addField($icon, 'start', 'end');
281
282 /* $icon_svg = new maxField('textarea');
283 $icon_svg->id = 'icon_svg';
284 $icon_svg->name = $icon_svg->id;
285 $icon_svg->value = $options['icon_svg'];
286 $icon_svg->label = __('SVG', 'mbsocial');
287 $icon_svg->start_conditional = htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('svg') )));
288
289 $admin->addField($icon_svg, 'start', 'end');
290 */
291 Utils::imageUploader(array(
292 'id' => 'icon_image',
293 'show_hover_link' => false,
294 'show_image_data' => false,
295 'label' => __('Icon Image', 'mbsocial'),
296 'data' => $options,
297 'admin' => $admin,
298 'conditional' => htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('image') ))),
299 ));
300
301 $color = new maxField('color');
302 $color->id = 'color';
303 $color->name = $color->id;
304 $color->value = $options['color'];
305 $color->label = __("Network Color", 'mbsocial');
306
307 $admin->addField($color, 'start', 'end');
308
309 $show_mobile = new maxField('switch');
310 $show_mobile->id = 'displayMobile';
311 $show_mobile->name = $show_mobile->id;
312 $show_mobile->value = 1;
313 $show_mobile->checked = checked($options['displayMobile'], 1, false);
314 $show_mobile->label = __('Show on Mobile', 'mbsocial');
315
316 $admin->addField($show_mobile, 'start', 'end');
317
318 $show_desktop = new maxField('switch');
319 $show_desktop->id = 'displayDesktop';
320 $show_desktop->name = $show_desktop->id;
321 $show_desktop->value = 1;
322 $show_desktop->checked = checked($options['displayDesktop'], 1, false);
323 $show_desktop->label = __('Show on Desktop', 'mbsocial');
324
325 $admin->addField($show_desktop, 'start', 'end');
326
327 $saveB = new maxField('button');
328 $saveB->id = 'save_network';
329 $saveB->name = $saveB->id;
330 $saveB->button_label = __('Save Network', 'mbsocial');
331 $saveB->dataaction = 'save-network';
332 $saveB->inputclass = 'mb-ajax-submit button-primary';
333
334 $admin->addField($saveB, 'start', '');
335
336 if (! $network->get('is_native'))
337 {
338 $label = __('Remove Imported Network', 'mbsocial');
339 }
340 else
341 $label = __('Return to defaults', 'mbsocial');
342
343 $default = new maxField('generic');
344 $default->name = 'return_default';
345 $default->id = $default->name;
346 $default->content = '<a class="mb-ajax-submit" data-action="remove-networksettings">' . $label . '</a>';
347
348 $admin->addField($default, '', 'end');
349
350 $fields = $admin->display_fields(true, true);
351
352 $output = array('status' => 'success',
353 'output' => $fields,
354 'title' => sprintf(__('Settings - %s', 'mbsocial'), $network->get_nice_name() ),
355 );
356
357 if ($echo)
358 {
359 echo json_encode($output);
360 exit();
361 }
362 else {
363 return $output;
364 }
365 }
366
367 /** Save network settings */
368 public function ajax_savenetwork($post)
369 {
370
371 if (isset($post['form']))
372 {
373 $form = array();
374 parse_str($post['form'], $form);
375 }
376
377 $name = $form['network'];
378
379 $network = $this->get($name);
380 $network_settings = $this->getNetworkSettings(); // get all network settings
381
382 if (! isset($network_settings[$name])) // create new entry if it doesn't exist.
383 {
384
385 $network_settings[$name] = array();
386 }
387
388 if ($network->get('is_native'))
389 {
390 $options = $network->get_all_defaults(); // get all options
391 }
392 else {
393 $custom = $this->getCustomNetworkSettings(true);
394 $options = $network->get_all_defaults(); // for all is_xx fields
395 $custom = isset($custom[$name]) ? $custom[$name] : array();
396
397 $options = array_merge($options, $custom);
398 }
399
400 $store_options = array(); // only save options that are not the default options of this network.
401
402 $fields_replaced = 0;
403
404 // ugly fix for checkboxes.
405 $checkboxes = array('active', 'popup', 'displayDesktop', 'displayMobile');
406
407 foreach($options as $item_name => $item_val)
408 {
409 if (isset($form[$item_name]) && $form[$item_name] != $item_val)
410 {
411 $value = sanitize_text_field($form[$item_name]);
412
413 $store_options[$item_name] = $value;
414 $fields_replaced++;
415 }
416 elseif ( in_array($item_name, $checkboxes) && ! isset($form[$item_name])) // checkbox
417 {
418 $store_options[$item_name] = 0;
419 $fields_replaced++;
420 }
421
422 }
423
424 $network_settings[$name] = $store_options;
425
426 update_option('mbsocial_network_settings', $network_settings, false);
427 $this->network_settings = $network_settings;
428
429 $param = array('param' => $name);
430 $output = $this->ajax_networksettings($param, false);
431 $output = $output['output'];
432
433 echo json_encode(
434 array('success' => true,
435 'reload' => false,
436 'title' => __('Settings saved', 'mbsocial'),
437 'output' => $output,
438 'fields_replaced' => $fields_replaced,
439 ));
440 exit('');
441 }
442
443 public function ajax_removesettings($post)
444 {
445 if (isset($post['form']))
446 {
447 $form = array();
448 parse_str($post['form'], $form);
449 }
450
451 $network_name = $form['network'];
452
453 $network = $this->get($network_name);
454 $network_settings = $this->getNetworkSettings();
455
456 $reload = false;
457 $output = false;
458
459 if (! $network->get('is_native') )
460 {
461 $custom = $this->getCustomNetworkSettings();
462 if (isset($custom[$network_name])) // the custom definition
463 {
464 unset($custom[$network_name]);
465 $network_settings['custom'] = $custom;
466 update_option('mbsocial_network_settings', $network_settings, false);
467 }
468 if (isset($network_settings[$network_name])) // custom user settings
469 {
470 unset($network_settings[$network_name]);
471 update_option('mbsocial_network_settings', $network_settings, false);
472 $this->network_settings = $network_settings; // new ones.
473 }
474 $reload = true;
475 }
476 else
477 {
478 if (isset($network_settings[$network_name]))
479 {
480 unset($network_settings[$network_name]);
481 update_option('mbsocial_network_settings', $network_settings, false);
482 $this->network_settings = $network_settings; // new ones.
483 }
484 $param = array('param' => $network_name);
485 $output = $this->ajax_networksettings($param, false);
486 $output = $output['output'];
487 }
488
489 echo json_encode(
490 array('success' => true,
491 'reload' => $reload,
492 'output' => $output,
493 'title' => __('Settings removed', 'mbsocial'),
494 ));
495 exit('');
496 }
497
498 public function ajax_showcustomnetworks()
499 {
500 $license = MB()->getClass('license');
501 if (! $license)
502 return false; // something wrong
503
504 $key = $license->get_key();
505
506 $post_args = array(
507 'url' => home_url(),
508 'license_key' => $key,
509
510 );
511
512 $remote_args = array(
513 'method' => 'POST',
514 'body' => array( 'license' => $key ),
515 );
516
517
518 $response = wp_remote_post($this->custom_api_url, $remote_args);
519
520 if ( is_wp_error($response))
521 {
522 // error
523 $output = array('status' => 'error',
524 'message' => __('Remote Error', 'mbsocial'),
525
526
527 );
528 echo json_encode($output);
529 exit();
530 }
531 else {
532 $result = wp_remote_retrieve_body($response);
533
534 $result = json_decode($result, true);
535
536 set_transient('mbsocial-social-networks', $result);
537
538 if (isset($result['status']) && $result['status'] == 'error')
539 {
540 $output = array('status' => 'error',
541 'message' => $result['message'],
542 );
543 echo json_encode($output);
544 exit();
545 }
546
547 $output = $this->parseCustomResult($result);
548
549 echo json_encode(
550 array(
551 'status' => 'success',
552 'output' => $output,
553 'title' => __("Available additional networks", 'mbsocial'),
554 ));
555
556
557 exit();
558 }
559
560 /*
561 $output = array('status' => 'success',
562 'output' => 'output');
563
564 echo json_encode($output);
565 */
566 exit();
567
568 }
569
570 public function parseCustomResult($result)
571 {
572 $output = '
573 <div class= "customnetwork_select">
574 <p class="cns-toolbar"><span class="selected">0</span> ' . __('Selected', 'mbsocial') . '
575 <input type="hidden" name="selected_custom" id="selected_custom" value="">
576 <button type="button" class="mb-ajax-action button-primary" data-action="import-customnetworks" disabled data-param-input="#selected_custom">
577 ' . __('Import', 'mbsocial') . '
578 </button>
579 <ul>';
580
581 $customs = $this->getCustomNetworkSettings();
582
583 foreach($result as $item)
584 {
585 $name = ( isset($item['nice_name']) ) ? $item['nice_name'] : ucfirst($item['name']);
586 $network_name = $item['name'];
587 $icon = isset($item['icon']) ? $item['icon'] : '';
588 $icon_type = isset($item['icon_type']) ? $item['icon_type'] : '';
589
590 $is_imported = isset($customs[$network_name]) ? true : false;
591
592 $icon_args = array('icon' => $icon,
593 'icon_type' => $icon_type,
594 'title' => $name,
595 'icon_size' => 20);
596 if ($icon_type == 'image')
597 {
598 $icon_args['image_url'] = $item['icon_image'];
599 $icon_args['icon_type'] = 'image-remote';
600 }
601
602 $the_icon = MBSocial()->admin()->renderIcon($icon_args
603 );
604
605 $output .= '<li class="the_network" data-network="' . $network_name . '">' . $the_icon . $name;
606 if ($is_imported)
607 {
608 $output .= '<span class="is_imported">' . __('Already Imported.', 'mbsocial') . '</span>';
609 }
610 $output .= ' </li>';
611 }
612
613 $output .= '</ul>
614 </div><p>' . sprintf(__('Network Missing? %s Email us %s','mbsocial'), '<a href="mailto:support@maxfoundry.com">', '</a>') . '</p>';
615
616 return $output;
617
618 }
619
620 public function ajax_importcustomnetworks($post)
621 {
622 $network_settings = $this->getNetworkSettings();
623 $custom_networks = get_transient('mbsocial-social-networks');
624
625 $to_import = isset($post['param']) ? $post['param'] : false;
626
627 if (! $to_import)
628 exit(); //error
629
630 $custom_active = isset($network_settings['custom']) ? $network_settings['custom'] : array();
631
632 $import_count = 0;
633 $to_import = explode(',', $to_import);
634
635 foreach($to_import as $item_name)
636 {
637 $network_name = trim($item_name);
638 $new_network = isset($custom_networks[$network_name]) ? $custom_networks[$network_name] : false;
639 //if ($new_network)
640 //{
641 $new_network = $this->checkRemoteImage($new_network);
642
643 $custom_active[$network_name] = $new_network;
644
645 $import_count++;
646 //}
647 }
648
649 if ($import_count > 0)
650 {
651 $network_settings['custom'] = $custom_active;
652 update_option('mbsocial_network_settings',$network_settings );
653 }
654
655 echo json_encode(
656 array('status' => 'success',
657 'import_count' => $import_count,
658 'title' => sprintf(__('%s Network(s) Imported','mbsocial'), $import_count),
659 )
660 );
661 exit();
662
663 }
664
665 public function checkPopup()
666 {
667 if (isset($this->network_data["popup"]) && $this->network_data["popup"] )
668 return true;
669 else
670 return false;
671 }
672
673 public function getPopupDimensions()
674 {
675 if (isset($this->network_data["popup_dimensions"]))
676 return $this->network_data["popup_dimensions"];
677 else
678 return array();
679 }
680
681
682 protected function checkRemoteImage($data)
683 {
684 if (isset($data['icon_image']) && strlen($data['icon_image']) > 0)
685 {
686 require_once ABSPATH . 'wp-admin/includes/file.php';
687 $image_url = $data['icon_image'];
688 $temp_file = download_url($image_url, 10);
689 if (is_wp_error($temp_file))
690 { var_dump($temp_file); var_dump($image_url); exit('temp file, WP error'); }
691
692 $mime_type = mime_content_type($temp_file);
693 $file = array(
694 'name' => basename($image_url), // ex: wp-header-logo.png
695 'type' => $mime_type,
696 'tmp_name' => $temp_file,
697 'error' => 0,
698 'size' => filesize($temp_file),
699 );
700
701 $overrides = array(
702 'test_form' => false,
703 'test_size' => true,
704 );
705
706 $results = wp_handle_sideload( $file, $overrides );
707
708 if ( !empty( $results['error'] ) ) {
709 exit('sideload failed');
710 } else {
711
712 $filename = $results['file']; // Full path to the file
713 $local_url = $results['url']; // URL to the file in the uploads dir
714 $type = $results['type']; // MIME type of the file
715
716 $wp_upload_dir = wp_upload_dir();
717 // Prepare an array of post data for the attachment.
718 $attachment_data = array(
719 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
720 'post_mime_type' => $type,
721 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
722 'post_content' => '',
723 'post_status' => 'inherit',
724 );
725 $attachment_id = wp_insert_attachment( $attachment_data, $filename );
726 $attach_data = wp_generate_attachment_metadata($attachment_id, $filename);
727 wp_update_attachment_metadata( $attachment_id, $attach_data );
728
729 unset($data['icon_image']);
730 $data['icon_type'] = 'image';
731 $data['icon_image_id'] = $attachment_id;
732 $data['icon_image_url'] = $local_url;
733 $data['icon_image_size'] = '';
734 return $data;
735 }
736 }
737
738 return $data;
739 }
740
741
742
743 } // class
744