PluginProbe
Social Share Buttons / 1.1
Social Share Buttons v1.1
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 / blocks / network-block.php

network-block.php in Social Share Buttons 1.1, at classes/blocks/network-block.php

736 lines 19.9 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 $collectionBlock["network"] = array('order' => 10,
6 'class' => "networkBlock");
7
8 use \simple_html_dom as simple_html_dom;
9 use \MaxButtons\maxField as maxField;
10 use \MaxButtons\maxBlocks as maxBlocks;
11 use \MaxButtons\maxInstall as maxInstall;
12
13 class networkBlock extends block
14 {
15 protected $blockname = "network";
16 protected $fields = array( 'network_active' => array('default' => array() )
17
18 );
19
20 protected $meta_fields = array(
21 'share_url' => array('default' => ''),
22 );
23
24 protected $share_data;
25
26 protected $index_helper = -1;
27
28 public function __construct()
29 {
30 parent::__construct();
31 MBSocial()->whistle()->listen('display/parse/network', array($this, 'setShareData'), 'tell');
32 }
33
34 public function parse($domObj, $args = array() )
35 {
36 $w = MBSocial()->whistle();
37
38 $output = '';
39 $blockdata = $this->data[$this->blockname];
40 $admin = MBSocial()->admin();
41
42 $network = $w->ask('display/parse/network');
43 $popup = $network->is_popup();
44
45 $itemObj = $domObj->find('a', 0);
46
47 $url = $itemObj->href;
48
49 // replace all {} variables with data
50 $url = $this->applyVars($url, $args);
51
52 // remove empty query parts from the URL
53 $parts = parse_url($url);
54 if(isset($parts['query']) > 0)
55 {
56 parse_str($parts['query'], $query);
57 foreach($query as $query_arg => $query_val )
58 {
59 if (strlen($query_val) == 0)
60 $url = remove_query_arg($query_arg, $url);
61 }
62 }
63
64 $itemObj->href = $url;
65
66 if ($popup)
67 {
68 $tag = 'data-popup';
69 $dimensions = $network->get('popup_dimensions');
70 $dimensions = array('width' => $dimensions[0], 'height' => $dimensions[1]);
71 $itemObj->$tag = htmlentities(json_encode($dimensions));
72 }
73 else {
74 if (! $network->get('forcesamewindow'))
75 $itemObj->target = "_blank";
76
77 }
78
79 $itemObj->rel = 'noopener nofollow';
80
81 $icon = $this->renderIcon();
82 if ($icon)
83 {
84 $obj = $domObj->find('.mb-social', 0);
85 if (is_object($obj))
86 {
87 $obj->innertext .= "<span class='mb-icon-wrapper'>
88 " . $icon . " </span>";
89 $this->collection->getStyle()->addDisplay('icon');
90 }
91 }
92
93 return $domObj;
94
95 }
96
97
98 public function renderIcon()
99 {
100 $w= MBSocial()->whistle();
101 $network = $w->ask('display/parse/network');
102
103 $icon = $network->get('icon');
104 $icon_type = $network->get('icon_type');
105
106 /*$fa5_present = method_exists('maxInstall', 'searchNewFa');
107 var_dump($fa5_present);
108 var_dump($icon_type);
109 $fa5_present = true;
110 if ($icon_type == 'fa' && $fa5_present)
111 {
112 $conversion_path = MB()->get_plugin_path() . '/assets/libraries/font-awesome-5/shims.json';
113 $conversion_array = json_decode(file_get_contents($conversion_path), ARRAY_A);
114
115 $icon = str_replace('fa-', '', $icon);
116 $icon_type = '';
117
118 $icon = maxInstall::searchNewFA($icon, $conversion_array);
119 var_dump($icon);
120 } */
121
122 $network_name = $network->get_nice_name();
123
124 switch($icon_type)
125 {
126 case 'fa':
127 case 'fab':
128 case 'nucleo':
129 $output = "<span class='mb-icon'><i class='" . $icon_type . " " . $icon . "' title='" . $network_name . "' > </i></span>";
130 break;
131 case 'svg':
132 case 'png':
133 $output = "<span class='mb-icon " . $icon_type . "'><img src='" . $icon . "' alt='" . $network_name . "'></span>";
134 break;
135
136 default:
137 $output = '';
138 break;
139 }
140
141 return apply_filters('display/render/icon', $output, $this, $network);
142
143 }
144
145 /** Get data from the page to use in sharing. Like page title, url and image
146
147 **/
148 public function setShareData()
149 {
150 if (! is_null($this->share_data))
151 return $this->share_data;
152
153 $w = MBSocial()->whistle();
154
155 $url = '';
156 $title = '';
157 $img = '';
158
159 $hook = $w->ask('display/env/hook'); // $this->collection->getHook();
160
161 $post_id = $w->ask('display/env/post_id');
162
163 $share_setting = 'auto';
164
165 if ( isset($this->data['display'][$hook . '_options']))
166 {
167 $options = $this->data['display'][$hook . '_options'];
168
169 $share_setting = isset($options['share_setting']) ? $options['share_setting'] : 'auto';
170 $custom = isset($options['share_custom_url']) ? $options['share_custom_url'] : '';
171 }
172 switch($share_setting)
173 {
174 case "home":
175 $url = get_home_url();
176 $title = get_bloginfo('name');
177 $img = $this->getImage(null, true);
178 break;
179 case "custom-url";
180 $url = $custom;
181 $img = $this->getImage();
182 break;
183 case "current-post":
184 $url = get_permalink();
185 $title = get_the_title();
186 $img = $this->getImage();
187 break;
188 case "auto":
189 default:
190 /* After / before being placed on the post data, therefore by default (auto) always use the POST URL
191 Static is being placed once(1) on the page, so get the greated to-share url ( of the whole section ) */
192 if ($this->collection->is_post)
193 {
194 $url = get_permalink();
195 $title = get_the_title();
196 $img = $this->getImage();
197
198 }
199 if ($this->collection->is_static)
200 {
201
202 if (is_front_page())
203 {
204 $url = get_home_url();
205 $title = get_bloginfo('name');
206 $img = $this->getImage(null, true);
207 }
208 elseif (is_category() )
209 {
210 $obj = get_queried_object();
211 $category_id = $obj->cat_ID;
212 $url = get_category_link($category_id);
213 $title = $obj->category_nicename;
214
215 }
216 else // are there any other cases? Page should give back their main url like this.
217 {
218 $url = get_permalink();
219 $title = get_the_title();
220 $img = $this->getImage();
221 }
222 }
223
224 break;
225 }
226
227 // Check for custom URL settings
228 if ($post_id > 0)
229 {
230 $metadata = $this->get_block_meta_data($post_id);
231 if (isset($metadata['share_url']) && strlen( trim($metadata['share_url']) ) > 0 )
232 {
233 $url = $metadata['share_url'];
234 }
235 }
236
237 $data = array("url" => $url,
238 "title" => $title,
239 "img" => $img);
240
241 $w->tell('display/vars/url', $url);
242 $w->tell('display/vars/title', $title);
243 $w->tell('display/vars/img', $img);
244
245 $this->share_data = $data;
246 return $data;
247
248 }
249
250 protected function getImage($post_id = null, $home = false)
251 {
252 if (is_admin())
253 return ''; // in admin all this is not set.
254
255 if ($home && get_option('show_on_front') === 'page')
256 {
257 $post_id = get_option('page_on_front');
258
259 }
260
261 $thumb_id = get_post_thumbnail_id($post_id); // First try to find thumbnail on the content
262
263 $image = false;
264 if (is_numeric($thumb_id))
265 {
266 $image = wp_get_attachment_url($thumb_id);
267 }
268
269 if ($image !== false)
270 return $image;
271
272 // In case of no thumbnails, tries to find first image from current content.
273
274 // if (! is_null($post_id))
275 // {
276 $post = get_post($post_id);
277 $content = '';
278 if (! is_null($post))
279 $content = $post->post_content;
280 /* }
281 else
282 {
283
284 $content = get_the_content();
285 } */
286 $domObj = new simple_html_dom();
287 $domObj->load($content);
288
289 $img = $domObj->find('img', 0);
290 if ($img)
291 {
292 $image = $img->src;
293 return $image;
294 }
295 else
296 return '';
297 }
298
299 public function applyVars($string)
300 {
301 //$vars = $this->getShareData();
302 $w = MBSocial()->whistle();
303
304 preg_match_all('/\{(.*?)\}/im', $string, $matches);
305 //$vars = (isset($vars[0]) && count($vars[0]) > 0) ? $vars[0] : array();
306
307 if (count($matches) == 0)
308 return $string;
309
310 $vars = $matches[0]; // i.e. {url}
311 $clean_vars = $matches[1]; // i.e. url
312
313 for($i = 0; $i < count($vars); $i++)
314 {
315 $var = $vars[$i];
316 $clean_var = $clean_vars[$i];
317
318 if (strpos($string, $var) !== false)
319 {
320 $val = $w->ask('display/vars/' . $clean_var);
321 $string = str_replace($var, $val, $string);
322 }
323
324 }
325
326
327
328 /*if (isset($vars["url"]))
329 $string = str_replace("{url}", urlencode(esc_url($vars["url"])), $string);
330 if (isset($vars["count"]))
331 {
332 $count = $this->formatCount($vars["count"]);
333 $string = str_replace("{count}", "<span class='share_count'>" . $count . "</span>", $string);
334 $string = str_replace("{c}", "<span class='share_count'>" . $count . "</span>", $string);
335 }
336 if (isset($vars["title"]))
337 $string = str_replace("{title}", $vars["title"], $string);
338 if (isset($vars["network_name"]))
339 $string = str_replace("{network_name}", $vars["network_name"], $string);
340
341 if (isset($vars["img"]))
342 $string = str_replace("{img}", $vars["img"], $string);
343 */
344 return $string;
345 }
346
347 function save_fields($data, $post)
348 {
349 $data = parent::save_fields($data, $post);
350
351 $selection = array();
352
353 $button = MB()->getClass("button");
354 $network_active = isset($post['network_item_active']) ? $post['network_item_active'] : array();
355
356
357 //mbcustom
358 $mbcustom = isset($post['mbcustom_id']) ? $post['mbcustom_id'] : array();
359 $data[$this->blockname]['mbcustom'] = array();
360
361 if (is_array($mbcustom))
362 {
363 $i = 0;
364 foreach($mbcustom as $index => $button_id)
365 {
366 if ( intval($button_id) > 0)
367 {
368 $usenetwork = isset($post['mbcustom_usenetwork'][$index]) ? intval($post['mbcustom_usenetwork'][$index]) : 0;
369 $network = isset($post['mbcustom_network'][$index]) ? sanitize_text_field($post['mbcustom_network'][$index]) : '';
370 $url = isset($post['mbcustom_url'][$index]) ? sanitize_text_field($post['mbcustom_url'][$index]) : '';
371 $text = isset($post['mbcustom_text'][$index]) ? sanitize_text_field($post['mbcustom_text'][$index]) : '';
372
373 $data[$this->blockname]['mbcustom'][$i] = array(
374 'button_id' => $button_id,
375 'use_network' => $usenetwork,
376 'network' => $network,
377 'url' => $url,
378 'text' => $text,
379 );
380 $i++; // this is not index. Start saving from index 0.
381 }
382
383
384 }
385 }
386
387 //echo "<PRE>"; print_R($post); print_R($data[$this->blockname]['mbcustom'] ); echo "</PRE>";
388 // new user, give default networks
389 $collection_id = $this->collection->getID();
390 if (count($network_active) == 0 && $collection_id == 0)
391 $network_active = array('facebook', 'twitter', 'linkedin'); // default options
392
393 $data[$this->blockname]["network_active"] = $network_active;
394 return $data;
395 }
396
397
398 public function do_meta_boxes($content, $post)
399 {
400 $admin = mbSocial()->admin();
401
402 $metadata = $this->get_block_meta_data($post->ID);
403
404 $url = new maxField();
405 $url->id = 'share_url';
406 $url->name = $url->id;
407 $url->label = __('URL to Share', 'mbsocial');
408 $url->note = __('By default the URL of the post is being shared. Leave empty for default', 'mbsocial');
409 $url->value = isset($metadata['share_url']) ? $metadata['share_url'] : '';
410 $url->placeholder = 'https://';
411
412 $admin->addField($url, 'start', 'end');
413
414 $fields = $admin->display_fields(true, true, false);
415
416
417 $content['share'] = array('title' => __('Share Options', 'mbsocial'),
418 'icon' => 'alt-share',
419 'content' => $fields,
420 );
421
422 return $content;
423
424 }
425
426 public function admin()
427 {
428 $w = mbSocial()->whistle();
429
430 $admin = mbSocial()->admin();
431 $blockdata = $this->data[$this->blockname];
432
433 $active_networks = isset($blockdata['network_active']) ? $blockdata['network_active'] : array();
434
435 $networks = $admin->get_networks($active_networks);
436
437 $styleObj = $this->collection->getStyle();
438 ?>
439 <div class='help-side'>
440 <h3><?php _e('Social Networks','mbsocial'); ?></h3>
441 <p>Drag and drop the networks you want to use into the 'active' box. You can find additional networks under the 'more networks' tab.</p>
442 <p>Hold the mouse pointer over the network icon to see the network's name</p>
443 <p>The <strong>Share Page</strong> networks will share your website's URL to other networks. </p>
444 <p>The <strong>Share Profile</strong> networks will link from the website to your social accounts</p>
445 </div>
446 <div class='options networks option-container' id='networkBlock' data-refresh='previewBlock'>
447 <?php
448 $field_obj = new maxField('hidden');
449 $field_obj->id = 'network_trigger_change';
450 $field_obj->name = $field_obj->id;
451 $field_obj->value = '';
452 $field_obj->placeholder = '';
453
454 $admin->addField($field_obj);
455 $admin->addUpdate($field_obj);
456 $admin->display_fields();
457
458 ?>
459 <div class='title'><?php _e('Social Networks','mbsocial') ?> </div>
460 <div class='inside'>
461 <div class='option active_network'>
462 <label><?php _e('Active','mbsocial'); ?></label>
463 <div class='drag-area input' data-area='active'>
464 <span class="updatables hidden">input[name="network_active"], input[name="network_item_active"]</span>
465
466 <?php if (isset($networks['selected'])): foreach($networks['selected'] as $index => $network): ?>
467
468 <?php
469 $args = array();
470 $args['input'] = 'network_item_active[]';
471 $args['network'] = $network;
472
473 $this->printNetwork($index, $network, $args );
474
475 ?>
476 <?php endforeach; endif; ?>
477 </div>
478 </div>
479
480
481 <div class='option inactive_network'>
482 <label><?php _e('Inactive', 'mbsocial'); ?></label>
483 <div class='drag-area input' data-area='inactive'>
484 <?php if (isset($networks['unselected'])): foreach($networks['unselected'] as $index => $network): ?>
485
486 <?php $this->printNetwork($index, $network); ?>
487
488 <?php endforeach; endif; ?>
489 </div>
490 </div>
491
492 <div class='option'>
493 <label>&nbsp;</label>
494 <div class='see-more toggle input'><span class='title'><?php _e('more networks', 'mbsocial'); ?></span>
495 <span class='see-more-wrap option toggle-target'>
496
497 <div class='drag-area'>
498 <?php if (isset($networks['readmore'])): foreach($networks['readmore'] as $index => $network): ?>
499
500 <?php $this->printNetwork($index, $network); ?>
501
502 <?php endforeach; endif; ?>
503 </div>
504
505 <p><?php _e("Need another network?","mbsocial"); ?>
506 <a href='mailto:support@maxfoundry.com'><?php _e('Email us','mbsocial'); ?> </a></p>
507 </span>
508
509 </div>
510 </div> <!-- option -->
511
512 <?php
513 // find caps of Networks
514 $networks = mbSocial()->networks()->get();
515 $nw_share = array();
516 $nw_profile = array();
517 foreach($networks as $nw)
518 {
519 if ($nw->is_share_icon())
520 {
521 $nw_profile[] = $nw->get_nice_name();
522 }
523 if ($nw->is_social_share())
524 {
525 $nw_share[] = $nw->get_nice_name();
526 }
527 }
528 sort($nw_share);
529 sort($nw_profile);
530 ?>
531
532 <div class='option network-help'>
533 <label>&nbsp;</label>
534
535 <!-- <p>
536 <div><span class='item'><span class="legend-circle share-icon"></span> <?php _e('Share Page', 'mbsocial'); ?> </span></div>
537 <div><span class='item'><span class="legend-circle social-icon"></span> <?php _e('Share Profile','mbsocial'); ?></span></div>
538 <div><span class='item'><span class="legend-circle share-icon"></span><span class="legend-circle social-icon"></span> <?php _e('Can be used as both','mbsocial'); ?></span></div>
539 </p> -->
540
541 <p><?php _e(sprintf("The Share Profile networks will link from the website to your social accounts: %s networks.", implode(', ', $nw_profile)), 'mbsocial'
542 ); ?> </p>
543
544 <p><?php _e(
545 sprintf("The Share Page networks will share your websites URL to other networks: These include %s networks.", implode(', ', $nw_share)), 'mbsocial'
546 ); ?>
547 </p>
548
549
550 <?php if (Install::isPro()): ?>
551 <p><?php _e('The MaxButtons option lets you a MaxButton and configure it.','mbsocial') ?></p>
552 <?php endif; ?>
553 </div>
554
555 <div class='hidden mbcustom-helper'>
556
557 <?php
558 $this->printMBCustomButton();
559 $this->printMBCustomOptions(array('index' => -1)); ?>
560 </div>
561
562 </div> <!-- inside -->
563 </div> <!-- option-container -->
564 <?php
565 }
566
567 protected function printNetwork($index, $network, $args = array() )
568 {
569 if (! is_object($network))
570 {
571 return false;
572 }
573 $defaults = array(
574 'input' => 'network_active',
575 'content' => '',
576 'network' => null);
577
578 $args = wp_parse_args($args, $defaults);
579
580 $w = mbSocial()->whistle();
581 $w->tell('display/parse/network', $network);
582 $network_name = $network->get('network');
583 $label = $network->get_nice_name();
584 $icon_output = $this->renderIcon($network);
585 $is_share_icon = $network->is_share_icon();
586 $is_social_share = $network->is_social_share();
587
588 $input = $args['input'];
589
590 $classes = $network_name;
591 ?>
592
593
594 <span class="item <?php echo $classes ?> " title="<?php echo $label ?>">
595 <?php echo $icon_output ?>
596
597 <?php
598 /* if ($is_share_icon)
599 echo '<span class="legend-circle share-icon"></span>';
600 if ($is_social_share)
601 echo '<span class="legend-circle social-icon"></span>';
602 */
603 ?>
604 <input type='hidden' name='<?php echo $input ?>' value='<?php echo $network_name ?>'>
605
606 <?php echo $args['content']; ?>
607 <?php
608 if (! is_null($args['network']))
609 {
610 $network = $args['network'];
611 if ($network->get('network') == 'maxbutton')
612 {
613 $this->printMBCustomButton();
614 $this->printMBCustomOptions(array('index' => $index));
615 }
616 }
617 ?>
618 </span>
619 <?php
620 }
621
622
623 protected function printMBCustomOptions($args = array() )
624 {
625 $index = isset($args['index']) ? intval($args['index']) : 0;
626
627 $admin = mbSocial()->admin();
628 $networks = mbSocial()->networks()->get();
629
630 $nw_options = array();
631 foreach($networks as $network) // list all networks, for option.
632 {
633 $network_name = $network->get('network');
634 $label = $network->get_nice_name();
635
636 if ($network_name == 'maxbutton')
637 continue;
638
639 $nw_options[$network_name] = $label;
640 }
641
642 $data = $this->data[$this->blockname];
643
644
645 if (isset($data['mbcustom']))
646 {
647 $customs = $data['mbcustom'];
648
649 $this->index_helper++;
650 $settings = isset($customs[$this->index_helper]) ? $customs[$this->index_helper] : false;
651 $button_id = isset($settings['button_id']) ? $settings['button_id'] : false;
652
653 $use_value = isset($settings['use_network']) ? $settings['use_network'] : false;
654 $network_value = isset($settings['network']) ? $settings['network'] : false;
655 $url_value = isset($settings['url']) ? $settings['url'] : false;
656 $text_value = isset($settings['text']) ? $settings['text'] : false;
657 }
658 else {
659 $button_id = 0;
660 $use_value = 0;
661 $network_value = '';
662 $url_value = '';
663 $text_value = '';
664 }
665
666 ?>
667 <div class='mbcustom-options hidden'>
668
669 <?php
670 $id = new maxField('hidden');
671 $id->id = 'mbcustom_id_' . $index;
672 $id->name = 'mbcustom_id[' . $index . ']';
673 $id->value = $button_id;
674
675 $findex = new maxField('hidden');
676 $findex->id = 'mbcustom_index[]';
677 $findex->name = 'mbcustom_index[]';
678 $findex->value = $index;
679
680 $admin->addField($id, false, false, false);
681 $admin->addField($findex, false, false, false);
682
683 $switch = new MaxField('switch');
684 $switch->id = 'mbcustom_usenetwork_' . $index;
685 $switch->name = 'mbcustom_usenetwork[' . $index. ']';
686 $switch->label = __('Use Selected Network Settings', 'mbsocial');
687 $switch->note = __('This will replace the button text and URL settings', 'mbsocial');
688 $switch->value = 1;
689 $switch->checked = checked($switch->value, $use_value, false);
690
691 $admin->addField($switch, 'start', 'end', false);
692
693 $custom = new MaxField('option_select');
694 $custom->id= 'mbcustom_network_' . $index;
695 $custom->name = 'mbcustom_network[' . $index. ']';
696 $custom->label = __('Handle as network');
697 $custom->options = $nw_options;
698 $custom->selected = $network_value;
699
700 $admin->addField($custom, 'start', 'end', false);
701
702 $url = new maxField('hidden');
703 $url->id = 'mbcustom_url_' . $index;
704 $url->name = 'mbcustom_url[' . $index . ']';
705 $url->value = $url_value;
706
707 $admin->addField($url, 'start', 'end', false);
708
709 $text = new maxField('hidden');
710 $text->id = 'mbcustom_text_' . $index;
711 $text->name = 'mbcustom_text[' . $index . ']';
712 $text->value = $text_value;
713
714 $admin->addField($text, 'start', 'end', false);
715
716 $admin->display_fields();
717
718 ?>
719
720 </div>
721
722 <?php
723
724 }
725
726 protected function printMBCustomButton()
727 {
728 ?>
729 <span class='config_button'><button type='button' class='button-primary' data-parent='#maxbuttons' data-callback='window.maxFoundry.maxSocial.selectMBCustomButton'><?php _e('Config', 'mbsocial'); ?></button></span>
730 <?php
731 }
732
733 }
734
735 ?>
736