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 classes like Backup 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 Backup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Backup { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The backup type, must be either complete, file or database |
||
| 16 | * |
||
| 17 | * @string |
||
| 18 | */ |
||
| 19 | private $type = ''; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The filename of the backup file |
||
| 23 | * |
||
| 24 | * @string |
||
| 25 | */ |
||
| 26 | private $archive_filename = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The filename of the database dump |
||
| 30 | * |
||
| 31 | * @string |
||
| 32 | */ |
||
| 33 | private $database_dump_filename = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The path to the zip command |
||
| 37 | * |
||
| 38 | * @string |
||
| 39 | */ |
||
| 40 | private $zip_command_path; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The path to the mysqldump command |
||
| 44 | * |
||
| 45 | * @string |
||
| 46 | */ |
||
| 47 | private $mysqldump_command_path; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The filename of the existing backup file |
||
| 51 | * |
||
| 52 | * @string |
||
| 53 | */ |
||
| 54 | private $existing_archive_filepath = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * An array of exclude rules |
||
| 58 | * |
||
| 59 | * @array |
||
| 60 | */ |
||
| 61 | private $excludes = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The path that should be backed up |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $root = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * An array of all the files in root |
||
| 72 | * excluding excludes and unreadable files |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $files = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * An array of all the files in root |
||
| 80 | * that are unreadable |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $unreadable_files = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * An array of all the files in root |
||
| 88 | * that will be included in the backup |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $included_files = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * An array of all the files in root |
||
| 96 | * that match the exclude rules |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $excluded_files = array(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Contains an array of errors |
||
| 104 | * |
||
| 105 | * @var mixed |
||
| 106 | */ |
||
| 107 | private $errors = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Contains an array of warnings |
||
| 111 | * |
||
| 112 | * @var mixed |
||
| 113 | */ |
||
| 114 | private $warnings = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The archive method used |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | private $archive_method = ''; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The mysqldump method used |
||
| 125 | * |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | private $mysqldump_method = ''; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | protected $mysqldump_verified = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var bool |
||
| 137 | */ |
||
| 138 | protected $archive_verified = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $action_callback = ''; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * List of patterns we want to exclude by default. |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | protected $default_excludes = array( |
||
| 150 | '.git/', |
||
| 151 | '.svn/', |
||
| 152 | '.DS_Store', |
||
| 153 | '.idea/', |
||
| 154 | 'backwpup-*', |
||
| 155 | 'updraft', |
||
| 156 | 'wp-snapshots', |
||
| 157 | 'backupbuddy_backups', |
||
| 158 | 'pb_backupbuddy', |
||
| 159 | 'backup-db', |
||
| 160 | 'Envato-backups', |
||
| 161 | 'managewp', |
||
| 162 | 'backupwordpress-*-backups', |
||
| 163 | ); |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns a filterable array of excluded directories and files. |
||
| 167 | * |
||
| 168 | * @return mixed|void |
||
| 169 | */ |
||
| 170 | public function default_excludes() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Check whether safe mode is active or not |
||
| 176 | * |
||
| 177 | * @param string $ini_get_callback |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public static function is_safe_mode_active( $ini_get_callback = 'ini_get' ) { |
||
| 182 | |||
| 183 | $safe_mode = @call_user_func( $ini_get_callback, 'safe_mode' ); |
||
| 184 | |||
| 185 | if ( $safe_mode && strtolower( $safe_mode ) != 'off' ) { |
||
|
1 ignored issue
–
show
|
|||
| 186 | return true; |
||
| 187 | } |
||
| 188 | |||
| 189 | return false; |
||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check whether shell_exec has been disabled. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public static function is_shell_exec_available() { |
||
| 199 | |||
| 200 | // Are we in Safe Mode |
||
| 201 | if ( self::is_safe_mode_active() ) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Is shell_exec or escapeshellcmd or escapeshellarg disabled? |
||
| 206 | if ( self::is_function_disabled( 'suhosin.executor.func.blacklist' ) ) { |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | // Functions can also be disabled via suhosin |
||
| 211 | if ( self::is_function_disabled( 'disable_functions' ) ) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Can we issue a simple echo command? |
||
| 216 | if ( ! @shell_exec( 'echo backupwordpress' ) ) { |
||
|
1 ignored issue
–
show
|
|||
| 217 | return false; |
||
| 218 | } |
||
| 219 | |||
| 220 | return true; |
||
| 221 | |||
| 222 | } |
||
| 223 | |||
| 224 | protected static function is_function_disabled( $ini_setting ) { |
||
| 225 | |||
| 226 | if ( array_intersect( array( |
||
| 227 | 'shell_exec', |
||
| 228 | 'escapeshellarg', |
||
| 229 | 'escapeshellcmd' |
||
| 230 | ), array_map( 'trim', explode( ',', @ini_get( $ini_setting ) ) ) ) ) { |
||
| 231 | return false; |
||
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Attempt to work out the root directory of the site, that |
||
| 239 | * is, the path equivelant of home_url(). |
||
| 240 | * |
||
| 241 | * @return string $home_path |
||
| 242 | */ |
||
| 243 | public static function get_home_path() { |
||
| 244 | |||
| 245 | if ( defined( 'HMBKP_ROOT' ) && HMBKP_ROOT ) { |
||
| 246 | return wp_normalize_path( HMBKP_ROOT ); |
||
| 247 | } |
||
| 248 | |||
| 249 | $home_url = home_url(); |
||
| 250 | $site_url = site_url(); |
||
| 251 | |||
| 252 | $home_path = ABSPATH; |
||
| 253 | |||
| 254 | // If site_url contains home_url and they differ then assume WordPress is installed in a sub directory |
||
| 255 | if ( $home_url !== $site_url && strpos( $site_url, $home_url ) === 0 ) { |
||
| 256 | $home_path = trailingslashit( substr( wp_normalize_path( ABSPATH ), 0, strrpos( wp_normalize_path( ABSPATH ), str_replace( $home_url, '', $site_url ) ) ) ); |
||
| 257 | } |
||
| 258 | |||
| 259 | return wp_normalize_path( $home_path ); |
||
| 260 | |||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets up the default properties |
||
| 265 | */ |
||
| 266 | public function __construct() { |
||
| 267 | |||
| 268 | // Raise the memory limit and max_execution time |
||
| 269 | @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
||
| 270 | @set_time_limit( 0 ); |
||
| 271 | |||
| 272 | // Set a custom error handler so we can track errors |
||
| 273 | set_error_handler( array( $this, 'error_handler' ) ); |
||
| 274 | |||
| 275 | // Some properties can be overridden with defines |
||
| 276 | if ( defined( 'HMBKP_EXCLUDE' ) && HMBKP_EXCLUDE ) { |
||
| 277 | $this->set_excludes( HMBKP_EXCLUDE, true ); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ( defined( 'HMBKP_MYSQLDUMP_PATH' ) ) { |
||
| 281 | $this->set_mysqldump_command_path( HMBKP_MYSQLDUMP_PATH ); |
||
| 282 | } |
||
| 283 | |||
| 284 | if ( defined( 'HMBKP_ZIP_PATH' ) ) { |
||
| 285 | $this->set_zip_command_path( HMBKP_ZIP_PATH ); |
||
| 286 | } |
||
| 287 | |||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Simple class wrapper for Path::get_path() |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | private function get_path() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the full filepath to the archive file |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function get_archive_filepath() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the filename of the archive file |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function get_archive_filename() { |
||
| 314 | |||
| 315 | if ( empty( $this->archive_filename ) ) { |
||
| 316 | $this->set_archive_filename( implode( '-', array( |
||
| 317 | sanitize_title( str_ireplace( array( |
||
| 318 | 'http://', |
||
| 319 | 'https://', |
||
| 320 | 'www' |
||
| 321 | ), '', home_url() ) ), |
||
| 322 | 'backup', |
||
| 323 | current_time( 'Y-m-d-H-i-s' ) |
||
| 324 | ) ) . '.zip' ); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $this->archive_filename; |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Set the filename of the archive file |
||
| 333 | * |
||
| 334 | * @param string $filename |
||
| 335 | * |
||
| 336 | * @return \WP_Error|null |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function set_archive_filename( $filename ) { |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get the full filepath to the database dump file. |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function get_database_dump_filepath() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the filename of the database dump file |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | public function get_database_dump_filename() { |
||
| 367 | |||
| 368 | if ( empty( $this->database_dump_filename ) ) { |
||
| 369 | $this->set_database_dump_filename( 'database_' . DB_NAME . '.sql' ); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $this->database_dump_filename; |
||
| 373 | |||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set the filename of the database dump file |
||
| 378 | * |
||
| 379 | * @param string $filename |
||
| 380 | * |
||
| 381 | * @return \WP_Error|null |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function set_database_dump_filename( $filename ) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get the root directory to backup from |
||
| 399 | * |
||
| 400 | * Defaults to the root of the path equivalent of your home_url |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function get_root() { |
||
| 405 | |||
| 406 | if ( empty( $this->root ) ) { |
||
| 407 | $this->set_root( wp_normalize_path( self::get_home_path() ) ); |
||
| 408 | } |
||
| 409 | |||
| 410 | return $this->root; |
||
| 411 | |||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set the root directory to backup from |
||
| 416 | * |
||
| 417 | * @param string $path |
||
| 418 | * |
||
| 419 | * @return \WP_Error|null |
||
| 420 | */ |
||
| 421 | View Code Duplication | public function set_root( $path ) { |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Get the filepath for the existing archive |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function get_existing_archive_filepath() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set the filepath for the existing archive |
||
| 442 | * |
||
| 443 | * @param string $existing_archive_filepath |
||
| 444 | * |
||
| 445 | * @return null |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function set_existing_archive_filepath( $existing_archive_filepath ) { |
|
|
1 ignored issue
–
show
|
|||
| 448 | |||
| 449 | if ( empty( $existing_archive_filepath ) || ! is_string( $existing_archive_filepath ) ) { |
||
| 450 | return new \WP_Error( 'invalid_existing_archive_filepath', sprintf( __( 'Invalid existing archive filepath <code>%s</code> must be a non-empty (string)', 'backupwordpress' ), $existing_archive_filepath ) ); |
||
| 451 | } |
||
| 452 | |||
| 453 | $this->existing_archive_filepath = wp_normalize_path( $existing_archive_filepath ); |
||
| 454 | |||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get the archive method that was used for the backup |
||
| 459 | * |
||
| 460 | * Will be either zip, ZipArchive or PclZip |
||
| 461 | * |
||
| 462 | */ |
||
| 463 | public function get_archive_method() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get the database dump method that was used for the backup |
||
| 469 | * |
||
| 470 | * Will be either mysqldump or mysqldump_fallback |
||
| 471 | * |
||
| 472 | */ |
||
| 473 | public function get_mysqldump_method() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get the backup type |
||
| 479 | * |
||
| 480 | * Defaults to complete |
||
| 481 | * |
||
| 482 | */ |
||
| 483 | public function get_type() { |
||
| 484 | |||
| 485 | if ( empty( $this->type ) ) { |
||
| 486 | $this->set_type( 'complete' ); |
||
| 487 | } |
||
| 488 | |||
| 489 | return $this->type; |
||
| 490 | |||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Set the backup type |
||
| 495 | * |
||
| 496 | * $type must be one of complete, database or file |
||
| 497 | * |
||
| 498 | * @param string $type |
||
| 499 | * |
||
| 500 | * @return \WP_Error|null |
||
| 501 | */ |
||
| 502 | public function set_type( $type ) { |
||
| 503 | |||
| 504 | if ( ! is_string( $type ) || ! in_array( $type, array( 'file', 'database', 'complete' ) ) ) { |
||
| 505 | return new \WP_Error( 'invalid_backup_type', sprintf( __( 'Invalid backup type <code>%s</code> must be one of (string) file, database or complete', 'backupwordpress' ), $type ) ); |
||
| 506 | } |
||
| 507 | |||
| 508 | $this->type = $type; |
||
| 509 | |||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get the path to the mysqldump bin |
||
| 514 | * |
||
| 515 | * If not explicitly set will attempt to work |
||
| 516 | * it out by checking common locations |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function get_mysqldump_command_path() { |
||
| 521 | |||
| 522 | // Check shell_exec is available |
||
| 523 | if ( ! self::is_shell_exec_available() ) { |
||
| 524 | return ''; |
||
| 525 | } |
||
| 526 | |||
| 527 | // Return now if it's already been set |
||
| 528 | if ( isset( $this->mysqldump_command_path ) ) { |
||
| 529 | return $this->mysqldump_command_path; |
||
| 530 | } |
||
| 531 | |||
| 532 | $this->mysqldump_command_path = ''; |
||
| 533 | |||
| 534 | // Does mysqldump work |
||
| 535 | if ( is_null( shell_exec( 'hash mysqldump 2>&1' ) ) ) { |
||
| 536 | |||
| 537 | // If so store it for later |
||
| 538 | $this->set_mysqldump_command_path( 'mysqldump' ); |
||
| 539 | |||
| 540 | // And return now |
||
| 541 | return $this->mysqldump_command_path; |
||
| 542 | |||
| 543 | } |
||
| 544 | |||
| 545 | // List of possible mysqldump locations |
||
| 546 | $mysqldump_locations = array( |
||
| 547 | '/usr/local/bin/mysqldump', |
||
| 548 | '/usr/local/mysql/bin/mysqldump', |
||
| 549 | '/usr/mysql/bin/mysqldump', |
||
| 550 | '/usr/bin/mysqldump', |
||
| 551 | '/opt/local/lib/mysql6/bin/mysqldump', |
||
| 552 | '/opt/local/lib/mysql5/bin/mysqldump', |
||
| 553 | '/opt/local/lib/mysql4/bin/mysqldump', |
||
| 554 | '/xampp/mysql/bin/mysqldump', |
||
| 555 | '/Program Files/xampp/mysql/bin/mysqldump', |
||
| 556 | '/Program Files/MySQL/MySQL Server 6.0/bin/mysqldump', |
||
| 557 | '/Program Files/MySQL/MySQL Server 5.7/bin/mysqldump', |
||
| 558 | '/Program Files/MySQL/MySQL Server 5.6/bin/mysqldump', |
||
| 559 | '/Program Files/MySQL/MySQL Server 5.5/bin/mysqldump', |
||
| 560 | '/Program Files/MySQL/MySQL Server 5.4/bin/mysqldump', |
||
| 561 | '/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump', |
||
| 562 | '/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump', |
||
| 563 | '/Program Files/MySQL/MySQL Server 4.1/bin/mysqldump', |
||
| 564 | '/opt/local/bin/mysqldump' |
||
| 565 | ); |
||
| 566 | |||
| 567 | // Find the first one which works |
||
| 568 | foreach ( $mysqldump_locations as $location ) { |
||
| 569 | if ( (is_null( shell_exec( 'hash ' . wp_normalize_path( $location ) . ' 2>&1' ) ) ) && @is_executable( wp_normalize_path( $location ) ) ) { |
||
| 570 | $this->set_mysqldump_command_path( $location ); |
||
| 571 | break; // Found one |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | return $this->mysqldump_command_path; |
||
| 576 | |||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Set the path to the mysqldump bin |
||
| 581 | * |
||
| 582 | * Setting the path to false will cause the database |
||
| 583 | * dump to use the php fallback |
||
| 584 | * |
||
| 585 | * @param mixed $path |
||
| 586 | */ |
||
| 587 | public function set_mysqldump_command_path( $path ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get the path to the zip bin |
||
| 593 | * |
||
| 594 | * If not explicitly set will attempt to work |
||
| 595 | * it out by checking common locations |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | public function get_zip_command_path() { |
||
| 600 | |||
| 601 | // Check shell_exec is available |
||
| 602 | if ( ! self::is_shell_exec_available() ) { |
||
| 603 | return ''; |
||
| 604 | } |
||
| 605 | |||
| 606 | // Return now if it's already been set |
||
| 607 | if ( isset( $this->zip_command_path ) ) { |
||
| 608 | return $this->zip_command_path; |
||
| 609 | } |
||
| 610 | |||
| 611 | $this->zip_command_path = ''; |
||
| 612 | |||
| 613 | // Does zip work |
||
| 614 | if ( is_null( shell_exec( 'hash zip 2>&1' ) ) ) { |
||
| 615 | |||
| 616 | // If so store it for later |
||
| 617 | $this->set_zip_command_path( 'zip' ); |
||
| 618 | |||
| 619 | // And return now |
||
| 620 | return $this->zip_command_path; |
||
| 621 | |||
| 622 | } |
||
| 623 | |||
| 624 | // List of possible zip locations |
||
| 625 | $zip_locations = array( |
||
| 626 | '/usr/bin/zip', |
||
| 627 | '/opt/local/bin/zip' |
||
| 628 | ); |
||
| 629 | |||
| 630 | // Find the first one which works |
||
| 631 | foreach ( $zip_locations as $location ) { |
||
| 632 | if ( @is_executable( wp_normalize_path( $location ) ) ) { |
||
| 633 | $this->set_zip_command_path( $location ); |
||
| 634 | break; // Found one |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | return $this->zip_command_path; |
||
| 639 | |||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Set the path to the zip bin |
||
| 644 | * |
||
| 645 | * Setting the path to false will cause the database |
||
| 646 | * dump to use the php fallback |
||
| 647 | * |
||
| 648 | * @param mixed $path |
||
| 649 | */ |
||
| 650 | public function set_zip_command_path( $path ) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Fire actions for the various backup stages |
||
| 656 | * |
||
| 657 | * Callers can register callbacks to be called using `set_action_callback` |
||
| 658 | * Both the action and the instance on Backup are then passed to the callback function |
||
| 659 | * |
||
| 660 | * @see set_action_callback |
||
| 661 | * |
||
| 662 | * @param string $action The event to fire |
||
| 663 | */ |
||
| 664 | protected function do_action( $action ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Allow the caller to set a callback function that will be invoked whenever |
||
| 687 | * an action fires |
||
| 688 | * |
||
| 689 | * @see do_action |
||
| 690 | * @see /do_action |
||
| 691 | * |
||
| 692 | * @param callable $callback The function or method to be called |
||
| 693 | * @param int $priority The priority of the callback |
||
| 694 | */ |
||
| 695 | public function set_action_callback( $callback, $priority = 10 ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Kick off a backup |
||
| 701 | * |
||
| 702 | * @todo should be renamed so it's not same as class |
||
| 703 | * @return null |
||
| 704 | */ |
||
| 705 | public function backup() { |
||
| 706 | |||
| 707 | $this->do_action( 'hmbkp_backup_started' ); |
||
| 708 | |||
| 709 | // Backup database |
||
| 710 | if ( $this->get_type() !== 'file' ) { |
||
| 711 | $this->dump_database(); |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Create the mysql backup |
||
| 723 | * |
||
| 724 | * Uses mysqldump if available, falls back to PHP |
||
| 725 | * if not. |
||
| 726 | * |
||
| 727 | */ |
||
| 728 | public function dump_database() { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Export the database to an .sql file via the command line with mysqldump |
||
| 746 | */ |
||
| 747 | public function mysqldump() { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * PHP mysqldump fallback functions, exports the database to a .sql file |
||
| 845 | * |
||
| 846 | */ |
||
| 847 | public function mysqldump_fallback() { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Zip up all the files. |
||
| 943 | * |
||
| 944 | * Attempts to use the shell zip command, if |
||
| 945 | * thats not available then it falls back to |
||
| 946 | * PHP ZipArchive. |
||
| 947 | * |
||
| 948 | */ |
||
| 949 | public function archive() { |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Zip using the native zip command |
||
| 997 | */ |
||
| 998 | public function zip() { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Fallback for creating zip archives if zip command is |
||
| 1056 | * unavailable. |
||
| 1057 | */ |
||
| 1058 | public function zip_archive() { |
||
| 1128 | |||
| 1129 | public function verify_mysqldump() { |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Verify that the archive is valid and contains all the files it should contain. |
||
| 1159 | * |
||
| 1160 | * @return bool |
||
| 1161 | */ |
||
| 1162 | public function verify_archive() { |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Return an array of all files in the filesystem. |
||
| 1187 | * |
||
| 1188 | * @param bool $ignore_default_exclude_rules If true then will return all files under root. Otherwise returns all files except those matching default exclude rules. |
||
| 1189 | * |
||
| 1190 | * @return array |
||
| 1191 | */ |
||
| 1192 | public function get_files( $ignore_default_exclude_rules = false ) { |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Returns an array of files that will be included in the backup. |
||
| 1220 | * |
||
| 1221 | * @return array |
||
| 1222 | */ |
||
| 1223 | View Code Duplication | public function get_included_files() { |
|
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Returns an array of files that match the exclude rules. |
||
| 1260 | * |
||
| 1261 | * @return array |
||
| 1262 | */ |
||
| 1263 | View Code Duplication | public function get_excluded_files() { |
|
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Returns an array of unreadable files. |
||
| 1298 | * |
||
| 1299 | * @return array |
||
| 1300 | */ |
||
| 1301 | public function get_unreadable_files() { |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Get an array of exclude rules |
||
| 1328 | * |
||
| 1329 | * The backup path is automatically excluded |
||
| 1330 | * |
||
| 1331 | * @return array |
||
| 1332 | */ |
||
| 1333 | public function get_excludes() { |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Set the excludes, expects and array |
||
| 1352 | * |
||
| 1353 | * @param Array $excludes |
||
| 1354 | * @param Bool $append |
||
| 1355 | */ |
||
| 1356 | public function set_excludes( $excludes, $append = false ) { |
||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * Generate the exclude param string for the zip backup |
||
| 1372 | * |
||
| 1373 | * Takes the exclude rules and formats them for use with either |
||
| 1374 | * the shell zip command or pclzip |
||
| 1375 | * |
||
| 1376 | * @param string $context . (default: 'zip') |
||
| 1377 | * |
||
| 1378 | * @return string |
||
| 1379 | */ |
||
| 1380 | public function exclude_string( $context = 'zip' ) { |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Get the errors |
||
| 1465 | * |
||
| 1466 | */ |
||
| 1467 | View Code Duplication | public function get_errors( $context = null ) { |
|
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Add an error to the errors stack |
||
| 1479 | * |
||
| 1480 | * @param string $context |
||
| 1481 | * @param mixed $error |
||
| 1482 | */ |
||
| 1483 | View Code Duplication | public function error( $context, $error ) { |
|
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Migrate errors to warnings |
||
| 1497 | * |
||
| 1498 | * @param null $context |
||
| 1499 | */ |
||
| 1500 | private function errors_to_warnings( $context = null ) { |
||
| 1521 | |||
| 1522 | /** |
||
| 1523 | * Get the warnings |
||
| 1524 | * |
||
| 1525 | */ |
||
| 1526 | View Code Duplication | public function get_warnings( $context = null ) { |
|
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Add an warning to the warnings stack |
||
| 1538 | * |
||
| 1539 | * @param string $context |
||
| 1540 | * @param mixed $warning |
||
| 1541 | */ |
||
| 1542 | View Code Duplication | private function warning( $context, $warning ) { |
|
| 1553 | |||
| 1554 | /** |
||
| 1555 | * Custom error handler for catching php errors |
||
| 1556 | * |
||
| 1557 | * @param $type |
||
| 1558 | * |
||
| 1559 | * @return bool |
||
| 1560 | */ |
||
| 1561 | public function error_handler( $type ) { |
||
| 1577 | |||
| 1578 | /** |
||
| 1579 | * Determine if user can connect via the CLI |
||
| 1580 | * |
||
| 1581 | * @return \WP_Error |
||
| 1582 | */ |
||
| 1583 | public function user_can_connect() { |
||
| 1662 | |||
| 1663 | } |
||
| 1664 |