Conditions | 54 |
Paths | > 20000 |
Total Lines | 285 |
Code Lines | 131 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
9 | function update() { |
||
10 | |||
11 | // Update from backUpWordPress 0.4.5 |
||
12 | if ( get_option( 'bkpwp_max_backups' ) ) { |
||
13 | |||
14 | // Carry over the custom path |
||
15 | if ( $legacy_path = get_option( 'bkpwppath' ) ) { |
||
16 | update_option( 'hmbkp_path', $legacy_path ); |
||
17 | } |
||
18 | |||
19 | // Options to remove |
||
20 | $legacy_options = array( |
||
21 | 'bkpwp_archive_types', |
||
22 | 'bkpwp_automail_from', |
||
23 | 'bkpwp_domain', |
||
24 | 'bkpwp_domain_path', |
||
25 | 'bkpwp_easy_mode', |
||
26 | 'bkpwp_excludelists', |
||
27 | 'bkpwp_install_user', |
||
28 | 'bkpwp_listmax_backups', |
||
29 | 'bkpwp_max_backups', |
||
30 | 'bkpwp_presets', |
||
31 | 'bkpwp_reccurrences', |
||
32 | 'bkpwp_schedules', |
||
33 | 'bkpwp_calculation', |
||
34 | 'bkpwppath', |
||
35 | 'bkpwp_status_config', |
||
36 | 'bkpwp_status', |
||
37 | ); |
||
38 | |||
39 | foreach ( $legacy_options as $option ) { |
||
40 | delete_option( $option ); |
||
41 | } |
||
42 | |||
43 | global $wp_roles; |
||
|
|||
44 | |||
45 | $wp_roles->remove_cap( 'administrator', 'manage_backups' ); |
||
46 | $wp_roles->remove_cap( 'administrator', 'download_backups' ); |
||
47 | |||
48 | wp_clear_scheduled_hook( 'bkpwp_schedule_bkpwp_hook' ); |
||
49 | |||
50 | } |
||
51 | |||
52 | // Version 1 to 2 |
||
53 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '2.0', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
54 | |||
55 | /** |
||
56 | * Setup a backwards compatible schedule |
||
57 | */ |
||
58 | $legacy_schedule = new Scheduled_Backup( 'backup' ); |
||
59 | |||
60 | // Backup type |
||
61 | if ( ( defined( 'HMBKP_FILES_ONLY' ) && HMBKP_FILES_ONLY ) || get_option( 'hmbkp_files_only' ) ) { |
||
62 | $legacy_schedule->set_type( 'file' ); |
||
63 | } elseif ( ( defined( 'HMBKP_DATABASE_ONLY' ) && HMBKP_DATABASE_ONLY ) || get_option( 'hmbkp_database_only' ) ) { |
||
64 | $legacy_schedule->set_type( 'database' ); |
||
65 | } else { |
||
66 | $legacy_schedule->set_type( 'complete' ); |
||
67 | } |
||
68 | |||
69 | // Daily schedule time |
||
70 | if ( defined( 'HMBKP_DAILY_SCHEDULE_TIME' ) && HMBKP_DAILY_SCHEDULE_TIME ) { |
||
71 | $legacy_schedule->set_schedule_start_time( strtotime( HMBKP_DAILY_SCHEDULE_TIME ) ); |
||
72 | } |
||
73 | |||
74 | // Backup schedule |
||
75 | $legacy_schedule->set_reoccurrence( get_option( 'hmbkp_schedule_frequency', 'daily' ) ); |
||
76 | |||
77 | // Automatic backups disabled? |
||
78 | if ( ( defined( 'HMBKP_DISABLE_AUTOMATIC_BACKUP' ) && HMBKP_DISABLE_AUTOMATIC_BACKUP ) || get_option( 'hmbkp_disable_automatic_backup' ) ) { |
||
79 | $legacy_schedule->set_reoccurrence( 'manually' ); |
||
80 | } |
||
81 | |||
82 | // Max backups |
||
83 | if ( defined( 'HMBKP_MAX_BACKUPS' ) && is_numeric( HMBKP_MAX_BACKUPS ) ) { |
||
84 | $legacy_schedule->set_max_backups( (int) HMBKP_MAX_BACKUPS ); |
||
85 | } else { |
||
86 | $legacy_schedule->set_max_backups( (int) get_option( 'hmbkp_max_backups', 10 ) ); |
||
87 | } |
||
88 | |||
89 | // Excludes |
||
90 | if ( get_option( 'hmbkp_excludes' ) ) { |
||
91 | $legacy_schedule->set_excludes( get_option( 'hmbkp_excludes' ) ); |
||
92 | } |
||
93 | |||
94 | // Backup email |
||
95 | if ( defined( 'HMBKP_EMAIL' ) && is_email( HMBKP_EMAIL ) ) { |
||
96 | $legacy_schedule->set_service_options( 'HMBKP_Email_Service', array( 'email' => HMBKP_EMAIL ) ); |
||
97 | } elseif ( is_email( get_option( 'hmbkp_email_address' ) ) ) { |
||
98 | $legacy_schedule->set_service_options( 'HMBKP_Email_Service', array( 'email' => get_option( 'hmbkp_email_address' ) ) ); |
||
99 | } |
||
100 | |||
101 | // Set the archive filename to what it used to be |
||
102 | $legacy_schedule->backup_filename = implode( '-', array( get_bloginfo( 'name' ), 'backup', current_time( 'Y-m-d-H-i-s' ) ) ) . '.zip'; |
||
103 | |||
104 | $legacy_schedule->save(); |
||
105 | |||
106 | $legacy_path = get_option( 'hmbkp_path' ); |
||
107 | |||
108 | if ( $legacy_path ) { |
||
109 | |||
110 | // Prepend 'backup-' to the beginning of any legacy backups so they are picked up by the legacy schedule |
||
111 | if ( $handle = opendir( $legacy_path ) ) { |
||
112 | while ( false !== ( $file = readdir( $handle ) ) ) { |
||
113 | if ( 'zip' === pathinfo( $file, PATHINFO_EXTENSION ) ) { |
||
114 | rename( trailingslashit( $legacy_path ) . $file, trailingslashit( $legacy_path ) . 'backup-' . $file ); |
||
115 | } |
||
116 | } |
||
117 | closedir( $handle ); |
||
118 | } |
||
119 | |||
120 | PATH::get_instance()->move_old_backups( $legacy_path ); |
||
121 | |||
122 | } |
||
123 | |||
124 | // Remove the legacy options |
||
125 | foreach ( array( 'hmbkp_database_only', 'hmbkp_files_only', 'hmbkp_max_backups', 'hmbkp_email_address', 'hmbkp_email', 'hmbkp_schedule_frequency', 'hmbkp_disable_automatic_backup' ) as $option_name ) { |
||
126 | delete_option( $option_name ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Update from 2.x to 3.0 |
||
131 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '2.0', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
132 | |||
133 | // Remove the plugin data cache |
||
134 | delete_transient( 'hmbkp_plugin_data' ); |
||
135 | |||
136 | } |
||
137 | |||
138 | // Update to 3.1 |
||
139 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '3.0', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
140 | |||
141 | // Remove the plugin data cache |
||
142 | delete_option( 'hmbkp_path' ); |
||
143 | delete_option( 'hmbkp_default_path' ); |
||
144 | |||
145 | } |
||
146 | |||
147 | // update to 3.1.4 |
||
148 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '3.1.4', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
149 | |||
150 | $old_option_names = array( |
||
151 | 'HM\BackUpWordPressDropbox\Dropbox_Service' => 'dropbox', |
||
152 | 'HMBKP_DX_Backup_Service' => 'dropbox', |
||
153 | 'HM\BackUpWordPressFTP\FTP_Backup_Service' => 'ftp', |
||
154 | 'HMBKP_FTP_Backup_Service' => 'ftp', |
||
155 | 'HM\BackUpWordPressGDrive\Google_Drive_BackUp' => 'google-drive', |
||
156 | 'HMBKP_GDV_Backup_Service' => 'google-drive', |
||
157 | 'HM\BackUpWordPressRackspace\RackSpace_BackUp' => 'rackspace-cloud', |
||
158 | 'HMBKP_RSC_Backup_Service' => 'rackspace-cloud', |
||
159 | 'HM\BackUpWordPressS3\S3_Backup' => 's3', |
||
160 | 'HMBKP_S3_Backup_Service' => 's3', |
||
161 | 'HM\BackUpWordPressWinAzure\WinAzure_Backup' => 'azure', |
||
162 | 'HMBKP_WAZ_Backup_Service' => 'azure', |
||
163 | 'HM\BackUpWordPress\Email_Service' => 'email', |
||
164 | ); |
||
165 | |||
166 | global $wpdb; |
||
167 | |||
168 | // Get all schedule options with a SELECT query and delete them. |
||
169 | $schedules = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", 'hmbkp_schedule_%' ) ); |
||
170 | |||
171 | if ( 0 < count( $schedules ) ) { |
||
172 | |||
173 | // Access each schedules settings to see if the addon settings names need to be be updated to the new naming convention which uses the service slug generated from the $name property. |
||
174 | foreach ( $schedules as $schedule_id ) { |
||
175 | |||
176 | // Load the settings for this schedule into an array |
||
177 | // so we can loop through the different service settings |
||
178 | $schedule_settings = get_option( $schedule_id ); |
||
179 | |||
180 | // Iterate over each schedule setting for this schedule and check its name against our array. |
||
181 | foreach ( $schedule_settings as $key => $val ) { |
||
182 | // Find the current element key in our control array and get its value. Set a new element in the settings array with the found value as its key. Aka rename the element key |
||
183 | if ( array_key_exists( $key, $old_option_names ) ) { |
||
184 | |||
185 | // move the value to our new key |
||
186 | $schedule_settings[ $old_option_names[ $key ] ] = $schedule_settings[ $key ]; |
||
187 | |||
188 | unset( $schedule_settings[ $key ] ); |
||
189 | |||
190 | } |
||
191 | } |
||
192 | |||
193 | // Save back to the DB |
||
194 | update_option( $schedule_id, $schedule_settings ); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | // Update to 3.1.5 |
||
200 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '3.1.5', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
201 | |||
202 | // Delete all transients |
||
203 | $transients = array( |
||
204 | 'hmbkp_plugin_data', |
||
205 | 'hmbkp_directory_filesizes', |
||
206 | 'hmbkp_directory_filesizes_running', |
||
207 | 'hmbkp_wp_cron_test_beacon', |
||
208 | 'hm_backdrop', |
||
209 | ); |
||
210 | |||
211 | array_map( 'delete_transient', $transients ); |
||
212 | |||
213 | // Clear duplicate schedules on multisite |
||
214 | if ( is_multisite() ) { |
||
215 | |||
216 | // get current blogs from DB |
||
217 | $blogs = wp_get_sites(); |
||
218 | |||
219 | foreach ( $blogs as $blog ) { |
||
220 | |||
221 | switch_to_blog( get_current_blog_id() ); |
||
222 | |||
223 | if ( is_main_site( get_current_blog_id() ) ) { |
||
224 | continue; |
||
225 | } |
||
226 | |||
227 | global $wpdb; |
||
228 | |||
229 | // Get the schedule options |
||
230 | $schedules = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", 'hmbkp_schedule_%' ) ); |
||
231 | |||
232 | // clear schedules |
||
233 | foreach ( array_map( function ( $item ) { |
||
234 | return ltrim( $item, 'hmbkp_schedule_' ); |
||
235 | }, $schedules ) as $item ) { |
||
236 | wp_clear_scheduled_hook( 'hmbkp_schedule_hook', array( 'id' => $item ) ); |
||
237 | } |
||
238 | |||
239 | // delete options |
||
240 | array_map( 'delete_option', $schedules ); |
||
241 | |||
242 | array_map( 'delete_option', array( 'hmbkp_enable_support', 'hmbkp_plugin_version', 'hmbkp_path', 'hmbkp_default_path', 'hmbkp_upsell' ) ); |
||
243 | |||
244 | // Delete all transients |
||
245 | array_map( 'delete_transient', array( 'hmbkp_plugin_data', 'hmbkp_directory_filesizes', 'hmbkp_directory_filesize_running', 'timeout_hmbkp_wp_cron_test_beacon', 'hmbkp_wp_cron_test_beacon' ) ); |
||
246 | |||
247 | } |
||
248 | |||
249 | restore_current_blog(); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // Update from 3.3.0 |
||
254 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '3.3.0', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
255 | |||
256 | $schedules = Schedules::get_instance(); |
||
257 | |||
258 | // Loop through all schedules and re-set the reccurrence to include hmbkp_ |
||
259 | foreach ( $schedules->get_schedules() as $schedule ) { |
||
260 | |||
261 | $reoccurrence = $schedule->get_reoccurrence(); |
||
262 | |||
263 | if ( 'manually' !== $reoccurrence && strpos( $reoccurrence, 'hmbkp_' ) === 0 ) { |
||
264 | $schedule->set_reoccurrence( substr( $reoccurrence, 6 ) ); |
||
265 | } |
||
266 | |||
267 | $schedule->save(); |
||
268 | |||
269 | } |
||
270 | } |
||
271 | |||
272 | // Update from 3.3.4 |
||
273 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( '3.4.0', get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
274 | delete_transient( 'hmbkp_directory_filesizes' ); |
||
275 | } |
||
276 | |||
277 | // Every update |
||
278 | if ( get_option( 'hmbkp_plugin_version' ) && version_compare( Plugin::PLUGIN_VERSION, get_option( 'hmbkp_plugin_version' ), '>' ) ) { |
||
279 | |||
280 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-setup.php' ); |
||
281 | |||
282 | \HMBKP_Setup::deactivate(); |
||
283 | |||
284 | Path::get_instance()->protect_path( 'reset' ); |
||
285 | |||
286 | } |
||
287 | |||
288 | // Update the stored version |
||
289 | if ( get_option( 'hmbkp_plugin_version' ) !== Plugin::PLUGIN_VERSION ) { |
||
290 | update_option( 'hmbkp_plugin_version', Plugin::PLUGIN_VERSION ); |
||
291 | } |
||
292 | |||
293 | } |
||
294 | |||
664 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state