GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch dev (953bb6)
by Liuta
01:57
created

Xcloner_Scheduler   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 422
Duplicated Lines 3.55 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 15
loc 422
rs 3.6
c 0
b 0
f 0
wmc 60
lcom 2
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A delete_schedule_by_id() 0 8 1
A deactivate_wp_cron_hooks() 0 10 2
B update_wp_cron_hooks() 4 24 6
A update_cron_hook() 9 17 3
A disable_single_cron() 0 19 1
A __construct() 0 11 1
A get_xcloner_container() 0 3 1
A set_xcloner_container() 0 3 1
A get_scheduler_list() 0 17 4
A get_next_run_schedule() 0 5 1
A get_schedule_by_id_object() 0 5 1
A get_schedule_by_id() 0 17 2
A update_hash() 0 15 1
A update_last_backup() 0 18 1
F _xcloner_scheduler_callback() 2 170 23
B xcloner_scheduler_callback() 0 27 8
A get_available_intervals() 0 15 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Xcloner_Scheduler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Xcloner_Scheduler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
class Xcloner_Scheduler {
4
5
	private $db;
6
	private $scheduler_table = "xcloner_scheduler";
7
8
	private $xcloner_remote_storage;
9
	private $archive_system;
10
	private $xcloner_database;
11
	private $xcloner_settings;
12
	private $logger;
13
	private $xcloner_file_system;
14
	private $xcloner_encryption;
15
16
	private $allowed_schedules = array( "hourly", "twicedaily", "daily", "weekly", "monthly" );
17
18
	/*public function __call($method, $args) {
19
		echo "$method is not defined";
20
	}*/
21
22
	public function __construct( Xcloner $xcloner_container ) {
23
		global $wpdb;
24
25
		$this->db          = $wpdb;
26
		$wpdb->show_errors = false;
27
28
		$this->xcloner_container = $xcloner_container;
0 ignored issues
show
Bug introduced by
The property xcloner_container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
		$this->xcloner_settings  = $xcloner_container->get_xcloner_settings();
0 ignored issues
show
Bug introduced by
The method get_xcloner_settings() does not seem to exist on object<Xcloner>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
		$this->scheduler_table = $this->db->prefix . $this->scheduler_table;
32
	}
33
34
	private function get_xcloner_container() {
35
		return $this->xcloner_container;
36
	}
37
38
	private function set_xcloner_container( Xcloner $container ) {
39
		$this->xcloner_container = $container;
40
	}
41
42
	public function get_scheduler_list( $return_only_enabled = 0 ) {
43
		$list = $this->db->get_results( "SELECT * FROM " . $this->scheduler_table );
44
45
		if ( $return_only_enabled ) {
46
			$new_list = array();
47
48
			foreach ( $list as $res ) {
49
				if ( $res->status ) {
50
					$res->next_run_time = wp_next_scheduled( 'xcloner_scheduler_' . $res->id, array( $res->id ) ) + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
51
					$new_list[]         = $res;
52
				}
53
			}
54
			$list = $new_list;
55
		}
56
57
		return $list;
58
	}
59
60
	public function get_next_run_schedule( $xcloner_file_system = "" ) {
0 ignored issues
show
Unused Code introduced by
The parameter $xcloner_file_system is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
		$list = $this->get_scheduler_list( $return_only_enabled = 1 );
62
63
		return $list;
64
	}
65
66
	public function get_schedule_by_id_object( $id ) {
67
		$data = $this->db->get_row( "SELECT * FROM " . $this->scheduler_table . " WHERE id=" . $id );
68
69
		return $data;
70
	}
71
72
	public function get_schedule_by_id( $id ) {
73
		$data = $this->db->get_row( "SELECT * FROM " . $this->scheduler_table . " WHERE id=" . $id, ARRAY_A );
74
75
		if ( ! $data ) {
76
			return false;
77
		}
78
79
		$params = json_decode( $data['params'] );
80
81
		//print_r($params);
82
		$data['params']         = "";
83
		$data['backup_params']  = $params->backup_params;
84
		$data['table_params']   = json_encode( $params->database );
85
		$data['excluded_files'] = json_encode( $params->excluded_files );
86
87
		return $data;
88
	}
89
90
	public function delete_schedule_by_id( $id ) {
91
		$hook = 'xcloner_scheduler_' . $id;
92
		wp_clear_scheduled_hook( $hook, array( $id ) );
93
94
		$data = $this->db->delete( $this->scheduler_table, array( 'id' => $id ) );
95
96
		return $data;
97
	}
98
99
	public function deactivate_wp_cron_hooks() {
100
		$list = $this->get_scheduler_list();
101
102
		foreach ( $list as $schedule ) {
103
			$hook = 'xcloner_scheduler_' . $schedule->id;
104
105
			$timestamp = wp_next_scheduled( $hook, array( $schedule->id ) );
106
			wp_unschedule_event( $timestamp, $hook, array( $schedule->id ) );
107
		}
108
	}
109
110
	public function update_wp_cron_hooks() {
111
		$list = $this->get_scheduler_list();
112
113
		foreach ( $list as $schedule ) {
114
			$hook = 'xcloner_scheduler_' . $schedule->id;
115
116
			//adding the xcloner_scheduler hook with xcloner_scheduler_callback callback
117
			add_action( $hook, array( $this, 'xcloner_scheduler_callback' ), 10, 1 );
118
119
			if ( ! wp_next_scheduled( $hook, array( $schedule->id ) ) and $schedule->status ) {
120
121 View Code Duplication
				if ( $schedule->recurrence == "single" ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
					wp_schedule_single_event( strtotime( $schedule->start_at ), $hook, array( $schedule->id ) );
123
				} else {
124
					wp_schedule_event( strtotime( $schedule->start_at ), $schedule->recurrence, $hook, array( $schedule->id ) );
125
				}
126
127
			} elseif ( ! $schedule->status ) {
128
				$timestamp = wp_next_scheduled( $hook, array( $schedule->id ) );
129
				wp_unschedule_event( $timestamp, $hook, array( $schedule->id ) );
130
			}
131
		}
132
133
	}
134
135
	public function update_cron_hook( $id ) {
136
		$schedule = $this->get_schedule_by_id_object( $id );
137
		$hook     = 'xcloner_scheduler_' . $schedule->id;
138
139
		$timestamp = wp_next_scheduled( $hook, array( $schedule->id ) );
140
		wp_unschedule_event( $timestamp, $hook, array( $schedule->id ) );
141
142 View Code Duplication
		if ( $schedule->status ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
144
			if ( $schedule->recurrence == "single" ) {
145
				wp_schedule_single_event( strtotime( $schedule->start_at ), $hook, array( $schedule->id ) );
146
			} else {
147
				wp_schedule_event( strtotime( $schedule->start_at ), $schedule->recurrence, $hook, array( $schedule->id ) );
148
			}
149
150
		}
151
	}
152
153
	public function disable_single_cron( $schedule_id ) {
154
		$hook      = 'xcloner_scheduler_' . $schedule_id;
155
		$timestamp = wp_next_scheduled( $hook, array( $schedule_id ) );
156
		wp_unschedule_event( $timestamp, $hook, array( $schedule_id ) );
157
158
		$schedule['status'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$schedule was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schedule = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
159
160
		$update = $this->db->update(
161
			$this->scheduler_table,
162
			$schedule,
163
			array( 'id' => $schedule_id ),
164
			array(
165
				'%s',
166
				'%s'
167
			)
168
		);
169
170
		return $update;
171
	}
172
173
	public function update_hash( $schedule_id, $hash ) {
174
		$schedule['hash'] = $hash;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$schedule was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schedule = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
175
176
		$update = $this->db->update(
177
			$this->scheduler_table,
178
			$schedule,
179
			array( 'id' => $schedule_id ),
180
			array(
181
				'%s',
182
				'%s'
183
			)
184
		);
185
186
		return $update;
187
	}
188
189
	public function update_last_backup( $schedule_id, $last_backup ) {
190
191
	    $this->logger->info(sprintf('Updating last backup %s for schedule id #%s', $last_backup, $schedule_id));
192
193
		$schedule['last_backup'] = $last_backup;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$schedule was never initialized. Although not strictly required by PHP, it is generally a good practice to add $schedule = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
194
195
		$update = $this->db->update(
196
			$this->scheduler_table,
197
			$schedule,
198
			array( 'id' => $schedule_id ),
199
			array(
200
				'%s',
201
				'%s'
202
			)
203
		);
204
205
		return $update;
206
	}
207
208
	private function _xcloner_scheduler_callback( $id, $schedule ) {
209
		set_time_limit( 0 );
210
211
		$xcloner = new XCloner();
212
		$xcloner->init();
0 ignored issues
show
Bug introduced by
The method init() does not seem to exist on object<Xcloner>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
		$this->set_xcloner_container( $xcloner );
214
215
		#$hash = $this->xcloner_settings->get_hash();
216
		#$this->get_xcloner_container()->get_xcloner_settings()->set_hash($hash);
217
218
		//$this->xcloner_settings 		= $this->get_xcloner_container()->get_xcloner_settings();		
219
		$this->xcloner_file_system    = $this->get_xcloner_container()->get_xcloner_filesystem();
220
		$this->xcloner_encryption    = $this->get_xcloner_container()->get_xcloner_encryption();
221
		$this->xcloner_database       = $this->get_xcloner_container()->get_xcloner_database();
222
		$this->archive_system         = $this->get_xcloner_container()->get_archive_system();
223
		$this->logger                 = $this->get_xcloner_container()->get_xcloner_logger()->withName( "xcloner_scheduler" );
224
		$this->xcloner_remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage();
225
226
		$this->logger->info( sprintf( "New schedule hash is %s", $this->xcloner_settings->get_hash() ) );
227
228
		if ( isset( $schedule['backup_params']->diff_start_date ) && $schedule['backup_params']->diff_start_date ) {
229
			$this->xcloner_file_system->set_diff_timestamp_start( $schedule['backup_params']->diff_start_date );
230
		}
231
232
		if ( $schedule['recurrence'] == "single" ) {
233
			$this->disable_single_cron( $schedule['id'] );
234
		}
235
236
		if ( ! $schedule ) {
237
			$this->logger->info( sprintf( "Could not load schedule with id'%s'", $id ), array( "CRON" ) );
238
239
			return;
240
		}
241
242
		//echo $this->get_xcloner_container()->get_xcloner_settings()->get_hash(); exit;
243
244
		$this->update_hash( $schedule['id'], $this->xcloner_settings->get_hash() );
245
246
		$this->logger->info( sprintf( "Starting cron schedule '%s'", $schedule['name'] ), array( "CRON" ) );
247
248
		$this->xcloner_file_system->set_excluded_files( json_decode( $schedule['excluded_files'] ) );
249
250
		$init     = 1;
251
		$continue = 1;
252
253
		while ( $continue ) {
254
			$continue = $this->xcloner_file_system->start_file_recursion( $init );
255
256
			$init = 0;
257
		}
258
259
		$this->logger->info( sprintf( "File scan finished" ), array( "CRON" ) );
260
261
		$this->logger->info( sprintf( "Starting the database backup" ), array( "CRON" ) );
262
263
		$init               = 1;
264
		$return['finished'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
265
266
		while ( ! $return['finished'] ) {
267
			$return = $this->xcloner_database->start_database_recursion( (array) json_decode( $schedule['table_params'] ), $return, $init );
268
			$init   = 0;
269
		}
270
271
		$this->logger->info( sprintf( "Database backup done" ), array( "CRON" ) );
272
273
		$this->logger->info( sprintf( "Starting file archive process" ), array( "CRON" ) );
274
275
		$init               = 0;
276
		$return['finished'] = 0;
277
		$return['extra']    = array();
278
279
		while ( ! $return['finished'] ) {
280
			$return = $this->archive_system->start_incremental_backup( (array) $schedule['backup_params'], $return['extra'], $init );
281
			$init   = 0;
282
		}
283
		$this->logger->info( sprintf( "File archive process FINISHED." ), array( "CRON" ) );
284
285
        //getting the last backup archive file
286
        $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension();
287 View Code Duplication
		if ( $this->xcloner_file_system->is_part( $this->archive_system->get_archive_name_with_extension() ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
			$return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart();
289
		}
290
291
		//Updating schedule last backup archive
292
        $this->update_last_backup( $schedule['id'], $return['extra']['backup_parent'] );
293
294
		//Encrypting the backup archive
295
        $return_encrypted['finished'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return_encrypted was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return_encrypted = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
296
        $return_encrypted['start'] = 0;
297
        $return_encrypted['iv'] = '';
298
        $return_encrypted['target_file'] = '';
299
        $part = 0;
300
        $backup_parts = array();
301
302
        if( $schedule['backup_params']->backup_encrypt){
303
            $this->logger->info( sprintf( "Encrypting backup archive %s.", $return['extra']['backup_parent'] ), array( "CRON" ) );
304
305
            $backup_file = $return['extra']['backup_parent'];
306
307
            if ($this->xcloner_file_system->is_multipart($return['extra']['backup_parent'])) {
308
                $backup_parts = $this->xcloner_file_system->get_multipart_files($return['extra']['backup_parent']);
309
                $backup_file = $backup_parts[$part];
310
            }
311
312
            while ( ! $return_encrypted['finished'] ) {
313
                $return_encrypted = $this->xcloner_encryption->encrypt_file(
314
                                            $backup_file,
315
                                            "",
316
                                            "",
317
                                            "",
318
                                            "",
319
                                            true,
320
                                            true
321
                );
322
323
                if($return_encrypted['finished']) {
324
                    ++$part;
325
326
                    if ($part < sizeof($backup_parts)) {
327
                        $return_encrypted['finished'] = 0;
328
                        $backup_file = $backup_parts[$part];
329
                    }
330
                }
331
            }
332
        }
333
334
        //Sending backup to remote storage
335
		if ( isset( $schedule['remote_storage'] ) && $schedule['remote_storage'] && array_key_exists( $schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages() ) ) {
336
			$backup_file = $return['extra']['backup_parent'];
337
338
			$this->logger->info( sprintf( "Transferring backup to remote storage %s", strtoupper( $schedule['remote_storage'] ) ), array( "CRON" ) );
339
340
			if ( method_exists( $this->xcloner_remote_storage, "upload_backup_to_storage" ) ) {
341
				call_user_func_array( array(
342
					$this->xcloner_remote_storage,
343
					"upload_backup_to_storage"
344
				), array( $backup_file, $schedule['remote_storage'] ) );
345
			}
346
		}
347
348
		//Sending email notification
349
		if ( isset( $schedule['backup_params']->email_notification ) and $to = $schedule['backup_params']->email_notification ) {
350
			try {
351
				$from                      = "";
352
				$additional['lines_total'] = $return['extra']['lines_total'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$additional was never initialized. Although not strictly required by PHP, it is generally a good practice to add $additional = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
353
				$subject                   = sprintf( __( "%s - new backup generated %s" ), $schedule['name'], $return['extra']['backup_parent'] );
354
355
				$this->archive_system->send_notification( $to, $from, $subject, $return['extra']['backup_parent'], $schedule, "", $additional );
356
357
			} catch ( Exception $e ) {
358
				$this->logger->error( $e->getMessage() );
359
			}
360
		}
361
362
		//CHECK IF WE SHOULD DELETE BACKUP AFTER REMOTE TRANSFER IS DONE
363
		if ( $schedule['remote_storage'] && $this->xcloner_settings->get_xcloner_option( 'xcloner_cleanup_delete_after_remote_transfer' ) ) {
364
			$this->logger->info( sprintf( "Deleting %s from local storage matching rule xcloner_cleanup_delete_after_remote_transfer", $return['extra']['backup_parent'] ) );
365
			$this->xcloner_file_system->delete_backup_by_name( $return['extra']['backup_parent'] );
366
367
		}
368
369
		//Removing the tmp filesystem used for backup
370
		$this->xcloner_file_system->remove_tmp_filesystem();
371
372
		//Backup Storage Cleanup
373
		$this->xcloner_file_system->backup_storage_cleanup();
374
375
		//Filesystem Cleanup
376
		$this->xcloner_file_system->cleanup_tmp_directories();
377
	}
378
379
	public function xcloner_scheduler_callback( $id, $schedule = "" ) {
380
		if ( $id ) {
381
			$schedule = $this->get_schedule_by_id( $id );
382
		}
383
384
		try {
385
            if( get_option('xcloner_disable_email_notification') ) {
386
                //we disable email notifications
387
                $schedule['backup_params']->email_notification = "";
388
            }
389
			$this->_xcloner_scheduler_callback( $id, $schedule );
390
391
		} catch ( Exception $e ) {
392
393
			//send email to site admin if email notification is not set in the scheduler
394
			if ( ! isset( $schedule['backup_params']->email_notification ) || ! $schedule['backup_params']->email_notification ) {
395
				$schedule['backup_params']->email_notification = get_option( 'admin_email' );
396
			}
397
398
			if ( isset( $schedule['backup_params']->email_notification ) && $to = $schedule['backup_params']->email_notification ) {
399
				$from = "";
400
				$this->archive_system->send_notification( $to, $from, $schedule['name'] . " - backup error", "", "", $e->getMessage() );
401
			}
402
403
		}
404
405
	}
406
407
	public function get_available_intervals() {
408
		$schedules     = wp_get_schedules();
409
		$new_schedules = array();
410
411
		foreach ( $schedules as $key => $row ) {
412
			if ( in_array( $key, $this->allowed_schedules ) ) {
413
				$new_schedules[ $key ] = $row;
414
				$intervals[ $key ]     = $row['interval'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$intervals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $intervals = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
415
			}
416
		}
417
418
		array_multisort( $intervals, SORT_ASC, $new_schedules );
419
420
		return $new_schedules;
421
	}
422
423
424
}
425