PluginProbe
Social Share Buttons / 1.16
Social Share Buttons v1.16
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 1.16, at classes/class-collection.php

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