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

1,070 lines 25.1 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
627 /* 1.8 Deprecated. Not included in MB() anymore, using svg method.
628 if (method_exists(MB(), 'load_library'))
629 {
630 MB()->load_library('fontawesome'); // always load lib since change is about 100% this includes FA.
631 }
632 */
633 maxUtils::startTime('collection-display-' . self::$count);
634
635 $w = MBSocial()->whistle();
636
637 wp_enqueue_style('mbsocial-buttons'); // load the general button style
638 wp_enqueue_script('mbsocial_front');
639
640
641 $defaults = array(
642 "preview" => false,
643 "echo" => true,
644 "style_tag" => false,
645 "compile" => false,
646 "js_tag" => true,
647 "load_type" => "footer",
648 );
649
650 $args = wp_parse_args($args, $defaults);
651
652 $w->tell('display/env/is_preview', $args['preview']);
653
654
655 //$cache = MaxCollections::checkCachedCollection($this->collection_id);
656 $cache = false;
657
658 if (! $cache)
659 {
660 //$cssParser = $this->getCSSParser();
661 $domObj = $this->parse($args);
662
663 //$css = $this->parseCSS($args);
664
665 $js = $this->parseJS($args);
666
667 $output = $domObj->save();
668
669 unset($domObj);
670
671
672 // CSS & JS output control
673 $output .= $this->displayCSS($this->css, $args);
674 $output .= $this->displayJS($js, $args);
675 }
676 else
677 $output = $cache;
678
679 //collections::addCachedCollection($this->collection_id, $output);
680
681 maxUtils::endTime('collection-display-' . self::$count);
682
683 if ($args["echo"])
684 {
685 echo $output;
686 }
687 else
688 {
689 return $output;
690 }
691
692 }
693
694 public function getDisplayCSS() // For file output
695 {
696 $meta = $this->get_meta('_parsed_css');
697 if (is_array($meta))
698 return array_shift($meta);
699 else
700 return false;
701
702 }
703
704 public function displayCSS($css, $args = array() ) // $echo = true, $style_tag = true)
705 {
706
707 $default = array(
708 "echo" => true,
709 "style_tag" => true,
710 "load_type" => "footer",
711 );
712 $args = wp_parse_args($args, $default);
713 if ($args['load_type'] == 'inline')
714 $args['style_tag'] =true;
715
716 $output = '';
717
718 if ($args["style_tag"])
719 $output .= "<style type='text/css'>";
720
721 $output .= apply_filters('mbsocial/displaycss/', $css);
722
723 if ($args["style_tag"])
724 $output .= "</style>";
725
726
727 if ($args["load_type"] == 'footer')
728 {
729 $load_id = 'collection-' . $this->collection_id; // must be unique for unique items, otherwise css won't be output
730
731 $use_file = get_option('maxbuttons_usecssfile', false);
732 if ($use_file)
733 {
734 wp_enqueue_style('maxbuttons-social-front', admin_url('admin-ajax.php').'?action=maxbuttons_social_css&id=' . $this->collection_id);
735 $this->update_meta($this->collection_id, '_parsed_css', $css);
736 }
737
738 if (self::$count > 1)
739 {
740 $load_id .= "-" . self::$count;
741 }
742
743
744 do_action('mb-footer',$load_id, $output);
745 }
746 elseif ($args["load_type"] == 'inline')
747 {
748 if ($args["echo"]) echo $output;
749 else return $output;
750
751 }
752
753 }
754
755 public function displayJS($js, $args = array() ) // $echo = true, $style_tag = true)
756 {
757 $default = array(
758 "echo" => true,
759 "js_tag" => true,
760 "load_type" => "footer",
761 "preview" => false,
762 );
763
764
765 $args = wp_parse_args($args, $default);
766
767 if ($args["preview"])
768 return; // no js on previews.
769
770 $output = '';
771
772 if (count($js) == 0)
773 return; // no output, holiday
774
775 if ($args["js_tag"])
776 {
777 $output .= "<script type='text/javascript'> ";
778
779 }
780
781 foreach($js as $index => $code)
782 {
783 $output .= $code;
784 }
785
786 if ($args["js_tag"])
787 {
788 $output .= " // }
789 // }
790 // window.onload = MBcollection" . $this->collection_id . "();
791 </script> ";
792 }
793
794
795 if ($args["load_type"] == 'footer')
796 {
797
798 do_action('mb-footer','collection-' . $this->collection_id, $output, "js");
799 }
800 elseif ($args["load_type"] == 'inline')
801 {
802 if ($args["echo"]) echo $output;
803 else return $output;
804
805 }
806 }
807
808 /* Parses the collection, via the blocks */
809 function parse($args)
810 {
811 $admin = mbSocial()->admin();
812 $w = MBSocial()->whistle();
813 $cssParser = $this->getCSSParser();
814 maxUtils::startTime('collection-parse-'. self::$count);
815
816 //$this->setEnv(); // sets the relevant data for the block (orientation etc)
817 $this->buttons = array(); // reset the buttons - needed for preview
818
819 $is_preview = isset($args["preview"]) ? $args["preview"] : false;
820
821 $collectionObj = new simple_html_dom();
822 $collection_id = $this->collection_id;
823
824 $styleObj = $this->styleObj;
825 $style_class = ''; //$styleObj->class;
826 $style_class = $this->orientation;
827
828 // create container
829 if (self::$count > 1)
830 {
831 $style_class .= ' doc-' . self::$count;
832 }
833 $node = "<div class='maxsocial maxsocial-" . $collection_id . " $style_class ' data-collection='" . $collection_id . "'>
834 </div>";
835 $node = apply_filters("maxbuttons/social/basic-container",$node);
836
837 $collectionObj->load($node);
838
839 $active = ( isset($this->data['network']['network_active']) && is_array($this->data['network']['network_active']) ) ? $this->data['network']['network_active'] : array();
840
841 if (count($active) == 0 && $args['preview'] == true)
842 {
843 $networks = $admin->get_default_networks();
844 $use_networks = $networks['unselected'];
845 }
846 elseif (count($active) == 0)
847 return $collectionObj; // no networks no collection
848 else
849 {
850 $networks = $admin->get_networks($active);
851 $use_networks = $networks['selected'];
852 }
853
854 $index = 0; // used as unique button_id
855
856 maxUtils::startTime('collection-parse-network-' . self::$count);
857 foreach($use_networks as $network)
858 {
859 if (! is_object($network))
860 {
861 continue;
862 }
863 if (! $is_preview)
864 {
865 if ( ($this->is_mobile && ! $network->forMobile() ) || $this->is_desktop && ! $network->forDesktop() )
866 {
867 continue;
868 }
869 }
870 $w->tell('display/parse/network', $network);
871
872 $button_args = array('link' => $network->get_url(),
873 'preview' => $is_preview,
874 'index' => $index,
875 'name' => $network->get('network'),
876 'data' => $this->data,
877 'collection_count' => self::$count,
878 );
879
880 $button_args = apply_filters('mbsocial/parsebutton/args', $button_args);
881 $button = $network->createButton($button_args);
882
883 // empty response == skip button
884 if (! $button || ! is_object($button))
885 continue;
886
887
888 foreach($this->blocks as $block)
889 {
890 /** Here some reset to block and to style, to denote new button creation. */
891 $block->setPreview($is_preview);
892 $button = $block->parse($button, $button_args);
893 }
894
895 if ($button)
896 {
897
898 // save DOM structure to parser
899 $cssParser->loadDom($button);
900 $css = '';
901
902 $css = $this->parseCSS( array('preview' => $is_preview) );
903
904 $this->buttons[] = array('item_class' => $button_args['name'],
905 'obj' => $button,
906 'css' => $css,
907 );
908
909 }
910
911 $index++;
912
913 } // network loop.
914 maxUtils::endTime('collection-parse-network-' . self::$count);
915
916 // general parsing
917 $button_output = '';
918 $css = '';
919
920 foreach($this->buttons as $button_item)
921 {
922 $buttonDom = $button_item['obj'];
923 $button_output .= $buttonDom->save() ;
924 $css .= $button_item['css'];
925 }
926
927 $count_block = $this->blocks['countBlock'];
928 $count_block->createTotal($collectionObj);
929
930 $cssParser->loadDom($collectionObj);
931 $css .= $this->parseCSS( array('preview' => $is_preview, 'skip_blocks' => true) );
932
933 $this->css = $css;
934
935 $collectionObj->find('.maxsocial', 0)->innertext .= $button_output;
936 $collectionObj->load($collectionObj->save());
937
938 maxUtils::endTime('collection-parse-'. self::$count);
939 return $collectionObj;
940 }
941
942
943 protected function parseCSS($args)
944 {
945 maxUtils::startTime('collection-parseCSS-'. self::$count);
946 $defaults = array(
947 'preview' => false,
948 'skip_blocks' => false
949 );
950 $args = wp_parse_args($args, $defaults);
951
952
953 $css = array();
954 if (! is_null($this->styleObj))
955 {
956 $prep_args = array(
957 'orientation' => $this->orientation,
958 'is_static' => $this->is_static,
959 'is_mobile' => $this->is_mobile,
960 'is_tablet' => $this->is_tablet,
961 'is_post' => $this->is_post);
962
963 $this->styleObj->prepareCSS($prep_args);
964 $css = $this->styleObj->getCSS();
965 }
966
967 foreach($this->blocks as $block)
968 {
969 // This needs changes to accomodate to parse the defined SCSS stylesheet instead of using CSSParser which will not work here.
970 // It will not work because CSSparse expects a very precise class definition and not a general broad definition of the items.
971 $css = $block->parseCSS($css, $args);
972
973 }
974
975 // before the parse let have style have one more go
976 $css = $this->styleObj->preParse($css);
977 $css = apply_filters('mbsocial/parsecss/', $css); // anybody else
978
979 $css = $this->getCSSParser()->parse($css);
980 maxUtils::endTime('collection-parseCSS-'. self::$count);
981 return $css;
982 }
983
984 protected function parseJS($args)
985 {
986 $js = array();
987
988 $defaults = array("preview" => false,
989 );
990
991 $args = wp_parse_args($args, $defaults);
992
993 if ($args["preview"])
994 return false; // no js on previews
995
996 foreach($this->blocks as $block)
997 {
998 $js = $block->parseJS($js, $args);
999
1000 }
1001
1002 return $js;
1003 }
1004
1005
1006
1007
1008 /** Metadata functions **/
1009
1010 public function get_metadata($post_id)
1011 {
1012 if ( ! is_array($this->meta_data) || count ($this->meta_data) == 0)
1013 {
1014 $data = get_post_meta($post_id, 'mbsocial_data', true);
1015 $this->meta_data = $data;
1016 }
1017
1018 return $this->meta_data;
1019 }
1020
1021 public function do_meta_boxes($post_type, $post)
1022 {
1023 $content_blocks = array();
1024
1025 foreach($this->blocks as $block)
1026 {
1027 $content_blocks = $block->do_meta_boxes($content_blocks, $post);
1028 }
1029
1030 return $content_blocks;
1031 }
1032
1033 /** @param $post_id Saving post_id
1034 * @param $post Post Array of submitted values
1035 **/
1036 public function save_meta_boxes($post_id, $post)
1037 {
1038 $post_meta = array();
1039
1040 foreach($this->blocks as $block)
1041 {
1042 $post_meta = $block->save_meta_boxes($post_meta, $post_id, $post);
1043
1044 }
1045
1046 if (count($post_meta) > 0)
1047 update_post_meta($post_id, 'mbsocial_data', $post_meta);
1048 }
1049
1050 function showBlocks()
1051 {
1052 foreach($this->blocks as $block)
1053 {
1054 echo $block->admin();
1055 }
1056
1057 $active = isset($this->data['network']['network_active']) ? $this->data['network']['network_active'] : array();
1058
1059 if ( is_array($active) && count($active) > 0)
1060 {
1061 foreach($active as $network_name)
1062 {
1063 $network = MbSocial()->networks()->get($network_name);
1064 if ( is_object($network))
1065 echo $network->admin($this->data);
1066 }
1067 }
1068 }
1069 } //class
1070