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

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