PluginProbe
CSS & JavaScript Toolbox / 7.2
CSS & JavaScript Toolbox v7.2
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
← All changes | models/blocks.php +67 -208 11.57.2 View file →
@@ -12,45 +12,46 @@
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 - *
49 - * @param mixed $block
50 - * @param mixed $forceAddCodeBlock
48 + *
49 + * @param mixed $data
50 + * @param mixed $pins
51 + * @param mixed $customPins
51 52 */
52 - public function add($block, $forceAddCodeBlock = false) {
53 + public function add($block) {
53 54 // Make sure it array and not stdClass as it has been used from various areas!
54 55 $block = (array) $block;
55 56 // Create Tables objects.
56 57 $blocks = new CJTBlocksTable($this->dbDriver);
@@ -56,32 +57,27 @@
56 57 $blocks = new CJTBlocksTable($this->dbDriver);
57 58 $codeFile = new CJTBlockFilesTable($this->dbDriver);
58 59 // Get new id if not specified.
59 60 if (!isset($block['id']) || !$block['id']) {
60 - $block['id'] = $blocks->getNextIdNEW();
61 + $block['id'] = $blocks->getNextId();
61 62 }
62 - // Backward compatibility for old block style
63 - // that has code field inside blocks table.
64 - /// if passed create master code file for the code field.
65 - if ($forceAddCodeBlock || isset($block['code'])) {
66 - // Cache code for MASTER file.
67 - $codeFile->set('code', $block['code']);
68 - unset($block['code']); // If it passed it mist be removed from blocks data(avoid field not found)
69 - // Add code files.
70 - $codeFile->set('blockId', $block['id'])
71 - ->set('id', 1)
72 - ->set('name', 'Master')
73 - ->save(true, true);
74 - }
63 + // Cache code for MASTER file.
64 + $codeFile->set('code', (isset($block['code']) ? $block['code'] : null));
65 + unset($block['code']); // If it passed it mist be removed from blocks data(avoid field not found)
75 66 // Add new block.
76 67 $blocks->insert($block);
68 + // Add code files.
69 + $codeFile->set('blockId', $block['id'])
70 + ->set('id', 1)
71 + ->set('name', 'Master')
72 + ->save(true, true);
77 73 // Return Newly added block id.
78 74 return $block['id'];
79 75 }
80 -
76 +
81 77 /**
82 78 * put your comment there...
83 - *
79 + *
84 80 * @param mixed $blockId
85 81 * @param mixed $activeFileId
86 82 */
87 83 public function addRevision($blockId, $activeFileId) {
@@ -89,9 +85,9 @@
89 85 $blocks = new CJTBlocksTable($this->dbDriver);
90 86 $pins = new CJTBlockPinsTable($this->dbDriver);
91 87 $codeFile = new CJTBlockFilesTable($this->dbDriver);
92 88 // 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
89 + // block code files So that a single block may has up to
94 90 // self::MAX_REVISIONS_PER_BLOCK * count(codeFiles)
95 91 $revisions['fields'] = array('id');
96 92 $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId);
97 93 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
@@ -98,9 +94,9 @@
98 94 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
99 95 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
100 96 $this->delete(array_shift($revisions)->id);
101 97 }
102 - // Get block data.
98 + // Get block data.
103 99 $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
104 100 // get() developed to return multiple blocks, fetch the first.
105 101 $result = $blocks->get($blockId, $block['fields']);
106 102 $block = reset($result);
@@ -109,12 +105,11 @@
109 105 $block->parent = $blockId;
110 106 $block->type = 'revision';
111 107 $block->created = current_time('mysql');
112 108 $block->owner = get_current_user_id();
113 - $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be
109 + $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be
114 110 // used as the masterFile!
115 - //$block->id = $blocks->getNextId(); // Get new id for revision rrecord.
116 - $block->id = $blocks->getNextIdNEW(); // Get new id for revision rrecord.
111 + $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
117 112 // Add block data.
118 113 $blocks->insert($block);
119 114 // Get block pins and insert pins for the revision block.
120 115 $blockPins = $pins->get($blockId);
@@ -129,22 +124,22 @@
129 124 ->load()
130 125 ->set('blockId', $block->id)
131 126 ->save(true, true);
132 127 }
133 -
128 +
134 129 /**
135 130 * Put your comments here...
136 131 *
137 132 *
138 - * @return
139 - */
133 + * @return
134 + */
140 135 public function dbDriver() {
141 136 return $this->dbDriver;
142 137 }
143 -
138 +
144 139 /**
145 140 * put your comment there...
146 - *
141 + *
147 142 * @param mixed $ids
148 143 */
149 144 public function delete($ids) {
150 145 // Allow single or multiple Ids to be passed.
@@ -186,59 +181,12 @@
186 181 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
187 182 // Chaining!
188 183 return $this;
189 184 }
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 -
185 +
238 186 /**
239 187 * put your comment there...
240 - *
188 + *
241 189 * @param mixed $id
242 190 * @param mixed $fields
243 191 */
244 192 public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
@@ -245,12 +193,12 @@
245 193 $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
246 194 $block = !empty($blocks) ? reset($blocks) : null;
247 195 return $block;
248 196 }
249 -
197 +
250 198 /**
251 199 * put your comment there...
252 - *
200 + *
253 201 * @param mixed $ids
254 202 */
255 203 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
256 204 // initialize.
@@ -294,12 +242,12 @@
294 242 }
295 243 }
296 244 return $blocks;
297 245 }
298 -
246 +
299 247 /**
300 248 * put your comment there...
301 - *
249 + *
302 250 * @param mixed $blockId
303 251 * @param mixed $authorId
304 252 */
305 253 public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
@@ -308,31 +256,11 @@
308 256 // Query block active file.
309 257 return $activeFileId;
310 258 }
311 259
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 -
332 260 /**
333 261 * put your comment there...
334 - *
262 + *
335 263 * @param mixed $blockId
336 264 * @param mixed $authorId
337 265 */
338 266 public function getCurrentAuthorBlockActiveFileId($blockId) {
@@ -340,62 +268,9 @@
340 268 }
341 269
342 270 /**
343 271 * put your comment there...
344 - *
345 - */
346 - public static function getCustomPostTypes()
347 - {
348 -
349 - static $postTypes = null;
350 -
351 - if ( $postTypes !== null )
352 - {
353 - return $postTypes;
354 - }
355 -
356 -
357 - $postTypes = array();
358 -
359 - // Create tabs for every custom post under the custom posts tab.
360 - // Get all registered custom posts.
361 - $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' );
362 -
363 - // Add tab for every custom post
364 - // Exclude 'Empty' Custom Post Types.
365 - foreach ( $customPosts as $typeName => $customPost )
366 - {
367 - // Check if has posts.
368 - $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) );
369 -
370 - // Add only types with at least one post exists.
371 - if ( $hasPosts )
372 - {
373 - $postTypes[ $typeName ] = array
374 - (
375 - 'title' => $customPost->labels->name,
376 - 'renderer' => 'objects-list',
377 - 'type' => array
378 - (
379 - 'type' => $typeName,
380 - 'group' => 'posts',
381 - 'targetType' => 'post'
382 - )
383 -
384 - );
385 -
386 - }
387 -
388 - }
389 -
390 - do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes );
391 -
392 - return $postTypes;
393 - }
394 -
395 - /**
396 - * put your comment there...
397 - *
272 + *
398 273 * @param mixed $id
399 274 */
400 275 public function getInfo($id) {
401 276 // Info fields.
@@ -409,28 +284,28 @@
409 284 // Set shortcode name.
410 285 $info->shortcode = "[cjtoolbox name='{$info->name}']";
411 286 return $info;
412 287 }
413 -
288 +
414 289 /**
415 290 * put your comment there...
416 - *
291 + *
417 292 */
418 293 public function getOrder() {
419 294 return get_option('meta-box-order_cjtoolbox');
420 295 }
421 -
296 +
422 297 /**
423 298 * put your comment there...
424 - *
299 + *
425 300 */
426 301 public function save() {
427 302 $this->dbDriver->processQueue();
428 303 }
429 -
304 +
430 305 /**
431 306 * put your comment there...
432 - *
307 + *
433 308 * @param mixed $order
434 309 */
435 310 public function setOrder($order) {
436 311 $orderOptionName = 'meta-box-order_cjtoolbox';
@@ -438,52 +313,36 @@
438 313 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
439 314 // Update CENTRALIZED order in the options table!
440 315 update_option($orderOptionName, $order);
441 316 }
442 -
317 +
443 318 /**
444 319 * put your comment there...
445 - *
320 + *
446 321 * @param mixed $block
447 322 */
448 - public function update( $block, $updatePins )
449 - {
450 -
451 - $block = ( array ) $block;
452 -
453 - $blocks = new CJTBlocksTable( $this->dbDriver );
454 - $pins = new CJTBlockPinsTable( $this->dbDriver );
455 -
323 + public function update($block, $updatePins) {
324 + // To be used by array_intersect_key.
325 + $block = (array) $block;
326 + // Create Tables objects.
327 + $blocks = new CJTBlocksTable($this->dbDriver);
328 + $pins = new CJTBlockPinsTable($this->dbDriver);
456 329 // Update block pins if requested.
457 - if ( $updatePins )
458 - {
330 + if ($updatePins) {
459 331 // Isolate block pins freom native block data.
460 - $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) );
461 -
462 - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData );
463 -
464 - $pins->update( $block[ 'id' ], $pinsData );
465 -
332 + $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
333 + $pins->update($block['id'], $pinsData);
466 334 }
467 -
468 335 // Update code file
469 - if ( isset( $block[ 'activeFileId' ] ) )
470 - {
471 -
472 - $codeFile = new CJTBlockFilesTable( $this->dbDriver );
473 -
474 - $codeFile->set( 'blockId', $block[ 'id' ] )
475 - ->set( 'id', $block[ 'activeFileId'] )
476 - ->set( 'code', $block[ 'code'] )
336 + if (isset($block['activeFileId'])) {
337 + $codeFile = new CJTBlockFilesTable($this->dbDriver);
338 + $codeFile->set('blockId', $block['id'])
339 + ->set('id', $block['activeFileId'])
340 + ->set('code', $block['code'])
477 341 ->save();
478 342 }
479 -
480 343 // Isolate block fields.
481 344 $blockData = array_intersect_key($block, $blocks->getFields());
482 -
483 - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData );
484 -
485 - $blocks->update( $blockData );
486 -
345 + $blocks->update($blockData);
487 346 }
488 -
347 +
489 348 } // End class.