PluginProbe
Social Share Buttons / 1.30
Social Share Buttons v1.30
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.30, at classes/blocks/network-block.php

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