PluginProbe
CSS & JavaScript Toolbox / 6.0
CSS & JavaScript Toolbox v6.0
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 +60 -300 116.0 View file →
@@ -12,99 +12,79 @@
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);
57 - $codeFile = new CJTBlockFilesTable($this->dbDriver);
58 58 // Get new id if not specified.
59 - if (!isset($block['id']) || !$block['id']) {
59 + if (!$block['id']) {
60 60 $block['id'] = $blocks->getNextId();
61 61 }
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 - }
75 - // Add new block.
76 62 $blocks->insert($block);
77 - // Return Newly added block id.
78 63 return $block['id'];
79 64 }
80 -
65 +
81 66 /**
82 67 * put your comment there...
83 - *
84 - * @param mixed $blockId
85 - * @param mixed $activeFileId
68 + *
69 + * @param mixed $id
86 70 */
87 - public function addRevision($blockId, $activeFileId) {
71 + public function addRevision($blockId) {
88 72 // Create Tables objects.
89 73 $blocks = new CJTBlocksTable($this->dbDriver);
90 74 $pins = new CJTBlockPinsTable($this->dbDriver);
91 - $codeFile = new CJTBlockFilesTable($this->dbDriver);
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
94 - // self::MAX_REVISIONS_PER_BLOCK * count(codeFiles)
75 + // We allow only up to self::MAX_REVISIONS_PER_BLOCK revisions per block.
95 76 $revisions['fields'] = array('id');
96 - $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId, 'masterFile' => $activeFileId);
77 + $revisions['filters'] = array('type' => 'revision', 'parent' => $blockId);
97 78 $revisions = $blocks->get(null, $revisions['fields'], $revisions['filters']);
98 79 // If revisions reached self::MAX_REVISIONS_PER_BLOCK delete first one.
99 80 if (count($revisions) == self::MAX_REVISIONS_PER_BLOCK) {
100 81 $this->delete(array_shift($revisions)->id);
101 82 }
102 - // Get block data.
103 - $block['fields'] = array('id', 'lastModified', 'pinPoint', 'links', 'expressions');
83 + // Get block data.
84 + $block['fields'] = array('lastModified', 'pinPoint', 'code', 'links', 'expressions');
104 85 // get() developed to return multiple blocks, fetch the first.
105 - $result = $blocks->get($blockId, $block['fields']);
106 - $block = reset($result);
86 + $block = array_shift($blocks->get($blockId, $block['fields']));
107 87 // Set other fields.
108 88 $block->location = $block->state = '';
109 89 $block->parent = $blockId;
110 90 $block->type = 'revision';
@@ -109,10 +89,8 @@
109 89 $block->parent = $blockId;
110 90 $block->type = 'revision';
111 91 $block->created = current_time('mysql');
112 92 $block->owner = get_current_user_id();
113 - $block->masterFile = $activeFileId; // Only the revisioned code file would be exists and must be
114 - // used as the masterFile!
115 93 $block->id = $blocks->getNextId(); // Get new id for revision rrecord.
116 94 // Add block data.
117 95 $blocks->insert($block);
118 96 // Get block pins and insert pins for the revision block.
@@ -119,42 +97,29 @@
119 97 $blockPins = $pins->get($blockId);
120 98 if (!empty($blockPins)) {
121 99 $pins->insertRaw($block->id, $blockPins);
122 100 }
123 - // Revision current ActiveFileId code record.
124 - // Simply, get a copy of it from the target block
125 - // and assign the copy to the new created block revision.
126 - $codeFile->set('id', $activeFileId)
127 - ->set('blockId', $blockId)
128 - ->load()
129 - ->set('blockId', $block->id)
130 - ->save(true, true);
131 101 }
132 -
102 +
133 103 /**
134 104 * Put your comments here...
135 105 *
136 106 *
137 - * @return
138 - */
107 + * @return
108 + */
139 109 public function dbDriver() {
140 110 return $this->dbDriver;
141 111 }
142 -
112 +
143 113 /**
144 114 * put your comment there...
145 - *
115 + *
146 116 * @param mixed $ids
147 117 */
148 118 public function delete($ids) {
149 - // Allow single or multiple Ids to be passed.
150 - if (!is_array($ids)) {
151 - $ids = array($ids);
152 - }
153 119 // Create Tables objects.
154 120 $blocks = new CJTBlocksTable($this->dbDriver);
155 121 $pins = new CJTBlockPinsTable($this->dbDriver);
156 - $codeFile = new CJTBlockFilesTable($this->dbDriver);
157 122 // Get blocks revisions.
158 123 $revisions['fields'] = array('id');
159 124 $revisions['filters']['parent'] = $ids;
160 125 $revisions['type'] = 'revision';
@@ -165,104 +130,40 @@
165 130 // only if there is at least one revision.
166 131 if (!empty($revisions)) {
167 132 $blocks->delete($revisions);
168 133 $pins->delete($revisions);
169 - // Delete code files.
170 - $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $revisions)));
171 134 }
172 - // Delete linked templates.
173 - $linkedOnlyTemplatesQuery = 'DELETE FROM #__cjtoolbox_block_templates WHERE blockId IN(%s)';
174 - $this->dbDriver->delete(sprintf($linkedOnlyTemplatesQuery, implode(',', $ids)));
175 - // Delete associated parameters.
176 - $mdlParams = new CJT_Models_Parameters();
177 - $mdlParams->delete($ids);
178 - // Delete form.
179 - $mdlForm = new CJT_Models_Forms();
180 - $mdlForm->delete($ids);
181 135 // Delete blocks.
182 136 $blocks->delete($ids);
183 137 $pins->delete($ids);
184 - // Delete code files.
185 - $this->dbDriver->delete(sprintf('DELETE FROM #__cjtoolbox_block_files WHERE blockId IN(%s)', implode(',', $ids)));
186 138 // Chaining!
187 139 return $this;
188 140 }
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 -
141 +
237 142 /**
238 143 * put your comment there...
239 - *
144 + *
240 145 * @param mixed $id
241 146 * @param mixed $fields
242 147 */
243 - public function getBlock($id, $filters = array(), $fields = array('*'), $useDefaultBackupFltr = true) {
244 - $blocks = $this->getBlocks($id, $filters, $fields, OBJECT_K, array(), $useDefaultBackupFltr);
148 + public function getBlock($id, $filters = array(), $fields = array('*')) {
149 + $blocks = $this->getBlocks($id, $filters, $fields);
245 150 $block = !empty($blocks) ? reset($blocks) : null;
246 151 return $block;
247 152 }
248 -
153 +
249 154 /**
250 155 * put your comment there...
251 - *
156 + *
252 157 * @param mixed $ids
253 158 */
254 - public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array(), $useDefaultBackupFltr = true) {
255 - // initialize.
159 + public function getBlocks($ids = array(), $filters = array(), $fields = array('*'), $returnType = OBJECT_K, $orderBy = array()) {
256 160 $blocks = array();
257 - $returnCodeFile = isset($filters['returnCodeFile']) && ($filters['returnCodeFile'] == true);
258 - unset($filters['returnCodeFile']);
259 161 // Create Tables objects.
260 162 $blocksTable = new CJTBlocksTable($this->dbDriver);
261 163 $pinsTable = new CJTBlockPinsTable($this->dbDriver);
262 - $blockFiles = new CJTBlockFilesTable($this->dbDriver);
263 164 // Read blocks.
264 - $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy, $useDefaultBackupFltr);
165 + $blocks = $blocksTable->get($ids, $fields, $filters, $returnType, $orderBy);
265 166 // Get only pins for retrieved blocks.
266 167 $ids = array_keys($blocks);
267 168 $pins = empty($ids) ? array() : $pinsTable->get($ids);
268 169 // Push pins into blocks.
@@ -271,130 +172,14 @@
271 172 // Each pin is an array.
272 173 // e.g blocks[ID]->pages[] = PAGE-ID.
273 174 $blocks[$pin->blockId]->{$pin->pin}[] = $pin->value;
274 175 }
275 - // Get active file code.
276 - // There always should be active file unless that the
277 - // requested block is never switched by current author before.
278 - // However we'll do that only if filters['returnCodeFile'] set to TRUE.
279 - if ($returnCodeFile) {
280 - foreach ($ids as $id) {
281 - if(!$activeFileId = $this->getCurrentAuthorBlockActiveFileId($id)) {
282 - // If first time use masterFile ID as he default.
283 - $activeFileId = $blocks[$id]->masterFile;
284 - }
285 - // Retreive code for block code file.
286 - $codeFile = (array) $blockFiles->setData(array('blockId' => $id, 'id' => $activeFileId))
287 - ->load()
288 - ->getData();
289 - unset($codeFile['description']);
290 - $blocks[$id]->file = (object) $codeFile;
291 - // Also return the active file id withing the set.
292 - $blocks[$id]->activeFileId = $activeFileId;
293 - }
294 - }
295 176 return $blocks;
296 177 }
297 -
178 +
298 179 /**
299 180 * put your comment there...
300 - *
301 - * @param mixed $blockId
302 - * @param mixed $authorId
303 - */
304 - public function getAuthorBlockActiveFileId($blockId, $authorId = null) {
305 - // Get author active file for the requested block.
306 - $activeFileId = (int) get_user_meta($authorId, "cjt_block_active_file_{$blockId}", true);
307 - // Query block active file.
308 - return $activeFileId;
309 - }
310 -
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 - /**
332 - * put your comment there...
333 - *
334 - * @param mixed $blockId
335 - * @param mixed $authorId
336 - */
337 - public function getCurrentAuthorBlockActiveFileId($blockId) {
338 - return $this->getAuthorBlockActiveFileId($blockId, get_current_user_id());
339 - }
340 -
341 - /**
342 - * 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 - *
181 + *
397 182 * @param mixed $id
398 183 */
399 184 public function getInfo($id) {
400 185 // Info fields.
@@ -405,31 +190,31 @@
405 190 // Get user object from id.
406 191 $info = reset($info);
407 192 $info->owner = get_userdata($info->owner);
408 193 // Set shortcode name.
409 - $info->shortcode = "[cjtoolbox name='{$info->name}']";
194 + $info->shortcode = "[cjtoolbox id='{$info->id}']";
410 195 return $info;
411 196 }
412 -
197 +
413 198 /**
414 199 * put your comment there...
415 - *
200 + *
416 201 */
417 202 public function getOrder() {
418 203 return get_option('meta-box-order_cjtoolbox');
419 204 }
420 -
205 +
421 206 /**
422 207 * put your comment there...
423 - *
208 + *
424 209 */
425 210 public function save() {
426 211 $this->dbDriver->processQueue();
427 212 }
428 -
213 +
429 214 /**
430 215 * put your comment there...
431 - *
216 + *
432 217 * @param mixed $order
433 218 */
434 219 public function setOrder($order) {
435 220 $orderOptionName = 'meta-box-order_cjtoolbox';
@@ -437,52 +222,27 @@
437 222 update_user_option(get_current_user_id(), $orderOptionName, $order, true);
438 223 // Update CENTRALIZED order in the options table!
439 224 update_option($orderOptionName, $order);
440 225 }
441 -
226 +
442 227 /**
443 228 * put your comment there...
444 - *
229 + *
445 230 * @param mixed $block
446 231 */
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 -
232 + public function update($block, $updatePins) {
233 + $block = (array) $block; // To be used by array_intersect_key.
234 + // Create Tables objects.
235 + $blocks = new CJTBlocksTable($this->dbDriver);
236 + $pins = new CJTBlockPinsTable($this->dbDriver);
455 237 // Update block pins if requested.
456 - if ( $updatePins )
457 - {
238 + if ($updatePins) {
458 239 // 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 -
240 + $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
241 + $pins->update($block['id'], $pinsData);
465 242 }
466 -
467 - // 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'] )
476 - ->save();
477 - }
478 -
479 243 // Isolate block fields.
480 - $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 -
244 + $block = array_intersect_key($block, $blocks->getFields());
245 + $blocks->update($block);
486 246 }
487 -
247 +
488 248 } // End class.