PluginProbe
Multi Device Switcher / 1.5.2
Multi Device Switcher v1.5.2
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 1.5.2, at wp-cli.php

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