PluginProbe
CSS & JavaScript Toolbox / 0.8
CSS & JavaScript Toolbox v0.8
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 / modules / tools / tools.php

tools.php in CSS & JavaScript Toolbox 0.8, at modules/tools/tools.php

226 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version $Id ; v0.3e.php 16:33:02 07:02:2012 Ahmed Said ;
4 */
5
6 /**
7 * Provide error Blocks CODE detection and correction
8 * for the old version plugin.
9 *
10 * @author Ahmed Said
11 */
12 class CJTTools extends CJTModuleBase{
13
14 /**
15 * Backups Wordpress option name.
16 */
17 const BACKUPS_OPTION_NAME = 'cjtoolbox_tools_backups';
18
19 /**
20 * Allowed ajax action names.
21 *
22 * @var array
23 */
24 private static $actions = array(
25 'api_getBackupInfo',
26 'api_backup',
27 );
28
29 /**
30 * Create CJTV03Error object.
31 *
32 * @param array Modul info struct.
33 * @return void
34 */
35 public function __construct($moduleInfo) {
36 parent::__construct($moduleInfo, true);
37 add_action('cjt_upgraded', array($this, 'pluginUpgrade'));
38 }
39
40 /**
41 * Override cssJSToolbox::admin_print_scripts method.
42 */
43 public function admin_print_scripts() {
44 // Enqueue cssJSToolbox scripts.
45 $this->cjToolbox->admin_print_scripts();
46 // Enquque additional scripts.
47 wp_enqueue_script(CJTOOLBOX_NAME . '-tools-tools', $this->getJSFileURL('tools.js'));
48 $strings = array(
49 'backupConfirm' => __('Content change has been detected.') . "\n\n" .
50 __('It seems that some changes made in the blocks code.') . "\n\n" .
51 __('This changes won\'t be saved in the backup.') . "\n\n" .
52 __('To save these changes click "Save Changes" and then click backup.') . "\n\n" .
53 __('Would you to discard these changes from the backup?'),
54 'couldNotBackup' => __('Could not backup the database, please try again.'),
55 'serverNotResponsing' => __('Server not responding!!! Please try again.'),
56 'noBackupAvailable' => __('No backup available!!!')
57 );
58 wp_localize_script(CJTOOLBOX_NAME . '-tools-tools', 'CJTToolsLocalization', $strings);
59 }
60
61 /**
62 * Override cssJSToolbox::ajax_request_template method.
63 */
64 public function ajax_request_template() {
65 // Make sure the requested template is belongs to this module.
66 $templateName = $_GET['name'];
67 $templateNameComponents = array();
68 if (preg_match('/tools_(.+)/', $templateName, $templateNameComponents)) {
69 // Remove module name from template name.
70 $_GET['name'] = $templateNameComponents[1];
71 $this->cjToolbox->ajax_request_template($this->getPath('views'), array('tools' => $this));
72 }
73 else {
74 $this->cjToolbox->ajax_request_template();
75 }
76 }
77
78 /**
79 * Override cssJSToolbox::admin_print_scripts method.
80 */
81 public function admin_print_styles() {
82 // Enqueue cssJSToolbox scripts.
83 $this->cjToolbox->admin_print_styles();
84 // Enquque additional scripts.
85 wp_enqueue_style(CJTOOLBOX_NAME . '-tools-tools', $this->getCSSFileURL('style.css'));
86 }
87
88 /**
89 * Dispatch ajax request and prepare the response.
90 *
91 * @return void
92 */
93 public function api() {
94 // Check if pemitted.
95 check_ajax_referer('cjtoolbox-admin', 'security');
96 // prepare the response in case dispatching failed.
97 $response = array(
98 'code' => 0x00,
99 'message' => 'Dispatching',
100 'response' => array(),
101 );
102 // Get method name from the action name.
103 $action = current_filter();
104 $actionComponents = array();
105 if (preg_match('/_tools_(\w+)$/', $action, $actionComponents)) {
106 $methodName = "api_{$actionComponents[1]}";
107 // Restrict access.
108 if (in_array($methodName, self::$actions)) {
109 // Dispatch the call.
110 $response['code'] = 0x01;
111 $response['response'] = $this->$methodName($_POST);
112 }
113 else {
114 $response['code'] = 0xFF;
115 }
116 }
117 header('Content-Type: text/plain');
118 $responseJSON = json_encode($response);
119 die($responseJSON);
120 }
121
122 /**
123 * Ajax method for backup the database.
124 *
125 * @return array Response array.
126 */
127 protected function api_backup($name = null, $author = null) {
128 global $current_user;
129 // When called directely/internally
130 // it may take the author as paramater.
131 $author = $author ? $author : $current_user->display_name;
132 $name = $name ? $name : trim($_GET['backupName']);
133 $backup = array(
134 'time' => date('h:i:s Y:M:d', time()),
135 'name' => $name,
136 'author' => $author,
137 'data' => $this->cjToolbox->getData(),
138 );
139 // Wrap in array. We may alow multiple backups in the future.
140 update_option(self::BACKUPS_OPTION_NAME, array($backup));
141 // Response to the rquest.
142 $response = array(
143 'backName' => $_GET['backupName'],
144 'success' => true,
145 );
146 return $response;
147 }
148
149 /**
150 * Ajax method for get backup info.
151 *
152 * @return array Response array.
153 */
154 protected function api_getBackupInfo() {
155 $backups = $this->getAvailableBackups();
156 $response = array('has' => !empty($backups));
157 return $response;
158 }
159
160 /**
161 * Display tools bar.
162 * Callback for cjt_post_body_before_blocks.
163 */
164 public function displayToolBar() {
165 $toolbar = $this->getView('tools-bar', array());
166 echo $toolbar;
167 }
168
169 /**
170 * Get available backups.
171 *
172 */
173 public function getAvailableBackups() {
174 $backups = get_option(self::BACKUPS_OPTION_NAME);
175 return is_array($backups) ? $backups : array();
176 }
177
178 /**
179 * Get module instance.
180 *
181 * @see __construct for parameters info.
182 * @return CJTVTools Module instance.
183 */
184 public static function getInstance($moduleInfo) {
185 return new CJTTools($moduleInfo);
186 }
187
188 /**
189 * Callback for cjt_upgrade hook.
190 */
191 public function pluginUpgrade() {
192 // Create a backup when the Plugin upgraded.
193 $this->api_backup('Upgrading', 'system');
194 }
195
196 /**
197 * Restoring blocks from the last backup.
198 *
199 * Callback for cjt_blocks_data filter.
200 *
201 * @return array Blocks data.
202 */
203 public function restore($blocksData) {
204 $backups = $this->getAvailableBackups();
205 $blocksData = $backups[0]['data']; // For now we've only one backup.
206 return $blocksData;
207 }
208
209 /**
210 * Override cssJSToolbox::start_plugin method.
211 */
212 public function start_plugin() {
213 add_action('cjt_post_body_before_blocks', array($this, 'displayToolBar'));
214 add_action('wp_ajax_cjtoolbox_tools_getBackupInfo', array($this, 'api'));
215 add_action('wp_ajax_cjtoolbox_tools_backup', array($this, 'api'));
216 // bind restore hook.
217 if (isset($_GET['restore']) && $_GET['restore']) {
218 add_filter('cjt_blocks_data', array($this, 'restore'));
219 }
220 $this->cjToolbox->start_plugin();
221 }
222
223 } // End class.
224
225 // Put the module in action.
226 return CJTTools::getInstance($module);