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
css-javascript-toolbox / models / blocks-backups.php

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

208 lines 5.7 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 // Blocks Table & Model..
52 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
53 require_once CJTOOLBOX_TABLES_PATH . '/block-pins.php';
54 require_once CJTOOLBOX_MODELS_PATH . '/blocks.php';
55 // Get blocks Table & Model instances.
56 $blocksTable = new CJTBlocksTable($this->dbDriver);
57 $blockPinsTable = new CJTBlockPinsTable($this->dbDriver);
58 $blocksModel = new CJTBlocksModel();
59 // Get source backup blocks.
60 $blocks['fields'] = array('*');
61 $blocks['filters']['backupId'] = $srcBackupId;
62 $blocks['filters']['types'] = array('block', 'revision');
63 // Its important to get revision blocks at the end
64 // to create blocks id map.
65 $blocks['orderBy'] = array('type');
66 $blocks = $blocksModel->getBlocks(null, $blocks['filters'], $blocks['fields'], OBJECT_K, $blocks['orderby']);
67 // Prepare vars before copying.
68 $desBlockId = $blocksTable->getNextId();
69 // For every block give new Id and insert block and pins data.
70 $blocksMap = array();
71 foreach ($blocks as $id => $block) {
72 // Change block Id & backupId.
73 $block->id = $desBlockId++;
74 $block->backupId = $desBackupId;
75 // Change revision "parentId" to the new block id.
76 if ($block->type == 'block') {
77 // Map the old id to the new one.
78 $blocksMap[$id] = $block->id;
79 }
80 else if ($block->type = 'revision') {
81 // Exclude revision blocks for other types than 'block' type (e.g metabox revisions).
82 if (!isset($blocksMap[$block->parent])) {
83 continue;
84 }
85 /*
86 * Use the new id instead of the old one.
87 * Get the parent block id from the map.
88 */
89 else {
90 $block->parent = $blocksMap[$block->parent];
91 }
92 }
93 // Cast stdClass to array.
94 $block = (array) $block;
95 // Insert block.
96 $blockData = array_intersect_key($block, $blocksTable->getFields());
97 $blocksTable->insert($blockData);
98 // Insert block Pins.
99 $pinsData = array_intersect_key($block, array_flip(array('pages', 'posts', 'categories')));
100 $blockPinsTable->insert($block['id'], $pinsData);
101 }
102 }
103
104 /**
105 * put your comment there...
106 *
107 * @param int|string $backupData
108 */
109 public function create($backupData) {
110 // Prepare backup data.
111 $backupData['owner'] = get_current_user_id();
112 $backupData['created'] = current_time('mysql');
113 $backupData['type'] = 'blocks';
114 $backupData['id'] = $this->backups->getNextId();
115 // Don't do anything is any statement fails!
116 $this->dbDriver->startTransaction();
117 // Insert new backup id.
118 $this->backups->insert($backupData);
119 // Copy Blocks to newly created backup.
120 $this->copyBackupBlocks(null, $backupData['id']);
121 // Commit Changes.
122 $this->dbDriver->commit();
123 // Get Owner User object for the returned object.
124 $backupData['owner'] = get_userdata($backupData['owner']);
125 // Return backups data back.
126 return $backupData;
127 }
128
129 /**
130 * put your comment there...
131 *
132 * @param mixed $id
133 */
134 public function delete($id) {
135 // Get backup record key.
136 $backupKey['id'] = $id;
137 // Delete backup record.
138 $this->backups->delete($backupKey);
139 // Delete backup blocks.
140 $this->deleteBackupBlocks($id);
141 }
142
143 /**
144 * put your comment there...
145 *
146 * @param mixed $backupId
147 * @param boolean Dont delete master blocks with backupId = NULL. This is very important in case $id param is null for any reason!
148 */
149 public function deleteBackupBlocks($backupId = null, $deleteMaster = false) {
150 if (!$backupId && !$deleteMaster) {
151 throw new Exception('Trying to delete master blocks while deleting normal backup');
152 }
153 // Blocks Table & Model..
154 require_once CJTOOLBOX_TABLES_PATH . '/blocks.php';
155 require_once CJTOOLBOX_MODELS_PATH . '/blocks.php';
156 // Get blocks Table & Model instances.
157 $blocksTable = new CJTBlocksTable($this->dbDriver);
158 $blocksModel = new CJTBlocksModel();
159 // Get backup blocks Ids.
160 $blocks['fields'] = array('id');
161 $blocks['filters']['backupId'] = $backupId;
162 // Don't delete 'metabox' blocks as its not part of the backup anyway!
163 $blocks['filters']['types'] = array('block', 'revision');
164 // Query backup blocks.
165 $blocks = $blocksTable->get(null, $blocks['fields'], $blocks['filters']);
166 // Delete blocks using its Id.
167 $ids = array_keys($blocks);
168 $blocksModel->delete($ids);
169 // In order for $this->processQueue to work
170 // we need to Merge db driver queues into the current
171 // local queue.
172 $this->dbDriver->merge($blocksModel->dbDriver());
173 }
174
175 /**
176 * put your comment there...
177 *
178 */
179 public function getAll() {
180 $backups = $this->backups->get('blocks');
181 // For every backup get owner data.
182 foreach ($backups as &$backup) {
183 $backup->owner = get_userdata($backup->owner);
184 }
185 return $backups;
186 }
187
188 /**
189 * put your comment there...
190 *
191 * @param mixed $backupId
192 */
193 public function restore($backupId) {
194 // Delete Current blocks.
195 $this->deleteBackupBlocks(null, true);
196 // Copy Backup Blocks.
197 $this->copyBackupBlocks($backupId, null);
198 }
199
200 /**
201 * put your comment there...
202 *
203 */
204 public function save() {
205 $this->dbDriver->processQueue();
206 }
207
208 } // End class.