| @@ -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 = 99; | |
| 29 | - | |
| 28 | + const MAX_REVISIONS_PER_BLOCK = 10; | |
| 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) { |
| @@ -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,9 +109,9 @@ | ||
| 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 | 115 | $block->id = $blocks->getNextId(); // Get new id for revision rrecord. |
| 116 | 116 | // Add block data. |
| 117 | 117 | $blocks->insert($block); |
| @@ -128,22 +128,22 @@ | ||
| 128 | 128 | ->load() |
| 129 | 129 | ->set('blockId', $block->id) |
| 130 | 130 | ->save(true, true); |
| 131 | 131 | } |
| 132 | - | |
| 132 | + | |
| 133 | 133 | /** |
| 134 | 134 | * Put your comments here... |
| 135 | 135 | * |
| 136 | 136 | * |
| 137 | - * @return | |
| 138 | - */ | |
| 137 | + * @return | |
| 138 | + */ | |
| 139 | 139 | public function dbDriver() { |
| 140 | 140 | return $this->dbDriver; |
| 141 | 141 | } |
| 142 | - | |
| 142 | + | |
| 143 | 143 | /** |
| 144 | 144 | * put your comment there... |
| 145 | - * | |
| 145 | + * | |
| 146 | 146 | * @param mixed $ids |
| 147 | 147 | */ |
| 148 | 148 | public function delete($ids) { |
| 149 | 149 | // Allow single or multiple Ids to be passed. |
| @@ -185,59 +185,12 @@ | ||
| 185 | 185 | $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids))); |
| 186 | 186 | // Chaining! |
| 187 | 187 | return $this; |
| 188 | 188 | } |
| 189 | - | |
| 190 | - /** | |
| 191 | - * put your comment there... | |
| 192 | - * | |
| 193 | - * @param mixed $block | |
| 194 | - */ | |
| 195 | - public static function getAllAssignments($block) { | |
| 196 | - | |
| 197 | - $inPages = $block->pages ? $block->pages : []; | |
| 198 | - $inPosts = $block->posts ? $block->posts : []; | |
| 199 | - $inCats = $block->categories ? $block->categories : []; | |
| 200 | - $inTags = $block->post_tags ? $block->post_tags : []; | |
| 201 | - $inLinks = ! empty( $block->links ) ? explode( "\n", $block->links ) : []; | |
| 202 | - $inExps = ! empty( $block->expressions ) ? explode( "\n", $block->expressions ) : []; | |
| 203 | - $inAux = array(); | |
| 204 | - | |
| 205 | - // Found selected Aux | |
| 206 | - $auxFlags = array( | |
| 207 | - CJTBlockModel::PINS_404_ERROR, | |
| 208 | - CJTBlockModel::PINS_ARCHIVE, | |
| 209 | - CJTBlockModel::PINS_ATTACHMENT, | |
| 210 | - CJTBlockModel::PINS_AUTHOR, | |
| 211 | - CJTBlockModel::PINS_BACKEND, | |
| 212 | - CJTBlockModel::PINS_CATEGORIES_ALL_CATEGORIES, | |
| 213 | - CJTBlockModel::PINS_EXPRESSIONS, | |
| 214 | - CJTBlockModel::PINS_FRONTEND, | |
| 215 | - CJTBlockModel::PINS_PAGES_ALL_PAGES, | |
| 216 | - CJTBlockModel::PINS_POSTS_ALL_POSTS, | |
| 217 | - CJTBlockModel::PINS_POSTS_BLOG_INDEX, | |
| 218 | - CJTBlockModel::PINS_POSTS_RECENT, | |
| 219 | - CJTBlockModel::PINS_SEARCH, | |
| 220 | - CJTBlockModel::PINS_TAG | |
| 221 | - ); | |
| 222 | - | |
| 223 | - foreach ($auxFlags as $auxFlag) { | |
| 224 | - | |
| 225 | - if ($auxFlag & $block->pinPoint) { | |
| 226 | - | |
| 227 | - $inAux[] = $auxFlag; | |
| 228 | - } | |
| 229 | - | |
| 230 | - } | |
| 231 | - | |
| 232 | - $allAssignment = array_merge( $inPages, $inPosts, $inCats, $inTags, $inLinks, $inExps, $inAux ); | |
| 233 | - | |
| 234 | - return $allAssignment; | |
| 235 | - } | |
| 236 | - | |
| 189 | + | |
| 237 | 190 | /** |
| 238 | 191 | * put your comment there... |
| 239 | - * | |
| 192 | + * | |
| 240 | 193 | * @param mixed $id |
| 241 | 194 | * @param mixed $fields |
| 242 | 195 | */ |
| 243 | 196 | public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) { |
| @@ -244,12 +197,12 @@ | ||
| 244 | 197 | $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr); |
| 245 | 198 | $block = !empty($blocks) ? reset($blocks) : null; |
| 246 | 199 | return $block; |
| 247 | 200 | } |
| 248 | - | |
| 201 | + | |
| 249 | 202 | /** |
| 250 | 203 | * put your comment there... |
| 251 | - * | |
| 204 | + * | |
| 252 | 205 | * @param mixed $ids |
| 253 | 206 | */ |
| 254 | 207 | public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) { |
| 255 | 208 | // initialize. |
| @@ -293,12 +246,12 @@ | ||
| 293 | 246 | } |
| 294 | 247 | } |
| 295 | 248 | return $blocks; |
| 296 | 249 | } |
| 297 | - | |
| 250 | + | |
| 298 | 251 | /** |
| 299 | 252 | * put your comment there... |
| 300 | - * | |
| 253 | + * | |
| 301 | 254 | * @param mixed $blockId |
| 302 | 255 | * @param mixed $authorId |
| 303 | 256 | */ |
| 304 | 257 | public function getAuthorBlockActiveFileId($blockId, $authorId = null) { |
| @@ -307,31 +260,11 @@ | ||
| 307 | 260 | // Query block active file. |
| 308 | 261 | return $activeFileId; |
| 309 | 262 | } |
| 310 | 263 | |
| 311 | - /** | |
| 312 | - * put your comment there... | |
| 313 | - * | |
| 314 | - * @param mixed $blockId | |
| 315 | - */ | |
| 316 | - public static function getCodeFilesCount($blockId) { | |
| 317 | - | |
| 318 | - global $wpdb; | |
| 319 | - | |
| 320 | - $query = " SELECT count(*) | |
| 321 | - FROM {$wpdb->prefix}cjtoolbox_block_files f | |
| 322 | - WHERE f.blockId = %d;"; | |
| 323 | - | |
| 324 | - $query = $wpdb->prepare($query, $blockId); | |
| 325 | - | |
| 326 | - $codeFilesCount = $wpdb->get_var($query); | |
| 327 | - | |
| 328 | - return $codeFilesCount; | |
| 329 | - } | |
| 330 | - | |
| 331 | 264 | /** |
| 332 | 265 | * put your comment there... |
| 333 | - * | |
| 266 | + * | |
| 334 | 267 | * @param mixed $blockId |
| 335 | 268 | * @param mixed $authorId |
| 336 | 269 | */ |
| 337 | 270 | public function getCurrentAuthorBlockActiveFileId($blockId) { |
| @@ -339,23 +272,23 @@ | ||
| 339 | 272 | } |
| 340 | 273 | |
| 341 | 274 | /** |
| 342 | 275 | * put your comment there... |
| 343 | - * | |
| 276 | + * | |
| 344 | 277 | */ |
| 345 | 278 | public static function getCustomPostTypes() |
| 346 | 279 | { |
| 347 | - | |
| 280 | + | |
| 348 | 281 | static $postTypes = null; |
| 349 | - | |
| 282 | + | |
| 350 | 283 | if ( $postTypes !== null ) |
| 351 | 284 | { |
| 352 | 285 | return $postTypes; |
| 353 | 286 | } |
| 354 | - | |
| 355 | - | |
| 287 | + | |
| 288 | + | |
| 356 | 289 | $postTypes = array(); |
| 357 | - | |
| 290 | + | |
| 358 | 291 | // Create tabs for every custom post under the custom posts tab. |
| 359 | 292 | // Get all registered custom posts. |
| 360 | 293 | $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' ); |
| 361 | 294 | |
| @@ -360,15 +293,15 @@ | ||
| 360 | 293 | $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' ); |
| 361 | 294 | |
| 362 | 295 | // Add tab for every custom post |
| 363 | 296 | // Exclude 'Empty' Custom Post Types. |
| 364 | - foreach ( $customPosts as $typeName => $customPost ) | |
| 297 | + foreach ( $customPosts as $typeName => $customPost ) | |
| 365 | 298 | { |
| 366 | - // Check if has posts. | |
| 299 | + // Check if has posts. | |
| 367 | 300 | $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) ); |
| 368 | - | |
| 301 | + | |
| 369 | 302 | // Add only types with at least one post exists. |
| 370 | - if ( $hasPosts ) | |
| 303 | + if ( $hasPosts ) | |
| 371 | 304 | { |
| 372 | 305 | $postTypes[ $typeName ] = array |
| 373 | 306 | ( |
| 374 | 307 | 'title' => $customPost->labels->name, |
| @@ -373,28 +306,28 @@ | ||
| 373 | 306 | ( |
| 374 | 307 | 'title' => $customPost->labels->name, |
| 375 | 308 | 'renderer' => 'objects-list', |
| 376 | 309 | 'type' => array |
| 377 | - ( | |
| 310 | + ( | |
| 378 | 311 | 'type' => $typeName, |
| 379 | 312 | 'group' => 'posts', |
| 380 | 313 | 'targetType' => 'post' |
| 381 | 314 | ) |
| 382 | - | |
| 315 | + | |
| 383 | 316 | ); |
| 384 | - | |
| 317 | + | |
| 385 | 318 | } |
| 386 | - | |
| 319 | + | |
| 387 | 320 | } |
| 388 | 321 | |
| 389 | 322 | do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes ); |
| 390 | - | |
| 323 | + | |
| 391 | 324 | return $postTypes; |
| 392 | 325 | } |
| 393 | - | |
| 326 | + | |
| 394 | 327 | /** |
| 395 | 328 | * put your comment there... |
| 396 | - * | |
| 329 | + * | |
| 397 | 330 | * @param mixed $id |
| 398 | 331 | */ |
| 399 | 332 | public function getInfo($id) { |
| 400 | 333 | // Info fields. |
| @@ -408,28 +341,28 @@ | ||
| 408 | 341 | // Set shortcode name. |
| 409 | 342 | $info->shortcode = "[cjtoolbox name='{$info->name}']"; |
| 410 | 343 | return $info; |
| 411 | 344 | } |
| 412 | - | |
| 345 | + | |
| 413 | 346 | /** |
| 414 | 347 | * put your comment there... |
| 415 | - * | |
| 348 | + * | |
| 416 | 349 | */ |
| 417 | 350 | public function getOrder() { |
| 418 | 351 | return get_option('meta-box-order_cjtoolbox'); |
| 419 | 352 | } |
| 420 | - | |
| 353 | + | |
| 421 | 354 | /** |
| 422 | 355 | * put your comment there... |
| 423 | - * | |
| 356 | + * | |
| 424 | 357 | */ |
| 425 | 358 | public function save() { |
| 426 | 359 | $this->dbDriver->processQueue(); |
| 427 | 360 | } |
| 428 | - | |
| 361 | + | |
| 429 | 362 | /** |
| 430 | 363 | * put your comment there... |
| 431 | - * | |
| 364 | + * | |
| 432 | 365 | * @param mixed $order |
| 433 | 366 | */ |
| 434 | 367 | public function setOrder($order) { |
| 435 | 368 | $orderOptionName = 'meta-box-order_cjtoolbox'; |
| @@ -437,52 +370,52 @@ | ||
| 437 | 370 | update_user_option(get_current_user_id(), $orderOptionName, $order, true); |
| 438 | 371 | // Update CENTRALIZED order in the options table! |
| 439 | 372 | update_option($orderOptionName, $order); |
| 440 | 373 | } |
| 441 | - | |
| 374 | + | |
| 442 | 375 | /** |
| 443 | 376 | * put your comment there... |
| 444 | - * | |
| 377 | + * | |
| 445 | 378 | * @param mixed $block |
| 446 | 379 | */ |
| 447 | - public function update( $block, $updatePins ) | |
| 380 | + public function update( $block, $updatePins ) | |
| 448 | 381 | { |
| 449 | - | |
| 382 | + | |
| 450 | 383 | $block = ( array ) $block; |
| 451 | - | |
| 384 | + | |
| 452 | 385 | $blocks = new CJTBlocksTable( $this->dbDriver ); |
| 453 | 386 | $pins = new CJTBlockPinsTable( $this->dbDriver ); |
| 454 | - | |
| 387 | + | |
| 455 | 388 | // Update block pins if requested. |
| 456 | - if ( $updatePins ) | |
| 389 | + if ( $updatePins ) | |
| 457 | 390 | { |
| 458 | 391 | // Isolate block pins freom native block data. |
| 459 | 392 | $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) ); |
| 460 | - | |
| 393 | + | |
| 461 | 394 | do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData ); |
| 462 | - | |
| 395 | + | |
| 463 | 396 | $pins->update( $block[ 'id' ], $pinsData ); |
| 464 | 397 | |
| 465 | 398 | } |
| 466 | - | |
| 399 | + | |
| 467 | 400 | // Update code file |
| 468 | - if ( isset( $block[ 'activeFileId' ] ) ) | |
| 401 | + if ( isset( $block[ 'activeFileId' ] ) ) | |
| 469 | 402 | { |
| 470 | - | |
| 403 | + | |
| 471 | 404 | $codeFile = new CJTBlockFilesTable( $this->dbDriver ); |
| 472 | - | |
| 405 | + | |
| 473 | 406 | $codeFile->set( 'blockId', $block[ 'id' ] ) |
| 474 | 407 | ->set( 'id', $block[ 'activeFileId'] ) |
| 475 | 408 | ->set( 'code', $block[ 'code'] ) |
| 476 | 409 | ->save(); |
| 477 | 410 | } |
| 478 | - | |
| 411 | + | |
| 479 | 412 | // Isolate block fields. |
| 480 | 413 | $blockData = array_intersect_key($block, $blocks->getFields()); |
| 481 | - | |
| 414 | + | |
| 482 | 415 | do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData ); |
| 483 | - | |
| 416 | + | |
| 484 | 417 | $blocks->update( $blockData ); |
| 485 | - | |
| 418 | + | |
| 486 | 419 | } |
| 487 | - | |
| 420 | + | |
| 488 | 421 | } // End class. |