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

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