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

746 lines 19.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\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->help = __("{url} - will insert the relevant URL <br /> {title} - Will insert the post title ", 'mbsocial');
222 $shareurl->label = __('Share URL', 'mbsocial');
223
224 $admin->addField($shareurl, 'start', 'end');
225
226 $profileurl = new maxField('text');
227 $profileurl->id = 'profile_url';
228 $profileurl->name = $profileurl->id;
229 $profileurl->value = $options['profile_url'];
230 //$profileurl->disabled = true;
231 $profileurl->help = __('{profile} - Will insert the profile from settings into the URL', 'mbsocial');
232 $profileurl->label = __('Profile URL', 'mbsocial');
233
234 $admin->addField($profileurl,'start', 'end');
235
236 $popup = new maxField('switch');
237 $popup->id = 'popup';
238 $popup->name = $popup->id;
239 $popup->checked = checked($options['popup'], 1, false);
240 $popup->value = 1;
241 $popup->label = __('Open in Popup','mbsocial');
242
243 $admin->addField($popup, 'start', 'end');
244
245 $popupwidth = new maxField('text');
246 $popupwidth->id = 'popup_width';
247 $popupwidth->name = $popupwidth->id;
248 $popupwidth->value = $options['popup_width'];
249 $popupwidth->inputclass ='small';
250 $popupwidth->after_input = 'px';
251 $popupwidth->label = __('Popup Width', 'mbsocial');
252
253 $admin->addField($popupwidth, 'start');
254
255 $popupheight = new maxField('text');
256 $popupheight->id = 'popup_height';
257 $popupheight->name = $popupheight->id;
258 $popupheight->value = $options['popup_height'];
259 $popupheight->inputclass = 'tiny';
260 $popupheight->after_input = 'px';
261 $popupheight->label = __('Popup Height', 'mbsocial');
262
263 $admin->addField($popupheight, '', 'end');
264
265 $icon_type = new maxField('option_select');
266 $icon_type->id = 'icon_type';
267 $icon_type->name = $icon_type->id;
268 $icon_type->label = __('Icon Type', 'mbsocial');
269 $icon_type->options = $icon_types;
270 $icon_type->selected = $options['icon_type'];
271
272 $admin->addField($icon_type, 'start', 'end');
273
274 $icon = new maxField('text');
275 $icon->id = 'icon';
276 $icon->name = $icon->id;
277 $icon->value= $options['icon'];
278 $icon->inputclass = 'large';
279 $icon->label = __('Icon', 'mbsocial');
280 $icon->start_conditional = htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('fas','far','fab', 'nucleo') )));
281
282 $admin->addField($icon, 'start', 'end');
283
284 /* $icon_svg = new maxField('textarea');
285 $icon_svg->id = 'icon_svg';
286 $icon_svg->name = $icon_svg->id;
287 $icon_svg->value = $options['icon_svg'];
288 $icon_svg->label = __('SVG', 'mbsocial');
289 $icon_svg->start_conditional = htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('svg') )));
290
291 $admin->addField($icon_svg, 'start', 'end');
292 */
293 Utils::imageUploader(array(
294 'id' => 'icon_image',
295 'show_hover_link' => false,
296 'show_image_data' => false,
297 'label' => __('Icon Image', 'mbsocial'),
298 'data' => $options,
299 'admin' => $admin,
300 'conditional' => htmlentities(json_encode(array('target' => $icon_type->id, 'values' => array('image') ))),
301 ));
302
303 $color = new maxField('color');
304 $color->id = 'color';
305 $color->name = $color->id;
306 $color->value = $options['color'];
307 $color->label = __("Network Color", 'mbsocial');
308
309 $admin->addField($color, 'start', 'end');
310
311 $show_mobile = new maxField('switch');
312 $show_mobile->id = 'displayMobile';
313 $show_mobile->name = $show_mobile->id;
314 $show_mobile->value = 1;
315 $show_mobile->checked = checked($options['displayMobile'], 1, false);
316 $show_mobile->label = __('Show on Mobile', 'mbsocial');
317
318 $admin->addField($show_mobile, 'start', 'end');
319
320 $show_desktop = new maxField('switch');
321 $show_desktop->id = 'displayDesktop';
322 $show_desktop->name = $show_desktop->id;
323 $show_desktop->value = 1;
324 $show_desktop->checked = checked($options['displayDesktop'], 1, false);
325 $show_desktop->label = __('Show on Desktop', 'mbsocial');
326
327 $admin->addField($show_desktop, 'start', 'end');
328
329 $saveB = new maxField('button');
330 $saveB->id = 'save_network';
331 $saveB->name = $saveB->id;
332 $saveB->button_label = __('Save Network', 'mbsocial');
333 $saveB->dataaction = 'save-network';
334 $saveB->inputclass = 'mb-ajax-submit button-primary';
335
336 $admin->addField($saveB, 'start', '');
337
338 if (! $network->get('is_native'))
339 {
340 $label = __('Remove Imported Network', 'mbsocial');
341 }
342 else
343 $label = __('Return to defaults', 'mbsocial');
344
345 $default = new maxField('generic');
346 $default->name = 'return_default';
347 $default->id = $default->name;
348 $default->content = '<a class="mb-ajax-submit" data-action="remove-networksettings">' . $label . '</a>';
349
350 $admin->addField($default, '', 'end');
351
352 $fields = $admin->display_fields(true, true);
353
354 $output = array('status' => 'success',
355 'output' => $fields,
356 'title' => sprintf(__('Settings - %s', 'mbsocial'), $network->get_nice_name() ),
357 );
358
359 if ($echo)
360 {
361 echo json_encode($output);
362 exit();
363 }
364 else {
365 return $output;
366 }
367 }
368
369 /** Save network settings */
370 public function ajax_savenetwork($post)
371 {
372
373 if (isset($post['form']))
374 {
375 $form = array();
376 parse_str($post['form'], $form);
377 }
378
379 $name = $form['network'];
380
381 $network = $this->get($name);
382 $network_settings = $this->getNetworkSettings(); // get all network settings
383
384 if (! isset($network_settings[$name])) // create new entry if it doesn't exist.
385 {
386
387 $network_settings[$name] = array();
388 }
389
390 if ($network->get('is_native'))
391 {
392 $options = $network->get_all_defaults(); // get all options
393 }
394 else {
395 $custom = $this->getCustomNetworkSettings(true);
396 $options = $network->get_all_defaults(); // for all is_xx fields
397 $custom = isset($custom[$name]) ? $custom[$name] : array();
398
399 $options = array_merge($options, $custom);
400 }
401
402 $store_options = array(); // only save options that are not the default options of this network.
403
404 $fields_replaced = 0;
405
406 // ugly fix for checkboxes.
407 $checkboxes = array('active', 'popup', 'displayDesktop', 'displayMobile');
408
409 foreach($options as $item_name => $item_val)
410 {
411 if (isset($form[$item_name]) && $form[$item_name] != $item_val)
412 {
413 $value = sanitize_text_field($form[$item_name]);
414
415 $store_options[$item_name] = $value;
416 $fields_replaced++;
417 }
418 elseif ( in_array($item_name, $checkboxes) && ! isset($form[$item_name])) // checkbox
419 {
420 $store_options[$item_name] = 0;
421 $fields_replaced++;
422 }
423
424 }
425
426 $network_settings[$name] = $store_options;
427
428 update_option('mbsocial_network_settings', $network_settings, false);
429 $this->network_settings = $network_settings;
430
431 $param = array('param' => $name);
432 $output = $this->ajax_networksettings($param, false);
433 $output = $output['output'];
434
435 echo json_encode(
436 array('success' => true,
437 'reload' => false,
438 'title' => __('Settings saved', 'mbsocial'),
439 'output' => $output,
440 'fields_replaced' => $fields_replaced,
441 ));
442 exit('');
443 }
444
445 public function ajax_removesettings($post)
446 {
447 if (isset($post['form']))
448 {
449 $form = array();
450 parse_str($post['form'], $form);
451 }
452
453 $network_name = $form['network'];
454
455 $network = $this->get($network_name);
456 $network_settings = $this->getNetworkSettings();
457
458 $reload = false;
459 $output = false;
460
461 if (! $network->get('is_native') )
462 {
463 $custom = $this->getCustomNetworkSettings();
464 if (isset($custom[$network_name])) // the custom definition
465 {
466 unset($custom[$network_name]);
467 $network_settings['custom'] = $custom;
468 update_option('mbsocial_network_settings', $network_settings, false);
469 }
470 if (isset($network_settings[$network_name])) // custom user settings
471 {
472 unset($network_settings[$network_name]);
473 update_option('mbsocial_network_settings', $network_settings, false);
474 $this->network_settings = $network_settings; // new ones.
475 }
476 $reload = true;
477 }
478 else
479 {
480 if (isset($network_settings[$network_name]))
481 {
482 unset($network_settings[$network_name]);
483 update_option('mbsocial_network_settings', $network_settings, false);
484 $this->network_settings = $network_settings; // new ones.
485 }
486 $param = array('param' => $network_name);
487 $output = $this->ajax_networksettings($param, false);
488 $output = $output['output'];
489 }
490
491 echo json_encode(
492 array('success' => true,
493 'reload' => $reload,
494 'output' => $output,
495 'title' => __('Settings removed', 'mbsocial'),
496 ));
497 exit('');
498 }
499
500 public function ajax_showcustomnetworks()
501 {
502 $license = MB()->getClass('license');
503 if (! $license)
504 return false; // something wrong
505
506 $key = $license->get_key();
507
508 $post_args = array(
509 'url' => home_url(),
510 'license_key' => $key,
511
512 );
513
514 $remote_args = array(
515 'method' => 'POST',
516 'body' => array( 'license' => $key ),
517 );
518
519
520 $response = wp_remote_post($this->custom_api_url, $remote_args);
521
522 if ( is_wp_error($response))
523 {
524 // error
525 $output = array('status' => 'error',
526 'message' => __('Remote Error', 'mbsocial'),
527
528
529 );
530 echo json_encode($output);
531 exit();
532 }
533 else {
534 $result = wp_remote_retrieve_body($response);
535
536 $result = json_decode($result, true);
537
538 set_transient('mbsocial-social-networks', $result);
539
540 if (isset($result['status']) && $result['status'] == 'error')
541 {
542 $output = array('status' => 'error',
543 'message' => $result['message'],
544 );
545 echo json_encode($output);
546 exit();
547 }
548
549 $output = $this->parseCustomResult($result);
550
551 echo json_encode(
552 array(
553 'status' => 'success',
554 'output' => $output,
555 'title' => __("Available additional networks", 'mbsocial'),
556 ));
557
558
559 exit();
560 }
561
562 /*
563 $output = array('status' => 'success',
564 'output' => 'output');
565
566 echo json_encode($output);
567 */
568 exit();
569
570 }
571
572 public function parseCustomResult($result)
573 {
574 $output = '
575 <div class= "customnetwork_select">
576 <p class="cns-toolbar"><span class="selected">0</span> ' . __('Selected', 'mbsocial') . '
577 <input type="hidden" name="selected_custom" id="selected_custom" value="">
578 <button type="button" class="mb-ajax-action button-primary" data-action="import-customnetworks" disabled data-param-input="#selected_custom">
579 ' . __('Import', 'mbsocial') . '
580 </button>
581 <ul>';
582
583 $customs = $this->getCustomNetworkSettings();
584
585 foreach($result as $item)
586 {
587 $name = ( isset($item['nice_name']) ) ? $item['nice_name'] : ucfirst($item['name']);
588 $network_name = $item['name'];
589 $icon = isset($item['icon']) ? $item['icon'] : '';
590 $icon_type = isset($item['icon_type']) ? $item['icon_type'] : '';
591
592 $is_imported = isset($customs[$network_name]) ? true : false;
593
594 $icon_args = array('icon' => $icon,
595 'icon_type' => $icon_type,
596 'title' => $name,
597 'icon_size' => 20);
598 if ($icon_type == 'image')
599 {
600 $icon_args['image_url'] = $item['icon_image'];
601 $icon_args['icon_type'] = 'image-remote';
602 }
603
604 $the_icon = MBSocial()->admin()->renderIcon($icon_args
605 );
606
607 $output .= '<li class="the_network" data-network="' . $network_name . '">' . $the_icon . $name;
608 if ($is_imported)
609 {
610 $output .= '<span class="is_imported">' . __('Already Imported.', 'mbsocial') . '</span>';
611 }
612 $output .= ' </li>';
613 }
614
615 $output .= '</ul>
616 </div><p>' . sprintf(__('Network Missing? %s Email us %s','mbsocial'), '<a href="mailto:support@maxfoundry.com">', '</a>') . '</p>';
617
618 return $output;
619
620 }
621
622 public function ajax_importcustomnetworks($post)
623 {
624 $network_settings = $this->getNetworkSettings();
625 $custom_networks = get_transient('mbsocial-social-networks');
626
627 $to_import = isset($post['param']) ? $post['param'] : false;
628
629 if (! $to_import)
630 exit(); //error
631
632 $custom_active = isset($network_settings['custom']) ? $network_settings['custom'] : array();
633
634 $import_count = 0;
635 $to_import = explode(',', $to_import);
636
637 foreach($to_import as $item_name)
638 {
639 $network_name = trim($item_name);
640 $new_network = isset($custom_networks[$network_name]) ? $custom_networks[$network_name] : false;
641 //if ($new_network)
642 //{
643 $new_network = $this->checkRemoteImage($new_network);
644
645 $custom_active[$network_name] = $new_network;
646
647 $import_count++;
648 //}
649 }
650
651 if ($import_count > 0)
652 {
653 $network_settings['custom'] = $custom_active;
654 update_option('mbsocial_network_settings',$network_settings );
655 }
656
657 echo json_encode(
658 array('status' => 'success',
659 'import_count' => $import_count,
660 'title' => sprintf(__('%s Network(s) Imported','mbsocial'), $import_count),
661 )
662 );
663 exit();
664
665 }
666
667 public function checkPopup()
668 {
669 if (isset($this->network_data["popup"]) && $this->network_data["popup"] )
670 return true;
671 else
672 return false;
673 }
674
675 public function getPopupDimensions()
676 {
677 if (isset($this->network_data["popup_dimensions"]))
678 return $this->network_data["popup_dimensions"];
679 else
680 return array();
681 }
682
683
684 protected function checkRemoteImage($data)
685 {
686 if (isset($data['icon_image']) && strlen($data['icon_image']) > 0)
687 {
688 require_once ABSPATH . 'wp-admin/includes/file.php';
689 $image_url = $data['icon_image'];
690 $temp_file = download_url($image_url, 10);
691 if (is_wp_error($temp_file))
692 { var_dump($temp_file); var_dump($image_url); exit('temp file, WP error'); }
693
694 $mime_type = mime_content_type($temp_file);
695 $file = array(
696 'name' => basename($image_url), // ex: wp-header-logo.png
697 'type' => $mime_type,
698 'tmp_name' => $temp_file,
699 'error' => 0,
700 'size' => filesize($temp_file),
701 );
702
703 $overrides = array(
704 'test_form' => false,
705 'test_size' => true,
706 );
707
708 $results = wp_handle_sideload( $file, $overrides );
709
710 if ( !empty( $results['error'] ) ) {
711 exit('sideload failed');
712 } else {
713
714 $filename = $results['file']; // Full path to the file
715 $local_url = $results['url']; // URL to the file in the uploads dir
716 $type = $results['type']; // MIME type of the file
717
718 $wp_upload_dir = wp_upload_dir();
719 // Prepare an array of post data for the attachment.
720 $attachment_data = array(
721 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
722 'post_mime_type' => $type,
723 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
724 'post_content' => '',
725 'post_status' => 'inherit',
726 );
727 $attachment_id = wp_insert_attachment( $attachment_data, $filename );
728 $attach_data = wp_generate_attachment_metadata($attachment_id, $filename);
729 wp_update_attachment_metadata( $attachment_id, $attach_data );
730
731 unset($data['icon_image']);
732 $data['icon_type'] = 'image';
733 $data['icon_image_id'] = $attachment_id;
734 $data['icon_image_url'] = $local_url;
735 $data['icon_image_size'] = '';
736 return $data;
737 }
738 }
739
740 return $data;
741 }
742
743
744
745 } // class
746