| @@ -12,41 +12,41 @@ | ||
| 12 | 12 | require_once CJTOOLBOX_TABLES_PATH . '/blocks.php'; |
| 13 | 13 | require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php'; |
| 14 | 14 | // MYSQL Queue Driver. |
| 15 | 15 | require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/queue-driver.inc.php'; |
| 16 | - | |
| 16 | + | |
| 17 | 17 | /** |
| 18 | 18 | * Provide simple access (read or write) to all Blocks data. |
| 19 | -* | |
| 19 | +* | |
| 20 | 20 | * @author Ahmed Said |
| 21 | 21 | * @version 6 |
| 22 | 22 | */ |
| 23 | 23 | class CJTBlocksModel { |
| 24 | - | |
| 24 | + | |
| 25 | 25 | /** |
| 26 | - * | |
| 26 | + * | |
| 27 | 27 | */ |
| 28 | - const MAX_REVISIONS_PER_BLOCK = 10; | |
| 29 | - | |
| 28 | + const MAX_REVISIONS_PER_BLOCK = 99; | |
| 29 | + | |
| 30 | 30 | /** |
| 31 | 31 | * put your comment there... |
| 32 | - * | |
| 32 | + * | |
| 33 | 33 | * @var mixed |
| 34 | 34 | */ |
| 35 | 35 | protected $dbDriver = null; |
| 36 | - | |
| 36 | + | |
| 37 | 37 | /** |
| 38 | 38 | * put your comment there... |
| 39 | - * | |
| 39 | + * | |
| 40 | 40 | */ |
| 41 | 41 | public function __construct() { |
| 42 | 42 | // Initialize CTTTable MYSQL Driver. |
| 43 | 43 | $this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']); |
| 44 | 44 | } |
| 45 | - | |
| 45 | + | |
| 46 | 46 | /** |
| 47 | 47 | * put your comment there... |
| 48 | - * | |
| 48 | + * | |
| 49 | 49 | * @param mixed $block |
| 50 | 50 | * @param mixed $forceAddCodeBlock |
| 51 | 51 | */ |
| 52 | 52 | public function add($block, $forceAddCodeBlock = false) { |
| @@ -56,9 +56,9 @@ | ||
| 56 | 56 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 57 | 57 | $codeFile = new CJTBlockFilesTable($this->dbDriver); |
| 58 | 58 | // Get new id if not specified. |
| 59 | 59 | if (!isset($block['id']) || !$block['id']) { |
| 60 | - $block['id'] = $blocks->getNextId(); | |
| 60 | + $block['id'] = $blocks->getNextIdNEW(); | |
| 61 | 61 | } |
| 62 | 62 | // Backward compatibility for old block style |
| 63 | 63 | // that has code field inside blocks table. |
| 64 | 64 | /// if passed create master code file for the code field. |
| @@ -76,12 +76,12 @@ | ||
| 76 | 76 | $blocks->insert($block); |
| 77 | 77 | // Return Newly added block id. |
| 78 | 78 | return $block['id']; |
| 79 | 79 | } |
| 80 | - | |
| 80 | + | |
| 81 | 81 | /** |
| 82 | 82 | * put your comment there... |
| 83 | - * | |
| 83 | + * | |
| 84 | 84 | * @param mixed $blockId |
| 85 | 85 | * @param mixed $activeFileId |
| 86 | 86 | */ |
| 87 | 87 | public function addRevision($blockId, $activeFileId) { |
| @@ -89,9 +89,9 @@ | ||
| 89 | 89 | $blocks = new CJTBlocksTable($this->dbDriver); |
| 90 | 90 | $pins = new CJTBlockPinsTable($this->dbDriver); |
| 91 | 91 | $codeFile = new CJTBlockFilesTable($this->dbDriver); |
| 92 | 92 | // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per |
| 93 | - // block code files So that a single block may has up to | |
| 93 | + // block code files So that a single block may has up to | |
| 94 | 94 | // self::MAX_REVISIONS_PER_BLOCK * count(codeFiles) |
| 95 | 95 | $revisions['fields'] = array('id'); |
| 96 | 96 | $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId); |
| 97 | 97 | $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']); |
| @@ -98,9 +98,9 @@ | ||
| 98 | 98 | // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one. |
| 99 | 99 | if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) { |
| 100 | 100 | $this->delete(array_shift($revisions)->id); |
| 101 | 101 | } |
| 102 | - // Get block data. | |
| 102 | + // Get block data. | |
| 103 | 103 | $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions'); |
| 104 | 104 | // get() developed to return multiple blocks, fetch the first. |
| 105 | 105 | $result = $blocks->get($blockId, $block['fields']); |
| 106 | 106 | $block = reset($result); |
| @@ -109,11 +109,12 @@ | ||
| 109 | 109 | $block->parent = $blockId; |
| 110 | 110 | $block->type = 'revision'; |
| 111 | 111 | $block->created = current_time('mysql'); |
| 112 | 112 | $block->owner = get_current_user_id(); |
| 113 | - $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be | |
| 113 | + $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be | |
| 114 | 114 | // used as the masterFile! |
| 115 | - $block->id = $blocks->getNextId(); // Get new id for revision rrecord. | |
| 115 | + //$block->id = $blocks->getNextId(); // Get new id for revision rrecord. | |
| 116 | + $block->id = $blocks->getNextIdNEW(); // Get new id for revision rrecord. | |
| 116 | 117 | // Add block data. |
| 117 | 118 | $blocks->insert($block); |
| 118 | 119 | // Get block pins and insert pins for the revision block. |
| 119 | 120 | $blockPins = $pins->get($blockId); |
| @@ -128,22 +129,22 @@ | ||
| 128 | 129 | ->load() |
| 129 | 130 | ->set('blockId', $block->id) |
| 130 | 131 | ->save(true, true); |
| 131 | 132 | } |
| 132 | - | |
| 133 | + | |
| 133 | 134 | /** |
| 134 | 135 | * Put your comments here... |
| 135 | 136 | * |
| 136 | 137 | * |
| 137 | - * @return | |
| 138 | - */ | |
| 138 | + * @return | |
| 139 | + */ | |
| 139 | 140 | public function dbDriver() { |
| 140 | 141 | return $this->dbDriver; |
| 141 | 142 | } |
| 142 | - | |
| 143 | + | |
| 143 | 144 | /** |
| 144 | 145 | * put your comment there... |
| 145 | - * | |
| 146 | + * | |
| 146 | 147 | * @param mixed $ids |
| 147 | 148 | */ |
| 148 | 149 | public function delete($ids) { |
| 149 | 150 | // Allow single or multiple Ids to be passed. |
| @@ -185,12 +186,59 @@ | ||
| 185 | 186 | $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids))); |
| 186 | 187 | // Chaining! |
| 187 | 188 | return $this; |
| 188 | 189 | } |
| 189 | - | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * put your comment there... | |
| 193 | + * | |
| 194 | + * @param mixed $block | |
| 195 | + */ | |
| 196 | + public static function getAllAssignments($block) { | |
| 197 | + | |
| 198 | + $inPages = $block->pages ? $block->pages : []; | |
| 199 | + $inPosts = $block->posts ? $block->posts : []; | |
| 200 | + $inCats = $block->categories ? $block->categories : []; | |
| 201 | + $inTags = $block->post_tags ? $block->post_tags : []; | |
| 202 | + $inLinks = ! empty( $block->links ) ? explode( "\n", $block->links ) : []; | |
| 203 | + $inExps = ! empty( $block->expressions ) ? explode( "\n", $block->expressions ) : []; | |
| 204 | + $inAux = array(); | |
| 205 | + | |
| 206 | + // Found selected Aux | |
| 207 | + $auxFlags = array( | |
| 208 | + CJTBlockModel::PINS_404_ERROR, | |
| 209 | + CJTBlockModel::PINS_ARCHIVE, | |
| 210 | + CJTBlockModel::PINS_ATTACHMENT, | |
| 211 | + CJTBlockModel::PINS_AUTHOR, | |
| 212 | + CJTBlockModel::PINS_BACKEND, | |
| 213 | + CJTBlockModel::PINS_CATEGORIES_ALL_CATEGORIES, | |
| 214 | + CJTBlockModel::PINS_EXPRESSIONS, | |
| 215 | + CJTBlockModel::PINS_FRONTEND, | |
| 216 | + CJTBlockModel::PINS_PAGES_ALL_PAGES, | |
| 217 | + CJTBlockModel::PINS_POSTS_ALL_POSTS, | |
| 218 | + CJTBlockModel::PINS_POSTS_BLOG_INDEX, | |
| 219 | + CJTBlockModel::PINS_POSTS_RECENT, | |
| 220 | + CJTBlockModel::PINS_SEARCH, | |
| 221 | + CJTBlockModel::PINS_TAG | |
| 222 | + ); | |
| 223 | + | |
| 224 | + foreach ($auxFlags as $auxFlag) { | |
| 225 | + | |
| 226 | + if ($auxFlag & $block->pinPoint) { | |
| 227 | + | |
| 228 | + $inAux[] = $auxFlag; | |
| 229 | + } | |
| 230 | + | |
| 231 | + } | |
| 232 | + | |
| 233 | + $allAssignment = array_merge( $inPages, $inPosts, $inCats, $inTags, $inLinks, $inExps, $inAux ); | |
| 234 | + | |
| 235 | + return $allAssignment; | |
| 236 | + } | |
| 237 | + | |
| 190 | 238 | /** |
| 191 | 239 | * put your comment there... |
| 192 | - * | |
| 240 | + * | |
| 193 | 241 | * @param mixed $id |
| 194 | 242 | * @param mixed $fields |
| 195 | 243 | */ |
| 196 | 244 | public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) { |
| @@ -197,12 +245,12 @@ | ||
| 197 | 245 | $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr); |
| 198 | 246 | $block = !empty($blocks) ? reset($blocks) : null; |
| 199 | 247 | return $block; |
| 200 | 248 | } |
| 201 | - | |
| 249 | + | |
| 202 | 250 | /** |
| 203 | 251 | * put your comment there... |
| 204 | - * | |
| 252 | + * | |
| 205 | 253 | * @param mixed $ids |
| 206 | 254 | */ |
| 207 | 255 | public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) { |
| 208 | 256 | // initialize. |
| @@ -246,12 +294,12 @@ | ||
| 246 | 294 | } |
| 247 | 295 | } |
| 248 | 296 | return $blocks; |
| 249 | 297 | } |
| 250 | - | |
| 298 | + | |
| 251 | 299 | /** |
| 252 | 300 | * put your comment there... |
| 253 | - * | |
| 301 | + * | |
| 254 | 302 | * @param mixed $blockId |
| 255 | 303 | * @param mixed $authorId |
| 256 | 304 | */ |
| 257 | 305 | public function getAuthorBlockActiveFileId($blockId, $authorId = null) { |
| @@ -260,11 +308,31 @@ | ||
| 260 | 308 | // Query block active file. |
| 261 | 309 | return $activeFileId; |
| 262 | 310 | } |
| 263 | 311 | |
| 312 | + /** | |
| 313 | + * put your comment there... | |
| 314 | + * | |
| 315 | + * @param mixed $blockId | |
| 316 | + */ | |
| 317 | + public static function getCodeFilesCount($blockId) { | |
| 318 | + | |
| 319 | + global $wpdb; | |
| 320 | + | |
| 321 | + $query = " SELECT count(*) | |
| 322 | + FROM {$wpdb->prefix}cjtoolbox_block_files f | |
| 323 | + WHERE f.blockId = %d;"; | |
| 324 | + | |
| 325 | + $query = $wpdb->prepare($query, $blockId); | |
| 326 | + | |
| 327 | + $codeFilesCount = $wpdb->get_var($query); | |
| 328 | + | |
| 329 | + return $codeFilesCount; | |
| 330 | + } | |
| 331 | + | |
| 264 | 332 | /** |
| 265 | 333 | * put your comment there... |
| 266 | - * | |
| 334 | + * | |
| 267 | 335 | * @param mixed $blockId |
| 268 | 336 | * @param mixed $authorId |
| 269 | 337 | */ |
| 270 | 338 | public function getCurrentAuthorBlockActiveFileId($blockId) { |
| @@ -272,23 +340,23 @@ | ||
| 272 | 340 | } |
| 273 | 341 | |
| 274 | 342 | /** |
| 275 | 343 | * put your comment there... |
| 276 | - * | |
| 344 | + * | |
| 277 | 345 | */ |
| 278 | 346 | public static function getCustomPostTypes() |
| 279 | 347 | { |
| 280 | - | |
| 348 | + | |
| 281 | 349 | static $postTypes = null; |
| 282 | - | |
| 350 | + | |
| 283 | 351 | if ( $postTypes !== null ) |
| 284 | 352 | { |
| 285 | 353 | return $postTypes; |
| 286 | 354 | } |
| 287 | - | |
| 288 | - | |
| 355 | + | |
| 356 | + | |
| 289 | 357 | $postTypes = array(); |
| 290 | - | |
| 358 | + | |
| 291 | 359 | // Create tabs for every custom post under the custom posts tab. |
| 292 | 360 | // Get all registered custom posts. |
| 293 | 361 | $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' ); |
| 294 | 362 | |
| @@ -293,15 +361,15 @@ | ||
| 293 | 361 | $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' ); |
| 294 | 362 | |
| 295 | 363 | // Add tab for every custom post |
| 296 | 364 | // Exclude 'Empty' Custom Post Types. |
| 297 | - foreach ( $customPosts as $typeName => $customPost ) | |
| 365 | + foreach ( $customPosts as $typeName => $customPost ) | |
| 298 | 366 | { |
| 299 | - // Check if has posts. | |
| 367 | + // Check if has posts. | |
| 300 | 368 | $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) ); |
| 301 | - | |
| 369 | + | |
| 302 | 370 | // Add only types with at least one post exists. |
| 303 | - if ( $hasPosts ) | |
| 371 | + if ( $hasPosts ) | |
| 304 | 372 | { |
| 305 | 373 | $postTypes[ $typeName ] = array |
| 306 | 374 | ( |
| 307 | 375 | 'title' => $customPost->labels->name, |
| @@ -306,28 +374,28 @@ | ||
| 306 | 374 | ( |
| 307 | 375 | 'title' => $customPost->labels->name, |
| 308 | 376 | 'renderer' => 'objects-list', |
| 309 | 377 | 'type' => array |
| 310 | - ( | |
| 378 | + ( | |
| 311 | 379 | 'type' => $typeName, |
| 312 | 380 | 'group' => 'posts', |
| 313 | 381 | 'targetType' => 'post' |
| 314 | 382 | ) |
| 315 | - | |
| 383 | + | |
| 316 | 384 | ); |
| 317 | - | |
| 385 | + | |
| 318 | 386 | } |
| 319 | - | |
| 387 | + | |
| 320 | 388 | } |
| 321 | 389 | |
| 322 | 390 | do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes ); |
| 323 | - | |
| 391 | + | |
| 324 | 392 | return $postTypes; |
| 325 | 393 | } |
| 326 | - | |
| 394 | + | |
| 327 | 395 | /** |
| 328 | 396 | * put your comment there... |
| 329 | - * | |
| 397 | + * | |
| 330 | 398 | * @param mixed $id |
| 331 | 399 | */ |
| 332 | 400 | public function getInfo($id) { |
| 333 | 401 | // Info fields. |
| @@ -341,28 +409,28 @@ | ||
| 341 | 409 | // Set shortcode name. |
| 342 | 410 | $info->shortcode = "[cjtoolbox name='{$info->name}']"; |
| 343 | 411 | return $info; |
| 344 | 412 | } |
| 345 | - | |
| 413 | + | |
| 346 | 414 | /** |
| 347 | 415 | * put your comment there... |
| 348 | - * | |
| 416 | + * | |
| 349 | 417 | */ |
| 350 | 418 | public function getOrder() { |
| 351 | 419 | return get_option('meta-box-order_cjtoolbox'); |
| 352 | 420 | } |
| 353 | - | |
| 421 | + | |
| 354 | 422 | /** |
| 355 | 423 | * put your comment there... |
| 356 | - * | |
| 424 | + * | |
| 357 | 425 | */ |
| 358 | 426 | public function save() { |
| 359 | 427 | $this->dbDriver->processQueue(); |
| 360 | 428 | } |
| 361 | - | |
| 429 | + | |
| 362 | 430 | /** |
| 363 | 431 | * put your comment there... |
| 364 | - * | |
| 432 | + * | |
| 365 | 433 | * @param mixed $order |
| 366 | 434 | */ |
| 367 | 435 | public function setOrder($order) { |
| 368 | 436 | $orderOptionName = 'meta-box-order_cjtoolbox'; |
| @@ -370,52 +438,52 @@ | ||
| 370 | 438 | update_user_option(get_current_user_id(), $orderOptionName, $order, true); |
| 371 | 439 | // Update CENTRALIZED order in the options table! |
| 372 | 440 | update_option($orderOptionName, $order); |
| 373 | 441 | } |
| 374 | - | |
| 442 | + | |
| 375 | 443 | /** |
| 376 | 444 | * put your comment there... |
| 377 | - * | |
| 445 | + * | |
| 378 | 446 | * @param mixed $block |
| 379 | 447 | */ |
| 380 | - public function update( $block, $updatePins ) | |
| 448 | + public function update( $block, $updatePins ) | |
| 381 | 449 | { |
| 382 | - | |
| 450 | + | |
| 383 | 451 | $block = ( array ) $block; |
| 384 | - | |
| 452 | + | |
| 385 | 453 | $blocks = new CJTBlocksTable( $this->dbDriver ); |
| 386 | 454 | $pins = new CJTBlockPinsTable( $this->dbDriver ); |
| 387 | - | |
| 455 | + | |
| 388 | 456 | // Update block pins if requested. |
| 389 | - if ( $updatePins ) | |
| 457 | + if ( $updatePins ) | |
| 390 | 458 | { |
| 391 | 459 | // Isolate block pins freom native block data. |
| 392 | 460 | $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) ); |
| 393 | - | |
| 461 | + | |
| 394 | 462 | do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData ); |
| 395 | - | |
| 463 | + | |
| 396 | 464 | $pins->update( $block[ 'id' ], $pinsData ); |
| 397 | 465 | |
| 398 | 466 | } |
| 399 | - | |
| 467 | + | |
| 400 | 468 | // Update code file |
| 401 | - if ( isset( $block[ 'activeFileId' ] ) ) | |
| 469 | + if ( isset( $block[ 'activeFileId' ] ) ) | |
| 402 | 470 | { |
| 403 | - | |
| 471 | + | |
| 404 | 472 | $codeFile = new CJTBlockFilesTable( $this->dbDriver ); |
| 405 | - | |
| 473 | + | |
| 406 | 474 | $codeFile->set( 'blockId', $block[ 'id' ] ) |
| 407 | 475 | ->set( 'id', $block[ 'activeFileId'] ) |
| 408 | 476 | ->set( 'code', $block[ 'code'] ) |
| 409 | 477 | ->save(); |
| 410 | 478 | } |
| 411 | - | |
| 479 | + | |
| 412 | 480 | // Isolate block fields. |
| 413 | 481 | $blockData = array_intersect_key($block, $blocks->getFields()); |
| 414 | - | |
| 482 | + | |
| 415 | 483 | do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData ); |
| 416 | - | |
| 484 | + | |
| 417 | 485 | $blocks->update( $blockData ); |
| 418 | - | |
| 486 | + | |
| 419 | 487 | } |
| 420 | - | |
| 488 | + | |
| 421 | 489 | } // End class. |