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

693 lines 18.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 = $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 //mbcustom
302 $mbcustom = isset($post['mbcustom_id']) ? $post['mbcustom_id'] : array();
303 $data[$this->blockname]['mbcustom'] = array();
304
305 if (is_array($mbcustom))
306 {
307 $i = 0;
308 foreach($mbcustom as $index => $button_id)
309 {
310 if ( intval($button_id) > 0)
311 {
312 $usenetwork = isset($post['mbcustom_usenetwork'][$index]) ? intval($post['mbcustom_usenetwork'][$index]) : 0;
313 $network = isset($post['mbcustom_network'][$index]) ? sanitize_text_field($post['mbcustom_network'][$index]) : '';
314 $url = isset($post['mbcustom_url'][$index]) ? sanitize_text_field($post['mbcustom_url'][$index]) : '';
315 $text = isset($post['mbcustom_text'][$index]) ? sanitize_text_field($post['mbcustom_text'][$index]) : '';
316
317 $data[$this->blockname]['mbcustom'][$i] = array(
318 'button_id' => $button_id,
319 'use_network' => $usenetwork,
320 'network' => $network,
321 'url' => $url,
322 'text' => $text,
323 );
324 $i++; // this is not index. Start saving from index 0.
325 }
326
327
328 }
329 }
330
331 // new user, give default networks
332 $collection_id = $this->collection->getID();
333 if (count($network_active) == 0 && $collection_id == 0)
334 $network_active = array('facebook', 'twitter'); // default options
335
336 $data[$this->blockname]["network_active"] = $network_active;
337 return $data;
338 }
339
340
341 public function do_meta_boxes($content, $post)
342 {
343 $admin = mbSocial()->admin();
344
345 $metadata = $this->get_block_meta_data($post->ID);
346
347 $url = new maxField();
348 $url->id = 'share_url';
349 $url->name = $url->id;
350 $url->label = __('URL to Share', 'mbsocial');
351 $url->note = __('By default the URL of the post is being shared. Leave empty for default', 'mbsocial');
352 $url->value = isset($metadata['share_url']) ? $metadata['share_url'] : '';
353 $url->placeholder = 'https://';
354
355 $admin->addField($url, 'start', 'end');
356
357 $fields = $admin->display_fields(true, true, false);
358
359
360 $content['share'] = array('title' => __('Share Options', 'mbsocial'),
361 'icon' => 'alt-share',
362 'content' => $fields,
363 );
364
365 return $content;
366
367 }
368
369 public function admin()
370 {
371 $w = mbSocial()->whistle();
372
373 $admin = mbSocial()->admin();
374 $blockdata = $this->data[$this->blockname];
375
376 $active_networks = isset($blockdata['network_active']) ? $blockdata['network_active'] : array();
377 $networks = $admin->get_networks($active_networks);
378 $styleObj = $this->collection->getStyle();
379
380 ?>
381 <div class='help-side'>
382 <h3><?php _e('Social Networks','mbsocial'); ?></h3>
383 <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>
384 <p>Hold the mouse pointer over the network icon to see the network's name</p>
385 <p>The <strong>Share Page</strong> networks will share your website's URL to other networks. </p>
386 <p>The <strong>Share Profile</strong> networks will link from the website to your social accounts</p>
387 </div>
388 <div class='options networks option-container' id='networkBlock' data-refresh='previewBlock'>
389 <?php
390 $field_obj = new maxField('hidden');
391 $field_obj->id = 'network_trigger_change';
392 $field_obj->name = $field_obj->id;
393 $field_obj->value = '';
394 $field_obj->placeholder = '';
395
396 $admin->addField($field_obj);
397 $admin->addUpdate($field_obj);
398 $admin->display_fields();
399
400 ?>
401 <div class='title'><?php _e('Social Networks','mbsocial') ?> </div>
402 <div class='inside'>
403 <div class='option active_network'>
404 <label><?php _e('Active','mbsocial'); ?></label>
405 <div class='drag-area input' data-area='active'>
406 <span class="updatables hidden">input[name="network_active"], input[name="network_item_active"]</span>
407
408 <?php if (isset($networks['selected'])): foreach($networks['selected'] as $index => $network): ?>
409
410 <?php
411 $args = array();
412 $args['input'] = 'network_item_active[]';
413 $args['network'] = $network;
414
415 $this->printNetwork($index, $network, $args );
416
417 ?>
418 <?php endforeach; endif; ?>
419 </div>
420 </div>
421
422
423 <div class='option inactive_network'>
424 <label><?php _e('Inactive', 'mbsocial'); ?></label>
425 <div class='drag-area input' data-area='inactive'>
426 <?php if (isset($networks['unselected'])): foreach($networks['unselected'] as $index => $network): ?>
427
428 <?php $this->printNetwork($index, $network); ?>
429
430 <?php endforeach; endif; ?>
431 </div>
432 </div>
433
434 <div class='option'>
435 <label>&nbsp;</label>
436 <div class='see-more toggle input'><span class='title'><?php _e('more networks', 'mbsocial'); ?></span>
437 <span class='see-more-wrap option toggle-target'>
438
439 <div class='drag-area'>
440 <?php if (isset($networks['readmore'])): foreach($networks['readmore'] as $index => $network): ?>
441
442 <?php $this->printNetwork($index, $network); ?>
443
444 <?php endforeach; endif; ?>
445 </div>
446
447 <p><?php _e("Need another network?","mbsocial"); ?>
448 <a href='mailto:support@maxfoundry.com'><?php _e('Email us','mbsocial'); ?> </a></p>
449 </span>
450
451 </div>
452 </div> <!-- option -->
453
454 <?php
455 // find caps of Networks
456 $networks = mbSocial()->networks()->get();
457
458 $nw_share = array();
459 $nw_profile = array();
460 foreach($networks as $nw)
461 {
462 if ($nw->is_share_icon())
463 {
464 $nw_profile[] = $nw->get_nice_name();
465 }
466 if ($nw->is_social_share())
467 {
468 $nw_share[] = $nw->get_nice_name();
469 }
470 }
471 sort($nw_share);
472 sort($nw_profile);
473 ?>
474
475 <div class='option network-help'>
476 <label>&nbsp;</label>
477
478 <!-- <p>
479 <div><span class='item'><span class="legend-circle share-icon"></span> <?php _e('Share Page', 'mbsocial'); ?> </span></div>
480 <div><span class='item'><span class="legend-circle social-icon"></span> <?php _e('Share Profile','mbsocial'); ?></span></div>
481 <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>
482 </p> -->
483
484 <p><?php _e(sprintf("The Share Profile networks will link from the website to your social accounts: %s networks.", implode(', ', $nw_profile)), 'mbsocial'
485 ); ?> </p>
486
487 <p><?php _e(
488 sprintf("The Share Page networks will share your websites URL to other networks: These include %s networks.", implode(', ', $nw_share)), 'mbsocial'
489 ); ?>
490 </p>
491
492
493 <?php if (Install::isPro()): ?>
494 <p><?php _e('The MaxButtons option lets you a MaxButton and configure it.','mbsocial') ?></p>
495 <?php endif; ?>
496 </div>
497
498 <div class='hidden mbcustom-helper'>
499
500 <?php
501 $this->printMBCustomButton();
502 $this->printMBCustomOptions(array('index' => -1)); ?>
503 </div>
504
505 </div> <!-- inside -->
506 </div> <!-- option-container -->
507 <?php
508 }
509
510 protected function printNetwork($index, $network, $args = array() )
511 {
512 if (! is_object($network))
513 {
514 return false;
515 }
516 $defaults = array(
517 'input' => 'network_active',
518 'content' => '',
519 'network' => null);
520
521 $args = wp_parse_args($args, $defaults);
522
523 $w = mbSocial()->whistle();
524 //$w->tell('display/parse/network', $network);
525 $network_name = $network->get('network');
526 $label = $network->get_nice_name();
527
528 $icon = $network->get('icon');
529 $icon_type = $network->get('icon_type');
530 $color = $network->get('color');
531
532 $icon_args = array('icon' => $icon, 'icon_type' => $icon_type);
533
534 if ($icon_type == 'image')
535 {
536 $icon_args['image_id'] = $network->get('icon_image_id');
537 $icon_args['image_size'] = $network->get('icon_image_size');
538 $icon_args['image_url'] = $network->get('icon_image_url');
539 }
540
541 $icon_output = MBSocial()->admin()->renderIcon($icon_args);
542 $is_share_icon = $network->is_share_icon();
543 $is_social_share = $network->is_social_share();
544
545 $input = $args['input'];
546
547 $classes = $network_name;
548 ?>
549
550
551 <span style='background-color: <?php echo $color ?>' class="item <?php echo $classes ?> " title="<?php echo $label ?>">
552 <?php echo $icon_output ?>
553
554 <?php
555 /* if ($is_share_icon)
556 echo '<span class="legend-circle share-icon"></span>';
557 if ($is_social_share)
558 echo '<span class="legend-circle social-icon"></span>';
559 */
560 ?>
561 <input type='hidden' name='<?php echo $input ?>' value='<?php echo $network_name ?>'>
562
563 <?php echo $args['content']; ?>
564 <?php
565 if (! is_null($args['network']))
566 {
567 $network = $args['network'];
568 if ($network->get('network') == 'maxbutton')
569 {
570 $this->printMBCustomButton();
571 $this->printMBCustomOptions(array('index' => $index));
572 }
573 }
574 ?>
575 </span>
576 <?php
577 }
578
579
580 protected function printMBCustomOptions($args = array() )
581 {
582 $index = isset($args['index']) ? intval($args['index']) : 0;
583
584 $admin = mbSocial()->admin();
585 $networks = mbSocial()->networks()->get();
586
587 $nw_options = array();
588 foreach($networks as $network) // list all networks, for option.
589 {
590 $network_name = $network->get('network');
591 $label = $network->get_nice_name();
592
593 if ($network_name == 'maxbutton')
594 continue;
595
596 $nw_options[$network_name] = $label;
597 }
598
599 $data = $this->data[$this->blockname];
600
601
602 if (isset($data['mbcustom']))
603 {
604 $customs = $data['mbcustom'];
605
606 $this->index_helper++;
607 $settings = isset($customs[$this->index_helper]) ? $customs[$this->index_helper] : false;
608 $button_id = isset($settings['button_id']) ? $settings['button_id'] : false;
609
610 $use_value = isset($settings['use_network']) ? $settings['use_network'] : false;
611 $network_value = isset($settings['network']) ? $settings['network'] : false;
612 $url_value = isset($settings['url']) ? $settings['url'] : false;
613 $text_value = isset($settings['text']) ? $settings['text'] : false;
614 }
615 else {
616 $button_id = 0;
617 $use_value = 0;
618 $network_value = '';
619 $url_value = '';
620 $text_value = '';
621 }
622
623 ?>
624 <div class='mbcustom-options hidden'>
625
626 <?php
627 $id = new maxField('hidden');
628 $id->id = 'mbcustom_id_' . $index;
629 $id->name = 'mbcustom_id[' . $index . ']';
630 $id->value = $button_id;
631
632 $findex = new maxField('hidden');
633 $findex->id = 'mbcustom_index[]';
634 $findex->name = 'mbcustom_index[]';
635 $findex->value = $index;
636
637 $admin->addField($id, false, false, false);
638 $admin->addField($findex, false, false, false);
639
640 $switch = new MaxField('switch');
641 $switch->id = 'mbcustom_usenetwork_' . $index;
642 $switch->name = 'mbcustom_usenetwork[' . $index. ']';
643 $switch->label = __('Use Selected Network Settings', 'mbsocial');
644 $switch->note = __('This will replace the button text and URL settings', 'mbsocial');
645 $switch->value = 1;
646 $switch->checked = checked($switch->value, $use_value, false);
647
648 $admin->addField($switch, 'start', 'end', false);
649
650 $custom = new MaxField('option_select');
651 $custom->id= 'mbcustom_network_' . $index;
652 $custom->name = 'mbcustom_network[' . $index. ']';
653 $custom->label = __('Handle as network');
654 $custom->options = $nw_options;
655 $custom->selected = $network_value;
656
657 $admin->addField($custom, 'start', 'end', false);
658
659 $url = new maxField('hidden');
660 $url->id = 'mbcustom_url_' . $index;
661 $url->name = 'mbcustom_url[' . $index . ']';
662 $url->value = $url_value;
663
664 $admin->addField($url, 'start', 'end', false);
665
666 $text = new maxField('hidden');
667 $text->id = 'mbcustom_text_' . $index;
668 $text->name = 'mbcustom_text[' . $index . ']';
669 $text->value = $text_value;
670
671 $admin->addField($text, 'start', 'end', false);
672
673 $admin->display_fields();
674
675 ?>
676
677 </div>
678
679 <?php
680
681 }
682
683 protected function printMBCustomButton()
684 {
685 ?>
686 <span class='config_button'><button type='button' class='button-primary' ><?php _e('Config', 'mbsocial'); ?></button></span>
687 <?php
688 }
689
690 }
691
692 ?>
693