PluginProbe
MaxButtons – Create buttons / 6.25
MaxButtons – Create buttons v6.25
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / collections / social-block.php

social-block.php in MaxButtons – Create buttons 6.25, at collections/social-block.php

729 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4 $collectionBlock["social"] = "socialCollectionBlock";
5
6 use \simple_html_dom as simple_html_dom;
7
8 class socialCollectionBlock extends collectionBlock
9 {
10 protected $blockname = "social";
11 protected $fields = array(
12 "multifields" => array(
13 "id" => "social-option",
14 "fields" => array(
15 "network" => array("default" => "none"),
16 "display_count" => array("default" => 0 ),
17 "count_threshold" => array("default" => 4),
18 "share_url_setting" => array("default" => "auto"),
19 "share_url_custom" => array("default" => ''),
20 "blog_url" => array("default" => ''),
21
22 ),
23 ),
24 );
25
26 protected $social_data = array();
27 protected $ajax_remote_count = 0; // failback for hanging remote requests in ajax_get_count.
28
29
30 function __construct()
31 {
32
33 require_once( MB()->get_plugin_path() . "/classes/social-networks.php");
34
35 }
36 function parse($domObj, $args)
37 {
38
39 $popup = isset($this->social_data["popup"]) ? $this->social_data["popup"] : array();
40 $onload = isset($this->social_data["onload"]) ? $this->social_data["onload"] : array();
41
42 $collection_items = $domObj->find('.maxcollection .mb-collection-item');
43 $i = 0;
44
45 foreach($collection_items as $item)
46 {
47 //$this_item = $item->find('.maxbutton');
48 $tag = 'data-doc-id';
49 $doc_id = $item->$tag;
50 if (isset($popup[$doc_id]))
51 {
52 $tag = 'data-popup'; // annoying.
53 $json = htmlentities(json_encode($popup[$doc_id]), ENT_QUOTES, 'UTF-8');
54 $item->$tag = $json;
55
56 }
57 if (isset($onload[$doc_id]))
58 {
59 $tag = 'data-onload';
60 $json = htmlentities(json_encode($onload[$doc_id]), ENT_QUOTES, 'UTF-8');
61 $item->$tag = $json;
62
63 }
64 $i++;
65
66 }
67
68 return $domObj;
69
70 }
71
72 function parseButtons($buttons)
73 {
74 $mf_field_id = $this->fields["multifields"]["id"];
75
76 // set social behavoir on the buttons, overriding things
77 foreach($buttons as $index => $button) // index is order of buttons and should be kept like that
78 {
79 $button_id = $button->getID();
80
81 if (isset($this->data[$mf_field_id][$index][$button_id]))
82 {
83 $button_data = $button->get();
84
85 // can happen if button collection button has been deleted.
86 $document_id = isset($button_data["document_id"]) ? $button_data["document_id"] : -1;
87
88 $field_data = $this->data[$mf_field_id][$index][$button_id];
89
90 $network = $field_data["network"];
91 $maxSN = new maxSN($network);
92 $network_name = $maxSN->getNetworkName();
93
94 $display_count = $field_data["display_count"];
95 $count_threshold = $field_data["count_threshold"];
96
97 // collect all variables here for conversion
98 $share_data = $this->getShareData($field_data);
99 $share_url = $share_data["url"];
100 $share_title = isset($share_data["title"]) ? $share_data["title"] : '';
101 $share_img = isset($share_data["img"]) ? $share_data["img"] : '';
102
103 $share_count = 0;
104
105 $apply_vars = array("url" => $share_url,
106 "count" => $share_count,
107 "title" => $share_title,
108 "network_name" => $network_name,
109 "img" => $share_img,
110 );
111
112 $network_url = $maxSN->getShareURL($network);
113 $network_url = $this->applyVars($network_url, $apply_vars);
114
115
116 if ($network_url)
117 {
118 $buttons[$index]->setData("basic",array("url" => $network_url) );
119
120 }
121
122 $count_to_display = '';
123 $text = isset($button_data["text"]["text"]) ? $button_data["text"]["text"] : '' ;
124 $text2 = isset($button_data["text"]["text2"]) ? $button_data["text"]["text2"] : '';
125
126 if ($display_count == 1 && $this->is_supported('display_count', $network) )
127 {
128 $count = $maxSN->getShareCount(array("url" => $share_url,
129 "preview" => $this->is_preview,
130 ));
131
132 if ($count === false) // signal for remote check
133 {
134 $this->social_data["onload"][$document_id] = array("network" => $network, "share_url" => esc_url($share_url), "count_threshold" => $count_threshold, "text" => $text, "text2" => $text2 );
135
136 }
137
138 if($count >= $count_threshold )
139 {
140 $count_to_display = $count;
141
142 }
143 }
144
145 // apply_vars. Always run this to get rid of placeholders ( defaults to emtpy )
146 $apply_vars = array("count" => $count_to_display, "network_name" => $network_name);
147
148 $text = $this->applyVars($text, $apply_vars);
149 $text2 = $this->applyVars($text2, $apply_vars);
150
151 $button->setData("text",array("text" => $text, "text2" => $text2) );
152
153 // arrange JS stuff
154 if ($maxSN->checkPopup() )
155 {
156
157 $dimensions = $maxSN->getPopupDimensions(); // ["popup_dimensions"];
158 $this->social_data["popup"][$document_id] = array("width" => $dimensions[0],
159 "height" => $dimensions[1],
160 );
161 }
162
163
164 } // isset button data
165
166 }
167
168 return $buttons;
169
170 }
171
172 function getShareData($data)
173 {
174 $share_setting = $data["share_url_setting"];
175 $custom = $data["share_url_custom"];
176
177 $url = '';
178 $title = '';
179 $img = '';
180
181 $basic = $this->collection->getBlock("basic");
182 $place_data = $basic->get();
183 $placement = $place_data["placement"];
184
185 switch($share_setting)
186 {
187 case "home":
188 $url = get_home_url();
189 $title = get_bloginfo('name');
190 $img = $this->getImage(null, true);
191 break;
192 case "custom-url";
193 $url = $custom;
194 $img = $this->getImage();
195 break;
196 case "current-post":
197 $url = get_permalink();
198 $title = get_the_title();
199 $img = $this->getImage();
200 break;
201 case "auto":
202 default:
203
204
205 /* After / before being placed on the post data, therefore by default (auto) always use the POST URL
206 Static is being placed once(1) on the page, so get the greated to-share url ( of the whole section ) */
207
208 switch($placement)
209 {
210 case "before":
211 case "after":
212 case "after-before":
213 $url = get_permalink();
214 $title = get_the_title();
215 $img = $this->getImage();
216 break;
217 case "static-left":
218 case "static-right":
219 case "static-top":
220 case "static-bottom":
221 if (is_front_page())
222 {
223 $url = get_home_url();
224 $title = get_bloginfo('name');
225 $img = $this->getImage(null, true);
226 }
227 else // are there any other cases? Page / Archive should give back their main url like this.
228 {
229 $url = get_permalink();
230 $title = get_the_title();
231 $img = $this->getImage();
232 }
233 break;
234
235
236 } // switch
237
238 break;
239 }
240 $data = array("url" => $url,
241 "title" => $title,
242 "img" => $img);
243 return $data;
244
245 }
246
247 function getImage($post_id = null, $home = false)
248 {
249 if (is_admin())
250 return ''; // in admin all this is not set.
251
252 if ($home && get_option('show_on_front') === 'page')
253 {
254 $post_id = get_option('page_on_front');
255
256 }
257
258 $thumb_id = get_post_thumbnail_id($post_id); // First try to find thumbnail on the content
259
260 $image = false;
261 if (is_numeric($thumb_id))
262 {
263 $image = wp_get_attachment_url($thumb_id);
264 }
265
266 if ($image !== false)
267 return $image;
268
269 // In case of no thumbnails, tries to find first image from current content.
270
271 // if (! is_null($post_id))
272 // {
273 $post = get_post($post_id);
274 $content = '';
275 if (! is_null($post))
276 $content = $post->post_content;
277 /* }
278 else
279 {
280
281 $content = get_the_content();
282 } */
283 $domObj = new simple_html_dom();
284 $domObj->load($content);
285
286 $img = $domObj->find('img', 0);
287 if ($img)
288 {
289 $image = $img->src;
290 return $image;
291 }
292 else
293 return '';
294 }
295
296 function applyVars($string, $vars)
297 {
298
299 if (isset($vars["url"]))
300 $string = str_replace("{url}", urlencode(esc_url($vars["url"])), $string);
301 if (isset($vars["count"]))
302 {
303 $count = $this->formatCount($vars["count"]);
304 $string = str_replace("{count}", "<span class='share_count'>" . $count . "</span>", $string);
305 $string = str_replace("{c}", "<span class='share_count'>" . $count . "</span>", $string);
306 }
307 if (isset($vars["title"]))
308 $string = str_replace("{title}", $vars["title"], $string);
309 if (isset($vars["network_name"]))
310 $string = str_replace("{network_name}", $vars["network_name"], $string);
311
312 if (isset($vars["img"]))
313 $string = str_replace("{img}", $vars["img"], $string);
314
315 return $string;
316 }
317
318 protected function formatCount($raw_count)
319 {
320 $count = $raw_count;
321 if ($count > 1000)
322 {
323 $count = round(($count/1000), 1). 'K';
324 }
325 $count = apply_filters("mb-collection-count-format", $count, $raw_count);
326 return $count;
327 }
328
329
330
331 // function to get specifics on network like where to get count / where to sent people etc.
332
333
334 function save_fields($data, $post)
335 {
336
337 $data = parent::save_fields($data, $post);
338
339
340 $picker = $this->collection->getBlock("picker");
341 if ($picker)
342 {
343
344 $picker_data = $picker->get();
345 $selection = $picker_data["selection"];
346
347 }
348
349 $mf_field_id = $this->fields["multifields"]["id"];
350
351 return $data;
352 }
353
354 function ajax_new_button($result, $data)
355 {
356 ob_start();
357 $button_array = json_decode(base64_decode($data["data"]), true);
358 $button_id = $data["button_id"];
359 $index = isset($data["index"]) ? intval($data["index"]) : 0;
360
361 $data = array();
362 $data["index"] = $index;
363
364 $fields = $this->fields["multifields"]["fields"];
365
366 if (isset($button_array["collection"]["social"]))
367 { $social_data = $button_array["collection"]["social"]; // removed array filter since it removes zero's as well..
368
369 }
370 else
371 $social_data = array();
372
373
374 foreach($fields as $field => $options)
375 {
376 if (isset($social_data[$field]))
377 $data[$field] = $social_data[$field];
378 else
379 $data[$field] = (isset($options["default"])) ? $options["default"] : '';
380 }
381
382
383 $this->init_admin_arrays();
384 $this->do_social_option($button_id, $data, $button_array ) ;
385
386 $output = ob_get_contents();
387 ob_end_clean();
388
389 $result["body"] = $output;
390 return $result;
391
392 }
393
394 function ajax_get_count($result, $data)
395 {
396
397 $network = (isset($data["network"])) ? sanitize_text_field($data["network"]) : '';
398 $share_url = (isset($data["share_url"])) ? sanitize_text_field($data["share_url"]) : '';
399
400 if ($network == '')
401 return $result;
402
403 // init network
404 $maxSN = new maxSN($network);
405
406 // check if share count appeared in the cache. If this is the case it's possible another process put it there.
407 $do_remote = true;
408 $share_count = $maxSN->getShareCount(array("url" => $share_url));
409 if ($share_count !== false)
410 {
411 $count = $share_count;
412 $do_remote = false; // stop annoying other servers
413 }
414
415 if ($do_remote)
416 {
417 // returns false, a number or 'locked' in case of locked.
418 $count = $maxSN->getRemoteShareCount($share_url);
419
420 /* If the request is lock, another request is probably asking for this and shouldn't take long.
421 Try a maximum of 5 times to prevent server process hanging until php times out ( we want to prevent that ) */
422 if ($count == 'locked')
423 {
424 if ($this->ajax_remote_count < 5)
425 {
426 sleep(1); // in seconds please.
427 // after retry result here will at some point have the count data, extract and return at the end.
428 $result = $this->ajax_get_count($result,$data);
429 $count = $result["data"]["count"];
430 }
431 else
432 $count = "TIMEOUT";
433 $this->ajax_remote_count++;
434 }
435 }
436
437 $result["data"] = array('count' => intval($count) );
438
439 return $result;
440 }
441
442 function init_admin_arrays()
443 {
444
445 global $supported_networks, $share_url_settings;
446
447 $supported_networks = maxSN::getSupportedNetworks();
448
449
450 $share_url_settings = array(
451 "auto" => __("Auto","maxbuttons"),
452 "current-post" => __("Current post or page","maxbuttons"),
453 "custom-url" => __("Custom URL","maxbuttons"),
454 "home" => __("Homepage URL","maxbuttons"),
455 );
456
457 }
458
459 /* These two functions to be merged / or do_supports should use this - next rework */
460 function is_supported ($field_type, $network)
461 {
462 switch ($field_type)
463 {
464 case "display_count":
465 case "count_threshold":
466 $networks = array("facebook","google-plus","linkedin","pinterest","reddit","stumbleupon","vkontakte");
467 break;
468 case "share_url_setting":
469 $networks = array_keys($supported_networks);
470 // unset exceptions
471 $networks = array_values(array_diff($networks, array("bloglovin", "none") ));
472 break;
473 case "blog_url":
474 $networks = array('bloglovin');
475 break;
476 }
477
478 if (in_array($network, $networks))
479 {
480 return true;
481 }
482 return false;
483 }
484
485 function do_supports($field, $button_id, $index)
486 {
487
488 global $supported_networks;
489
490 $basic_supports = array("share_url_setting", "share_url_custom"); // conditionals interface
491 $share_supports = array("display_count", "count_threshold");
492
493 $networks = array();
494
495 switch ($field)
496 {
497 case "display_count":
498 case "count_threshold":
499 $networks = array("facebook","google-plus","linkedin","pinterest","reddit","stumbleupon","vkontakte");
500 break;
501
502 case "share_url_setting":
503 //case "share_url_custom":
504 $networks = array_keys($supported_networks);
505 // unset exceptions
506 $networks = array_values(array_diff($networks, array("bloglovin", "none") ));
507
508 break;
509
510 case "blog_url":
511 $networks = array('bloglovin');
512 break;
513
514 }
515
516 $conditional = htmlentities(json_encode(array("target" => "network-$button_id-$index", "values" => $networks)));
517 return $conditional;
518
519
520 }
521
522 public function map_fields($map)
523 {
524 $map = parent::map_fields($map);
525 $map["network"]["func"] = "parseTags";
526 $map["display_count"]["func"] = "parseTags";
527
528
529 return $map;
530 }
531
532 function admin_fields()
533 {
534
535 $picker = $this->collection->getBlock("picker");
536 if ($picker)
537 {
538
539 $picker_data = $picker->get();
540 $selection = $picker_data["selection"];
541
542 }
543
544 $this->init_admin_arrays();
545
546
547 $mf_field_id = $this->fields["multifields"]["id"];
548 $mf_fields = $this->fields["multifields"]["fields"];
549
550 ?><div class="mb_tab social_block option-container" data-options="social">
551 <div class="title">
552 <span class='dashicons dashicons-share'> </span>
553 <span class="title"><?php _e("Social options", "maxbuttons"); ?></span>
554 <span class='manual-box'><a class='manual-toggle' href='javascript:void(0)' data-target="social"> <?php _e("Getting Started","maxbuttons-pro"); ?> </a></span>
555 <span class='right'><button name="save" data-form='collection_edit' type="submit" class="button button-primary"><?php _e("Save All","maxbuttons"); ?></button>
556 </span>
557 </div>
558 <div class="inside">
559 <?php
560
561 $defaults = array();
562 foreach($mf_fields as $mf_field => $options)
563 {
564 $defaults[$mf_field] = isset($options["default"]) ? $options["default"] : "";
565 }
566
567 $buttondata = isset($this->data[$mf_field_id]) ? $this->data[$mf_field_id] : array();
568
569 foreach($selection as $index => $button_id)
570 {
571 $mf_data = array();
572 $mf_data = $defaults; // defauls until found
573
574 //foreach($this->data[$mf_field_id] as $b_index => $buttondata)
575 //{
576
577
578 if (isset($buttondata[$index][$button_id]))
579 {
580
581 $mf_data = $buttondata[$index][$button_id];
582
583
584 }
585
586 //}
587 $mf_data["index"] = $index;
588 $this->do_social_option($button_id, $mf_data);
589
590 }
591
592 ?>
593
594 <?php if (count($selection) == 0) // no buttons
595 {
596 ?>
597 <p class='no-buttons'><?php _e("No buttons selected yet. Please select some buttons first.","maxbuttons"); ?></p>
598
599 <?php
600
601 }
602 ?>
603 </div>
604 </div> <!-- option container -->
605
606
607 <!-- manual entry -->
608 <div class="manual-entry" data-manual="social">
609 <h3><?php _e("Social settings", "maxbuttons"); ?>
610 <span class="dashicons dashicons-no window close manual-toggle" data-target="social"></span>
611 </h3>
612
613 <p><?php _e("For each button you have selected pick the Social Media network that corresponds to the icon. For each Social Sharing button you can choose to use the current page, the your homepage URL or a custom URL.","maxbuttons"); ?></p>
614
615 </div>
616
617 <?php
618 }
619
620 function do_social_option($button_id, $data, $button_array = array() )
621 {
622
623 global $supported_networks, $share_url_settings;
624
625 //$data = array_filter($data); // packs from xml can give back empty arrays if value is not set ( attribute of pack handler ).
626
627 $defaults = $this->fields['multifields']['fields']; // array filter removes empty fields, set to defaults.
628
629 // array filter doesn't work since vars need to be set, or be able to have zero. Put to default if array ( xml import )
630 $network = is_array($data['network']) ? $defaults['network']['default'] : $data['network'];
631 $display_count = is_array($data['display_count']) ? $defaults['display_count']['default'] : $data['display_count'];
632 $count_threshold = is_array($data['count_threshold']) ? $defaults['count_threshold']['default'] : $data['count_threshold'];
633 $share_url_setting = is_array($data['share_url_setting']) ? $defaults['share_url_setting']['default'] : $data['share_url_setting'];
634 $share_url_custom = is_array($data['share_url_custom']) ? $defaults['share_url_custom']['default'] : $data['share_url_custom'];
635 $blog_url = is_array($data['blog_url']) ? $defaults['blog_url']['default'] : $data['blog_url'];
636
637 //extract($data);
638
639 $index = isset($data["index"]) ? $data["index"] : 0;
640
641 $button = MB()->getClass("button");
642
643 if ($button_id > 0 && (! isset($button_array["meta"]) || ! $button_array["meta"]["is_virtual"]) )
644 {
645 $button->set($button_id);
646 }
647 elseif (count($button_array) > 0)
648 {
649 $button_array["name"] = $button_array["basic"]["name"];
650 $button_array["status"] = $button_array["basic"]["status"];
651 $button->clear();
652 maxButtons::buttonLoad(array("button_id" => $button_id)); // central registration - from button
653 $button->setupData($button_array);
654 }
655
656 $document_id = $button->getDocumentID();
657
658
659 ?>
660 <div class="social-option" data-id="<?php echo $button_id ?>" data-document_id="<?php echo $document_id ?>">
661 <input type="hidden" name="social-option[]" value="<?php echo $button_id ?>" />
662 <div class="option">
663 <div class="shortcode-container">
664 <?php
665
666
667 $button->display(array("mode" => "preview"));
668
669 ?>
670
671 </div>
672 <span class="button_name"><?php echo $button->getName(); ?></span>
673 </div>
674
675
676 <div class="option ">
677 <label><?php _e("Sharing network","maxbuttons"); ?></label>
678 <?php echo maxUtils::selectify("network-$button_id-$index",$supported_networks, $network, 'network'); ?>
679
680 </div>
681
682 <div class="option conditional-option" data-show="<?php echo $this->do_supports('display_count', $button_id, $index); ?>">
683 <label><?php _e("Show Share Counts","maxbuttons"); ?></label>
684 <input type="checkbox" name="display_count-<?php echo $button_id ?>-<?php echo $index ?>" value="1" <?php checked($display_count, 1); ?> />
685 </div>
686
687 <div class="option conditional-option" data-show="<?php echo $this->do_supports('count_threshold', $button_id, $index); ?>">
688 <label><?php _e("Minimum share count","maxbuttons"); ?></label>
689 <input type="number" class="tiny" name="count_threshold-<?php echo $button_id ?>-<?php echo $index ?>" min="0"
690 value="<?php echo intval($count_threshold) ?>" />
691 </div>
692
693 <div class="option conditional-option" data-show="<?php echo $this->do_supports('share_url_setting', $button_id, $index); ?>">
694 <label><?php _e("Share URL settings","maxbuttons"); ?></label>
695 <?php echo maxUtils::selectify("share_url_setting-$button_id-$index", $share_url_settings, $share_url_setting); ?>
696
697
698 <div class="help fa fa-question-circle ">
699 <span><?php _e("Which URL (link) is to be shared. Auto will share the current page on pages, and the full site URL on the homepage", "maxbuttons"); ?>
700 </span>
701 </div>
702
703 </div>
704
705 <!-- bloglovin -->
706 <div class="option conditional-option" data-show="<?php echo $this->do_supports('blog_url', $button_id, $index); ?>">
707 <label><?php _e("Bloglovin' Blog URL","maxbuttons"); ?></label>
708 <input type="text" name="blog_url-<?php echo $button_id ?>-<?php echo $index ?>"
709 value="<?php echo esc_attr($blog_url) ?>">
710 </div>
711
712 <?php $condition = array("target" => "share_url_setting-$button_id-$index", "values" => array("custom-url")) ;
713 $custom_conditional = htmlentities(json_encode($condition ));
714 ?>
715
716
717 <div class="option conditional-option" data-show="<?php echo $custom_conditional ?>">
718 <label><?php _e("Custom URL","maxbuttons"); ?></label>
719 <input type="text" name="share_url_custom-<?php echo $button_id ?>-<?php echo $index ?>"
720 value="<?php echo esc_attr($share_url_custom) ?>">
721
722 </div>
723 </div> <!-- social option -->
724 <?php
725 }
726 }
727
728 ?>
729