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