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

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