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() { |
||
| 223 | |||
| 224 | protected static function is_function_disabled( $ini_setting ) { |
||
| 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() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets up the default properties |
||
| 265 | */ |
||
| 266 | public function __construct() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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 ) { |
|
| 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() { |
||
| 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 ) { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 848 | |||
| 849 | $this->errors_to_warnings( $this->get_mysqldump_method() ); |
||
| 850 | |||
| 851 | $this->mysqldump_method = 'mysqldump_fallback'; |
||
| 852 | |||
| 853 | $this->do_action( 'hmbkp_mysqldump_started' ); |
||
| 854 | |||
| 855 | // Guess port or socket connection type |
||
| 856 | $port_or_socket = strstr( DB_HOST, ':' ); |
||
| 857 | |||
| 858 | $host = DB_HOST; |
||
| 859 | |||
| 860 | View Code Duplication | if ( ! empty( $port_or_socket ) ) { |
|
|
1 ignored issue
–
show
|
|||
| 861 | |||
| 862 | $host = substr( DB_HOST, 0, strpos( DB_HOST, ':' ) ); |
||
| 863 | |||
| 864 | $port_or_socket = substr( $port_or_socket, 1 ); |
||
| 865 | |||
| 866 | if ( 0 !== strpos( $port_or_socket, '/' ) ) { |
||
| 867 | |||
| 868 | $port = intval( $port_or_socket ); |
||
| 869 | |||
| 870 | $maybe_socket = strstr( $port_or_socket, ':' ); |
||
| 871 | |||
| 872 | if ( ! empty( $maybe_socket ) ) { |
||
| 873 | |||
| 874 | $socket = substr( $maybe_socket, 1 ); |
||
| 875 | |||
| 876 | } |
||
| 877 | |||
| 878 | } else { |
||
| 879 | |||
| 880 | $socket = $port_or_socket; |
||
| 881 | |||
| 882 | } |
||
| 883 | } |
||
| 884 | |||
| 885 | // PDO connection string formats: |
||
| 886 | // mysql:host=localhost;port=3307;dbname=testdb |
||
| 887 | // mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
| 888 | |||
| 889 | if ( $port_or_socket ) { |
||
| 890 | if ( isset( $port ) ) { |
||
| 891 | $dsn = 'mysql:host=' . DB_HOST . ';port=' . $port . ';dbname=' . DB_NAME; |
||
| 892 | } elseif ( isset( $socket ) ) { |
||
| 893 | $dsn = 'mysql:unix_socket=' . $socket . ';dbname=' . DB_NAME; |
||
| 894 | } |
||
| 895 | } else { |
||
| 896 | $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME; |
||
| 897 | } |
||
| 898 | |||
| 899 | // Get character set from constant if it is declared. |
||
| 900 | if ( defined( 'DB_CHARSET' ) && DB_CHARSET ) { |
||
| 901 | $charset = DB_CHARSET; |
||
| 902 | } else { |
||
| 903 | $charset = 'utf8'; |
||
| 904 | } |
||
| 905 | |||
| 906 | if ( defined( 'DB_PASSWORD' ) && DB_PASSWORD ) { |
||
| 907 | $pwd = DB_PASSWORD; |
||
| 908 | } else { |
||
| 909 | $pwd = ''; |
||
| 910 | } |
||
| 911 | |||
| 912 | if ( ! defined( 'HMBKP_MYSQLDUMP_SINGLE_TRANSACTION' ) || false !== HMBKP_MYSQLDUMP_SINGLE_TRANSACTION ) { |
||
| 913 | $single_transaction = true; |
||
| 914 | } else { |
||
| 915 | $single_transaction = false; |
||
| 916 | } |
||
| 917 | |||
| 918 | $dump_settings = array( |
||
| 919 | 'default-character-set' => $charset, |
||
| 920 | 'hex-blob' => true, |
||
| 921 | 'single-transaction' => $single_transaction, |
||
| 922 | ); |
||
| 923 | |||
| 924 | try { |
||
| 925 | |||
| 926 | // Allow passing custom options to dump process. |
||
| 927 | $dump_settings = apply_filters( 'hmbkp_mysqldump_fallback_dump_settings', $dump_settings ); |
||
| 928 | |||
| 929 | $dump = new IMysqldump\Mysqldump( $dsn, DB_USER, $pwd, $dump_settings ); |
||
| 930 | |||
| 931 | $dump->start( $this->get_database_dump_filepath() ); |
||
| 932 | |||
| 933 | } catch ( \Exception $e ) { |
||
| 934 | |||
| 935 | return new \WP_Error( 'mysql-fallback-error', sprintf( __( 'mysqldump fallback error %s', 'backupwordpress' ), $e->getMessage() ) ); |
||
| 936 | |||
| 937 | } |
||
| 938 | |||
| 939 | } |
||
| 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() { |
||
| 1059 | |||
| 1060 | $this->errors_to_warnings( $this->get_archive_method() ); |
||
| 1061 | $this->archive_method = 'ziparchive'; |
||
| 1062 | |||
| 1063 | $this->do_action( 'hmbkp_archive_started' ); |
||
| 1064 | |||
| 1065 | $zip = new \ZipArchive(); |
||
| 1066 | |||
| 1067 | if ( ! class_exists( 'ZipArchive' ) || ! $zip->open( $this->get_archive_filepath(), \ZIPARCHIVE::CREATE ) ) { |
||
| 1068 | return; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | $excludes = $this->exclude_string( 'regex' ); |
||
| 1072 | |||
| 1073 | // Add the database |
||
| 1074 | if ( $this->get_type() !== 'file' && file_exists( $this->get_database_dump_filepath() ) ) { |
||
| 1075 | $zip->addFile( $this->get_database_dump_filepath(), $this->get_database_dump_filename() ); |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | if ( $this->get_type() !== 'database' ) { |
||
| 1079 | |||
| 1080 | $files_added = 0; |
||
| 1081 | |||
| 1082 | foreach ( $this->get_files() as $file ) { |
||
| 1083 | |||
| 1084 | // Skip dot files, they should only exist on versions of PHP between 5.2.11 -> 5.3 |
||
| 1085 | if ( method_exists( $file, 'isDot' ) && $file->isDot() ) { |
||
| 1086 | continue; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | // Skip unreadable files |
||
| 1090 | if ( ! @realpath( $file->getPathname() ) || ! $file->isReadable() ) { |
||
| 1091 | continue; |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | // Excludes |
||
| 1095 | if ( $excludes && preg_match( '(' . $excludes . ')', str_ireplace( trailingslashit( $this->get_root() ), '', wp_normalize_path( $file->getPathname() ) ) ) ) { |
||
| 1096 | continue; |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | if ( $file->isDir() ) { |
||
| 1100 | $zip->addEmptyDir( trailingslashit( str_ireplace( trailingslashit( $this->get_root() ), '', wp_normalize_path( $file->getPathname() ) ) ) ); |
||
| 1101 | } elseif ( $file->isFile() ) { |
||
| 1102 | $zip->addFile( $file->getPathname(), str_ireplace( trailingslashit( $this->get_root() ), '', wp_normalize_path( $file->getPathname() ) ) ); |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | if ( ++ $files_added % 500 === 0 ) { |
||
| 1106 | if ( ! $zip->close() || ! $zip->open( $this->get_archive_filepath(), \ZIPARCHIVE::CREATE ) ) { |
||
| 1107 | return; |
||
| 1108 | } |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | } |
||
| 1112 | |||
| 1113 | } |
||
| 1114 | |||
| 1115 | if ( $zip->status ) { |
||
| 1116 | $this->warning( $this->get_archive_method(), $zip->status ); |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | if ( $zip->statusSys ) { |
||
| 1120 | $this->warning( $this->get_archive_method(), $zip->statusSys ); |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | $zip->close(); |
||
| 1124 | |||
| 1125 | $this->verify_archive(); |
||
| 1126 | |||
| 1127 | } |
||
| 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() { |
||
| 1334 | |||
| 1335 | $excludes = array(); |
||
| 1336 | |||
| 1337 | if ( isset( $this->excludes ) ) { |
||
| 1338 | $excludes = $this->excludes; |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | // If path() is inside root(), exclude it |
||
| 1342 | if ( strpos( $this->get_path(), $this->get_root() ) !== false ) { |
||
| 1343 | array_unshift( $excludes, trailingslashit( $this->get_path() ) ); |
||
| 1344 | } |
||
| 1345 | |||
| 1346 | return array_unique( $excludes ); |
||
| 1347 | |||
| 1348 | } |
||
| 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 |