PluginProbe
Social Share Buttons / 0.9
Social Share Buttons v0.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 / class-collection.php

class-collection.php in Social Share Buttons 0.9, at classes/class-collection.php

1,101 lines 25.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 use \maxbuttons\maxUtils as maxUtils;
6 use \maxbuttons\maxCSSParser as maxCSSParser;
7 use \simple_html_dom as simple_html_dom;
8 use \MaxButtons\maxBlocks as maxBlocks;
9
10 class collection
11 {
12 protected $blocks = array(); // array of objects
13 protected $buttons = array();
14 protected $data; // data of the collection
15 protected $meta_data; // data stored specific to posts / pages etc
16
17 protected $collection_id = 0;
18 protected $collection_type = 'social_share';
19 protected $styleObj = null;
20
21 // layout functions
22 protected $cssParser;
23
24 // save hook currently active
25 protected $doingHook;
26
27
28 public $is_showable = true; // keeps state of hidden options
29 public $is_static = false;
30 public $is_post = false; // embedded in a post situation
31 public $is_mobile = false;
32 public $is_tablet = false;
33 public $orientation = 'horizontal';
34 public $display_mode = 'hidden';
35 public $post_id;
36
37
38 /* Constructor
39 Sets the blocks used in this collection. These are mostly derived from subclasses of the specific collection types.
40 */
41 public function __construct($collection_id = 0)
42 {
43 collections::checkExpireTrans();
44
45 $blocks = collections::getBlocks();
46
47 $default_data = array();
48
49
50 foreach($blocks as $block_name)
51 {
52 $block = collections::getBlock($block_name);
53 if ($block)
54 {
55 $block->setCollection($this);
56 $default_data = $block->save_fields($default_data, null ); // force the fields to set a default pos.
57
58 $this->blocks[$block_name] = $block;
59
60 }
61 maxBlocks::add($block);
62 }
63
64 // then load the default as data
65 maxBlocks::setData($default_data);
66 $this->data = $default_data;
67
68 foreach($this->blocks as $block)
69 {
70 if ($block)
71 {
72 $block->set($default_data);
73 }
74
75 }
76
77 if ($collection_id > 0)
78 $this->set($collection_id);
79
80 }
81 /* Get all buttons that are currently loaded */
82 /*public function getLoadedButtons()
83 {
84 return $this->buttons;
85 } */
86
87
88
89
90 /* Get a certain block class by name */
91 public function getBlock($blockname)
92 {
93
94 if (isset($this->blocks[$blockname]))
95 return $this->blocks[$blockname];
96 else
97 return false;
98 }
99
100 /* Get the type of the collection */
101 /*public function getType()
102 {
103 return $this->collection_type;
104 } */
105
106 /* Get the ID of the collection */
107 public function getID()
108 {
109 return $this->collection_id;
110 }
111
112 /** Get the MaxCSSParser classes which handles SCSS. This function is derived from the one in Button class */
113 public function getCSSParser()
114 {
115 if (! $this->cssParser)
116 { $this->cssParser = new maxCSSParser();
117 $this->cssParser->anchor_class = '.maxbutton-social';
118 }
119 return $this->cssParser;
120 }
121
122 /** Set the collection by ID
123 * @param $collection_id ID of the collection stored in database
124 */
125 public function set($collection_id)
126 {
127 $this->collection_id = $collection_id;
128
129 // make every block set it's stuff.
130 $data = $this->get_meta($collection_id);
131
132 foreach($this->blocks as $name => $block)
133 {
134 // run every blocks setter
135 $block->set($data);
136 }
137
138 $this->data = $data;
139 }
140
141 public function setData($data)
142 {
143 $this->data = $data;
144 }
145
146 public function setHook($hook)
147 {
148 $this->doingHook = $hook;
149 $this->setEnv();
150 }
151
152 // sets the style object to a collection. This is important for styling CSS
153 public function setStyle($style)
154 {
155 if (! is_object($style))
156 return false;
157
158 $this->styleObj = $style;
159 $style->setActive();
160 }
161
162 public function getStyle()
163 {
164 return $this->styleObj;
165
166 }
167
168 public function getHook()
169 {
170 if (is_null($this->doingHook))
171 return false;
172 else
173 return $this->doingHook;
174 }
175
176 public function setEnv() // load orientations and specifics about the current collections. After data has loaded and all that.
177 {
178 $data = $this->data;
179
180 $hook = $this->doingHook;
181 $display = isset($this->data[$hook]) ? $this->data[$hook] : 'hidden';
182 $this->display_mode = $display;
183
184 if (! is_admin() )
185 {
186 global $post;
187 $this->post_id = $post->ID;
188 }
189 else
190 $this->post_id = 0;
191
192
193 if ($display == 'static')
194 {
195 $this->is_static = true;
196 }
197 elseif ($display == 'after' || $display == 'before' || $display == 'after-before')
198 $this->is_post = true;
199
200 // check if on some mobile
201 $mobileDetect = new Mobile_Detect;
202 $this->is_mobile = $mobileDetect->isMobile();
203 $this->is_tablet = $mobileDetect->isTablet();
204
205 $options = isset($data['display'][$hook . '_options']) ? $data['display'][$hook . '_options'] : array();
206
207 $orientation = isset($options['orientation']) ? $options['orientation'] : 'auto';
208 $static = isset($options['static']) ? $options['static'] : 'auto';
209
210 if ($orientation == 'auto' && $display == 'static')
211 {
212 switch($static) // auto align on basis of placement on screen
213 {
214 case "left": // vertical items
215 case "right":
216 case "auto":
217 $orientation = "vertical";
218 break;
219 case "top": // horizontal
220 case "bottom":
221 default:
222 $orientation = "horizontal";
223 break;
224 }
225 }
226 elseif ($orientation == 'auto')
227 {
228 $orientation = 'horizontal';
229 }
230
231 $this->orientation = $orientation;
232
233 // Check if any hidden variables are given.
234 $active = isset($this->data['general']['active']) ? $this->data['general']['active'] : true;
235
236 if ($active === 0)
237 {
238 $this->is_showable = false;
239 }
240
241 if ($this->post_id > 0)
242 {
243 $meta = $this->get_metadata($this->post_id);
244 $is_active = (isset($meta['general']['active'])) ? $meta['general']['active'] : true;
245 if ($is_active === 0)
246 $this->is_showable = false;
247 }
248
249 }
250
251 /** Interface for handling AJAX requests to communicate with the seperate blocks.
252 * @param $result The initial (default) result to JSON back
253 * @param $block_name The name of the block in question
254 * @param $action The name of the block function to call
255 * @param $data Data specified by JS to use for this function
256 * @return $result - Standardized result block including error, body
257 */
258 public function doBlockAJAX($result, $block_name, $action, $data)
259 {
260 // core class is being called.
261 if ($block_name == 'collection')
262 {
263 $result = $this->$action($result, $data);
264 return $result;
265 }
266
267 if (isset($this->blocks[$block_name]))
268 {
269 $block= $this->blocks[$block_name];
270 $result = $block->$action($result, $data);
271 return $result;
272
273 }
274
275 $result["error"] = true;
276 $result["title"] = __("Error : Block was not there","maxbuttons");
277 return $result;
278 }
279
280 /** Save the collection data to the database. This will invoke the save functions of the blocks itself to gather, verify and manipulate data */
281 public function save($post)
282 {
283 $data = array();
284
285 // Collection id needed to save meta data and others.
286 if ($this->collection_id == 0) // assume new
287 {
288 $collection_id = $this->getNewCollectionID();
289
290 $this->collection_id = $collection_id;
291 }
292
293 // run this by each block and collect data
294 foreach($this->blocks as $block_name => $block)
295 {
296
297 $data = $block->save_fields($data, $post);
298 }
299
300 // clean what was not set
301 $this->clean_meta($data);
302
303 // write data as a per block per key value into the database
304 foreach($data as $blockname => $blockdata)
305 {
306 // seems dangerous
307 $meta_id = $this->update_meta($this->collection_id, $blockname, $blockdata);
308 }
309
310 // insert collection type
311 $this->update_meta($this->collection_id, 'collection_type', 'social_share') ;
312
313 return $this->collection_id;
314
315 }
316
317 // remove post meta not in data post.
318 public function clean_meta($data)
319 {
320 $data_blocks = array_keys($data);
321
322 $meta = $this->get_meta($this->collection_id);
323
324 foreach($meta as $meta_key => $values)
325 {
326 if (! in_array($meta_key, $data_blocks))
327 {
328 $this->delete_meta($this->collection_id, $meta_key);
329 }
330 }
331
332 }
333
334 protected function update_meta($collection_id, $collection_key, $collection_value)
335 {
336 global $wpdb;
337 $table = maxUtils::get_collection_table_name();
338
339 if ($collection_value == '') return; // no data no database entry
340 if (is_array($collection_value) && count($collection_value) == 0) return; // same for empty arrays
341
342 if (is_array($collection_value))
343 $collection_value = json_encode($collection_value);
344
345 if ($collection_id == 0) // assume new
346 {
347 $collection_id = $this->getNewCollectionID();
348
349 $this->collection_id = $collection_id;
350 }
351
352 $sql = "SELECT meta_id from $table where collection_id = %d and collection_key = %s ";
353 $sql = $wpdb->prepare($sql, $collection_id, $collection_key);
354
355 $results = $wpdb->get_results($sql, ARRAY_A);
356
357 if (count($results) == 1)
358 {
359 $meta_id = $results[0]["meta_id"];
360 $data = array("collection_value" => $collection_value); // what's being updated
361 $where = array("meta_id" => $meta_id);
362
363 $wpdb->update($table, $data, $where);
364 return $meta_id;
365 }
366 if (count($results) == 0)
367 {
368 $data = array("collection_value" => $collection_value,
369 "collection_key" => $collection_key,
370 "collection_id" => $collection_id,
371 );
372
373 $meta_id = $wpdb->insert($table, $data);
374
375 return $meta_id;
376 }
377 else
378 {
379 MB()->add_notice("error", __("Update Collection meta has multiple rows, this should not be possible.","maxbuttons"));
380
381 }
382 return false;
383 }
384
385
386 /** Determine the next ID for collection */
387 protected function getNewCollectionID()
388 {
389 global $wpdb;
390 $table = maxUtils::get_collection_table_name();
391
392 $sql = "SELECT max(collection_id) as max from $table";
393
394 $max = intval($wpdb->get_var($sql));
395
396 $max = $max + 1;
397 return $max;
398 }
399
400 protected function delete_meta($collection_id, $collection_key)
401 {
402 if (intval($collection_id) > 0 && $collection_key != "")
403 {
404 global $wpdb;
405 // delete_post_meta($collection_id, $collection_key);
406 $table = maxUtils::get_collection_table_name();
407 $where = array("collection_id" => $collection_id,
408 "collection_key" => $collection_key
409 );
410 $where_format = array("%d", "%s");
411 $wpdb->delete($table, $where, $format);
412
413 }
414 }
415
416
417 /** Delete the collection. This is done via AJAX request */
418 /*public function delete($result, $data)
419 {
420 global $wpdb;
421 if(! $this->collection_id > 0)
422 return false;
423
424 $picker = $this->getBlock("picker");
425 $picker_data = $picker->get();
426 $buttons = $picker_data["selection"];
427 $button = MB()->getClass("button");
428
429 $buttons_removed = 0;
430
431 foreach($buttons as $button_id)
432 {
433 $deleted = $this->maybe_delete_button($button_id);
434 if ($deleted)
435 $buttons_removed++;
436
437 }
438
439 $table = maxUtils::get_collection_table_name();
440 $where = array("collection_id" => $this->collection_id);
441 $where_format = array("%d");
442 $wpdb->delete($table, $where, $where_format);
443
444 $result["data"]["body"] = __("The collection is removed","maxbuttons");
445 $result["data"]["title"] = __("Removed","maxbuttons");
446 $result["data"]["buttons_removed"] = $buttons_removed;
447 $result["data"]["collection_id"] = $this->collection_id;
448 return $result;
449
450
451 } */
452
453 /** On deletion of a collection check for each button if this is an auto-generated button just made for this collection * and if
454 * no changes where made to this button. If both conditions are true, remove the button */
455 /*function maybe_delete_button($button_id)
456 {
457 $button = MB()->getClass("button");
458
459 $button->set($button_id);
460 $button_data = $button->get();
461
462 $collection_id= $this->collection_id;
463
464 // remove unedited buttons created for this collection - use with care.
465 if (isset($button_data["meta"]["user_edited"]))
466 {
467 $created_source = (isset($button_data["meta"]["created_source"])) ? $button_data["meta"]["created_source"] : '';
468 if ($button_data["meta"]["user_edited"] === false && $created_source == 'collection')
469 {
470 $in_collections = $button_data["meta"]["in_collections"];
471
472 $key = array_search($collection_id, $in_collections);
473
474 if ($key !== false)
475 {
476 unset($button_data["meta"]["in_collections"][$key]);
477
478 if (count($button_data["meta"]["in_collections"]) == 0)
479 { $button->delete($button_id);
480 return true;
481 }
482 else
483 {
484 if ($button_id > 0) // safety.
485 $button->update($button_data);
486 }
487 }
488 }
489
490
491 }
492 return false;
493 } */
494
495 function get_meta ($collection_id, $collection_key = '')
496 {
497 global $wpdb;
498 $table = maxUtils::get_collection_table_name();
499
500 $prepare = array($collection_id);
501
502 $sql = "SELECT * from $table where collection_id = %d ";
503 if ($collection_key != '')
504 {
505 $sql .= " and collection_key = %s ";
506 array_push($prepare, $collection_key);
507
508 }
509
510 $sql = $wpdb->prepare($sql, $prepare);
511
512 $results = $wpdb->get_results($sql, ARRAY_A);
513
514 $result_array = array(); // format array by field name = values to feed blocks and others.
515 if (! is_null($results))
516 {
517 $nr = array();
518 foreach($results as $row)
519 {
520 $key = $row["collection_key"];
521 /* A field can be either plain text or JSON */
522 if(json_decode($row["collection_value"]))
523 $value = json_decode($row["collection_value"], true);
524 else
525 $value = $row["collection_value"];
526 $result_array[$key] = $value;
527
528 //$row["collection_value"] = unserialize($row["collection_value"]);
529 }
530
531 return $result_array;
532 }
533 else
534 {
535 return false;
536 }
537 }
538
539 function display($args = array())
540 {
541 if (! $this->is_showable)
542 return;
543
544 wp_enqueue_style('mbsocial-buttons'); // load the general button style
545 wp_enqueue_script('mbsocial_front');
546
547 maxUtils::startTime('collection-display');
548 $defaults = array(
549 "preview" => false,
550 "echo" => true,
551 "style_tag" => false,
552 "compile" => false,
553 "js_tag" => true,
554 "load_type" => "footer",
555 );
556
557 $args = wp_parse_args($args, $defaults);
558
559 //$cache = MaxCollections::checkCachedCollection($this->collection_id);
560 $cache = false;
561
562 if (! $cache)
563 {
564 $cssParser = $this->getCSSParser();
565 $domObj = $this->parse($args);
566
567 $css = $this->parseCSS($args);
568
569 $js = $this->parseJS($args);
570
571 $output = $domObj->save();
572
573 unset($domObj);
574
575
576 // CSS & JS output control
577 $output .= $this->displayCSS($css, $args);
578 $output .= $this->displayJS($js, $args);
579 }
580 else
581 $output = $cache;
582
583 //collections::addCachedCollection($this->collection_id, $output);
584
585 maxUtils::endTime('collection-display');
586
587
588 if ($args["echo"])
589 {
590 echo $output;
591 }
592 else
593 {
594 return $output;
595 }
596
597 }
598
599 public function displayCSS($css, $args = array() ) // $echo = true, $style_tag = true)
600 {
601
602 $default = array(
603 "echo" => true,
604 "style_tag" => true,
605 "load_type" => "footer",
606 );
607 $args = wp_parse_args($args, $default);
608 if ($args['load_type'] == 'inline')
609 $args['style_tag'] =true;
610
611 $output = '';
612
613 if ($args["style_tag"])
614 $output .= "<style type='text/css'>";
615
616 $output .= $css;
617
618 if ($args["style_tag"])
619 $output .= "</style>";
620
621
622 if ($args["load_type"] == 'footer')
623 {
624
625 do_action('mb-footer','collection-' . $this->collection_id, $output);
626 }
627 elseif ($args["load_type"] == 'inline')
628 {
629 if ($args["echo"]) echo $output;
630 else return $output;
631
632 }
633
634 }
635
636 public function displayJS($js, $args = array() ) // $echo = true, $style_tag = true)
637 {
638 $default = array(
639 "echo" => true,
640 "js_tag" => true,
641 "load_type" => "footer",
642 "preview" => false,
643 );
644
645
646 $args = wp_parse_args($args, $default);
647
648 if ($args["preview"])
649 return; // no js on previews.
650
651 $output = '';
652
653 if (count($js) == 0)
654 return; // no output, holiday
655
656 if ($args["js_tag"])
657 {
658 $output .= "<script type='text/javascript'> ";
659 // $output .= " if (typeof MBcollection" . $this->collection_id . " == 'undefined') { ";
660 // $output .= " function MBcollection" . $this->collection_id . "() { ";
661
662
663 }
664
665 foreach($js as $index => $code)
666 {
667 $output .= $code;
668 }
669
670 if ($args["js_tag"])
671 {
672 $output .= " // }
673 // }
674 // window.onload = MBcollection" . $this->collection_id . "();
675 </script> ";
676 }
677
678
679 if ($args["load_type"] == 'footer')
680 {
681
682 do_action('mb-footer','collection-' . $this->collection_id, $output, "js");
683 }
684 elseif ($args["load_type"] == 'inline')
685 {
686 if ($args["echo"]) echo $output;
687 else return $output;
688
689 }
690 }
691
692 /* Parses the collection, via the blocks */
693 function parse($args)
694 {
695 $admin = mbSocial()->admin();
696 //$this->setEnv(); // sets the relevant data for the block (orientation etc)
697 $this->buttons = array(); // reset the buttons - needed for preview
698
699 $preview = isset($args["preview"]) ? $args["preview"] : false;
700
701 $collectionObj = new simple_html_dom();
702 $collection_id = $this->collection_id;
703
704 $styleObj = $this->styleObj;
705 $style_class = $styleObj->class;
706 $style_class .= ' ' . $this->orientation;
707
708 $node = "<div class='maxcollection maxcollection-" . $collection_id . " $style_class ' data-collection='" . $collection_id . "'>
709 </div>";
710 $node = apply_filters("maxbuttons/social/basic-container",$node);
711 $collectionObj->load($node);
712
713 $active = ( isset($this->data['network']['network_active']) && is_array($this->data['network']['network_active']) ) ? $this->data['network']['network_active'] : array();
714
715 if (count($active) == 0 && $args['preview'] == true)
716 {
717 $networks = $admin->get_default_networks();
718 $use_networks = $networks['unselected'];
719 }
720 elseif (count($active) == 0)
721 return $collectionObj; // no networks no collection
722 else
723 {
724 $networks = $admin->get_networks($active);
725 $use_networks = $networks['selected'];
726 }
727
728 maxUtils::startTime('collection-parse-parsebuttons');
729 foreach($use_networks as $network)
730 {
731 //$style = $this->data['style']['style'];
732
733 $args = array('link' => $network->get('share_url'),
734 'name' => $network->get('network'),
735 // 'classes' => $style,
736 'network' => $network,
737 'preview' => $preview,
738
739 );
740
741 $args = apply_filters('mbsocial/parsebutton/args', $args);
742
743 maxUtils::endTime('collection-parse-parsebuttons');
744
745 $button = $this->createButton($args);
746
747 maxUtils::startTime('collection-parse-blockparse');
748
749
750 foreach($this->blocks as $block)
751 {
752 $block->setPreview($preview);
753 $button = $block->parse($button, $args);
754 }
755
756 if ($button)
757 {
758 $this->buttons[] = array('item_class' => $args['name'],
759 'obj' => $button);
760 }
761 } // network loop.
762
763 // general parsing
764
765 maxUtils::endTime('collection-parse-blockparse');
766
767 $button_output = '';
768
769 foreach($this->buttons as $button_item)
770 {
771 $buttonDom = $button_item['obj'];
772 $button_output .= $buttonDom->save() ;
773
774 }
775
776 $this->createTotal($collectionObj);
777
778 $collectionObj->find('.maxcollection', 0)->innertext .= $button_output;
779
780
781 $collectionObj->load($collectionObj->save());
782
783 $cssParser = $this->getCSSParser();
784 //$collectionObj->load($collectionObj->save() );
785
786 // save DOM structure to parser
787 $cssParser->loadDom($collectionObj);
788
789 return $collectionObj;
790 }
791
792 // create a block for total shares
793 public function createTotal($collectionObj, $args = array() )
794 {
795
796 $count_block = $this->blocks['countBlock'];
797
798 if (! $count_block->showTotal() )
799 return ;
800
801 $total = $count_block->get_total();
802 $total_label = maxBlocks::getValue('total_count_label'); //$count_block->get_total_label();
803
804 $total_html = '<div class="collection-item total">
805 <div class="maxbutton-social-total">
806 <div class="mb-icon-total"><i class="mb-icon-total-icon dashicons dashicons-share"></i></div>
807 <div class="mb-totals">
808 <div class="mb-count-total">' . $total . '</div>
809 <div class="mb-label-total">' . $total_label .'</div>
810 </div>
811 </div>
812 </div>';
813
814 $collectionObj->find('.maxcollection',0)->innertext = $total_html;
815 }
816
817 public function createButton($args)
818 {
819 $defaults = array(
820 'link' => 'javascript:void(0)',
821 'classes' => array(),
822 'item_classes' => array(),
823 'name' => '',
824 // 'show_share_count' => false,
825 // 'share_count' => 0,
826 );
827
828 $args = wp_parse_args($args, $defaults);
829
830 $item_classes = array_merge ($args['item_classes'], array($args['name']) );
831
832 $icon_output = $this->styleObj->renderIcon($args['network']);
833
834
835 $html = "<span class='collection-item " . implode(' ', $item_classes) . "'><a href='" . $args['link'] . "' class='maxbutton-social '>
836 <span class='mb-icon-wrapper'>
837 " . $icon_output . " </span> ";
838
839 //if ($args['show_share_count'])
840 // $html .= "<span class='share-count'>" . $args['share_count'] . "</span>";
841
842 $html .= "</a></span>";
843
844
845 $button = str_get_html($html);
846 return $button;
847 }
848
849 function parseCSS($args)
850 {
851 $css = array();
852 if (! is_null($this->styleObj))
853 {
854 $prep_args = array(
855 'orientation' => $this->orientation,
856 'is_static' => $this->is_static,
857 'is_mobile' => $this->is_mobile,
858 'is_tablet' => $this->is_tablet,
859 'is_post' => $this->is_post);
860
861 $this->styleObj->prepareCSS($prep_args);
862 $css = $this->styleObj->getCSS();
863 }
864
865 foreach($this->blocks as $block)
866 {
867 // This needs changes to accomodate to parse the defined SCSS stylesheet instead of using CSSParser which will not work here.
868 // It will not work because CSSparse expects a very precise class definition and not a general broad definition of the items.
869 $css = $block->parseCSS($css, $args);
870
871 }
872
873 // before the parse let have style have one more go
874 $css = $this->styleObj->preParse($css);
875
876 $css = $this->getCSSParser()->parse($css);
877
878 return $css;
879 }
880
881 function parseJS($args)
882 {
883 $js = array();
884
885 $defaults = array("preview" => false,
886 );
887
888 $args = wp_parse_args($args, $defaults);
889
890 if ($args["preview"])
891 return false; // no js on previews
892
893 foreach($this->blocks as $block)
894 {
895 $js = $block->parseJS($js, $args);
896
897 }
898
899 return $js;
900 }
901
902
903 public function shortcode($atts, $content = null)
904 {
905
906 // ugly -need to rework logic here.
907 //$collection = maxCollections::getCollection('social');
908 $display_args = shortcode_atts(array(
909 "echo" => false,
910 "mode" => "normal",
911 "nocache" => false,
912 "style" => 'footer',
913 ),
914
915 $atts);
916
917 $collection_id = $this->collection_id;
918
919 //$this->set($collection_id);
920 $display_args["compile"] = $display_args["nocache"];
921 $display_args['load_type'] = $display_args['style'];
922 unset($display_args['style']);
923 unset($display_args["nocache"]);
924
925 $output = $this->display($display_args);
926
927 return $output;
928 }
929
930 /** Metadata functions **/
931
932 public function get_metadata($post_id)
933 {
934 if ( ! is_array($this->meta_data) || count ($this->meta_data) == 0)
935 {
936 $data = get_post_meta($post_id, 'mbsocial_data', true);
937 $this->meta_data = $data;
938 }
939
940 return $this->meta_data;
941 }
942
943 public function do_meta_boxes($post_type, $post)
944 {
945 $content_blocks = array();
946
947 foreach($this->blocks as $block)
948 {
949 $content_blocks = $block->do_meta_boxes($content_blocks, $post);
950 }
951
952 return $content_blocks;
953 }
954
955 /** @param $post_id Saving post_id
956 @param $post Post Array of submitted values
957 **/
958 public function save_meta_boxes($post_id, $post)
959 {
960 $post_meta = array();
961
962 foreach($this->blocks as $block)
963 {
964 $post_meta = $block->save_meta_boxes($post_meta, $post_id, $post);
965
966 }
967
968 if (count($post_meta) > 0)
969 update_post_meta($post_id, 'mbsocial_data', $post_meta);
970 }
971
972
973 // Get the pack definition that are present in the system.
974 /*function editor_getPacks()
975 {
976 //$pack_paths = apply_filters('mb-col-pack-paths', array(MB()->get_plugin_path() . "/") );
977 $packs["maxbuttons"]["func"] = array($this, 'editor_getButtons');
978 $packs["maxbuttons"]["tab"] = __("Your MaxButtons","maxbuttons");
979 return $packs;
980
981 } */
982
983 /* public function ajax_getButtons($result, $data)
984 {
985 $packs = $this->editor_getPacks();
986 $req_pack= $data["pack"];
987
988 foreach($packs as $index => $pack_data)
989 {
990 if ($req_pack == $index)
991 {
992 $buttons = call_user_func($pack_data["func"], $index, $data);
993
994 }
995 }
996 $output = '';
997
998
999 $page_args = array();
1000 if (isset($data["paged"]))
1001 {
1002 $page_args["paged"] = $data["paged"];
1003 }
1004
1005 if ($req_pack == 'maxbuttons')
1006 {
1007 $page_args["limit"] = 18;
1008 ob_start();
1009 do_action("mb-display-meta");
1010 do_action("mb-display-pagination", $page_args);
1011 $pagination = "<div class='tablenav top'>" .ob_get_contents() . "</div>";
1012 ob_end_clean();
1013
1014 $output .= $pagination;
1015 }
1016
1017 foreach($buttons as $button)
1018 {
1019 $button_data = $button->get();
1020
1021 $button_id = $button->getID();
1022 $name = $button->getName();
1023
1024 $meta = isset($button_data["meta"]) ? $button_data["meta"] : array();
1025 do_action('mb-data-load',$button_data); // weakness of the bubbling filter model
1026
1027 $output .= "<div class='item shortcode-container' data-id='$button_id'> ";
1028 $output .= $button->display(array("mode" => "preview", 'echo' => false, 'load_css' => 'inline') ); //"mode" => "preview", "compile" => true,
1029 $output .= "<span class='button_data'>" . base64_encode(json_encode($button_data)) . "</span>";
1030 $output .= "<span class='button_name'>" . $name . "</span>";
1031 $output .= "</div>";
1032
1033 }
1034
1035 if ($req_pack == 'maxbuttons')
1036 {
1037 $output .= $pagination;
1038 }
1039
1040 $result["body"] = $output;
1041 return $result;
1042
1043 }
1044 */
1045
1046 // show the available buttons
1047 /* function editor_getButtons($pack, $data)
1048 {
1049 $button_array = array();
1050
1051 $admin = MB()->getClass("admin");
1052 $button = MB()->getClass("button");
1053
1054 $paged = (isset($data["paged"])) ? $data["paged"] : 1;
1055
1056 $buttons = $admin->getButtons(array(
1057 "orderby" => "id",
1058 "order" => "DESC",
1059 "paged" => $paged,
1060 "limit" => 18,
1061 ));
1062
1063
1064 foreach($buttons as $btn)
1065 {
1066 $id = $btn["id"];
1067 $b = MB()->getClass('button');
1068 $b->clear();
1069 $b->set($id);
1070 $button_data = $b->get();
1071 // exclude auto-generated non-user edited buttons.
1072 if ( ! isset($button_data["meta"]["user_edited"]) || ($button_data["meta"]["user_edited"] == true || $button_data["meta"]["created_source"] != "collection"))
1073 $button_array[] = $b; // the object
1074 }
1075 return $button_array;
1076 } */
1077
1078
1079 function showBlocks()
1080 {
1081 maxBlocks::setData($this->data);
1082 foreach($this->blocks as $block)
1083 {
1084 echo $block->admin();
1085 }
1086
1087 $active = isset($this->data['network']['network_active']) ? $this->data['network']['network_active'] : array();
1088
1089 if ( is_array($active) && count($active) > 0)
1090 {
1091
1092
1093 foreach($active as $network_name)
1094 {
1095 $network = MbSocial()->networks()->get($network_name);
1096 echo $network->admin($this->data);
1097 }
1098 }
1099 }
1100 } //class
1101