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

1,079 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 \MaxButtons\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
301 if ( Install::isPRO() ) {
302 $show_mobile = (isset($data['display']['show_mobile']) && $data['display']['show_mobile'] == 0) ? false: true;
303 $show_desktop = (isset($data['display']['show_desktop']) && $data['display']['show_desktop'] == 0) ? false: true;
304
305 if ($this->is_mobile && ! $show_mobile)
306 {
307 $this->is_showable = false;
308 }
309 if ($this->is_desktop && ! $show_desktop)
310 {
311 $this->is_showable = false;
312 }
313 }
314
315 // new method of getting data around
316 $w->tell('display/env/hook', $hook);
317 $w->tell('display/env/post_id', $this->post_id);
318 $w->tell('display/env/is_static', $this->is_static );
319 $w->tell('display/env/is_post',$this->is_post);
320 $w->tell('display/env/is_mobile', $this->is_mobile);
321 $w->tell('display/env/is_tablet', $this->is_tablet);
322 $w->tell('display/env/is_active', $this->is_showable);
323
324 }
325
326 /** Interface for handling AJAX requests to communicate with the seperate blocks.
327 * @param $result The initial (default) result to JSON back
328 * @param $block_name The name of the block in question
329 * @param $action The name of the block function to call
330 * @param $data Data specified by JS to use for this function
331 * @return $result - Standardized result block including error, body
332 */
333 public function doBlockAJAX($result, $block_name, $action, $data)
334 {
335 // core class is being called.
336 if ($block_name == 'collection')
337 {
338 $result = $this->$action($result, $data);
339 return $result;
340 }
341
342 if (isset($this->blocks[$block_name]))
343 {
344 $block= $this->blocks[$block_name];
345 $result = $block->$action($result, $data);
346 return $result;
347
348 }
349
350 $result["error"] = true;
351 $result["title"] = __("Error : Block was not there","maxbuttons");
352 return $result;
353 }
354
355 /** Save the collection post data to the database. This will invoke the save functions of the blocks itself to gather, verify and manipulate data */
356 public function save($post)
357 {
358 $data = array();
359
360 // run this by each block and collect data
361 foreach($this->blocks as $block_name => $block)
362 {
363
364 $data = $block->save_fields($data, $post);
365 }
366
367 $this->data = $data;
368 return $this->saveData();
369
370 }
371
372 /* The actual save function. Save data to the database */
373 public function saveData()
374 {
375 // Collection id needed to save meta data and others.
376 if ($this->collection_id == 0) // assume new
377 {
378 $collection_id = $this->getNewCollectionID();
379
380 $this->collection_id = $collection_id;
381 }
382
383 // clean what was not set
384 $this->clean_meta($this->data);
385
386 // write data as a per block per key value into the database
387 foreach($this->data as $blockname => $blockdata)
388 {
389 // seems dangerous
390 $meta_id = $this->update_meta($this->collection_id, $blockname, $blockdata);
391 }
392
393 // insert collection type
394 $this->update_meta($this->collection_id, 'collection_type', 'social_share') ;
395
396 return $this->collection_id;
397 }
398
399 // remove post meta not in data post.
400 public function clean_meta($data)
401 {
402 $data_blocks = array_keys($data);
403
404 $meta = $this->get_meta();
405
406 foreach($meta as $meta_key => $values)
407 {
408 if (! in_array($meta_key, $data_blocks))
409 {
410 $this->delete_meta($this->collection_id, $meta_key);
411 }
412 }
413
414 }
415
416 public function update_meta($collection_id, $collection_key, $collection_value)
417 {
418 global $wpdb;
419 $table = maxUtils::get_collection_table_name();
420
421 if ($collection_value == '') return; // no data no database entry
422 if (is_array($collection_value) && count($collection_value) == 0) return; // same for empty arrays
423
424 if (is_array($collection_value))
425 $collection_value = json_encode($collection_value);
426
427 if ($collection_id == 0) // assume new
428 {
429 $collection_id = $this->getNewCollectionID();
430
431 $this->collection_id = $collection_id;
432 }
433
434 $sql = "SELECT meta_id from $table where collection_id = %d and collection_key = %s ";
435 $sql = $wpdb->prepare($sql, $collection_id, $collection_key);
436
437 $results = $wpdb->get_results($sql, ARRAY_A);
438
439 if (count($results) == 1)
440 {
441 $meta_id = $results[0]["meta_id"];
442 $data = array("collection_value" => $collection_value); // what's being updated
443 $where = array("meta_id" => $meta_id);
444
445 $wpdb->update($table, $data, $where);
446 return $meta_id;
447 }
448 if (count($results) == 0)
449 {
450 $data = array("collection_value" => $collection_value,
451 "collection_key" => $collection_key,
452 "collection_id" => $collection_id,
453 );
454
455 $meta_id = $wpdb->insert($table, $data);
456
457 return $meta_id;
458 }
459 else
460 {
461 MB()->add_notice("error", __("Update Collection meta has multiple rows, this should not be possible.","maxbuttons"));
462
463 }
464 return false;
465 }
466
467
468 /** Determine the next ID for collection */
469 protected function getNewCollectionID()
470 {
471 global $wpdb;
472 $table = maxUtils::get_collection_table_name();
473
474 $sql = "SELECT max(collection_id) as max from $table";
475
476 $max = intval($wpdb->get_var($sql));
477
478 $max = $max + 1;
479 return $max;
480 }
481
482 protected function delete_meta($collection_id, $collection_key)
483 {
484 if (intval($collection_id) > 0 && $collection_key != "")
485 {
486 global $wpdb;
487 // delete_post_meta($collection_id, $collection_key);
488 $table = maxUtils::get_collection_table_name();
489 $where = array("collection_id" => $collection_id,
490 "collection_key" => $collection_key
491 );
492 $where_format = array("%d", "%s");
493 $wpdb->delete($table, $where, $where_format);
494
495 }
496 }
497
498
499 /** Delete the collection. This is done via AJAX request */
500 /*public function delete($result, $data)
501 {
502 global $wpdb;
503 if(! $this->collection_id > 0)
504 return false;
505
506 $picker = $this->getBlock("picker");
507 $picker_data = $picker->get();
508 $buttons = $picker_data["selection"];
509 $button = MB()->getClass("button");
510
511 $buttons_removed = 0;
512
513 foreach($buttons as $button_id)
514 {
515 $deleted = $this->maybe_delete_button($button_id);
516 if ($deleted)
517 $buttons_removed++;
518
519 }
520
521 $table = maxUtils::get_collection_table_name();
522 $where = array("collection_id" => $this->collection_id);
523 $where_format = array("%d");
524 $wpdb->delete($table, $where, $where_format);
525
526 $result["data"]["body"] = __("The collection is removed","maxbuttons");
527 $result["data"]["title"] = __("Removed","maxbuttons");
528 $result["data"]["buttons_removed"] = $buttons_removed;
529 $result["data"]["collection_id"] = $this->collection_id;
530 return $result;
531
532
533 } */
534
535 /** On deletion of a collection check for each button if this is an auto-generated button just made for this collection * and if
536 * no changes where made to this button. If both conditions are true, remove the button */
537 /*function maybe_delete_button($button_id)
538 {
539 $button = MB()->getClass("button");
540
541 $button->set($button_id);
542 $button_data = $button->get();
543
544 $collection_id= $this->collection_id;
545
546 // remove unedited buttons created for this collection - use with care.
547 if (isset($button_data["meta"]["user_edited"]))
548 {
549 $created_source = (isset($button_data["meta"]["created_source"])) ? $button_data["meta"]["created_source"] : '';
550 if ($button_data["meta"]["user_edited"] === false && $created_source == 'collection')
551 {
552 $in_collections = $button_data["meta"]["in_collections"];
553
554 $key = array_search($collection_id, $in_collections);
555
556 if ($key !== false)
557 {
558 unset($button_data["meta"]["in_collections"][$key]);
559
560 if (count($button_data["meta"]["in_collections"]) == 0)
561 { $button->delete($button_id);
562 return true;
563 }
564 else
565 {
566 if ($button_id > 0) // safety.
567 $button->update($button_data);
568 }
569 }
570 }
571
572
573 }
574 return false;
575 } */
576
577 function get_meta ($collection_key = '')
578 {
579 global $wpdb;
580 $table = maxUtils::get_collection_table_name();
581
582 $prepare = array($this->collection_id);
583
584 $sql = "SELECT * from $table where collection_id = %d ";
585 if ($collection_key != '')
586 {
587 $sql .= " and collection_key = %s ";
588 array_push($prepare, $collection_key);
589
590 }
591
592 $sql = $wpdb->prepare($sql, $prepare);
593
594 $results = $wpdb->get_results($sql, ARRAY_A);
595
596 $result_array = array(); // format array by field name = values to feed blocks and others.
597 if (! is_null($results))
598 {
599
600 $nr = array();
601 foreach($results as $row)
602 {
603 $key = $row["collection_key"];
604 /* A field can be either plain text or JSON */
605 if(json_decode($row["collection_value"]))
606 $value = json_decode($row["collection_value"], true);
607 else
608 $value = $row["collection_value"];
609
610 $result_array[$key] = $value;
611
612 //$row["collection_value"] = unserialize($row["collection_value"]);
613 }
614
615 return $result_array;
616 }
617 else
618 {
619 return false;
620 }
621 }
622
623 function display($args = array())
624 {
625 if (! $this->is_showable)
626 return;
627
628 /* 1.8 Deprecated. Not included in MB() anymore, using svg method.
629 if (method_exists(MB(), 'load_library'))
630 {
631 MB()->load_library('fontawesome'); // always load lib since change is about 100% this includes FA.
632 }
633 */
634 maxUtils::startTime('collection-display-' . self::$count);
635
636 $w = MBSocial()->whistle();
637
638 wp_enqueue_style('mbsocial-buttons'); // load the general button style
639 wp_enqueue_script('mbsocial_front');
640
641
642 $defaults = array(
643 "preview" => false,
644 "echo" => true,
645 "style_tag" => false,
646 "compile" => false,
647 "js_tag" => true,
648 "load_type" => "footer",
649 );
650
651 $args = wp_parse_args($args, $defaults);
652
653 $w->tell('display/env/is_preview', $args['preview']);
654
655
656 //$cache = MaxCollections::checkCachedCollection($this->collection_id);
657 $cache = false;
658
659 if (! $cache)
660 {
661 //$cssParser = $this->getCSSParser();
662 $domObj = $this->parse($args);
663
664 //$css = $this->parseCSS($args);
665
666 $js = $this->parseJS($args);
667
668 $output = $domObj->save();
669
670 unset($domObj);
671
672
673 // CSS & JS output control
674 $output .= $this->displayCSS($this->css, $args);
675 $output .= $this->displayJS($js, $args);
676 }
677 else
678 $output = $cache;
679
680 //collections::addCachedCollection($this->collection_id, $output);
681
682 maxUtils::endTime('collection-display-' . self::$count);
683
684 if ($args["echo"])
685 {
686 echo $output;
687 }
688 else
689 {
690 return $output;
691 }
692
693 }
694
695 public function getDisplayCSS($doc_id = false) // For file output
696 {
697 $key = '_parsed_css';
698
699 if ($doc_id)
700 $key .= '_' . $doc_id;
701
702 $meta = $this->get_meta($key);
703 if (is_array($meta))
704 return array_shift($meta);
705 else
706 return false;
707
708 }
709
710 public function displayCSS($css, $args = array() ) // $echo = true, $style_tag = true)
711 {
712
713 $default = array(
714 "echo" => true,
715 "style_tag" => true,
716 "load_type" => "footer",
717 );
718 $args = wp_parse_args($args, $default);
719 if ($args['load_type'] == 'inline')
720 $args['style_tag'] =true;
721
722 $output = '';
723 $css = apply_filters('mbsocial/displaycss/', $css);
724
725 if ($args["style_tag"])
726 $output .= "<style type='text/css'>";
727
728 $output .= $css;
729
730 if ($args["style_tag"])
731 $output .= "</style>";
732
733
734 if ($args["load_type"] == 'footer')
735 {
736 $load_id = 'collection-' . $this->collection_id; // must be unique for unique items, otherwise css won't be output
737
738 $use_file = get_option('maxbuttons_usecssfile', false);
739 if ($use_file)
740 {
741
742 wp_enqueue_style('maxbuttons-social-front-' . self::$count, admin_url('admin-ajax.php').'?action=maxbuttons_social_css&id=' . $this->collection_id . '&doc=' . self::$count) ;
743 $this->update_meta($this->collection_id, '_parsed_css_' . self::$count, $css);
744 }
745
746 if (self::$count > 1)
747 {
748 $load_id .= "-" . self::$count;
749 }
750
751
752 do_action('mb-footer',$load_id, $output);
753 }
754 elseif ($args["load_type"] == 'inline')
755 {
756 if ($args["echo"]) echo $output;
757 else return $output;
758
759 }
760
761 }
762
763 public function displayJS($js, $args = array() ) // $echo = true, $style_tag = true)
764 {
765 $default = array(
766 "echo" => true,
767 "js_tag" => true,
768 "load_type" => "footer",
769 "preview" => false,
770 );
771
772
773 $args = wp_parse_args($args, $default);
774
775 if ($args["preview"])
776 return; // no js on previews.
777
778 $output = '';
779
780 if (count($js) == 0)
781 return; // no output, holiday
782
783 if ($args["js_tag"])
784 {
785 $output .= "<script type='text/javascript'> ";
786
787 }
788
789 foreach($js as $index => $code)
790 {
791 $output .= $code;
792 }
793
794 if ($args["js_tag"])
795 {
796 $output .= " // }
797 // }
798 // window.onload = MBcollection" . $this->collection_id . "();
799 </script> ";
800 }
801
802
803 if ($args["load_type"] == 'footer')
804 {
805
806 do_action('mb-footer','collection-' . $this->collection_id, $output, "js");
807 }
808 elseif ($args["load_type"] == 'inline')
809 {
810 if ($args["echo"]) echo $output;
811 else return $output;
812
813 }
814 }
815
816 /* Parses the collection, via the blocks */
817 function parse($args)
818 {
819 $admin = mbSocial()->admin();
820 $w = MBSocial()->whistle();
821 $cssParser = $this->getCSSParser();
822 maxUtils::startTime('collection-parse-'. self::$count);
823
824 //$this->setEnv(); // sets the relevant data for the block (orientation etc)
825 $this->buttons = array(); // reset the buttons - needed for preview
826
827 $is_preview = isset($args["preview"]) ? $args["preview"] : false;
828
829 $collectionObj = new simple_html_dom();
830 $collection_id = $this->collection_id;
831
832 $styleObj = $this->styleObj;
833 $style_class = ''; //$styleObj->class;
834 $style_class = $this->orientation;
835
836 // create container
837 if (self::$count > 1)
838 {
839 $style_class .= ' doc-' . self::$count;
840 }
841 $node = "<div class='maxsocial maxsocial-" . $collection_id . " $style_class ' data-collection='" . $collection_id . "'>
842 </div>";
843 $node = apply_filters("maxbuttons/social/basic-container",$node);
844
845 $collectionObj->load($node);
846
847 $active = ( isset($this->data['network']['network_active']) && is_array($this->data['network']['network_active']) ) ? $this->data['network']['network_active'] : array();
848
849 if (count($active) == 0 && $args['preview'] == true)
850 {
851 $networks = $admin->get_default_networks();
852 $use_networks = $networks['unselected'];
853 }
854 elseif (count($active) == 0)
855 return $collectionObj; // no networks no collection
856 else
857 {
858 $networks = $admin->get_networks($active);
859 $use_networks = $networks['selected'];
860 }
861
862 $index = 0; // used as unique button_id
863
864 maxUtils::startTime('collection-parse-network-' . self::$count);
865 foreach($use_networks as $network)
866 {
867 if (! is_object($network))
868 {
869 continue;
870 }
871 if (! $is_preview)
872 {
873 if ( ($this->is_mobile && ! $network->forMobile() ) || $this->is_desktop && ! $network->forDesktop() )
874 {
875 continue;
876 }
877 }
878 $w->tell('display/parse/network', $network);
879
880 $button_args = array('link' => $network->get_url(),
881 'preview' => $is_preview,
882 'index' => $index,
883 'name' => $network->get('network'),
884 'data' => $this->data,
885 'collection_count' => self::$count,
886 );
887
888 $button_args = apply_filters('mbsocial/parsebutton/args', $button_args);
889 $button = $network->createButton($button_args);
890
891 // empty response == skip button
892 if (! $button || ! is_object($button))
893 continue;
894
895
896 foreach($this->blocks as $block)
897 {
898 /** Here some reset to block and to style, to denote new button creation. */
899 $block->setPreview($is_preview);
900 $button = $block->parse($button, $button_args);
901 }
902
903 if ($button)
904 {
905
906 // save DOM structure to parser
907 $cssParser->loadDom($button);
908 $css = '';
909
910 $css = $this->parseCSS( array('preview' => $is_preview) );
911
912 $this->buttons[] = array('item_class' => $button_args['name'],
913 'obj' => $button,
914 'css' => $css,
915 );
916
917 }
918
919 $index++;
920
921 } // network loop.
922 maxUtils::endTime('collection-parse-network-' . self::$count);
923
924 // general parsing
925 $button_output = '';
926 $css = '';
927
928 foreach($this->buttons as $button_item)
929 {
930 $buttonDom = $button_item['obj'];
931 $button_output .= $buttonDom->save() ;
932 $css .= $button_item['css'];
933 }
934
935 $count_block = $this->blocks['countBlock'];
936 $count_block->createTotal($collectionObj);
937
938 $cssParser->loadDom($collectionObj);
939 $css .= $this->parseCSS( array('preview' => $is_preview, 'skip_blocks' => true) );
940
941 $this->css = $css;
942
943 $collectionObj->find('.maxsocial', 0)->innertext .= $button_output;
944 $collectionObj->load($collectionObj->save());
945
946 maxUtils::endTime('collection-parse-'. self::$count);
947 return $collectionObj;
948 }
949
950
951 protected function parseCSS($args)
952 {
953 maxUtils::startTime('collection-parseCSS-'. self::$count);
954 $defaults = array(
955 'preview' => false,
956 'skip_blocks' => false
957 );
958 $args = wp_parse_args($args, $defaults);
959
960
961 $css = array();
962 if (! is_null($this->styleObj))
963 {
964 $prep_args = array(
965 'orientation' => $this->orientation,
966 'is_static' => $this->is_static,
967 'is_mobile' => $this->is_mobile,
968 'is_tablet' => $this->is_tablet,
969 'is_post' => $this->is_post);
970
971 $this->styleObj->prepareCSS($prep_args);
972 $css = $this->styleObj->getCSS();
973 }
974
975 foreach($this->blocks as $block)
976 {
977 // This needs changes to accomodate to parse the defined SCSS stylesheet instead of using CSSParser which will not work here.
978 // It will not work because CSSparse expects a very precise class definition and not a general broad definition of the items.
979 $css = $block->parseCSS($css, $args);
980
981 }
982
983 // before the parse let have style have one more go
984 $css = $this->styleObj->preParse($css);
985 $css = apply_filters('mbsocial/parsecss/', $css); // anybody else
986
987 $css = $this->getCSSParser()->parse($css);
988
989 maxUtils::endTime('collection-parseCSS-'. self::$count);
990 return $css;
991 }
992
993 protected function parseJS($args)
994 {
995 $js = array();
996
997 $defaults = array("preview" => false,
998 );
999
1000 $args = wp_parse_args($args, $defaults);
1001
1002 if ($args["preview"])
1003 return false; // no js on previews
1004
1005 foreach($this->blocks as $block)
1006 {
1007 $js = $block->parseJS($js, $args);
1008
1009 }
1010
1011 return $js;
1012 }
1013
1014
1015
1016
1017 /** Metadata functions **/
1018
1019 public function get_metadata($post_id)
1020 {
1021 if ( ! is_array($this->meta_data) || count ($this->meta_data) == 0)
1022 {
1023 $data = get_post_meta($post_id, 'mbsocial_data', true);
1024 $this->meta_data = $data;
1025 }
1026
1027 return $this->meta_data;
1028 }
1029
1030 public function do_meta_boxes($post_type, $post)
1031 {
1032 $content_blocks = array();
1033
1034 foreach($this->blocks as $block)
1035 {
1036 $content_blocks = $block->do_meta_boxes($content_blocks, $post);
1037 }
1038
1039 return $content_blocks;
1040 }
1041
1042 /** @param $post_id Saving post_id
1043 * @param $post Post Array of submitted values
1044 **/
1045 public function save_meta_boxes($post_id, $post)
1046 {
1047 $post_meta = array();
1048
1049 foreach($this->blocks as $block)
1050 {
1051 $post_meta = $block->save_meta_boxes($post_meta, $post_id, $post);
1052
1053 }
1054
1055 if (count($post_meta) > 0)
1056 update_post_meta($post_id, 'mbsocial_data', $post_meta);
1057 }
1058
1059 function showBlocks()
1060 {
1061 foreach($this->blocks as $block)
1062 {
1063 echo $block->admin();
1064 }
1065
1066 $active = isset($this->data['network']['network_active']) ? $this->data['network']['network_active'] : array();
1067
1068 if ( is_array($active) && count($active) > 0)
1069 {
1070 foreach($active as $network_name)
1071 {
1072 $network = MbSocial()->networks()->get($network_name);
1073 if ( is_object($network))
1074 echo $network->admin($this->data);
1075 }
1076 }
1077 }
1078 } //class
1079