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 / blocks / network-block.php

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

719 lines 19.3 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 $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 <div class='help-side'>
392 <h3><?php _e('Social Networks','mbsocial'); ?></h3>
393 <p><strong>Drag and drop</strong> the networks you want to use into the 'active' box. You can find additional networks under the 'more networks' tab.</p>
394 <p>Hold the mouse pointer over the network icon to see the network's name</p>
395 <p>The <strong>Share Page</strong> networks will share your website's URL to other networks. </p>
396 <p>The <strong>Share Profile</strong> networks will link from the website to your social accounts</p>
397 </div>
398 <div class='options networks option-container' id='networkBlock' data-refresh='previewBlock'>
399 <?php
400 $field_obj = new maxField('hidden');
401 $field_obj->id = 'network_trigger_change';
402 $field_obj->name = $field_obj->id;
403 $field_obj->value = '';
404 $field_obj->placeholder = '';
405
406 $admin->addField($field_obj);
407 $admin->addUpdate($field_obj);
408 $admin->display_fields();
409
410
411 ?>
412 <div class='title'><?php _e('Social Networks','mbsocial') ?> </div>
413 <div class='inside'>
414 <div class='option active_network'>
415 <label><?php _e('Active','mbsocial'); ?></label>
416 <div class='drag-area input' data-area='active'>
417 <span class="updatables hidden">input[name="network_active"], input[name="network_item_active"]</span>
418
419 <input type="hidden" name="network_item_active[]" value="dummy">
420
421 <?php if (isset($networks['selected'])): foreach($networks['selected'] as $index => $network): ?>
422
423 <?php
424 $args = array();
425 $args['input'] = 'network_item_active[]';
426 $args['network'] = $network;
427
428 $this->printNetwork($index, $network, $args );
429
430 ?>
431 <?php endforeach;
432 else: ?>
433 <?php
434 endif; ?>
435 </div>
436 </div>
437
438
439 <div class='option inactive_network'>
440 <label><?php _e('Inactive', 'mbsocial'); ?></label>
441 <div class='drag-area input' data-area='inactive'>
442 <?php if (isset($networks['unselected'])): foreach($networks['unselected'] as $index => $network): ?>
443
444 <?php $this->printNetwork($index, $network); ?>
445
446 <?php endforeach; endif; ?>
447 </div>
448 </div>
449
450 <div class='option'>
451 <label>&nbsp;</label>
452 <div class='see-more toggle input'><span class='title'><?php _e('more networks', 'mbsocial'); ?></span>
453 <span class='see-more-wrap option toggle-target'>
454
455 <div class='drag-area'>
456 <?php if (isset($networks['readmore'])): foreach($networks['readmore'] as $index => $network): ?>
457
458 <?php $this->printNetwork($index, $network); ?>
459
460 <?php endforeach; endif; ?>
461 </div>
462
463 <?php if (Install::IsPro()): ?>
464 <p><?php printf(__("Need another network? Check out the %s Network settings %s","mbsocial"),
465 '<a href="' . admin_url('admin.php') . '?page=maxbuttons-social-settings" target="_blank">', '</a>');
466 ?>
467
468 </p>
469 <?php else : ?>
470 <p>
471 <?php printf(__("More Networks or Customization needed? %s Check out our PRO offer %s ", 'mbsocial'),
472 '<a href="https://maxbuttons.com/wordpress-share-buttons/#compare" target="_blank">', '</a>'); ?>
473 </p>
474 <?php endif; ?>
475 </span>
476
477 </div>
478 </div> <!-- option -->
479
480 <?php
481 // find caps of Networks
482 $networks = mbSocial()->networks()->get();
483
484 $nw_share = array();
485 $nw_profile = array();
486 foreach($networks as $nw)
487 {
488 if ($nw->is_share_icon())
489 {
490 $nw_profile[] = $nw->get_nice_name();
491 }
492 if ($nw->is_social_share())
493 {
494 $nw_share[] = $nw->get_nice_name();
495 }
496 }
497 sort($nw_share);
498 sort($nw_profile);
499 ?>
500
501 <div class='option network-help'>
502 <label>&nbsp;</label>
503
504 <!-- <p>
505 <div><span class='item'><span class="legend-circle share-icon"></span> <?php _e('Share Page', 'mbsocial'); ?> </span></div>
506 <div><span class='item'><span class="legend-circle social-icon"></span> <?php _e('Share Profile','mbsocial'); ?></span></div>
507 <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>
508 </p> -->
509
510 <p><?php _e(sprintf("The Share Profile networks will link from the website to your social accounts: %s networks.", implode(', ', $nw_profile)), 'mbsocial'
511 ); ?> </p>
512
513 <p><?php _e(
514 sprintf("The Share Page networks will share your websites URL to other networks: These include %s networks.", implode(', ', $nw_share)), 'mbsocial'
515 ); ?>
516 </p>
517
518
519 <?php if (Install::isPro()): ?>
520 <p><?php _e('The MaxButtons option lets you add a MaxButton and configure it.','mbsocial') ?></p>
521 <?php endif; ?>
522 </div>
523
524 <div class='hidden mbcustom-helper'>
525
526 <?php
527 $this->printMBCustomButton();
528 $this->printMBCustomOptions(array('index' => -1)); ?>
529 </div>
530
531 </div> <!-- inside -->
532 </div> <!-- option-container -->
533 <?php
534 }
535
536 protected function printNetwork($index, $network, $args = array() )
537 {
538 if (! is_object($network))
539 {
540 return false;
541 }
542 $defaults = array(
543 'input' => 'network_active',
544 'content' => '',
545 'network' => null);
546
547 $args = wp_parse_args($args, $defaults);
548
549 $w = mbSocial()->whistle();
550 //$w->tell('display/parse/network', $network);
551 $network_name = $network->get('network');
552 $label = $network->get_nice_name();
553
554 $icon = $network->get('icon');
555 $icon_type = $network->get('icon_type');
556 $color = $network->get('color');
557
558 $icon_args = array('icon' => $icon, 'icon_type' => $icon_type);
559
560 if ($icon_type == 'image')
561 {
562 $icon_args['image_id'] = $network->get('icon_image_id');
563 $icon_args['image_size'] = $network->get('icon_image_size');
564 $icon_args['image_url'] = $network->get('icon_image_url');
565 }
566
567 $icon_output = MBSocial()->admin()->renderIcon($icon_args);
568 $is_share_icon = $network->is_share_icon();
569 $is_social_share = $network->is_social_share();
570
571 $input = $args['input'];
572
573 $classes = $network_name;
574 ?>
575
576
577 <span style='background-color: <?php echo $color ?>' class="item <?php echo $classes ?> " title="<?php echo $label ?>">
578 <?php echo $icon_output ?>
579
580 <?php
581 /* if ($is_share_icon)
582 echo '<span class="legend-circle share-icon"></span>';
583 if ($is_social_share)
584 echo '<span class="legend-circle social-icon"></span>';
585 */
586 ?>
587 <input type='hidden' name='<?php echo $input ?>' value='<?php echo $network_name ?>'>
588
589 <?php echo $args['content']; ?>
590 <?php
591 if (! is_null($args['network']))
592 {
593 $network = $args['network'];
594 if ($network->get('network') == 'maxbutton')
595 {
596 $this->printMBCustomButton();
597 $this->printMBCustomOptions(array('index' => $index));
598 }
599 }
600 ?>
601 </span>
602 <?php
603 }
604
605
606 protected function printMBCustomOptions($args = array() )
607 {
608 $index = isset($args['index']) ? intval($args['index']) : 0;
609
610 $admin = mbSocial()->admin();
611 $networks = mbSocial()->networks()->get();
612
613 $nw_options = array();
614 foreach($networks as $network) // list all networks, for option.
615 {
616 $network_name = $network->get('network');
617 $label = $network->get_nice_name();
618
619 if ($network_name == 'maxbutton')
620 continue;
621
622 $nw_options[$network_name] = $label;
623 }
624
625 $data = $this->data[$this->blockname];
626
627
628 if (isset($data['mbcustom']))
629 {
630 $customs = $data['mbcustom'];
631
632 $this->index_helper++;
633 $settings = isset($customs[$this->index_helper]) ? $customs[$this->index_helper] : false;
634 $button_id = isset($settings['button_id']) ? $settings['button_id'] : false;
635
636 $use_value = isset($settings['use_network']) ? $settings['use_network'] : false;
637 $network_value = isset($settings['network']) ? $settings['network'] : false;
638 $url_value = isset($settings['url']) ? $settings['url'] : false;
639 $text_value = isset($settings['text']) ? $settings['text'] : false;
640 }
641 else {
642 $button_id = 0;
643 $use_value = 0;
644 $network_value = '';
645 $url_value = '';
646 $text_value = '';
647 }
648
649 ?>
650 <div class='mbcustom-options hidden'>
651
652 <?php
653 $id = new maxField('hidden');
654 $id->id = 'mbcustom_id_' . $index;
655 $id->name = 'mbcustom_id[' . $index . ']';
656 $id->value = $button_id;
657
658 $findex = new maxField('hidden');
659 $findex->id = 'mbcustom_index[]';
660 $findex->name = 'mbcustom_index[]';
661 $findex->value = $index;
662
663 $admin->addField($id, false, false, false);
664 $admin->addField($findex, false, false, false);
665
666 $switch = new MaxField('switch');
667 $switch->id = 'mbcustom_usenetwork_' . $index;
668 $switch->name = 'mbcustom_usenetwork[' . $index. ']';
669 $switch->label = __('Use Selected Network Settings', 'mbsocial');
670 $switch->note = __('This will replace the button text and URL settings', 'mbsocial');
671 $switch->value = 1;
672 $switch->checked = checked($switch->value, $use_value, false);
673
674 $admin->addField($switch, 'start', 'end', false);
675
676 $custom = new MaxField('option_select');
677 $custom->id= 'mbcustom_network_' . $index;
678 $custom->name = 'mbcustom_network[' . $index. ']';
679 $custom->label = __('Handle as network');
680 $custom->options = $nw_options;
681 $custom->selected = $network_value;
682
683 $admin->addField($custom, 'start', 'end', false);
684
685 $url = new maxField('hidden');
686 $url->id = 'mbcustom_url_' . $index;
687 $url->name = 'mbcustom_url[' . $index . ']';
688 $url->value = $url_value;
689
690 $admin->addField($url, 'start', 'end', false);
691
692 $text = new maxField('hidden');
693 $text->id = 'mbcustom_text_' . $index;
694 $text->name = 'mbcustom_text[' . $index . ']';
695 $text->value = $text_value;
696
697 $admin->addField($text, 'start', 'end', false);
698
699 $admin->display_fields();
700
701 ?>
702
703 </div>
704
705 <?php
706
707 }
708
709 protected function printMBCustomButton()
710 {
711 ?>
712 <span class='config_button'><button type='button' class='button-primary' ><?php _e('Config', 'mbsocial'); ?></button></span>
713 <?php
714 }
715
716 }
717
718 ?>
719