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

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