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