-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.php
63 lines (45 loc) · 1.66 KB
/
command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* Enable or disable the maintenance mode
*
* @when after_wp_load
*/
$maintenance_command = function() {
try {
$wp_config_path = ABSPATH . 'wp-config.php';
$config_transformer = new WPConfigTransformer( $wp_config_path );
$config_type = 'constant';
$config_name = 'MAINTENANCE';
$config_transformer_options = array(
'raw' => true,
);
$success_message = 'Maintenance mode ';
if ( $config_transformer->exists( $config_type, $config_name ) ) {
$config_value = filter_var( $config_transformer->get_value( $config_type, $config_name ), FILTER_VALIDATE_BOOLEAN );
$config_transformer->update( $config_type, $config_name, $config_value ? 'false' : 'true', $config_transformer_options );
$config_value = ! $config_value;
} else {
$config_value = true;
$config_transformer->add( $config_type, $config_name, var_export( $config_value, true ), $config_transformer_options );
}
$success_message .= ( $config_value ? 'en' : 'dis' ) . 'abled';
} catch ( Exception $e ){
WP_CLI::error( $e->getMessage() );
}
$maintenance_path = get_stylesheet_directory() . '/maintenance/maintenance.php';
if ( ! file_exists( $maintenance_path ) ) {
global $wp_filesystem;
WP_Filesystem();
$wp_filesystem->mkdir( dirname( $maintenance_path ) );
if ( ! $wp_filesystem->put_contents( $maintenance_path, $wp_filesystem->get_contents( __DIR__ . '/maintenance.stub' ) ) ) {
WP_CLI::error( "Error creating file: $maintenance_path" );
} else {
WP_CLI::log( 'Created: ' . $maintenance_path );
}
}
WP_CLI::success( $success_message );
};
WP_CLI::add_command( 'maintenance', $maintenance_command );