| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('No direct access permitted'); |
| 3 |
|
| 4 |
class maxCollections |
| 5 |
{ |
| 6 |
static $init = false; |
| 7 |
//static $collections = array(); |
| 8 |
static $collectionClass = array(); |
| 9 |
static $collectionBlock = array(); |
| 10 |
|
| 11 |
static $hooks = array(); |
| 12 |
|
| 13 |
static $cached_collections = array(); |
| 14 |
protected static $transientChecked = false; |
| 15 |
|
| 16 |
static $collectionButtons = null; // all button ID's in a collection. |
| 17 |
|
| 18 |
static function init() |
| 19 |
{ |
| 20 |
$collection_paths = apply_filters("mbcollection_paths", array( MB()->get_plugin_path() . '/collections/') ); |
| 21 |
$collectionClass = array(); |
| 22 |
$collectionBlock = array(); |
| 23 |
|
| 24 |
foreach($collection_paths as $cpath) |
| 25 |
{ |
| 26 |
$dir_iterator = new RecursiveDirectoryIterator($cpath, FilesystemIterator::SKIP_DOTS); |
| 27 |
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); |
| 28 |
|
| 29 |
foreach ($iterator as $fileinfo) |
| 30 |
{ |
| 31 |
$collection = $fileinfo->getFilename(); |
| 32 |
|
| 33 |
|
| 34 |
if (file_exists($cpath . $collection)) |
| 35 |
{ |
| 36 |
require_once( $cpath . $collection); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
self::$collectionClass = $collectionClass; |
| 41 |
self::$collectionBlock = $collectionBlock; |
| 42 |
|
| 43 |
self::$init = true; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
Check our custom transients for expiration. This should be done only once per run or less. |
| 49 |
*/ |
| 50 |
static function checkExpireTrans() |
| 51 |
{ |
| 52 |
if (! self::$transientChecked) |
| 53 |
{ |
| 54 |
maxUtils::removeExpiredTrans(); // instead of on each button to reduce load. |
| 55 |
self::$transientChecked = true; |
| 56 |
} |
| 57 |
} |
| 58 |
/* |
| 59 |
Function to hook into WordPress for automatic display of collections. |
| 60 |
*/ |
| 61 |
static function setupHooks() |
| 62 |
{ |
| 63 |
// check for admin side, we need not hooks nor queries for this. |
| 64 |
if (is_admin()) |
| 65 |
return; |
| 66 |
|
| 67 |
global $pagenow; |
| 68 |
if ( in_array($pagenow, array('wp-login.php', 'wp-register.php')) ) |
| 69 |
return; |
| 70 |
|
| 71 |
global $wpdb; |
| 72 |
$table = maxUtils::get_collection_table_name(); |
| 73 |
$sql = "select collection_id, collection_key, collection_value from $table where |
| 74 |
collection_key in ('show_homepage','show_page','show_post', 'show_archive') "; |
| 75 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 76 |
|
| 77 |
$hook_array = array(); |
| 78 |
|
| 79 |
foreach($results as $result) |
| 80 |
{ |
| 81 |
$id = $result["collection_id"]; |
| 82 |
$key = $result["collection_key"]; |
| 83 |
$placement = $result["collection_value"]; |
| 84 |
|
| 85 |
// override - if needed you can specify a different placement for each position |
| 86 |
$placement = apply_filters("mb-collection-$id-$key-placement", $placement); |
| 87 |
|
| 88 |
switch($placement) |
| 89 |
{ |
| 90 |
case "after": |
| 91 |
case "before": |
| 92 |
case "after-before": |
| 93 |
$show_type = "post"; // show on the post loop |
| 94 |
break; |
| 95 |
case "static-left": |
| 96 |
case "static-right": |
| 97 |
case "static-top": |
| 98 |
case "static-bottom": |
| 99 |
$show_type = "once"; // show once - otherwise for overviews they will be many static buttons. |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
$hook_array[$show_type][$key][] = array("collection_id" => $id, "placement" => $placement); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
self::$hooks = $hook_array; |
| 108 |
|
| 109 |
if (isset($hook_array["post"]) && count($hook_array["post"]) > 0) |
| 110 |
{ |
| 111 |
add_filter("the_content", array('maxCollections', "doContentHooks")); |
| 112 |
} |
| 113 |
if (isset($hook_array["once"]) && count($hook_array["once"]) > 0) |
| 114 |
{ |
| 115 |
//self::doFooterHooks(); // the stuff that goes once. |
| 116 |
add_action('wp_head', array('maxCollections', 'doFooterHooks')); |
| 117 |
} |
| 118 |
|
| 119 |
if (count($hook_array) > 0) |
| 120 |
return true; // yes, bind action and check |
| 121 |
else |
| 122 |
return false; // no binds, don't check. |
| 123 |
} |
| 124 |
|
| 125 |
/* Try to find the current place we are at in the site ( front / blog / page etc ) .*/ |
| 126 |
protected static function getCurrentHook() |
| 127 |
{ |
| 128 |
$hook = ''; |
| 129 |
if (is_front_page()) // check if home page ( home page > page ) |
| 130 |
{ |
| 131 |
$hook = "show_homepage"; |
| 132 |
|
| 133 |
} |
| 134 |
elseif (is_page()) // check if page |
| 135 |
{ |
| 136 |
$hook = "show_page"; |
| 137 |
} |
| 138 |
elseif (is_single() || is_home()) // check if post |
| 139 |
{ |
| 140 |
$hook = "show_post"; |
| 141 |
} |
| 142 |
elseif (is_archive()) |
| 143 |
{ |
| 144 |
$hook = "show_archive"; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
return $hook; |
| 149 |
} |
| 150 |
|
| 151 |
static function doContentHooks($content) |
| 152 |
{ |
| 153 |
$hook_array = self::$hooks["post"]; |
| 154 |
$hook = self::getCurrentHook(); |
| 155 |
|
| 156 |
if ($hook == '') |
| 157 |
return $content; // nothing |
| 158 |
|
| 159 |
if (! isset($hook_array[$hook])) // nothing as well |
| 160 |
return $content; |
| 161 |
|
| 162 |
$collections = $hook_array[$hook]; |
| 163 |
|
| 164 |
// do all collections on hook -- check for placement as well. |
| 165 |
foreach($collections as $settings) |
| 166 |
{ |
| 167 |
$collection_id = $settings["collection_id"]; |
| 168 |
$placement = $settings["placement"]; |
| 169 |
|
| 170 |
$col = self::getCollectionByID($collection_id); |
| 171 |
$col->set($collection_id); |
| 172 |
$output = $col->display(array("echo" => false)); // output default, no echo |
| 173 |
|
| 174 |
|
| 175 |
switch($placement) // where to output, rather limited atm. |
| 176 |
{ |
| 177 |
case "before": |
| 178 |
$place = "before"; |
| 179 |
break; |
| 180 |
case "after-before"; |
| 181 |
$place = "both"; |
| 182 |
break; |
| 183 |
default: |
| 184 |
$place = "after"; |
| 185 |
break; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if($place == 'before' || $place == 'both') |
| 192 |
{ |
| 193 |
$content = $output . $content; |
| 194 |
} |
| 195 |
if($place == 'after' || $place == 'both') |
| 196 |
{ |
| 197 |
|
| 198 |
$content = $content . $output; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $content; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
static function doFooterHooks() |
| 207 |
{ |
| 208 |
$hook_array = self::$hooks["once"]; |
| 209 |
$hook = self::getCurrentHook(); |
| 210 |
|
| 211 |
if (! isset($hook_array[$hook])) // nothing |
| 212 |
return; |
| 213 |
|
| 214 |
$collections = $hook_array[$hook]; |
| 215 |
foreach($collections as $settings) |
| 216 |
{ |
| 217 |
$collection_id = $settings["collection_id"]; |
| 218 |
$placement = $settings["placement"]; |
| 219 |
|
| 220 |
$col = self::getCollectionByID($collection_id); |
| 221 |
$col->set($collection_id); |
| 222 |
|
| 223 |
$output = $col->display(array("echo" => false)); // output default, no echo |
| 224 |
|
| 225 |
do_action('mb-footer', 'collection-' . $collection_id, $output, 'collection_output'); |
| 226 |
|
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
static function checkCachedCollection($collection_id) |
| 231 |
{ |
| 232 |
/* The cache is pointless since for every collection the shared URL's and fields can change. This means that the same collection |
| 233 |
can server up multiple situations. Deactivated this when discovered in /blogs/ all have the same share URL on different posts. |
| 234 |
*/ |
| 235 |
return false; |
| 236 |
|
| 237 |
// if (isset(self::$cached_collections[$collection_id])) |
| 238 |
// return self::$cached_collections[$collection_id]; |
| 239 |
|
| 240 |
// return false; |
| 241 |
} |
| 242 |
|
| 243 |
static function addCachedCollection($collection_id, $data) |
| 244 |
{ |
| 245 |
self::$cached_collections[$collection_id] = $data; |
| 246 |
} |
| 247 |
|
| 248 |
public static function isButtonInCollection($button_id) |
| 249 |
{ |
| 250 |
if (is_null(self::$collectionButtons)) |
| 251 |
{ |
| 252 |
global $wpdb; |
| 253 |
$table = maxUtils::get_collection_table_name(); |
| 254 |
$sql = 'SELECT collection_id, collection_value FROM ' . $table . ' WHERE |
| 255 |
collection_key = "picker";'; |
| 256 |
$result = $wpdb->get_results($sql, ARRAY_A); |
| 257 |
|
| 258 |
$buttonarray = array(); |
| 259 |
foreach ($result as $index => $row) |
| 260 |
{ |
| 261 |
$collection_id = $row['collection_id']; |
| 262 |
$buttons = json_decode($row['collection_value']); |
| 263 |
|
| 264 |
if (isset($buttons->selection)) |
| 265 |
$buttons = $buttons->selection; |
| 266 |
else |
| 267 |
$buttons = array(); |
| 268 |
|
| 269 |
foreach($buttons as $index => $b_id) |
| 270 |
{ |
| 271 |
if (isset(static::$collectionButtons[$b_id])) |
| 272 |
self::$collectionButtons[$b_id][] = $collection_id; |
| 273 |
else |
| 274 |
self::$collectionButtons[$b_id] = array($collection_id); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
if (isset(self::$collectionButtons[$button_id])) |
| 282 |
return self::$collectionButtons[$button_id]; |
| 283 |
else |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
static function ajax_save() |
| 288 |
{ |
| 289 |
$nonce = sanitize_text_field($_POST["nonce"]); |
| 290 |
$action = sanitize_text_field($_POST["action"]); |
| 291 |
|
| 292 |
|
| 293 |
$collection_id = intval($_POST["collection_id"]); |
| 294 |
$collection_type = sanitize_text_field($_POST["collection_type"]); |
| 295 |
|
| 296 |
$admin = MB()->getClass('admin'); |
| 297 |
|
| 298 |
$result_title = array("success" => __("Your collection was saved","maxbuttons"), |
| 299 |
"error" => __("Error","maxbuttons") |
| 300 |
); |
| 301 |
|
| 302 |
$close_text = __('Close', 'maxbuttons'); |
| 303 |
|
| 304 |
$result = array( |
| 305 |
"error" => false, |
| 306 |
"body" => '', |
| 307 |
"result" => true, |
| 308 |
"data" => array(), |
| 309 |
"close_text" => $close_text, |
| 310 |
"new_nonce" => 0, |
| 311 |
); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if (! wp_verify_nonce($nonce, $action . "-" . $collection_id)) |
| 316 |
{ |
| 317 |
$result["error"] = true; |
| 318 |
$result["body"] = __("Nonce not verified","maxbuttons"); |
| 319 |
$result["result"] = false; |
| 320 |
$result["title"] = $result_title["error"]; |
| 321 |
$result["data"] = array("id" => $collection_id); |
| 322 |
|
| 323 |
$admin->endAjaxRequest($result); |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
if (! isset($collection_type)) |
| 328 |
{ |
| 329 |
MB()->add_notice("error", __("Collection type not found in save. Aborting","maxbuttons")); |
| 330 |
$result["error"] = true; |
| 331 |
$result["body"] = MB()->display_notices(false); |
| 332 |
$result["result"] = false; |
| 333 |
$result["title"] = $result_title["error"]; |
| 334 |
|
| 335 |
$admin->endAjaxRequest($result); |
| 336 |
} |
| 337 |
|
| 338 |
$collection = self::getCollection($collection_type); |
| 339 |
$collection->set($collection_id); |
| 340 |
|
| 341 |
$force_reload = false; |
| 342 |
if ($collection_id == 0) |
| 343 |
$force_reload = true; |
| 344 |
|
| 345 |
// this can be a new id (!) |
| 346 |
$collection_id = $collection->save($_POST); |
| 347 |
|
| 348 |
$result["data"]["id"] = $collection_id; |
| 349 |
$result["data"]["new_nonce"] = wp_create_nonce($action . "-" . $collection_id); |
| 350 |
$result["data"]["reload"] = apply_filters("collections_ajax_force_reload",$force_reload); |
| 351 |
|
| 352 |
$result["title"] = $result_title["success"]; |
| 353 |
|
| 354 |
$admin->endAjaxRequest($result); |
| 355 |
|
| 356 |
//echo json_encode($result); |
| 357 |
//exit(); |
| 358 |
} |
| 359 |
|
| 360 |
static function ajax_action_front() |
| 361 |
{ |
| 362 |
// only for trivial front page actions! |
| 363 |
self::ajax_action(array("ajax_nopriv" => true)); |
| 364 |
} |
| 365 |
|
| 366 |
static function ajax_action($args = array()) |
| 367 |
{ |
| 368 |
ob_start(); |
| 369 |
$defaults = array("ajax_nopriv" => false); |
| 370 |
$args = wp_parse_args($args, $defaults); |
| 371 |
|
| 372 |
|
| 373 |
$admin = MB()->getClass('admin'); |
| 374 |
|
| 375 |
$nonce = isset($_POST["nonce"]) ? $_POST["nonce"] : false; |
| 376 |
$block_name = sanitize_text_field($_POST["block_name"]); |
| 377 |
$block_action = sanitize_text_field($_POST["block_action"]); |
| 378 |
$block_data = (isset($_POST["block_data"])) ? $_POST["block_data"] : ''; |
| 379 |
$action = sanitize_text_field($_POST["action"]); |
| 380 |
|
| 381 |
$collection_id = intval($_POST["collection_id"]); |
| 382 |
$collection_type = sanitize_text_field($_POST["collection_type"]); |
| 383 |
|
| 384 |
if(! $args["ajax_nopriv"]) |
| 385 |
{ |
| 386 |
if (! wp_verify_nonce($nonce, $action . "-" . $collection_id)) |
| 387 |
{ |
| 388 |
$result["error"] = true; |
| 389 |
$result["body"] = __("Nonce not verified","maxbuttons"); |
| 390 |
$result["result"] = false; |
| 391 |
$result["title"] = __("Security error","maxbuttons"); |
| 392 |
$result["data"] = array("id" => $collection_id); |
| 393 |
|
| 394 |
$admin->endAjaxRequest($result); |
| 395 |
|
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
$result = array( |
| 400 |
"error" => false, |
| 401 |
"body" => '', |
| 402 |
"result" => true, |
| 403 |
"data" => array(), |
| 404 |
"new_nonce" => 0, |
| 405 |
); |
| 406 |
|
| 407 |
$collection = self::getCollection($collection_type); |
| 408 |
$collection->set($collection_id); |
| 409 |
|
| 410 |
$result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data); |
| 411 |
|
| 412 |
//ob_end_clean(); // prevent PHP errors from breaking JSON response. |
| 413 |
|
| 414 |
$admin->endAjaxRequest($result); |
| 415 |
$results = $collection->get_meta($name, 'collection_name'); |
| 416 |
} |
| 417 |
|
| 418 |
static function getCollections() |
| 419 |
{ |
| 420 |
if (! self::$init) self::init(); |
| 421 |
|
| 422 |
global $wpdb; |
| 423 |
|
| 424 |
$table = maxUtils::get_collection_table_name(); |
| 425 |
|
| 426 |
$sql = "SELECT distinct collection_id from $table"; |
| 427 |
|
| 428 |
$results = $wpdb->get_results($sql,ARRAY_A); |
| 429 |
return $results; |
| 430 |
} |
| 431 |
|
| 432 |
/* This will invoke a collection class by name ( i.e. social-collection or basic-collection */ |
| 433 |
static function getCollection($name) |
| 434 |
{ |
| 435 |
if (! self::$init) self::init(); |
| 436 |
|
| 437 |
if (isset(self::$collectionClass[$name])) |
| 438 |
return new self::$collectionClass[$name]; |
| 439 |
|
| 440 |
} |
| 441 |
|
| 442 |
/* This will find an user defined collection from the database by ID */ |
| 443 |
static function getCollectionByID($id) |
| 444 |
{ |
| 445 |
$collection = new maxCollection(); |
| 446 |
$results = $collection->get_meta($id, 'collection_type'); |
| 447 |
|
| 448 |
|
| 449 |
if ( count($results) == 1) |
| 450 |
{ |
| 451 |
|
| 452 |
$type = $results["collection_type"]; |
| 453 |
|
| 454 |
$usecol = self::getCollection($type); |
| 455 |
$usecol->set($id); |
| 456 |
return $usecol; |
| 457 |
} |
| 458 |
|
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
/* Find a collection from the database by name */ |
| 463 |
static function getCollectionbyName($name) |
| 464 |
{ |
| 465 |
//$collection = new maxCollection(); |
| 466 |
global $wpdb; |
| 467 |
$sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s "; |
| 468 |
$sql = $wpdb->prepare($sql, $name); |
| 469 |
$result = $wpdb->get_row($sql, ARRAY_A); // find first |
| 470 |
|
| 471 |
|
| 472 |
if (count($result) > 0) |
| 473 |
{ |
| 474 |
if (isset($result["collection_id"])) |
| 475 |
{ |
| 476 |
|
| 477 |
$usecol = self::getCollectionByID($result["collection_id"]); |
| 478 |
return $usecol; |
| 479 |
} |
| 480 |
|
| 481 |
} |
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
static function getBlock($name) |
| 486 |
{ |
| 487 |
if (! self::$init) self::init(); |
| 488 |
|
| 489 |
if (isset(self::$collectionBlock[$name])) |
| 490 |
{ |
| 491 |
return new self::$collectionBlock[$name]; |
| 492 |
} |
| 493 |
else return false; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
} |
| 498 |
|