-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.php
187 lines (161 loc) · 5.51 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// Ensure we are executed under wp command.
if ( !defined( 'WP_CLI') ) {
return;
}
require_once __DIR__ . '/admin.php';
/**
* Manage assets pack.
*/
class Assets_Pack_Command extends WP_CLI_Command {
/**
* @var Assets_Pack_Admin
*/
protected $admin;
/**
* Creates a command.
*/
public function __construct() {
parent::__construct();
$this->admin = Assets_Pack_Admin::get_instance();
}
/**
* @param boolean $value
* @return string
*/
protected function str_enabled( $value ) {
return $value ? 'enabled' : 'disabled';
}
/**
* @param array $value
* @return string
*/
protected function str_skip( $value ) {
return count( $value ) ? implode( ', ', $value) : '-';
}
/**
* Show JS status.
*/
protected function info_js() {
WP_CLI::log( 'JS aggregation: ' . $this->str_enabled( $this->admin->get_setting( 'enable_js' ) ) );
WP_CLI::log( 'JS debug: ' . $this->str_enabled( $this->admin->get_setting( 'debug_js' ) ) );
WP_CLI::log( 'JS skip: ' . $this->str_skip( $this->admin->get_setting( 'skip_js' ) ) );
}
/**
* Show CSS status.
*/
protected function info_css() {
WP_CLI::log( 'CSS aggregation: ' . $this->str_enabled( $this->admin->get_setting( 'enable_css' ) ) );
WP_CLI::log( 'CSS debug: ' . $this->str_enabled( $this->admin->get_setting( 'debug_css' ) ) );
WP_CLI::log( 'CSS skip: ' . $this->str_skip( $this->admin->get_setting( 'skip_css' ) ) );
WP_CLI::log( 'CSS inline url: ' . $this->str_enabled( $this->admin->get_setting( 'css_inline_url' ) ) );
}
/**
* Show plugin info.
*
* [--assets-dir=<directory>]
*
* [--assets-url=<url>]
*/
public function info( $args, $assoc_args ) {
$this->change_option( 'assets_dir', 'assets-dir', $assoc_args );
$this->change_option( 'assets_url', 'assets-url', $assoc_args );
WP_CLI::log( 'Assets dir: ' . $this->admin->get_setting( 'assets_dir' ) );
WP_CLI::log( 'Assets url: ' . $this->admin->get_setting( 'assets_url' ) );
$this->info_js();
$this->info_css();
}
/**
* Clears assets.
*/
public function clear( $args, $assoc_args ) {
WP_CLI::confirm( 'Are you sure you want to clear the assets ?', $assoc_args );
$result = $this->admin->clear_assets();
if ( $result ) {
WP_CLI::success( 'Done!' );
}
}
/**
* Scripts aggregation.
*
* Show status or change scripts aggregation.
*
* [--enable=<on|off>]
* : Enable of disable scripts aggregation.
*
* [--debug-info=<on|off>]
* : Enable or disable debug mode.
*
* [--skip=<scripts>]
* : Skip following scripts on aggregation. Where <scripts> is a list of script names
* separated by commas. To clear skip list pass empty string: --skip=""
*/
public function js( $args, $assoc_args ) {
$this->change_option( 'enable_js', 'enable', $assoc_args );
$this->change_option( 'debug_js', 'debug-info', $assoc_args );
$this->change_option( 'skip_js', 'skip', $assoc_args );
$this->info_js();
}
/**
* CSS styles aggregation.
*
* Show status or change styles aggregation.
*
* [--enable=<on|off>]
* : Enable or disable styles aggregation.
*
* [--debug-info=<on|off>]
* : Enable or disable debug mode.
*
* [--skip=<styles>]
* : Skip following styles on aggregation. Where <styles> is a list of style names
* separated by commas. To clear skip list pass empty string: --skip=""
*
* [--inline-url=<on|off>]
* : Enable or disable url links convertation inside "url()". Assets must
* be cleared after this setting is changed.
*/
public function css( $args, $assoc_args ) {
$this->change_option( 'enable_css', 'enable', $assoc_args );
$this->change_option( 'debug_css', 'debug-info', $assoc_args );
$this->change_option( 'skip_css', 'skip', $assoc_args );
$this->change_option( 'css_inline_url', 'inline-url', $assoc_args );
$this->info_css();
}
/**
* Change plugin option.
*
* @param string $option Setting option name.
* @param string $cmd_opt Appropriate command line option name.
* @param array $args Command line arguments.
*/
protected function change_option( $option, $cmd_opt, $args ) {
if ( !isset( $args[$cmd_opt] ) ) {
return;
}
$status = true;
$value = $args[$cmd_opt];
switch ( $cmd_opt ) {
case 'enable':
case 'debug-info':
case 'inline-url':
if ( $value === 'on' || $value === 'off' ) {
$value = $value == 'on' ? true : false;
$status = $this->admin->set_setting( $option, $value );
} else {
WP_CLI::error( 'Option "' . $cmd_opt . '" accepts only "on" or "off" value.' );
}
break;
case 'skip':
case 'assets-dir':
case 'assets-url':
$status = $this->admin->set_setting( $option, $value );
break;
}
if ( is_string( $status ) ) {
WP_CLI::error( $status );
}
}
}
// Register 'assets' command.
WP_CLI::add_command( 'assets', new Assets_Pack_Command() );