@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class Xcloner_Scheduler{ |
|
| 3 | +class Xcloner_Scheduler { |
|
| 4 | 4 | |
| 5 | 5 | private $db; |
| 6 | 6 | private $scheduler_table = "xcloner_scheduler"; |
@@ -20,23 +20,23 @@ discard block |
||
| 20 | 20 | global $wpdb; |
| 21 | 21 | $this->db = $wpdb; |
| 22 | 22 | |
| 23 | - $wpdb->show_errors = false; |
|
| 23 | + $wpdb->show_errors = false; |
|
| 24 | 24 | |
| 25 | 25 | $this->xcloner_settings = new Xcloner_Settings(); |
| 26 | 26 | |
| 27 | 27 | $this->scheduler_table = $this->db->prefix.$this->scheduler_table; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - public function get_scheduler_list($return_only_enabled = 0 ) |
|
| 30 | + public function get_scheduler_list($return_only_enabled = 0) |
|
| 31 | 31 | { |
| 32 | 32 | $list = $this->db->get_results("SELECT * FROM ".$this->scheduler_table); |
| 33 | 33 | |
| 34 | - if($return_only_enabled) |
|
| 34 | + if ($return_only_enabled) |
|
| 35 | 35 | { |
| 36 | - $new_list= array(); |
|
| 36 | + $new_list = array(); |
|
| 37 | 37 | |
| 38 | - foreach($list as $res) |
|
| 39 | - if($res->status) |
|
| 38 | + foreach ($list as $res) |
|
| 39 | + if ($res->status) |
|
| 40 | 40 | { |
| 41 | 41 | $res->next_run_time = wp_next_scheduled('xcloner_scheduler_'.$res->id, array($res->id)); |
| 42 | 42 | $new_list[] = $res; |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | { |
| 65 | 65 | $data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id, ARRAY_A); |
| 66 | 66 | |
| 67 | - if(!$data) |
|
| 67 | + if (!$data) |
|
| 68 | 68 | return false; |
| 69 | 69 | |
| 70 | 70 | $params = json_decode($data['params']); |
@@ -81,10 +81,10 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | public function delete_schedule_by_id($id) |
| 83 | 83 | { |
| 84 | - $hook = 'xcloner_scheduler_'.$id; |
|
| 85 | - wp_clear_scheduled_hook( $hook, array($id) ); |
|
| 84 | + $hook = 'xcloner_scheduler_'.$id; |
|
| 85 | + wp_clear_scheduled_hook($hook, array($id)); |
|
| 86 | 86 | |
| 87 | - $data = $this->db->delete( $this->scheduler_table , array( 'id' => $id ) ); |
|
| 87 | + $data = $this->db->delete($this->scheduler_table, array('id' => $id)); |
|
| 88 | 88 | |
| 89 | 89 | return $data; |
| 90 | 90 | } |
@@ -93,12 +93,12 @@ discard block |
||
| 93 | 93 | { |
| 94 | 94 | $list = $this->get_scheduler_list(); |
| 95 | 95 | |
| 96 | - foreach($list as $schedule) |
|
| 96 | + foreach ($list as $schedule) |
|
| 97 | 97 | { |
| 98 | - $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 98 | + $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 99 | 99 | |
| 100 | - $timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
| 101 | - wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
| 100 | + $timestamp = wp_next_scheduled($hook, array($schedule->id)); |
|
| 101 | + wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
|
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
@@ -106,24 +106,24 @@ discard block |
||
| 106 | 106 | { |
| 107 | 107 | $list = $this->get_scheduler_list(); |
| 108 | 108 | |
| 109 | - foreach($list as $schedule) |
|
| 109 | + foreach ($list as $schedule) |
|
| 110 | 110 | { |
| 111 | - $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 111 | + $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 112 | 112 | |
| 113 | 113 | //adding the xcloner_scheduler hook with xcloner_scheduler_callback callback |
| 114 | - add_action( $hook, array($this, 'xcloner_scheduler_callback'), 10, 1 ); |
|
| 114 | + add_action($hook, array($this, 'xcloner_scheduler_callback'), 10, 1); |
|
| 115 | 115 | |
| 116 | - if ( ! wp_next_scheduled( $hook, array($schedule->id) ) and $schedule->status) { |
|
| 116 | + if (!wp_next_scheduled($hook, array($schedule->id)) and $schedule->status) { |
|
| 117 | 117 | |
| 118 | - if($schedule->recurrence == "single") |
|
| 119 | - wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 118 | + if ($schedule->recurrence == "single") |
|
| 119 | + wp_schedule_single_event(strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 120 | 120 | else |
| 121 | - wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
| 121 | + wp_schedule_event(strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id)); |
|
| 122 | 122 | |
| 123 | - }elseif(!$schedule->status) |
|
| 123 | + }elseif (!$schedule->status) |
|
| 124 | 124 | { |
| 125 | - $timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
| 126 | - wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
| 125 | + $timestamp = wp_next_scheduled($hook, array($schedule->id)); |
|
| 126 | + wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
|
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | public function update_cron_hook($id) |
| 133 | 133 | { |
| 134 | 134 | $schedule = $this->get_schedule_by_id_object($id); |
| 135 | - $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 135 | + $hook = 'xcloner_scheduler_'.$schedule->id; |
|
| 136 | 136 | |
| 137 | - $timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
|
| 138 | - wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
|
| 137 | + $timestamp = wp_next_scheduled($hook, array($schedule->id)); |
|
| 138 | + wp_unschedule_event($timestamp, $hook, array($schedule->id)); |
|
| 139 | 139 | |
| 140 | 140 | if ($schedule->status) { |
| 141 | 141 | |
| 142 | - if($schedule->recurrence == "single") |
|
| 143 | - wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 144 | - else{ |
|
| 145 | - wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
| 142 | + if ($schedule->recurrence == "single") |
|
| 143 | + wp_schedule_single_event(strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 144 | + else { |
|
| 145 | + wp_schedule_event(strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id)); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | } |
@@ -150,16 +150,16 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | public function disable_single_cron($schedule_id) |
| 152 | 152 | { |
| 153 | - $hook = 'xcloner_scheduler_'.$schedule_id; |
|
| 154 | - $timestamp = wp_next_scheduled( $hook , array($schedule_id) ); |
|
| 155 | - wp_unschedule_event( $timestamp, $hook, array($schedule_id) ); |
|
| 153 | + $hook = 'xcloner_scheduler_'.$schedule_id; |
|
| 154 | + $timestamp = wp_next_scheduled($hook, array($schedule_id)); |
|
| 155 | + wp_unschedule_event($timestamp, $hook, array($schedule_id)); |
|
| 156 | 156 | |
| 157 | 157 | $schedule['status'] = 0; |
| 158 | 158 | |
| 159 | 159 | $update = $this->db->update( |
| 160 | 160 | $this->scheduler_table, |
| 161 | 161 | $schedule, |
| 162 | - array( 'id' => $schedule_id ), |
|
| 162 | + array('id' => $schedule_id), |
|
| 163 | 163 | array( |
| 164 | 164 | '%s', |
| 165 | 165 | '%s' |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | $update = $this->db->update( |
| 176 | 176 | $this->scheduler_table, |
| 177 | 177 | $schedule, |
| 178 | - array( 'id' => $schedule_id ), |
|
| 178 | + array('id' => $schedule_id), |
|
| 179 | 179 | array( |
| 180 | 180 | '%s', |
| 181 | 181 | '%s' |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | $update = $this->db->update( |
| 192 | 192 | $this->scheduler_table, |
| 193 | 193 | $schedule, |
| 194 | - array( 'id' => $schedule_id ), |
|
| 194 | + array('id' => $schedule_id), |
|
| 195 | 195 | array( |
| 196 | 196 | '%s', |
| 197 | 197 | '%s' |
@@ -206,20 +206,20 @@ discard block |
||
| 206 | 206 | |
| 207 | 207 | $this->xcloner_settings->generate_new_hash(); |
| 208 | 208 | |
| 209 | - $this->xcloner_file_system = new Xcloner_File_System($this->xcloner_settings->get_hash()); |
|
| 210 | - $this->xcloner_database = new XCloner_Database($this->xcloner_settings->get_hash()); |
|
| 211 | - $this->archive_system = new Xcloner_Archive($this->xcloner_settings->get_hash()); |
|
| 212 | - $this->logger = new XCloner_Logger('xcloner_scheduler', $this->xcloner_settings->get_hash()); |
|
| 213 | - $this->xcloner_remote_storage = new Xcloner_Remote_Storage($this->xcloner_settings->get_hash()); |
|
| 209 | + $this->xcloner_file_system = new Xcloner_File_System($this->xcloner_settings->get_hash()); |
|
| 210 | + $this->xcloner_database = new XCloner_Database($this->xcloner_settings->get_hash()); |
|
| 211 | + $this->archive_system = new Xcloner_Archive($this->xcloner_settings->get_hash()); |
|
| 212 | + $this->logger = new XCloner_Logger('xcloner_scheduler', $this->xcloner_settings->get_hash()); |
|
| 213 | + $this->xcloner_remote_storage = new Xcloner_Remote_Storage($this->xcloner_settings->get_hash()); |
|
| 214 | 214 | |
| 215 | 215 | //$schedule = $this->get_schedule_by_id($id); |
| 216 | 216 | |
| 217 | - if($schedule['recurrence'] == "single") |
|
| 217 | + if ($schedule['recurrence'] == "single") |
|
| 218 | 218 | { |
| 219 | 219 | $this->disable_single_cron($schedule['id']); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - if(!$schedule) |
|
| 222 | + if (!$schedule) |
|
| 223 | 223 | { |
| 224 | 224 | $this->logger->info(sprintf("Could not load schedule with id'%s'", $id), array("CRON")); |
| 225 | 225 | return; |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | $init = 1; |
| 235 | 235 | $continue = 1; |
| 236 | 236 | |
| 237 | - while($continue) |
|
| 237 | + while ($continue) |
|
| 238 | 238 | { |
| 239 | 239 | $continue = $this->xcloner_file_system->start_file_recursion($init); |
| 240 | 240 | |
@@ -248,9 +248,9 @@ discard block |
||
| 248 | 248 | $init = 1; |
| 249 | 249 | $return['finished'] = 0; |
| 250 | 250 | |
| 251 | - while(!$return['finished']) |
|
| 251 | + while (!$return['finished']) |
|
| 252 | 252 | { |
| 253 | - $return = $this->xcloner_database->start_database_recursion((array)json_decode($schedule['table_params']), $return, $init); |
|
| 253 | + $return = $this->xcloner_database->start_database_recursion((array)json_decode($schedule['table_params']), $return, $init); |
|
| 254 | 254 | $init = 0; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | $return['finished'] = 0; |
| 263 | 263 | $return['extra'] = array(); |
| 264 | 264 | |
| 265 | - while(!$return['finished']) |
|
| 265 | + while (!$return['finished']) |
|
| 266 | 266 | { |
| 267 | 267 | $return = $this->archive_system->start_incremental_backup((array)$schedule['backup_params'], $return['extra'], $init); |
| 268 | 268 | $init = 0; |
@@ -271,28 +271,28 @@ discard block |
||
| 271 | 271 | |
| 272 | 272 | //getting the last backup archive file |
| 273 | 273 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension(); |
| 274 | - if($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) |
|
| 274 | + if ($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) |
|
| 275 | 275 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
| 276 | 276 | |
| 277 | 277 | $this->update_last_backup($schedule['id'], $return['extra']['backup_parent']); |
| 278 | 278 | |
| 279 | - if($schedule['remote_storage'] and array_key_exists($schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages())) |
|
| 279 | + if ($schedule['remote_storage'] and array_key_exists($schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages())) |
|
| 280 | 280 | { |
| 281 | 281 | $backup_file = $return['extra']['backup_parent']; |
| 282 | 282 | |
| 283 | 283 | $this->logger->info(sprintf("Transferring backup to remote storage %s", strtoupper($schedule['remote_storage'])), array("CRON")); |
| 284 | 284 | |
| 285 | - if(method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) |
|
| 285 | + if (method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) |
|
| 286 | 286 | call_user_func_array(array($this->xcloner_remote_storage, "upload_backup_to_storage"), array($backup_file, $schedule['remote_storage'])); |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | |
| 290 | - if(isset($schedule['backup_params']->email_notification) and $to=$schedule['backup_params']->email_notification) |
|
| 290 | + if (isset($schedule['backup_params']->email_notification) and $to = $schedule['backup_params']->email_notification) |
|
| 291 | 291 | { |
| 292 | - try{ |
|
| 292 | + try { |
|
| 293 | 293 | $from = "XCloner Schedule - ".$schedule['name']; |
| 294 | 294 | $this->archive_system->send_notification($to, $from, "", $return['extra']['backup_parent'], $schedule); |
| 295 | - }catch(Exception $e) |
|
| 295 | + }catch (Exception $e) |
|
| 296 | 296 | { |
| 297 | 297 | $this->logger->error($e->getMessage()); |
| 298 | 298 | } |
@@ -307,16 +307,16 @@ discard block |
||
| 307 | 307 | { |
| 308 | 308 | $schedule = $this->get_schedule_by_id($id); |
| 309 | 309 | |
| 310 | - try{ |
|
| 310 | + try { |
|
| 311 | 311 | |
| 312 | 312 | $this->__xcloner_scheduler_callback($id, $schedule); |
| 313 | 313 | |
| 314 | - }catch(Exception $e){ |
|
| 314 | + }catch (Exception $e) { |
|
| 315 | 315 | |
| 316 | - if(isset($schedule['backup_params']->email_notification) and $to=$schedule['backup_params']->email_notification) |
|
| 316 | + if (isset($schedule['backup_params']->email_notification) and $to = $schedule['backup_params']->email_notification) |
|
| 317 | 317 | { |
| 318 | 318 | $from = "XCloner Schedule - ".$schedule['name']; |
| 319 | - $this->archive_system->send_notification($to, $from, "Scheduled backup error","", "", $e->getMessage()); |
|
| 319 | + $this->archive_system->send_notification($to, $from, "Scheduled backup error", "", "", $e->getMessage()); |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | } |
@@ -35,10 +35,11 @@ discard block |
||
| 35 | 35 | { |
| 36 | 36 | $new_list= array(); |
| 37 | 37 | |
| 38 | - foreach($list as $res) |
|
| 39 | - if($res->status) |
|
| 38 | + foreach($list as $res) { |
|
| 39 | + if($res->status) |
|
| 40 | 40 | { |
| 41 | 41 | $res->next_run_time = wp_next_scheduled('xcloner_scheduler_'.$res->id, array($res->id)); |
| 42 | + } |
|
| 42 | 43 | $new_list[] = $res; |
| 43 | 44 | } |
| 44 | 45 | $list = $new_list; |
@@ -64,8 +65,9 @@ discard block |
||
| 64 | 65 | { |
| 65 | 66 | $data = $this->db->get_row("SELECT * FROM ".$this->scheduler_table." WHERE id=".$id, ARRAY_A); |
| 66 | 67 | |
| 67 | - if(!$data) |
|
| 68 | - return false; |
|
| 68 | + if(!$data) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | 72 | $params = json_decode($data['params']); |
| 71 | 73 | |
@@ -115,12 +117,13 @@ discard block |
||
| 115 | 117 | |
| 116 | 118 | if ( ! wp_next_scheduled( $hook, array($schedule->id) ) and $schedule->status) { |
| 117 | 119 | |
| 118 | - if($schedule->recurrence == "single") |
|
| 119 | - wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 120 | - else |
|
| 121 | - wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
| 120 | + if($schedule->recurrence == "single") { |
|
| 121 | + wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 122 | + } else { |
|
| 123 | + wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
|
| 124 | + } |
|
| 122 | 125 | |
| 123 | - }elseif(!$schedule->status) |
|
| 126 | + } elseif(!$schedule->status) |
|
| 124 | 127 | { |
| 125 | 128 | $timestamp = wp_next_scheduled( $hook , array($schedule->id) ); |
| 126 | 129 | wp_unschedule_event( $timestamp, $hook, array($schedule->id) ); |
@@ -139,9 +142,9 @@ discard block |
||
| 139 | 142 | |
| 140 | 143 | if ($schedule->status) { |
| 141 | 144 | |
| 142 | - if($schedule->recurrence == "single") |
|
| 143 | - wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 144 | - else{ |
|
| 145 | + if($schedule->recurrence == "single") { |
|
| 146 | + wp_schedule_single_event( strtotime($schedule->start_at), $hook, array($schedule->id)); |
|
| 147 | + } else{ |
|
| 145 | 148 | wp_schedule_event( strtotime($schedule->start_at), $schedule->recurrence, $hook, array($schedule->id) ); |
| 146 | 149 | } |
| 147 | 150 | |
@@ -271,8 +274,9 @@ discard block |
||
| 271 | 274 | |
| 272 | 275 | //getting the last backup archive file |
| 273 | 276 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension(); |
| 274 | - if($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) |
|
| 275 | - $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
|
| 277 | + if($this->xcloner_file_system->is_part($this->archive_system->get_archive_name_with_extension())) { |
|
| 278 | + $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
|
| 279 | + } |
|
| 276 | 280 | |
| 277 | 281 | $this->update_last_backup($schedule['id'], $return['extra']['backup_parent']); |
| 278 | 282 | |
@@ -282,8 +286,9 @@ discard block |
||
| 282 | 286 | |
| 283 | 287 | $this->logger->info(sprintf("Transferring backup to remote storage %s", strtoupper($schedule['remote_storage'])), array("CRON")); |
| 284 | 288 | |
| 285 | - if(method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) |
|
| 286 | - call_user_func_array(array($this->xcloner_remote_storage, "upload_backup_to_storage"), array($backup_file, $schedule['remote_storage'])); |
|
| 289 | + if(method_exists($this->xcloner_remote_storage, "upload_backup_to_storage")) { |
|
| 290 | + call_user_func_array(array($this->xcloner_remote_storage, "upload_backup_to_storage"), array($backup_file, $schedule['remote_storage'])); |
|
| 291 | + } |
|
| 287 | 292 | } |
| 288 | 293 | |
| 289 | 294 | |
@@ -292,7 +297,7 @@ discard block |
||
| 292 | 297 | try{ |
| 293 | 298 | $from = "XCloner Schedule - ".$schedule['name']; |
| 294 | 299 | $this->archive_system->send_notification($to, $from, "", $return['extra']['backup_parent'], $schedule); |
| 295 | - }catch(Exception $e) |
|
| 300 | + } catch(Exception $e) |
|
| 296 | 301 | { |
| 297 | 302 | $this->logger->error($e->getMessage()); |
| 298 | 303 | } |
@@ -311,7 +316,7 @@ discard block |
||
| 311 | 316 | |
| 312 | 317 | $this->__xcloner_scheduler_callback($id, $schedule); |
| 313 | 318 | |
| 314 | - }catch(Exception $e){ |
|
| 319 | + } catch(Exception $e){ |
|
| 315 | 320 | |
| 316 | 321 | if(isset($schedule['backup_params']->email_notification) and $to=$schedule['backup_params']->email_notification) |
| 317 | 322 | { |