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 +65 -205 117.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);
@@ -58,30 +59,25 @@
58 59 // Get new id if not specified.
59 60 if (!isset($block['id']) || !$block['id']) {
60 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,9 +105,9 @@
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 111 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
116 112 // Add block data.
117 113 $blocks->insert($block);
@@ -128,22 +124,22 @@
128 124 ->load()
129 125 ->set('blockId', $block->id)
130 126 ->save(true, true);
131 127 }
132 -
128 +
133 129 /**
134 130 * Put your comments here...
135 131 *
136 132 *
137 - * @return
138 - */
133 + * @return
134 + */
139 135 public function dbDriver() {
140 136 return $this->dbDriver;
141 137 }
142 -
138 +
143 139 /**
144 140 * put your comment there...
145 - *
141 + *
146 142 * @param mixed $ids
147 143 */
148 144 public function delete($ids) {
149 145 // Allow single or multiple Ids to be passed.
@@ -185,59 +181,12 @@
185 181 $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
186 182 // Chaining!
187 183 return $this;
188 184 }
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 -
185 +
237 186 /**
238 187 * put your comment there...
239 - *
188 + *
240 189 * @param mixed $id
241 190 * @param mixed $fields
242 191 */
243 192 public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
@@ -244,12 +193,12 @@
244 193 $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
245 194 $block = !empty($blocks) ? reset($blocks) : null;
246 195 return $block;
247 196 }
248 -
197 +
249 198 /**
250 199 * put your comment there...
251 - *
200 + *
252 201 * @param mixed $ids
253 202 */
254 203 public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
255 204 // initialize.
@@ -293,12 +242,12 @@
293 242 }
294 243 }
295 244 return $blocks;
296 245 }
297 -
246 +
298 247 /**
299 248 * put your comment there...
300 - *
249 + *
301 250 * @param mixed $blockId
302 251 * @param mixed $authorId
303 252 */
304 253 public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
@@ -307,31 +256,11 @@
307 256 // Query block active file.
308 257 return $activeFileId;
309 258 }
310 259
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 260 /**
332 261 * put your comment there...
333 - *
262 + *
334 263 * @param mixed $blockId
335 264 * @param mixed $authorId
336 265 */
337 266 public function getCurrentAuthorBlockActiveFileId($blockId) {
@@ -339,62 +268,9 @@
339 268 }
340 269
341 270 /**
342 271 * put your comment there...
343 - *
344 - */
345 - public static function getCustomPostTypes()
346 - {
347 -
348 - static $postTypes = null;
349 -
350 - if ( $postTypes !== null )
351 - {
352 - return $postTypes;
353 - }
354 -
355 -
356 - $postTypes = array();
357 -
358 - // Create tabs for every custom post under the custom posts tab.
359 - // Get all registered custom posts.
360 - $customPosts = get_post_types( array( 'public' => 1, 'show_ui' => true, '_builtin' => false ), 'objects' );
361 -
362 - // Add tab for every custom post
363 - // Exclude 'Empty' Custom Post Types.
364 - foreach ( $customPosts as $typeName => $customPost )
365 - {
366 - // Check if has posts.
367 - $hasPosts = count( get_posts( array( 'post_type' => $typeName, 'offset' => 0, 'numberposts' => 1 ) ) );
368 -
369 - // Add only types with at least one post exists.
370 - if ( $hasPosts )
371 - {
372 - $postTypes[ $typeName ] = array
373 - (
374 - 'title' => $customPost->labels->name,
375 - 'renderer' => 'objects-list',
376 - 'type' => array
377 - (
378 - 'type' => $typeName,
379 - 'group' => 'posts',
380 - 'targetType' => 'post'
381 - )
382 -
383 - );
384 -
385 - }
386 -
387 - }
388 -
389 - do_action( CJTPluggableHelper::ACTION_BLOCK_CUSTOM_POST_TYPES, $postTypes );
390 -
391 - return $postTypes;
392 - }
393 -
394 - /**
395 - * put your comment there...
396 - *
272 + *
397 273 * @param mixed $id
398 274 */
399 275 public function getInfo($id) {
400 276 // Info fields.
@@ -408,28 +284,28 @@
408 284 // Set shortcode name.
409 285 $info->shortcode = "[cjtoolbox name='{$info->name}']";
410 286 return $info;
411 287 }
412 -
288 +
413 289 /**
414 290 * put your comment there...
415 - *
291 + *
416 292 */
417 293 public function getOrder() {
418 294 return get_option('meta-box-order_cjtoolbox');
419 295 }
420 -
296 +
421 297 /**
422 298 * put your comment there...
423 - *
299 + *
424 300 */
425 301 public function save() {
426 302 $this->dbDriver->processQueue();
427 303 }
428 -
304 +
429 305 /**
430 306 * put your comment there...
431 - *
307 + *
432 308 * @param mixed $order
433 309 */
434 310 public function setOrder($order) {
435 311 $orderOptionName = 'meta-box-order_cjtoolbox';
@@ -437,52 +313,36 @@
437 313 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
438 314 // Update CENTRALIZED order in the options table!
439 315 update_option($orderOptionName, $order);
440 316 }
441 -
317 +
442 318 /**
443 319 * put your comment there...
444 - *
320 + *
445 321 * @param mixed $block
446 322 */
447 - public function update( $block, $updatePins )
448 - {
449 -
450 - $block = ( array ) $block;
451 -
452 - $blocks = new CJTBlocksTable( $this->dbDriver );
453 - $pins = new CJTBlockPinsTable( $this->dbDriver );
454 -
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);
455 329 // Update block pins if requested.
456 - if ( $updatePins )
457 - {
330 + if ($updatePins) {
458 331 // Isolate block pins freom native block data.
459 - $pinsData = array_intersect_key( $block, array_flip( array_keys( CJTBlockModel::getCustomPins() ) ) );
460 -
461 - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK_PINS, $block, $pinsData );
462 -
463 - $pins->update( $block[ 'id' ], $pinsData );
464 -
332 + $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
333 + $pins->update($block['id'], $pinsData);
465 334 }
466 -
467 335 // Update code file
468 - if ( isset( $block[ 'activeFileId' ] ) )
469 - {
470 -
471 - $codeFile = new CJTBlockFilesTable( $this->dbDriver );
472 -
473 - $codeFile->set( 'blockId', $block[ 'id' ] )
474 - ->set( 'id', $block[ 'activeFileId'] )
475 - ->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'])
476 341 ->save();
477 342 }
478 -
479 343 // Isolate block fields.
480 344 $blockData = array_intersect_key($block, $blocks->getFields());
481 -
482 - do_action( CJTPluggableHelper::FILTER_BLOCK_MODEL_PRE_UPDATE_BLOCK, $updatePins, $blockData );
483 -
484 - $blocks->update( $blockData );
485 -
345 + $blocks->update($blockData);
486 346 }
487 -
347 +
488 348 } // End class.