PluginProbe
Multi Device Switcher / trunk
Multi Device Switcher vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 All 34 releases
multi-device-switcher / wp-cli.php

wp-cli.php in Multi Device Switcher trunk, at wp-cli.php

485 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Multi Device Switcher Command
4 *
5 * @package Multi_Device_Switcher
6 */
7
8 /**
9 * Core class Multi_Device_Switcher_Command
10 *
11 * @since 1.0.0
12 */
13 class Multi_Device_Switcher_Command extends WP_CLI_Command {
14
15 /**
16 * Public variable.
17 *
18 * @access public
19 *
20 * @var string $option_name The option name
21 */
22 public $option_name = 'multi_device_switcher_options';
23
24 /**
25 * Get status of settings
26 *
27 * ## EXAMPLES
28 *
29 * wp multi-device status
30 */
31 public function status( $args, $assoc_args ) {
32 $options = get_option( $this->option_name );
33 $rows = array();
34
35 $slug_table = array( 'None' => '' );
36 $themes = wp_get_themes();
37 foreach ( $themes as $theme_slug => $header ) {
38 $slug_table[ $header->get( 'Name' ) ] = $theme_slug;
39 }
40
41 $rows[] = array(
42 'Device' => 'smartphone (Smart Phone)',
43 'Theme' => $options['theme_smartphone'],
44 'Slug' => $slug_table[ $options['theme_smartphone'] ],
45 'UserAgent' => $options['userAgent_smart'],
46 );
47 $rows[] = array(
48 'Device' => 'tablet (Tablet PC)',
49 'Theme' => $options['theme_tablet'],
50 'Slug' => $slug_table[ $options['theme_tablet'] ],
51 'UserAgent' => $options['userAgent_tablet'],
52 );
53 $rows[] = array(
54 'Device' => 'mobile (Mobile Phone)',
55 'Theme' => $options['theme_mobile'],
56 'Slug' => $slug_table[ $options['theme_mobile'] ],
57 'UserAgent' => $options['userAgent_mobile'],
58 );
59 $rows[] = array(
60 'Device' => 'game (Game Platforms)',
61 'Theme' => $options['theme_game'],
62 'Slug' => $slug_table[ $options['theme_game'] ],
63 'UserAgent' => $options['userAgent_game'],
64 );
65
66 foreach ( $options as $custom_switcher_option => $custom_switcher_theme ) {
67 if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
68 continue;
69 }
70
71 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
72
73 $rows[] = array(
74 'Device' => $custom_switcher_name,
75 'Theme' => $options[ 'custom_switcher_theme_' . $custom_switcher_name ],
76 'Slug' => $slug_table[ $options[ 'custom_switcher_theme_' . $custom_switcher_name ] ],
77 'UserAgent' => $options[ 'custom_switcher_userAgent_' . $custom_switcher_name ],
78 );
79 }
80
81 $default_theme = wp_get_theme()->get( 'Name' );
82 $default_theme .= ' | ';
83 $default_theme .= get_stylesheet();
84 WP_CLI::line( 'Active Theme: ' . $default_theme );
85
86 WP_CLI\Utils\format_items( 'table', $rows, array( 'Device', 'Theme', 'Slug', 'UserAgent' ) );
87
88 $line = '';
89 $line .= 'PC Switcher: ';
90 $line .= $options['pc_switcher'] ? 'on' : 'off';
91 $line .= "\n";
92 $line .= 'default CSS: ';
93 $line .= $options['default_css'] ? 'on' : 'off';
94
95 WP_CLI::line( $line );
96 }
97
98 /**
99 * Get or Switch a theme
100 *
101 * ## OPTIONS
102 *
103 * <device>
104 * : The name of device or Custom Switcher
105 *
106 * [<slug>]
107 * : The slug of theme
108 * input 'None', if you want to without theme
109 *
110 * [--theme=<theme>]
111 * : The name of theme
112 * input 'None', if you want to without theme
113 *
114 * ## EXAMPLES
115 *
116 * # get a theme|slug of smartphone
117 * wp multi-device theme smartphone
118 *
119 * # switch twentyfifteen in theme of smartphone using theme slug
120 * wp multi-device theme smartphone twentyfifteen
121 *
122 * # switch twentyfifteen in theme of smartphone using theme argument
123 * wp multi-device theme smartphone --theme='Twenty Fifteen'
124 *
125 * @synopsis <device> [<slug>] [--theme=<theme>]
126 */
127 public function theme( $args, $assoc_args ) {
128 $name = isset( $args[0] ) ? $args[0] : null;
129 $slug = isset( $args[1] ) ? $args[1] : null;
130 $theme = isset( $assoc_args['theme'] ) ? $assoc_args['theme'] : null;
131
132 $options = get_option( $this->option_name );
133
134 if ( isset( $slug ) ) {
135 if ( '' === $slug || 'None' === $slug ) {
136 $theme = 'None';
137 }
138 }
139
140 $slug_table = array( 'None' => '' );
141 $themes = wp_get_themes();
142 foreach ( $themes as $theme_slug => $header ) {
143 $slug_table[ $header->get( 'Name' ) ] = $theme_slug;
144 if ( $slug === $theme_slug ) {
145 $theme = $header->get( 'Name' );
146 }
147 }
148
149 if ( isset( $slug ) && is_null( $theme ) ) {
150 WP_CLI::error( $slug . ' theme is not installed' );
151 }
152
153 if ( isset( $theme ) ) {
154 $default_theme = wp_get_theme()->get( 'Name' );
155 if ( $default_theme === $theme ) {
156 WP_CLI::error( $theme . ' theme is in active' );
157 }
158
159 if ( ! isset( $slug_table[ $theme ] ) ) {
160 WP_CLI::error( $theme . ' theme is not installed' );
161 }
162
163 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
164 if ( 'smart' === $name ) {
165 $name = 'smartphone';
166 }
167 $options[ 'theme_' . $name ] = $theme;
168
169 update_option( $this->option_name, $options );
170 WP_CLI::success( 'switch ' . $name . ' theme to ' . $theme );
171 }
172 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
173 $options[ 'custom_switcher_theme_' . $name ] = $theme;
174
175 update_option( $this->option_name, $options );
176 WP_CLI::success( 'switch ' . $name . ' theme to ' . $theme );
177 }
178 else {
179 WP_CLI::error( $name . ' don\'t exist' );
180 }
181 }
182 // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
183 else {
184 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
185 if ( 'smart' === $name ) {
186 $name = 'smartphone';
187 }
188 WP_CLI::success( $options[ 'theme_' . $name ] . ' | ' . $slug_table[ $options[ 'theme_' . $name ] ] );
189 }
190 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
191 WP_CLI::success( $options[ 'custom_switcher_theme_' . $name ] . ' | ' . $slug_table[ $options[ 'custom_switcher_theme_' . $name ] ] );
192 }
193 else {
194 WP_CLI::error( $name . ' don\'t exist' );
195 }
196 }
197 }
198
199 /**
200 * Get or Set UserAgent
201 *
202 * ## OPTIONS
203 *
204 * <device>
205 * : The name of device or Custom Switcher
206 *
207 * [<UserAgent>]
208 * : UserAgent
209 * Comma-separated values (csv) format
210 *
211 * ## EXAMPLES
212 *
213 * # get UserAgent of tablet
214 * wp multi-device useragent tablet
215 *
216 * # set UserAgent in theme of tablet
217 * wp multi-device useragent tablet 'iPad, Kindle, Sony Tablet, Nexus 7'
218 *
219 * @synopsis <device> [<UserAgent>]
220 */
221 public function useragent( $args, $assoc_args ) {
222 $name = isset( $args[0] ) ? $args[0] : null;
223 $useragent = isset( $args[1] ) ? $args[1] : null;
224
225 $options = get_option( $this->option_name );
226
227 if ( isset( $useragent ) ) {
228 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
229 if ( 'smartphone' === $name ) {
230 $name = 'smart';
231 }
232 $options[ 'userAgent_' . $name ] = $useragent;
233
234 update_option( $this->option_name, $options );
235 WP_CLI::success( 'set ' . $name . ' UserAgent to ' . $useragent );
236 }
237 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
238 $options[ 'custom_switcher_userAgent_' . $name ] = $useragent;
239
240 update_option( $this->option_name, $options );
241 WP_CLI::success( 'set ' . $name . ' UserAgent to ' . $useragent );
242 }
243 else {
244 WP_CLI::error( $name . ' don\'t exist' );
245 }
246 }
247 // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
248 else {
249 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
250 if ( 'smartphone' === $name ) {
251 $name = 'smart';
252 }
253 WP_CLI::success( $options[ 'userAgent_' . $name ] );
254 }
255 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
256 WP_CLI::success( $options[ 'custom_switcher_userAgent_' . $name ] );
257 }
258 else {
259 WP_CLI::error( $name . ' don\'t exist' );
260 }
261 }
262 }
263
264 /**
265 * Reset Settings to Default UserAgent
266 *
267 * ## EXAMPLES
268 *
269 * wp multi-device reset
270 *
271 * @synopsis
272 */
273 public function reset( $args, $assoc_args ) {
274 $options = get_option( $this->option_name );
275 $default_options = multi_device_switcher_get_default_options();
276
277 $options['userAgent_smart'] = $default_options['userAgent_smart'];
278 $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
279 $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
280 $options['userAgent_game'] = $default_options['userAgent_game'];
281
282 update_option( $this->option_name, $options );
283 WP_CLI::success( 'reset Settings to Default UserAgent' );
284 }
285
286 /**
287 * Add Custom Switcher
288 *
289 * ## OPTIONS
290 *
291 * <device>
292 * : The name of Custom Switcher
293 * 20 characters max, alphanumeric
294 *
295 * [<slug>]
296 * : The slug of theme
297 *
298 * [<UserAgent>]
299 * : UserAgent
300 * Comma-separated values (csv) format
301 *
302 * [--theme=<theme>]
303 * : The name of theme
304 *
305 * ## EXAMPLES
306 *
307 * # add example Custom Switcher
308 * wp multi-device add example
309 *
310 * # add example Custom Switcher. set twentyfifteen theme and UserAgent using theme slug
311 * wp multi-device add example twentyfifteen 'iPad, Kindle, Sony Tablet, Nexus 7'
312 *
313 * # add example Custom Switcher. set twentyfifteen theme and UserAgent using theme argument
314 * wp multi-device add example --theme='Twenty Fifteen'
315 *
316 * @synopsis <device> [<slug>] [<UserAgent>] [--theme=<theme>]
317 */
318 public function add( $args, $assoc_args ) {
319 $name = isset( $args[0] ) ? $args[0] : null;
320 $slug = isset( $args[1] ) ? $args[1] : null;
321 $useragent = isset( $args[2] ) ? $args[2] : null;
322 $theme = isset( $assoc_args['theme'] ) ? $assoc_args['theme'] : null;
323
324 if ( ! preg_match( '/^[A-Za-z0-9]{1,20}$/', $name ) ) {
325 WP_CLI::error( '20 characters max, alphanumeric' );
326 }
327
328 $slug_table = array( 'None' => '' );
329 $themes = wp_get_themes();
330 foreach ( $themes as $theme_slug => $header ) {
331 $slug_table[ $header->get( 'Name' ) ] = $theme_slug;
332 if ( ! isset( $assoc_args['theme'] ) && $slug === $theme_slug ) {
333 $theme = $header->get( 'Name' );
334 }
335 }
336
337 $options = get_option( $this->option_name );
338 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
339 WP_CLI::error( 'Default Switcher can\'t add' );
340 }
341 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
342 WP_CLI::error( 'Custom Switcher already exists' );
343 }
344 else {
345 $default_theme = wp_get_theme()->get( 'Name' );
346 if ( $default_theme === $theme ) {
347 WP_CLI::error( $theme . ' theme is in active' );
348 }
349
350 if ( isset( $slug ) || isset( $assoc_args['theme'] ) ) {
351 if ( isset( $slug_table[ $theme ] ) ) {
352 $options[ 'custom_switcher_theme_' . $name ] = $theme;
353 }
354 else {
355 WP_CLI::error( $slug . ' theme is not installed' );
356 }
357 }
358 else {
359 $options[ 'custom_switcher_theme_' . $name ] = 'None';
360 }
361
362 $options[ 'custom_switcher_userAgent_' . $name ] = isset( $useragent ) ? $useragent : '';
363
364 update_option( $this->option_name, $options );
365 WP_CLI::success( 'add ' . $name . ' Custom Switcher' );
366 }
367 }
368
369 /**
370 * Delete Custom Switcher
371 *
372 * ## OPTIONS
373 *
374 * <device>
375 * : The name of Custom Switcher
376 *
377 * ## EXAMPLES
378 *
379 * # delete example Custom Switcher
380 * wp multi-device delete example
381 *
382 * @synopsis <device>
383 */
384 public function delete( $args, $assoc_args ) {
385 $name = isset( $args[0] ) ? $args[0] : null;
386
387 $options = get_option( $this->option_name );
388
389 if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) {
390 WP_CLI::error( 'Default Switcher can\'t delete' );
391 }
392 elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) {
393 unset( $options[ 'custom_switcher_theme_' . $name ] );
394 unset( $options[ 'custom_switcher_userAgent_' . $name ] );
395
396 update_option( $this->option_name, $options );
397 WP_CLI::success( 'delete ' . $name . ' Custom Switcher' );
398 }
399 else {
400 WP_CLI::error( 'Custom Switcher don\'t exist' );
401 }
402 }
403
404 /**
405 * Turn on/off PC Switcher
406 *
407 * ## OPTIONS
408 *
409 * <flag>
410 * : on
411 * off
412 *
413 * ## EXAMPLES
414 *
415 * # turn on PC Switcher
416 * wp multi-device pc-switcher on
417 *
418 * # turn off PC Switcher
419 * wp multi-device pc-switcher off
420 *
421 * @synopsis <flag>
422 * @subcommand pc-switcher
423 */
424 public function pc_switcher( $args, $assoc_args ) {
425 $flag = isset( $args[0] ) ? $args[0] : null;
426
427 $options = get_option( $this->option_name );
428
429 if ( 'on' === $flag ) {
430 $options['pc_switcher'] = 1;
431 update_option( $this->option_name, $options );
432 WP_CLI::success( 'turn on PC Switcher' );
433 }
434 elseif ( 'off' === $flag ) {
435 $options['pc_switcher'] = 0;
436 update_option( $this->option_name, $options );
437 WP_CLI::success( 'turn off PC Switcher' );
438 }
439 else {
440 WP_CLI::error( 'Invalid flag' );
441 }
442 }
443
444 /**
445 * Turn on/off default CSS
446 *
447 * ## OPTIONS
448 *
449 * <flag>
450 * : on
451 * off
452 *
453 * ## EXAMPLES
454 *
455 * # turn on default CSS
456 * wp multi-device css on
457 *
458 * # turn off default CSS
459 * wp multi-device css off
460 *
461 * @synopsis <flag>
462 */
463 public function css( $args, $assoc_args ) {
464 $flag = isset( $args[0] ) ? $args[0] : null;
465
466 $options = get_option( $this->option_name );
467
468 if ( 'on' === $flag ) {
469 $options['default_css'] = 1;
470 update_option( $this->option_name, $options );
471 WP_CLI::success( 'turn on default CSS' );
472 }
473 elseif ( 'off' === $flag ) {
474 $options['default_css'] = 0;
475 update_option( $this->option_name, $options );
476 WP_CLI::success( 'turn off default CSS' );
477 }
478 else {
479 WP_CLI::error( 'Invalid flag' );
480 }
481 }
482 }
483
484 WP_CLI::add_command( 'multi-device', 'Multi_Device_Switcher_Command' );
485