1 | <?php |
||
10 | class BackUpWordPress_WP_CLI_Command extends WP_CLI_Command { |
||
11 | |||
12 | /** |
||
13 | * Perform a Backup. |
||
14 | * |
||
15 | * ## OPTIONS |
||
16 | * |
||
17 | * [--files_only] |
||
18 | * : Backup files only, default to off |
||
19 | * |
||
20 | * [--database_only] |
||
21 | * : Backup database only, defaults to off |
||
22 | * |
||
23 | * [--destination] |
||
24 | * : dir that the backup should be save in, defaults to your existing backups directory |
||
25 | * |
||
26 | * [--root] |
||
27 | * : dir that should be backed up, defaults to site root. |
||
28 | * |
||
29 | * [--zip_command_path] |
||
30 | * : path to your zip binary, standard locations are automatically used |
||
31 | * |
||
32 | * [--mysqldump_command_path] |
||
33 | * : path to your mysqldump binary, standard locations are automatically used |
||
34 | * |
||
35 | * [--archive_filename] |
||
36 | * : filename for the resulting zip file |
||
37 | * |
||
38 | * [--excludes] |
||
39 | * : list of paths you'd like to exclude |
||
40 | * |
||
41 | * ## Usage |
||
42 | * |
||
43 | * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] |
||
44 | * |
||
45 | * @todo errors should be bubbled from Backup, Scheduled_Backup and the like instead of being repeated. |
||
46 | */ |
||
47 | public function backup( $args, $assoc_args ) { |
||
48 | |||
49 | add_action( 'hmbkp_mysqldump_started', function () { |
||
50 | WP_CLI::line( __( 'Backup: Dumping database...', 'backupwordpress' ) ); |
||
51 | } ); |
||
52 | |||
53 | add_action( 'hmbkp_archive_started', function () { |
||
54 | WP_CLI::line( __( 'Backup: Zipping everything up...', 'backupwordpress' ) ); |
||
55 | } ); |
||
56 | |||
57 | $hm_backup = new HM\BackUpWordPress\Backup(); |
||
58 | |||
59 | if ( ! empty( $assoc_args['destination'] ) ) { |
||
60 | HM\BackUpWordPress\Path::get_instance()->set_path( $assoc_args['destination'] ); |
||
61 | } |
||
62 | |||
63 | HM\BackUpWordPress\Path::get_instance()->cleanup(); |
||
64 | |||
65 | if ( ! empty( $assoc_args['root'] ) ) { |
||
66 | HM\BackUpWordPress\Path->set_root( $assoc_args['root'] ); |
||
67 | } |
||
68 | |||
69 | if ( ( ! is_dir( Path::get_path() ) ) ) { |
||
70 | WP_CLI::error( __( 'Invalid backup path', 'backupwordpress' ) ); |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | if ( ! is_dir( Path::get_root() ) || ! is_readable( Path::get_root() ) ) { |
||
75 | WP_CLI::error( __( 'Invalid root path', 'backupwordpress' ) ); |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | if ( isset( $assoc_args['archive_filename'] ) ) { |
||
80 | $hm_backup->set_archive_filename( $assoc_args['archive_filename'] ); |
||
81 | } |
||
82 | |||
83 | if ( ! empty( $assoc_args['files_only'] ) ) { |
||
84 | $hm_backup->set_type( 'file' ); |
||
85 | } |
||
86 | |||
87 | if ( ! empty( $assoc_args['database_only'] ) ) { |
||
88 | $hm_backup->set_type( 'database' ); |
||
89 | } |
||
90 | |||
91 | if ( isset( $assoc_args['mysqldump_command_path'] ) ) { |
||
92 | $hm_backup->set_mysqldump_command_path( $assoc_args['mysqldump_command_path'] ); |
||
93 | } |
||
94 | |||
95 | if ( isset( $assoc_args['zip_command_path'] ) ) { |
||
96 | $hm_backup->set_zip_command_path( $assoc_args['zip_command_path'] ); |
||
97 | } |
||
98 | |||
99 | if ( ! empty( $assoc_args['excludes'] ) ) { |
||
100 | $hm_backup->set_excludes( $assoc_args['excludes'] ); |
||
101 | } |
||
102 | |||
103 | $hm_backup->backup(); |
||
104 | |||
105 | if ( file_exists( $hm_backup->get_archive_filepath() ) ) { |
||
106 | WP_CLI::success( __( 'Backup Complete: ', 'backupwordpress' ) . $hm_backup->get_archive_filepath() ); |
||
107 | } else { |
||
108 | WP_CLI::error( __( 'Backup Failed', 'backupwordpress' ) ); |
||
109 | } |
||
110 | |||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
116 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.