PluginProbe
CSS & JavaScript Toolbox / 8.4.1
CSS & JavaScript Toolbox v8.4.1
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
css-javascript-toolbox / models / blocks-backups.php

blocks-backups.php in CSS & JavaScript Toolbox 8.4.1, at models/blocks-backups.php

215 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 /**
7 * No direct access.
8 */
9 defined('ABSPATH') or die("Access denied");
10
11 // Blocks Database tables.
12 require_once CJTOOLBOX_TABLES_PATH . '/backups.php';
13 // MYSQL Queue Driver.
14 require_once CJTOOLBOX_INCLUDE_PATH . '/db/mysql/queue-driver.inc.php';
15
16 /**
17 *
18 */
19 class CJTBlocksBackupsModel {
20
21 /**
22 * put your comment there...
23 *
24 * @var mixed
25 */
26 private $backups = null;
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 private $dbDriver = null;
34
35 /**
36 * put your comment there...
37 *
38 */
39 public function __construct() {
40 $this->dbDriver = new CJTMYSQLQueueDriver($GLOBALS['wpdb']);
41 $this->backups = new CJTBackupsTable($this->dbDriver);
42 }
43
44 /**
45 * put your comment there...
46 *
47 * @param mixed $srcBackupId
48 * @param mixed $desBackupId
49 */
50 public function copyBackupBlocks($srcBackupId, $desBackupId) {
51 // Initialie.
52 $codeFilesQuery = 'INSERT INTO #__cjtoolbox_block_files (blockId, id, name, description, `type`, code, `order`)
53 SELECT %d, id, name, description, `type`, code, `order` FROM #__cjtoolbox_block_files WHERE blockId = %d;';
54 // Blocks Table & Model..
55 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
56 require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php';
57 require_once CJTOOLBOX_MODELS_PATH . '/blocks.php';
58 // Get blocks Table & Model instances.
59 $blocksTable = new CJTBlocksTable($this->dbDriver);
60 $blockPinsTable = new CJTBlockPinsTable($this->dbDriver);
61 $blocksModel = new CJTBlocksModel();
62 // Get source backup blocks.
63 $blocks['fields'] = array('*');
64 $blocks['filters']['backupId'] = $srcBackupId;
65 $blocks['filters']['types'] = array('block', 'revision');
66 // Its important to get revision blocks at the end
67 // to create blocks id map.
68 $blocks['orderby'] = array('type');
69 $blocks = $blocksModel->getBlocks(null, $blocks['filters'], $blocks['fields'], OBJECT_K, $blocks['orderby']);
70 // Prepare vars before copying.
71 $desBlockId = $blocksTable->getNextId();
72 // For every block give new Id and insert block and pins data.
73 $blocksMap = array();
74 foreach ($blocks as $id => $block) {
75 // Change block Id & backupId.
76 $block->id = $desBlockId++;
77 $block->backupId = $desBackupId;
78 // Change revision "parentId" to the new block id.
79 if ($block->type == 'block') {
80 // Map the old id to the new one.
81 $blocksMap[$id] = $block->id;
82 }
83 else if ($block->type = 'revision') {
84 // Exclude revision blocks for other types than 'block' type (e.g metabox revisions).
85 if (!isset($blocksMap[$block->parent])) {
86 continue;
87 }
88 /*
89 * Use the new id instead of the old one.
90 * Get the parent block id from the map.
91 */
92 else {
93 $block->parent = $blocksMap[$block->parent];
94 }
95 }
96 // Cast stdClass to array.
97 $block = (array) $block;
98 // Insert block.
99 $blockData = array_intersect_key($block, $blocksTable->getFields());
100 $blocksTable->insert($blockData);
101 // Insert block Pins.
102 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
103 $blockPinsTable->insert($block['id'], $pinsData);
104 // Copy code files.
105 $this->dbDriver->insert(sprintf($codeFilesQuery, $block['id'], $id))->processQueue();
106 }
107 }
108
109 /**
110 * put your comment there...
111 *
112 * @param int|string $backupData
113 */
114 public function create($backupData) {
115 // Prepare backup data.
116 $backupData['owner'] = get_current_user_id();
117 $backupData['created'] = current_time('mysql');
118 $backupData['type'] = 'blocks';
119 $backupData['id'] = $this->backups->getNextId();
120 // Don't do anything is any statement fails!
121 $this->dbDriver->startTransaction();
122 // Insert new backup id.
123 $this->backups->insert($backupData);
124 // Copy Blocks to newly created backup.
125 $this->copyBackupBlocks(null, $backupData['id']);
126 // Commit Changes.
127 $this->dbDriver->commit();
128 // Get Owner User object for the returned object.
129 $backupData['owner'] = get_userdata($backupData['owner']);
130 // Return backups data back.
131 return $backupData;
132 }
133
134 /**
135 * put your comment there...
136 *
137 * @param mixed $id
138 */
139 public function delete($id) {
140 // Get backup record key.
141 $backupKey['id'] = $id;
142 // Delete backup record.
143 $this->backups->delete($backupKey);
144 // Delete backup blocks.
145 $this->deleteBackupBlocks($id);
146 }
147
148 /**
149 * put your comment there...
150 *
151 * @param mixed $backupId
152 * @param boolean Dont delete master blocks with backupId = NULL. This is very important in case $id param is null for any reason!
153 */
154 public function deleteBackupBlocks($backupId = null, $deleteMaster = false) {
155 if (!$backupId && !$deleteMaster) {
156 throw new Exception('Trying to delete master blocks while deleting normal backup');
157 }
158 // Blocks Table & Model..
159 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
160 require_once CJTOOLBOX_MODELS_PATH . '/blocks.php';
161 // Get blocks Table & Model instances.
162 $blocksTable = new CJTBlocksTable($this->dbDriver);
163 $blocksModel = new CJTBlocksModel();
164 // Get backup blocks Ids.
165 $blocks['fields'] = array('id');
166 $blocks['filters']['backupId'] = $backupId;
167 // Don't delete 'metabox' blocks as its not part of the backup anyway!
168 $blocks['filters']['types'] = array('block', 'revision');
169 // Query backup blocks.
170 $blocks = $blocksTable->get(null, $blocks['fields'], $blocks['filters']);
171 // Delete blocks using its Id.
172 $ids = array_keys($blocks);
173 $blocksModel->delete($ids);
174 // Delete code files.
175 $this->dbDriver->delete('DELETE FROM #__cjtoolbox_block_files where blockId IN (' . implode(',', $ids) . ');');
176 // In order for $this->processQueue to work
177 // we need to Merge db driver queues into the current
178 // local queue.
179 $this->dbDriver->merge($blocksModel->dbDriver());
180 }
181
182 /**
183 * put your comment there...
184 *
185 */
186 public function getAll() {
187 $backups = $this->backups->get('blocks');
188 // For every backup get owner data.
189 foreach ($backups as &$backup) {
190 $backup->owner = get_userdata($backup->owner);
191 }
192 return $backups;
193 }
194
195 /**
196 * put your comment there...
197 *
198 * @param mixed $backupId
199 */
200 public function restore($backupId) {
201 // Delete Current blocks.
202 $this->deleteBackupBlocks(null, true);
203 // Copy Backup Blocks.
204 $this->copyBackupBlocks($backupId, null);
205 }
206
207 /**
208 * put your comment there...
209 *
210 */
211 public function save() {
212 $this->dbDriver->processQueue();
213 }
214
215 } // End class.