| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace MBSocial; |
| 3 | 3 | defined('ABSPATH') or die('No direct access permitted'); |
| 4 | 4 | |
| 5 | -use \maxbuttons\maxUtils as maxUtils; | |
| 6 | -use \maxbuttons\maxCSSParser as maxCSSParser; | |
| 7 | -use \simple_html_dom as simple_html_dom; | |
| 8 | -use \MaxButtons\maxBlocks as maxBlocks; | |
| 5 | +use \MaxButtons\maxUtils as maxUtils; | |
| 6 | +use \MaxButtons\maxCSSParser as maxCSSParser; | |
| 7 | +use \MaxButtons\simple_html_dom as simple_html_dom; | |
| 9 | 8 | |
| 9 | + | |
| 10 | 10 | class collection |
| 11 | 11 | { |
| 12 | 12 | protected $blocks = array(); // array of objects |
| 13 | 13 | protected $buttons = array(); |
| @@ -13,9 +13,12 @@ | ||
| 13 | 13 | protected $buttons = array(); |
| 14 | 14 | protected $data; // data of the collection |
| 15 | 15 | protected $meta_data; // data stored specific to posts / pages etc |
| 16 | 16 | |
| 17 | + protected $css; | |
| 18 | + | |
| 17 | 19 | protected $collection_id = 0; |
| 20 | + protected static $count = 0; | |
| 18 | 21 | protected $collection_type = 'social_share'; |
| 19 | 22 | protected $styleObj = null; |
| 20 | 23 | |
| 21 | 24 | // layout functions |
| @@ -23,14 +26,15 @@ | ||
| 23 | 26 | |
| 24 | 27 | // save hook currently active |
| 25 | 28 | protected $doingHook; |
| 26 | 29 | |
| 27 | - | |
| 28 | 30 | public $is_showable = true; // keeps state of hidden options |
| 29 | 31 | public $is_static = false; |
| 30 | 32 | public $is_post = false; // embedded in a post situation |
| 31 | 33 | public $is_mobile = false; |
| 32 | 34 | public $is_tablet = false; |
| 35 | + public $is_desktop = false; | |
| 36 | + | |
| 33 | 37 | public $orientation = 'horizontal'; |
| 34 | 38 | public $display_mode = 'hidden'; |
| 35 | 39 | public $post_id; |
| 36 | 40 | |
| @@ -40,8 +44,11 @@ | ||
| 40 | 44 | */ |
| 41 | 45 | public function __construct($collection_id = 0) |
| 42 | 46 | { |
| 43 | 47 | collections::checkExpireTrans(); |
| 48 | + $w = MBSocial()->whistle(); | |
| 49 | + $w->tell('collection/new', $collection_id); | |
| 50 | + self::$count++; | |
| 44 | 51 | |
| 45 | 52 | $blocks = collections::getBlocks(); |
| 46 | 53 | |
| 47 | 54 | $default_data = array(); |
| @@ -52,18 +59,16 @@ | ||
| 52 | 59 | $block = collections::getBlock($block_name); |
| 53 | 60 | if ($block) |
| 54 | 61 | { |
| 55 | 62 | $block->setCollection($this); |
| 56 | - $default_data = $block->save_fields($default_data, null ); // force the fields to set a default pos. | |
| 63 | + $default_data = $block->save_fields($default_data, array() ); // force the fields to set a default pos. | |
| 57 | 64 | |
| 58 | 65 | $this->blocks[$block_name] = $block; |
| 59 | 66 | |
| 60 | 67 | } |
| 61 | - maxBlocks::add($block); | |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | // then load the default as data |
| 65 | - maxBlocks::setData($default_data); | |
| 66 | 71 | $this->data = $default_data; |
| 67 | 72 | |
| 68 | 73 | foreach($this->blocks as $block) |
| 69 | 74 | { |
| @@ -74,10 +79,11 @@ | ||
| 74 | 79 | |
| 75 | 80 | } |
| 76 | 81 | |
| 77 | 82 | if ($collection_id > 0) |
| 78 | - $this->set($collection_id); | |
| 79 | - | |
| 83 | + { | |
| 84 | + $this->set($collection_id); // set function loads the block values | |
| 85 | + } | |
| 80 | 86 | } |
| 81 | 87 | /* Get all buttons that are currently loaded */ |
| 82 | 88 | /*public function getLoadedButtons() |
| 83 | 89 | { |
| @@ -113,9 +119,9 @@ | ||
| 113 | 119 | public function getCSSParser() |
| 114 | 120 | { |
| 115 | 121 | if (! $this->cssParser) |
| 116 | 122 | { $this->cssParser = new maxCSSParser(); |
| 117 | - $this->cssParser->anchor_class = '.maxbutton-social'; | |
| 123 | + $this->cssParser->anchor_class = '.mb-social'; | |
| 118 | 124 | } |
| 119 | 125 | return $this->cssParser; |
| 120 | 126 | } |
| 121 | 127 | |
| @@ -125,10 +131,16 @@ | ||
| 125 | 131 | public function set($collection_id) |
| 126 | 132 | { |
| 127 | 133 | $this->collection_id = $collection_id; |
| 128 | 134 | |
| 135 | + | |
| 129 | 136 | // make every block set it's stuff. |
| 130 | - $data = $this->get_meta($collection_id); | |
| 137 | + $data = $this->get_meta(); | |
| 138 | + if ( isset($data['style']['style']) ) | |
| 139 | + { | |
| 140 | + Install::convertToOne($this); | |
| 141 | + $data = $this->get_meta(); | |
| 142 | + } | |
| 131 | 143 | |
| 132 | 144 | foreach($this->blocks as $name => $block) |
| 133 | 145 | { |
| 134 | 146 | // run every blocks setter |
| @@ -137,11 +149,30 @@ | ||
| 137 | 149 | |
| 138 | 150 | $this->data = $data; |
| 139 | 151 | } |
| 140 | 152 | |
| 141 | - public function setData($data) | |
| 153 | + public function setData($blockname, $data) | |
| 142 | 154 | { |
| 143 | - $this->data = $data; | |
| 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 | + } | |
| 144 | 175 | } |
| 145 | 176 | |
| 146 | 177 | public function setHook($hook) |
| 147 | 178 | { |
| @@ -148,16 +179,22 @@ | ||
| 148 | 179 | $this->doingHook = $hook; |
| 149 | 180 | $this->setEnv(); |
| 150 | 181 | } |
| 151 | 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 | + | |
| 152 | 189 | // sets the style object to a collection. This is important for styling CSS |
| 153 | 190 | public function setStyle($style) |
| 154 | 191 | { |
| 155 | 192 | if (! is_object($style)) |
| 156 | 193 | return false; |
| 157 | - | |
| 194 | + | |
| 158 | 195 | $this->styleObj = $style; |
| 159 | - $style->setActive(); | |
| 196 | + $style->setActive($this); | |
| 160 | 197 | } |
| 161 | 198 | |
| 162 | 199 | public function getStyle() |
| 163 | 200 | { |
| @@ -174,15 +211,22 @@ | ||
| 174 | 211 | } |
| 175 | 212 | |
| 176 | 213 | public function setEnv() // load orientations and specifics about the current collections. After data has loaded and all that. |
| 177 | 214 | { |
| 215 | + $w = MBSocial()->whistle(); | |
| 216 | + | |
| 178 | 217 | $data = $this->data; |
| 179 | 218 | |
| 180 | 219 | $hook = $this->doingHook; |
| 220 | + | |
| 181 | 221 | $display = isset($this->data[$hook]) ? $this->data[$hook] : 'hidden'; |
| 222 | + if ($this->doingHook == 'shortcode') | |
| 223 | + $display = 'shortcode'; // exception | |
| 224 | + | |
| 182 | 225 | $this->display_mode = $display; |
| 183 | 226 | |
| 184 | - if (! is_admin() ) | |
| 227 | + | |
| 228 | + if (! is_admin() && is_singular() ) | |
| 185 | 229 | { |
| 186 | 230 | global $post; |
| 187 | 231 | $this->post_id = $post->ID; |
| 188 | 232 | } |
| @@ -193,9 +237,9 @@ | ||
| 193 | 237 | if ($display == 'static') |
| 194 | 238 | { |
| 195 | 239 | $this->is_static = true; |
| 196 | 240 | } |
| 197 | - elseif ($display == 'after' || $display == 'before' || $display == 'after-before') | |
| 241 | + elseif ($display == 'after' || $display == 'before' || $display == 'after-before' || $display == 'shortcode') | |
| 198 | 242 | $this->is_post = true; |
| 199 | 243 | |
| 200 | 244 | // check if on some mobile |
| 201 | 245 | $mobileDetect = new Mobile_Detect; |
| @@ -200,8 +244,9 @@ | ||
| 200 | 244 | // check if on some mobile |
| 201 | 245 | $mobileDetect = new Mobile_Detect; |
| 202 | 246 | $this->is_mobile = $mobileDetect->isMobile(); |
| 203 | 247 | $this->is_tablet = $mobileDetect->isTablet(); |
| 248 | + $this->is_desktop = ($this->is_mobile) ? false : true; | |
| 204 | 249 | |
| 205 | 250 | $options = isset($data['display'][$hook . '_options']) ? $data['display'][$hook . '_options'] : array(); |
| 206 | 251 | |
| 207 | 252 | $orientation = isset($options['orientation']) ? $options['orientation'] : 'auto'; |
| @@ -230,11 +275,11 @@ | ||
| 230 | 275 | |
| 231 | 276 | $this->orientation = $orientation; |
| 232 | 277 | |
| 233 | 278 | // Check if any hidden variables are given. |
| 234 | - $active = isset($this->data['general']['active']) ? $this->data['general']['active'] : true; | |
| 279 | + $is_active = isset($this->data['general']['active']) ? $this->data['general']['active'] : true; | |
| 235 | 280 | |
| 236 | - if ($active === 0) | |
| 281 | + if ($is_active === 0) | |
| 237 | 282 | { |
| 238 | 283 | $this->is_showable = false; |
| 239 | 284 | } |
| 240 | 285 | |
| @@ -240,13 +285,43 @@ | ||
| 240 | 285 | |
| 241 | 286 | if ($this->post_id > 0) |
| 242 | 287 | { |
| 243 | 288 | $meta = $this->get_metadata($this->post_id); |
| 244 | - $is_active = (isset($meta['general']['active'])) ? $meta['general']['active'] : true; | |
| 245 | - if ($is_active === 0) | |
| 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 | + { | |
| 246 | 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; | |
| 247 | 298 | } |
| 248 | 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 | + | |
| 249 | 324 | } |
| 250 | 325 | |
| 251 | 326 | /** Interface for handling AJAX requests to communicate with the seperate blocks. |
| 252 | 327 | * @param $result The initial (default) result to JSON back |
| @@ -276,13 +351,28 @@ | ||
| 276 | 351 | $result["title"] = __("Error : Block was not there","maxbuttons"); |
| 277 | 352 | return $result; |
| 278 | 353 | } |
| 279 | 354 | |
| 280 | - /** Save the collection data to the database. This will invoke the save functions of the blocks itself to gather, verify and manipulate data */ | |
| 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 */ | |
| 281 | 356 | public function save($post) |
| 282 | 357 | { |
| 283 | 358 | $data = array(); |
| 284 | 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 | + { | |
| 285 | 375 | // Collection id needed to save meta data and others. |
| 286 | 376 | if ($this->collection_id == 0) // assume new |
| 287 | 377 | { |
| 288 | 378 | $collection_id = $this->getNewCollectionID(); |
| @@ -289,20 +379,13 @@ | ||
| 289 | 379 | |
| 290 | 380 | $this->collection_id = $collection_id; |
| 291 | 381 | } |
| 292 | 382 | |
| 293 | - // run this by each block and collect data | |
| 294 | - foreach($this->blocks as $block_name => $block) | |
| 295 | - { | |
| 383 | + // clean what was not set | |
| 384 | + $this->clean_meta($this->data); | |
| 296 | 385 | |
| 297 | - $data = $block->save_fields($data, $post); | |
| 298 | - } | |
| 299 | - | |
| 300 | - // clean what was not set | |
| 301 | - $this->clean_meta($data); | |
| 302 | - | |
| 303 | 386 | // write data as a per block per key value into the database |
| 304 | - foreach($data as $blockname => $blockdata) | |
| 387 | + foreach($this->data as $blockname => $blockdata) | |
| 305 | 388 | { |
| 306 | 389 | // seems dangerous |
| 307 | 390 | $meta_id = $this->update_meta($this->collection_id, $blockname, $blockdata); |
| 308 | 391 | } |
| @@ -310,9 +393,8 @@ | ||
| 310 | 393 | // insert collection type |
| 311 | 394 | $this->update_meta($this->collection_id, 'collection_type', 'social_share') ; |
| 312 | 395 | |
| 313 | 396 | return $this->collection_id; |
| 314 | - | |
| 315 | 397 | } |
| 316 | 398 | |
| 317 | 399 | // remove post meta not in data post. |
| 318 | 400 | public function clean_meta($data) |
| @@ -318,9 +400,9 @@ | ||
| 318 | 400 | public function clean_meta($data) |
| 319 | 401 | { |
| 320 | 402 | $data_blocks = array_keys($data); |
| 321 | 403 | |
| 322 | - $meta = $this->get_meta($this->collection_id); | |
| 404 | + $meta = $this->get_meta(); | |
| 323 | 405 | |
| 324 | 406 | foreach($meta as $meta_key => $values) |
| 325 | 407 | { |
| 326 | 408 | if (! in_array($meta_key, $data_blocks)) |
| @@ -330,9 +412,9 @@ | ||
| 330 | 412 | } |
| 331 | 413 | |
| 332 | 414 | } |
| 333 | 415 | |
| 334 | - protected function update_meta($collection_id, $collection_key, $collection_value) | |
| 416 | + public function update_meta($collection_id, $collection_key, $collection_value) | |
| 335 | 417 | { |
| 336 | 418 | global $wpdb; |
| 337 | 419 | $table = maxUtils::get_collection_table_name(); |
| 338 | 420 | |
| @@ -407,9 +489,9 @@ | ||
| 407 | 489 | $where = array("collection_id" => $collection_id, |
| 408 | 490 | "collection_key" => $collection_key |
| 409 | 491 | ); |
| 410 | 492 | $where_format = array("%d", "%s"); |
| 411 | - $wpdb->delete($table, $where, $format); | |
| 493 | + $wpdb->delete($table, $where, $where_format); | |
| 412 | 494 | |
| 413 | 495 | } |
| 414 | 496 | } |
| 415 | 497 | |
| @@ -491,14 +573,14 @@ | ||
| 491 | 573 | } |
| 492 | 574 | return false; |
| 493 | 575 | } */ |
| 494 | 576 | |
| 495 | - function get_meta ($collection_id, $collection_key = '') | |
| 577 | + function get_meta ($collection_key = '') | |
| 496 | 578 | { |
| 497 | 579 | global $wpdb; |
| 498 | 580 | $table = maxUtils::get_collection_table_name(); |
| 499 | 581 | |
| 500 | - $prepare = array($collection_id); | |
| 582 | + $prepare = array($this->collection_id); | |
| 501 | 583 | |
| 502 | 584 | $sql = "SELECT * from $table where collection_id = %d "; |
| 503 | 585 | if ($collection_key != '') |
| 504 | 586 | { |
| @@ -513,8 +595,9 @@ | ||
| 513 | 595 | |
| 514 | 596 | $result_array = array(); // format array by field name = values to feed blocks and others. |
| 515 | 597 | if (! is_null($results)) |
| 516 | 598 | { |
| 599 | + | |
| 517 | 600 | $nr = array(); |
| 518 | 601 | foreach($results as $row) |
| 519 | 602 | { |
| 520 | 603 | $key = $row["collection_key"]; |
| @@ -522,8 +605,9 @@ | ||
| 522 | 605 | if(json_decode($row["collection_value"])) |
| 523 | 606 | $value = json_decode($row["collection_value"], true); |
| 524 | 607 | else |
| 525 | 608 | $value = $row["collection_value"]; |
| 609 | + | |
| 526 | 610 | $result_array[$key] = $value; |
| 527 | 611 | |
| 528 | 612 | //$row["collection_value"] = unserialize($row["collection_value"]); |
| 529 | 613 | } |
| @@ -540,12 +624,22 @@ | ||
| 540 | 624 | { |
| 541 | 625 | if (! $this->is_showable) |
| 542 | 626 | return; |
| 543 | 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 | + | |
| 544 | 638 | wp_enqueue_style('mbsocial-buttons'); // load the general button style |
| 545 | 639 | wp_enqueue_script('mbsocial_front'); |
| 546 | 640 | |
| 547 | - maxUtils::startTime('collection-display'); | |
| 641 | + | |
| 548 | 642 | $defaults = array( |
| 549 | 643 | "preview" => false, |
| 550 | 644 | "echo" => true, |
| 551 | 645 | "style_tag" => false, |
| @@ -555,17 +649,20 @@ | ||
| 555 | 649 | ); |
| 556 | 650 | |
| 557 | 651 | $args = wp_parse_args($args, $defaults); |
| 558 | 652 | |
| 653 | + $w->tell('display/env/is_preview', $args['preview']); | |
| 654 | + | |
| 655 | + | |
| 559 | 656 | //$cache = MaxCollections::checkCachedCollection($this->collection_id); |
| 560 | 657 | $cache = false; |
| 561 | 658 | |
| 562 | 659 | if (! $cache) |
| 563 | 660 | { |
| 564 | - $cssParser = $this->getCSSParser(); | |
| 661 | + //$cssParser = $this->getCSSParser(); | |
| 565 | 662 | $domObj = $this->parse($args); |
| 566 | 663 | |
| 567 | - $css = $this->parseCSS($args); | |
| 664 | + //$css = $this->parseCSS($args); | |
| 568 | 665 | |
| 569 | 666 | $js = $this->parseJS($args); |
| 570 | 667 | |
| 571 | 668 | $output = $domObj->save(); |
| @@ -573,9 +670,9 @@ | ||
| 573 | 670 | unset($domObj); |
| 574 | 671 | |
| 575 | 672 | |
| 576 | 673 | // CSS & JS output control |
| 577 | - $output .= $this->displayCSS($css, $args); | |
| 674 | + $output .= $this->displayCSS($this->css, $args); | |
| 578 | 675 | $output .= $this->displayJS($js, $args); |
| 579 | 676 | } |
| 580 | 677 | else |
| 581 | 678 | $output = $cache; |
| @@ -581,11 +678,10 @@ | ||
| 581 | 678 | $output = $cache; |
| 582 | 679 | |
| 583 | 680 | //collections::addCachedCollection($this->collection_id, $output); |
| 584 | 681 | |
| 585 | - maxUtils::endTime('collection-display'); | |
| 682 | + maxUtils::endTime('collection-display-' . self::$count); | |
| 586 | 683 | |
| 587 | - | |
| 588 | 684 | if ($args["echo"]) |
| 589 | 685 | { |
| 590 | 686 | echo $output; |
| 591 | 687 | } |
| @@ -595,8 +691,23 @@ | ||
| 595 | 691 | } |
| 596 | 692 | |
| 597 | 693 | } |
| 598 | 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 | + | |
| 599 | 710 | public function displayCSS($css, $args = array() ) // $echo = true, $style_tag = true) |
| 600 | 711 | { |
| 601 | 712 | |
| 602 | 713 | $default = array( |
| @@ -608,8 +719,9 @@ | ||
| 608 | 719 | if ($args['load_type'] == 'inline') |
| 609 | 720 | $args['style_tag'] =true; |
| 610 | 721 | |
| 611 | 722 | $output = ''; |
| 723 | + $css = apply_filters('mbsocial/displaycss/', $css); | |
| 612 | 724 | |
| 613 | 725 | if ($args["style_tag"]) |
| 614 | 726 | $output .= "<style type='text/css'>"; |
| 615 | 727 | |
| @@ -620,10 +732,25 @@ | ||
| 620 | 732 | |
| 621 | 733 | |
| 622 | 734 | if ($args["load_type"] == 'footer') |
| 623 | 735 | { |
| 736 | + $load_id = 'collection-' . $this->collection_id; // must be unique for unique items, otherwise css won't be output | |
| 624 | 737 | |
| 625 | - do_action('mb-footer','collection-' . $this->collection_id, $output); | |
| 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); | |
| 626 | 753 | } |
| 627 | 754 | elseif ($args["load_type"] == 'inline') |
| 628 | 755 | { |
| 629 | 756 | if ($args["echo"]) echo $output; |
| @@ -655,12 +782,9 @@ | ||
| 655 | 782 | |
| 656 | 783 | if ($args["js_tag"]) |
| 657 | 784 | { |
| 658 | 785 | $output .= "<script type='text/javascript'> "; |
| 659 | -// $output .= " if (typeof MBcollection" . $this->collection_id . " == 'undefined') { "; | |
| 660 | -// $output .= " function MBcollection" . $this->collection_id . "() { "; | |
| 661 | 786 | |
| 662 | - | |
| 663 | 787 | } |
| 664 | 788 | |
| 665 | 789 | foreach($js as $index => $code) |
| 666 | 790 | { |
| @@ -692,23 +816,33 @@ | ||
| 692 | 816 | /* Parses the collection, via the blocks */ |
| 693 | 817 | function parse($args) |
| 694 | 818 | { |
| 695 | 819 | $admin = mbSocial()->admin(); |
| 820 | + $w = MBSocial()->whistle(); | |
| 821 | + $cssParser = $this->getCSSParser(); | |
| 822 | + maxUtils::startTime('collection-parse-'. self::$count); | |
| 823 | + | |
| 696 | 824 | //$this->setEnv(); // sets the relevant data for the block (orientation etc) |
| 697 | 825 | $this->buttons = array(); // reset the buttons - needed for preview |
| 698 | 826 | |
| 699 | - $preview = isset($args["preview"]) ? $args["preview"] : false; | |
| 827 | + $is_preview = isset($args["preview"]) ? $args["preview"] : false; | |
| 700 | 828 | |
| 701 | 829 | $collectionObj = new simple_html_dom(); |
| 702 | 830 | $collection_id = $this->collection_id; |
| 703 | 831 | |
| 704 | 832 | $styleObj = $this->styleObj; |
| 705 | - $style_class = $styleObj->class; | |
| 706 | - $style_class .= ' ' . $this->orientation; | |
| 833 | + $style_class = ''; //$styleObj->class; | |
| 834 | + $style_class = $this->orientation; | |
| 707 | 835 | |
| 708 | - $node = "<div class='maxcollection maxcollection-" . $collection_id . " $style_class ' data-collection='" . $collection_id . "'> | |
| 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 . "'> | |
| 709 | 842 | </div>"; |
| 710 | 843 | $node = apply_filters("maxbuttons/social/basic-container",$node); |
| 844 | + | |
| 711 | 845 | $collectionObj->load($node); |
| 712 | 846 | |
| 713 | 847 | $active = ( isset($this->data['network']['network_active']) && is_array($this->data['network']['network_active']) ) ? $this->data['network']['network_active'] : array(); |
| 714 | 848 | |
| @@ -724,131 +858,107 @@ | ||
| 724 | 858 | $networks = $admin->get_networks($active); |
| 725 | 859 | $use_networks = $networks['selected']; |
| 726 | 860 | } |
| 727 | 861 | |
| 728 | - maxUtils::startTime('collection-parse-parsebuttons'); | |
| 862 | + $index = 0; // used as unique button_id | |
| 863 | + | |
| 864 | + maxUtils::startTime('collection-parse-network-' . self::$count); | |
| 729 | 865 | foreach($use_networks as $network) |
| 730 | 866 | { |
| 731 | - //$style = $this->data['style']['style']; | |
| 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); | |
| 732 | 879 | |
| 733 | - $args = array('link' => $network->get('share_url'), | |
| 734 | - 'name' => $network->get('network'), | |
| 735 | - // 'classes' => $style, | |
| 736 | - 'network' => $network, | |
| 737 | - 'preview' => $preview, | |
| 738 | - | |
| 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, | |
| 739 | 886 | ); |
| 740 | 887 | |
| 741 | - $args = apply_filters('mbsocial/parsebutton/args', $args); | |
| 888 | + $button_args = apply_filters('mbsocial/parsebutton/args', $button_args); | |
| 889 | + $button = $network->createButton($button_args); | |
| 742 | 890 | |
| 743 | - maxUtils::endTime('collection-parse-parsebuttons'); | |
| 891 | + // empty response == skip button | |
| 892 | + if (! $button || ! is_object($button)) | |
| 893 | + continue; | |
| 744 | 894 | |
| 745 | - $button = $this->createButton($args); | |
| 746 | 895 | |
| 747 | - maxUtils::startTime('collection-parse-blockparse'); | |
| 748 | - | |
| 749 | - | |
| 750 | 896 | foreach($this->blocks as $block) |
| 751 | 897 | { |
| 752 | - $block->setPreview($preview); | |
| 753 | - $button = $block->parse($button, $args); | |
| 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); | |
| 754 | 901 | } |
| 755 | 902 | |
| 756 | 903 | if ($button) |
| 757 | 904 | { |
| 758 | - $this->buttons[] = array('item_class' => $args['name'], | |
| 759 | - 'obj' => $button); | |
| 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 | + | |
| 760 | 917 | } |
| 918 | + | |
| 919 | + $index++; | |
| 920 | + | |
| 761 | 921 | } // network loop. |
| 922 | + maxUtils::endTime('collection-parse-network-' . self::$count); | |
| 762 | 923 | |
| 763 | 924 | // general parsing |
| 764 | - | |
| 765 | - maxUtils::endTime('collection-parse-blockparse'); | |
| 766 | - | |
| 767 | 925 | $button_output = ''; |
| 926 | + $css = ''; | |
| 768 | 927 | |
| 769 | 928 | foreach($this->buttons as $button_item) |
| 770 | 929 | { |
| 771 | 930 | $buttonDom = $button_item['obj']; |
| 772 | 931 | $button_output .= $buttonDom->save() ; |
| 773 | - | |
| 932 | + $css .= $button_item['css']; | |
| 774 | 933 | } |
| 775 | 934 | |
| 776 | - $this->createTotal($collectionObj); | |
| 935 | + $count_block = $this->blocks['countBlock']; | |
| 936 | + $count_block->createTotal($collectionObj); | |
| 777 | 937 | |
| 778 | - $collectionObj->find('.maxcollection', 0)->innertext .= $button_output; | |
| 938 | + $cssParser->loadDom($collectionObj); | |
| 939 | + $css .= $this->parseCSS( array('preview' => $is_preview, 'skip_blocks' => true) ); | |
| 779 | 940 | |
| 941 | + $this->css = $css; | |
| 780 | 942 | |
| 943 | + $collectionObj->find('.maxsocial', 0)->innertext .= $button_output; | |
| 781 | 944 | $collectionObj->load($collectionObj->save()); |
| 782 | 945 | |
| 783 | - $cssParser = $this->getCSSParser(); | |
| 784 | - //$collectionObj->load($collectionObj->save() ); | |
| 785 | - | |
| 786 | - // save DOM structure to parser | |
| 787 | - $cssParser->loadDom($collectionObj); | |
| 788 | - | |
| 946 | + maxUtils::endTime('collection-parse-'. self::$count); | |
| 789 | 947 | return $collectionObj; |
| 790 | 948 | } |
| 791 | 949 | |
| 792 | - // create a block for total shares | |
| 793 | - public function createTotal($collectionObj, $args = array() ) | |
| 794 | - { | |
| 795 | 950 | |
| 796 | - $count_block = $this->blocks['countBlock']; | |
| 797 | - | |
| 798 | - if (! $count_block->showTotal() ) | |
| 799 | - return ; | |
| 800 | - | |
| 801 | - $total = $count_block->get_total(); | |
| 802 | - $total_label = maxBlocks::getValue('total_count_label'); //$count_block->get_total_label(); | |
| 803 | - | |
| 804 | - $total_html = '<div class="collection-item total"> | |
| 805 | - <div class="maxbutton-social-total"> | |
| 806 | - <div class="mb-icon-total"><i class="mb-icon-total-icon dashicons dashicons-share"></i></div> | |
| 807 | - <div class="mb-totals"> | |
| 808 | - <div class="mb-count-total">' . $total . '</div> | |
| 809 | - <div class="mb-label-total">' . $total_label .'</div> | |
| 810 | - </div> | |
| 811 | - </div> | |
| 812 | - </div>'; | |
| 813 | - | |
| 814 | - $collectionObj->find('.maxcollection',0)->innertext = $total_html; | |
| 815 | - } | |
| 816 | - | |
| 817 | - public function createButton($args) | |
| 951 | + protected function parseCSS($args) | |
| 818 | 952 | { |
| 953 | + maxUtils::startTime('collection-parseCSS-'. self::$count); | |
| 819 | 954 | $defaults = array( |
| 820 | - 'link' => 'javascript:void(0)', | |
| 821 | - 'classes' => array(), | |
| 822 | - 'item_classes' => array(), | |
| 823 | - 'name' => '', | |
| 824 | - // 'show_share_count' => false, | |
| 825 | - // 'share_count' => 0, | |
| 826 | - ); | |
| 827 | - | |
| 955 | + 'preview' => false, | |
| 956 | + 'skip_blocks' => false | |
| 957 | + ); | |
| 828 | 958 | $args = wp_parse_args($args, $defaults); |
| 829 | 959 | |
| 830 | - $item_classes = array_merge ($args['item_classes'], array($args['name']) ); | |
| 831 | 960 | |
| 832 | - $icon_output = $this->styleObj->renderIcon($args['network']); | |
| 833 | - | |
| 834 | - | |
| 835 | - $html = "<span class='collection-item " . implode(' ', $item_classes) . "'><a href='" . $args['link'] . "' class='maxbutton-social '> | |
| 836 | - <span class='mb-icon-wrapper'> | |
| 837 | - " . $icon_output . " </span> "; | |
| 838 | - | |
| 839 | - //if ($args['show_share_count']) | |
| 840 | - // $html .= "<span class='share-count'>" . $args['share_count'] . "</span>"; | |
| 841 | - | |
| 842 | - $html .= "</a></span>"; | |
| 843 | - | |
| 844 | - | |
| 845 | - $button = str_get_html($html); | |
| 846 | - return $button; | |
| 847 | - } | |
| 848 | - | |
| 849 | - function parseCSS($args) | |
| 850 | - { | |
| 851 | 961 | $css = array(); |
| 852 | 962 | if (! is_null($this->styleObj)) |
| 853 | 963 | { |
| 854 | 964 | $prep_args = array( |
| @@ -871,15 +981,17 @@ | ||
| 871 | 981 | } |
| 872 | 982 | |
| 873 | 983 | // before the parse let have style have one more go |
| 874 | 984 | $css = $this->styleObj->preParse($css); |
| 985 | + $css = apply_filters('mbsocial/parsecss/', $css); // anybody else | |
| 875 | 986 | |
| 876 | 987 | $css = $this->getCSSParser()->parse($css); |
| 877 | 988 | |
| 989 | + maxUtils::endTime('collection-parseCSS-'. self::$count); | |
| 878 | 990 | return $css; |
| 879 | 991 | } |
| 880 | 992 | |
| 881 | - function parseJS($args) | |
| 993 | + protected function parseJS($args) | |
| 882 | 994 | { |
| 883 | 995 | $js = array(); |
| 884 | 996 | |
| 885 | 997 | $defaults = array("preview" => false, |
| @@ -899,35 +1011,10 @@ | ||
| 899 | 1011 | return $js; |
| 900 | 1012 | } |
| 901 | 1013 | |
| 902 | 1014 | |
| 903 | - public function shortcode($atts, $content = null) | |
| 904 | - { | |
| 905 | 1015 | |
| 906 | - // ugly -need to rework logic here. | |
| 907 | - //$collection = maxCollections::getCollection('social'); | |
| 908 | - $display_args = shortcode_atts(array( | |
| 909 | - "echo" => false, | |
| 910 | - "mode" => "normal", | |
| 911 | - "nocache" => false, | |
| 912 | - "style" => 'footer', | |
| 913 | - ), | |
| 914 | 1016 | |
| 915 | - $atts); | |
| 916 | - | |
| 917 | - $collection_id = $this->collection_id; | |
| 918 | - | |
| 919 | - //$this->set($collection_id); | |
| 920 | - $display_args["compile"] = $display_args["nocache"]; | |
| 921 | - $display_args['load_type'] = $display_args['style']; | |
| 922 | - unset($display_args['style']); | |
| 923 | - unset($display_args["nocache"]); | |
| 924 | - | |
| 925 | - $output = $this->display($display_args); | |
| 926 | - | |
| 927 | - return $output; | |
| 928 | - } | |
| 929 | - | |
| 930 | 1017 | /** Metadata functions **/ |
| 931 | 1018 | |
| 932 | 1019 | public function get_metadata($post_id) |
| 933 | 1020 | { |
| @@ -952,9 +1039,9 @@ | ||
| 952 | 1039 | return $content_blocks; |
| 953 | 1040 | } |
| 954 | 1041 | |
| 955 | 1042 | /** @param $post_id Saving post_id |
| 956 | - @param $post Post Array of submitted values | |
| 1043 | + * @param $post Post Array of submitted values | |
| 957 | 1044 | **/ |
| 958 | 1045 | public function save_meta_boxes($post_id, $post) |
| 959 | 1046 | { |
| 960 | 1047 | $post_meta = array(); |
| @@ -968,118 +1055,10 @@ | ||
| 968 | 1055 | if (count($post_meta) > 0) |
| 969 | 1056 | update_post_meta($post_id, 'mbsocial_data', $post_meta); |
| 970 | 1057 | } |
| 971 | 1058 | |
| 972 | - | |
| 973 | - // Get the pack definition that are present in the system. | |
| 974 | - /*function editor_getPacks() | |
| 975 | - { | |
| 976 | - //$pack_paths = apply_filters('mb-col-pack-paths', array(MB()->get_plugin_path() . "/") ); | |
| 977 | - $packs["maxbuttons"]["func"] = array($this, 'editor_getButtons'); | |
| 978 | - $packs["maxbuttons"]["tab"] = __("Your MaxButtons","maxbuttons"); | |
| 979 | - return $packs; | |
| 980 | - | |
| 981 | - } */ | |
| 982 | - | |
| 983 | - /* public function ajax_getButtons($result, $data) | |
| 984 | - { | |
| 985 | - $packs = $this->editor_getPacks(); | |
| 986 | - $req_pack= $data["pack"]; | |
| 987 | - | |
| 988 | - foreach($packs as $index => $pack_data) | |
| 989 | - { | |
| 990 | - if ($req_pack == $index) | |
| 991 | - { | |
| 992 | - $buttons = call_user_func($pack_data["func"], $index, $data); | |
| 993 | - | |
| 994 | - } | |
| 995 | - } | |
| 996 | - $output = ''; | |
| 997 | - | |
| 998 | - | |
| 999 | - $page_args = array(); | |
| 1000 | - if (isset($data["paged"])) | |
| 1001 | - { | |
| 1002 | - $page_args["paged"] = $data["paged"]; | |
| 1003 | - } | |
| 1004 | - | |
| 1005 | - if ($req_pack == 'maxbuttons') | |
| 1006 | - { | |
| 1007 | - $page_args["limit"] = 18; | |
| 1008 | - ob_start(); | |
| 1009 | - do_action("mb-display-meta"); | |
| 1010 | - do_action("mb-display-pagination", $page_args); | |
| 1011 | - $pagination = "<div class='tablenav top'>" .ob_get_contents() . "</div>"; | |
| 1012 | - ob_end_clean(); | |
| 1013 | - | |
| 1014 | - $output .= $pagination; | |
| 1015 | - } | |
| 1016 | - | |
| 1017 | - foreach($buttons as $button) | |
| 1018 | - { | |
| 1019 | - $button_data = $button->get(); | |
| 1020 | - | |
| 1021 | - $button_id = $button->getID(); | |
| 1022 | - $name = $button->getName(); | |
| 1023 | - | |
| 1024 | - $meta = isset($button_data["meta"]) ? $button_data["meta"] : array(); | |
| 1025 | - do_action('mb-data-load',$button_data); // weakness of the bubbling filter model | |
| 1026 | - | |
| 1027 | - $output .= "<div class='item shortcode-container' data-id='$button_id'> "; | |
| 1028 | - $output .= $button->display(array("mode" => "preview", 'echo' => false, 'load_css' => 'inline') ); //"mode" => "preview", "compile" => true, | |
| 1029 | - $output .= "<span class='button_data'>" . base64_encode(json_encode($button_data)) . "</span>"; | |
| 1030 | - $output .= "<span class='button_name'>" . $name . "</span>"; | |
| 1031 | - $output .= "</div>"; | |
| 1032 | - | |
| 1033 | - } | |
| 1034 | - | |
| 1035 | - if ($req_pack == 'maxbuttons') | |
| 1036 | - { | |
| 1037 | - $output .= $pagination; | |
| 1038 | - } | |
| 1039 | - | |
| 1040 | - $result["body"] = $output; | |
| 1041 | - return $result; | |
| 1042 | - | |
| 1043 | - } | |
| 1044 | - */ | |
| 1045 | - | |
| 1046 | - // show the available buttons | |
| 1047 | - /* function editor_getButtons($pack, $data) | |
| 1048 | - { | |
| 1049 | - $button_array = array(); | |
| 1050 | - | |
| 1051 | - $admin = MB()->getClass("admin"); | |
| 1052 | - $button = MB()->getClass("button"); | |
| 1053 | - | |
| 1054 | - $paged = (isset($data["paged"])) ? $data["paged"] : 1; | |
| 1055 | - | |
| 1056 | - $buttons = $admin->getButtons(array( | |
| 1057 | - "orderby" => "id", | |
| 1058 | - "order" => "DESC", | |
| 1059 | - "paged" => $paged, | |
| 1060 | - "limit" => 18, | |
| 1061 | - )); | |
| 1062 | - | |
| 1063 | - | |
| 1064 | - foreach($buttons as $btn) | |
| 1065 | - { | |
| 1066 | - $id = $btn["id"]; | |
| 1067 | - $b = MB()->getClass('button'); | |
| 1068 | - $b->clear(); | |
| 1069 | - $b->set($id); | |
| 1070 | - $button_data = $b->get(); | |
| 1071 | - // exclude auto-generated non-user edited buttons. | |
| 1072 | - if ( ! isset($button_data["meta"]["user_edited"]) || ($button_data["meta"]["user_edited"] == true || $button_data["meta"]["created_source"] != "collection")) | |
| 1073 | - $button_array[] = $b; // the object | |
| 1074 | - } | |
| 1075 | - return $button_array; | |
| 1076 | - } */ | |
| 1077 | - | |
| 1078 | - | |
| 1079 | 1059 | function showBlocks() |
| 1080 | 1060 | { |
| 1081 | - maxBlocks::setData($this->data); | |
| 1082 | 1061 | foreach($this->blocks as $block) |
| 1083 | 1062 | { |
| 1084 | 1063 | echo $block->admin(); |
| 1085 | 1064 | } |
| @@ -1087,14 +1066,13 @@ | ||
| 1087 | 1066 | $active = isset($this->data['network']['network_active']) ? $this->data['network']['network_active'] : array(); |
| 1088 | 1067 | |
| 1089 | 1068 | if ( is_array($active) && count($active) > 0) |
| 1090 | 1069 | { |
| 1091 | - | |
| 1092 | - | |
| 1093 | 1070 | foreach($active as $network_name) |
| 1094 | 1071 | { |
| 1095 | 1072 | $network = MbSocial()->networks()->get($network_name); |
| 1096 | - echo $network->admin($this->data); | |
| 1073 | + if ( is_object($network)) | |
| 1074 | + echo $network->admin($this->data); | |
| 1097 | 1075 | } |
| 1098 | 1076 | } |
| 1099 | 1077 | } |
| 1100 | 1078 | } //class |