PluginProbe
MaxButtons – Create buttons / 6.9
MaxButtons – Create buttons v6.9
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.9, at collections/social-block.php

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