PluginProbe
Multi Device Switcher / 1.0.3
Multi Device Switcher v1.0.3
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 / multi-device-switcher.php

multi-device-switcher.php in Multi Device Switcher 1.0.3, at multi-device-switcher.php

541 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Multi Device Switcher
4 Plugin URI: https://github.com/thingsym/multi-device-switcher
5 Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game).
6 Version: 1.0.3
7 Author: thingsym
8 Author URI: http://www.thingslabo.com/
9 License: GPL2
10 */
11
12 /*
13 Copyright 2012 thingsym (http://www.thingslabo.com/)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 */
29
30 class Multi_Device_Switcher {
31
32 function Multi_Device_Switcher() {
33
34 $userAgent = $this->get_options_userAgent();
35
36 if ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
37 $this->device = 'smart';
38 }
39 elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
40 $this->device = 'tablet';
41 }
42 elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
43 $this->device = 'mobile';
44 }
45 elseif ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
46 $this->device = 'game';
47 }
48 else {
49 $this->device = '';
50 }
51
52 if ($this->device) {
53 add_filter('stylesheet', array(&$this, 'get_stylesheet'));
54 add_filter('template', array(&$this, 'get_template'));
55 }
56 }
57
58 function get_options_userAgent() {
59 $options = get_option('multi_device_switcher_options');
60 $default_options = multi_device_switcher_get_default_options();
61
62 if ( ! isset( $options['userAgent_smart'] ) )
63 $options['userAgent_smart'] = $default_options['userAgent_smart'];
64 if ( ! isset( $options['userAgent_tablet'] ) )
65 $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
66 if ( ! isset( $options['userAgent_mobile'] ) )
67 $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
68 if ( ! isset( $options['userAgent_game'] ) )
69 $options['userAgent_game'] = $default_options['userAgent_game'];
70
71 if ( $options['userAgent_smart'] )
72 $userAgent['smart'] = preg_split( "/,\s*/", $options['userAgent_smart'] );
73 if ( $options['userAgent_tablet'] )
74 $userAgent['tablet'] = preg_split( "/,\s*/", $options['userAgent_tablet'] );
75 if ( $options['userAgent_mobile'] )
76 $userAgent['mobile'] = preg_split( "/,\s*/", $options['userAgent_mobile'] );
77 if ( $options['userAgent_game'] )
78 $userAgent['game'] = preg_split( "/,\s*/", $options['userAgent_game'] );
79
80 return $userAgent;
81 }
82
83 function get_stylesheet($stylesheet = '') {
84 $name = $this->get_device_theme();
85
86 if ( empty($name) )
87 return $stylesheet;
88
89 $theme = get_theme($name);
90
91 if ( empty($theme) )
92 return $stylesheet;
93
94 if ( isset($theme['Status']) && $theme['Status'] != 'publish' )
95 return $stylesheet;
96
97 return $theme['Stylesheet'];
98 }
99
100 function get_template($template = '') {
101 $name = $this->get_device_theme();
102
103 if ( empty($name) )
104 return $template;
105
106 $theme = get_theme($name);
107
108 if ( empty( $theme ) )
109 return $template;
110
111 if ( isset($theme['Status']) && $theme['Status'] != 'publish' )
112 return $template;
113
114 return $theme['Template'];
115 }
116
117 function get_device_theme() {
118 $options = get_option('multi_device_switcher_options');
119
120 if ($this->device == 'smart') {
121 return $options['theme_smartphone'];
122 }
123 elseif ($this->device == 'tablet') {
124 return $options['theme_tablet'];
125 }
126 elseif ($this->device == 'mobile') {
127 return $options['theme_mobile'];
128 }
129 elseif ($this->device == 'game') {
130 return $options['theme_game'];
131 }
132
133 return;
134 }
135 }
136
137 if ( ! is_admin() )
138 $multi_device_switcher = new Multi_Device_Switcher;
139
140 /**
141 * Properly enqueue scripts for our multi_device_switcher options page.
142 *
143 * This function is attached to the admin_enqueue_scripts action hook.
144 *
145 * @since 1.0
146 *
147 */
148 function multi_device_switcher_admin_enqueue_scripts( $hook_suffix ) {
149 wp_enqueue_script('jquery-ui-tabs');
150 wp_enqueue_script( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.js', array( 'jquery' ), '2011-08-22' );
151 }
152
153 /**
154 * Properly enqueue styles for our multi_device_switcher options page.
155 *
156 * This function is attached to the admin_enqueue_styles action hook.
157 *
158 * @since 1.0
159 *
160 */
161 function multi_device_switcher_admin_enqueue_styles( $hook_suffix ) {
162 wp_enqueue_style( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.css', false, '2011-08-22' );
163 wp_enqueue_style( 'thickbox', includes_url() . '/js/thickbox/thickbox.css', false, '20090514' );
164 }
165
166 /**
167 * Register the form setting for our multi_device_switcher array.
168 *
169 * This function is attached to the admin_init action hook.
170 *
171 * This call to register_setting() registers a validation callback, multi_device_switcher_validate(),
172 * which is used when the option is saved, to ensure that our option values are complete, properly
173 * formatted, and safe.
174 *
175 * @since 1.0
176 */
177 function multi_device_switcher_init() {
178 // If we have no options in the database, let's add them now.
179 if ( false === multi_device_switcher_get_options() )
180 add_option( 'multi_device_switcher_options' );
181
182 register_setting(
183 'multi_device_switcher', // Options group, see settings_fields() call in multi_device_switcher_render_page()
184 'multi_device_switcher_options', // Database option, see multi_device_switcher_get_options()
185 'multi_device_switcher_validate' // The sanitization callback, see multi_device_switcher_validate()
186 );
187 }
188
189 add_action( 'admin_init', 'multi_device_switcher_init' );
190
191 /**
192 * Change the capability required to save the 'multi_device_switcher' options group.
193 *
194 * @see multi_device_switcher_init() First parameter to register_setting() is the name of the options group.
195 * @see multi_device_switcher_add_page() The edit_theme_options capability is used for viewing the page.
196 *
197 * By default, the options groups for all registered settings require the manage_options capability.
198 * By default, only administrators have either of these capabilities, but the desire here is
199 * to allow for finer-grained control for roles and users.
200 *
201 * @param string $capability The capability used for the page, which is manage_options by default.
202 * @return string The capability to actually use.
203 */
204 function multi_device_switcher_page_capability( $capability ) {
205 return 'edit_theme_options';
206 }
207 add_filter( 'option_page_capability_multi_device_switcher', 'multi_device_switcher_page_capability' );
208
209 /**
210 * Add our options page to the admin menu, including some help documentation.
211 *
212 * This function is attached to the admin_menu action hook.
213 *
214 * @since 1.0
215 */
216 function multi_device_switcher_add_page() {
217 load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
218
219 $theme_page = add_theme_page(
220 __( 'Multi Device Switcher', 'multi-device-switcher' ), // Name of page
221 __( 'Multi Device Switcher', 'multi-device-switcher' ), // Label in menu
222 'manage_options', // Capability required
223 'multi-device-switcher', // Menu slug, used to uniquely identify the page
224 'multi_device_switcher_render_page' // Function that renders the options page
225 );
226
227 if ( ! $theme_page )
228 return;
229
230 $help = '';
231
232 if ($help)
233 add_contextual_help( $theme_page, $help );
234
235 add_action( "admin_print_scripts-$theme_page", 'multi_device_switcher_admin_enqueue_scripts' );
236 add_action( "admin_print_styles-$theme_page", 'multi_device_switcher_admin_enqueue_styles' );
237 }
238 add_action( 'admin_menu', 'multi_device_switcher_add_page' );
239
240 /**
241 * Returns the default options.
242 *
243 * @since 1.0
244 */
245 function multi_device_switcher_get_default_options() {
246 $default_theme_options = array(
247 'theme_smartphone' => 'None',
248 'theme_tablet' => 'None',
249 'theme_mobile' => 'None',
250 'theme_game' => 'None',
251 'userAgent_smart' => 'iPhone, iPod, Android, dream, CUPCAKE, Windows Phone, webOS, BlackBerry8707, BlackBerry9000, BlackBerry9300, BlackBerry9500, BlackBerry9530, BlackBerry9520, BlackBerry9550, BlackBerry9700, BlackBerry9800',
252 'userAgent_tablet' => 'iPad',
253 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
254 'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, Nitro, Nintendo 3DS, Nintendo Wii',
255 );
256
257 return $default_theme_options;
258 }
259
260 /**
261 * Returns the options array.
262 *
263 * @since 1.0
264 */
265 function multi_device_switcher_get_options() {
266 $options = get_option( 'multi_device_switcher_options' );
267 $default_options = multi_device_switcher_get_default_options();
268
269 if ( ! isset( $options['theme_smartphone'] ) )
270 $options['theme_smartphone'] = $default_options['theme_smartphone'];
271 if ( ! isset( $options['theme_tablet'] ) )
272 $options['theme_tablet'] = $default_options['theme_tablet'];
273 if ( ! isset( $options['theme_mobile'] ) )
274 $options['theme_mobile'] = $default_options['theme_mobile'];
275 if ( ! isset( $options['theme_game'] ) )
276 $options['theme_game'] = $default_options['theme_game'];
277
278 if ( ! isset( $options['userAgent_smart'] ) )
279 $options['userAgent_smart'] = $default_options['userAgent_smart'];
280 if ( ! isset( $options['userAgent_tablet'] ) )
281 $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
282 if ( ! isset( $options['userAgent_mobile'] ) )
283 $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
284 if ( ! isset( $options['userAgent_game'] ) )
285 $options['userAgent_game'] = $default_options['userAgent_game'];
286
287 return $options;
288 }
289
290 /**
291 * Rendering Options Setting Page.
292 *
293 * @since 1.0
294 */
295 function multi_device_switcher_render_page() {
296 ?>
297 <div class="wrap" style="width: 60%; float: left;">
298 <div id="icon-themes" class="icon32"><br></div>
299 <h2><?php printf( __( 'Multi Device Switcher', 'multi-device-switcher' ), get_current_theme() ); ?></h2>
300 <?php settings_errors(); ?>
301
302 <form method="post" action="options.php">
303 <?php
304 settings_fields( 'multi_device_switcher' );
305 $options = multi_device_switcher_get_options();
306 ?>
307
308 <div id="admin-tabs">
309 <fieldset id="Theme" class="options">
310 <h3 class="label"><?php _e( 'Theme', 'multi-device-switcher' ); ?></h3>
311 <table class="form-table">
312 <tr><th scope="row"><?php _e( 'Smart Phone Theme', 'multi-device-switcher' ); ?></th>
313 <td>
314
315 <?php
316 $themes = get_themes();
317 $default_theme = get_current_theme();
318
319 if (count($themes) > 1) {
320 $theme_names = array_keys($themes);
321 natcasesort($theme_names);
322 $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
323
324 if (($options['theme_smartphone'] == 'None') || ($options['theme_smartphone'] == '')) {
325 $html .= '<option value="None" selected="selected">None</option>';
326 }
327 else {
328 $html .= '<option value="None">None</option>';
329 }
330
331 foreach ($theme_names as $theme_name) {
332 if ($options['theme_smartphone'] == $theme_name) {
333 $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
334 }
335 else {
336 $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
337 }
338 }
339 $html .= '</select>';
340 }
341 echo $html;
342 ?>
343 </td>
344 </tr>
345 <tr><th scope="row"><?php _e( 'Tablet PC Theme', 'multi-device-switcher' ); ?></th>
346 <td>
347
348 <?php
349 $themes = get_themes();
350 $default_theme = get_current_theme();
351
352 if (count($themes) > 1) {
353 $theme_names = array_keys($themes);
354 natcasesort($theme_names);
355 $html = '<select name="multi_device_switcher_options[theme_tablet]">';
356
357 if (($options['theme_tablet'] == 'None') || ($options['theme_tablet'] == '')) {
358 $html .= '<option value="None" selected="selected">None</option>';
359 }
360 else {
361 $html .= '<option value="None">None</option>';
362 }
363
364 foreach ($theme_names as $theme_name) {
365 if ($options['theme_tablet'] == $theme_name) {
366 $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
367 }
368 else {
369 $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
370 }
371 }
372 $html .= '</select>';
373 }
374 echo $html;
375 ?>
376 </td>
377 </tr>
378 <tr><th scope="row"><?php _e( 'Mobile Phone Theme', 'multi-device-switcher' ); ?></th>
379 <td>
380
381 <?php
382 $themes = get_themes();
383 $default_theme = get_current_theme();
384
385 if (count($themes) > 1) {
386 $theme_names = array_keys($themes);
387 natcasesort($theme_names);
388 $html = '<select name="multi_device_switcher_options[theme_mobile]">';
389
390 if (($options['theme_mobile'] == 'None') || ($options['theme_mobile'] == '')) {
391 $html .= '<option value="None" selected="selected">None</option>';
392 }
393 else {
394 $html .= '<option value="None">None</option>';
395 }
396
397 foreach ($theme_names as $theme_name) {
398 if ($options['theme_mobile'] == $theme_name) {
399 $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
400 }
401 else {
402 $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
403 }
404 }
405 $html .= '</select>';
406 }
407 echo $html;
408 ?>
409 </td>
410 </tr>
411 <tr><th scope="row"><?php _e( 'Game Platforms Theme', 'multi-device-switcher' ); ?></th>
412 <td>
413
414 <?php
415 $themes = get_themes();
416 $default_theme = get_current_theme();
417
418 if (count($themes) > 1) {
419 $theme_names = array_keys($themes);
420 natcasesort($theme_names);
421 $html = '<select name="multi_device_switcher_options[theme_game]">';
422
423 if (($options['theme_game'] == 'None') || ($options['theme_game'] == '')) {
424 $html .= '<option value="None" selected="selected">None</option>';
425 }
426 else {
427 $html .= '<option value="None">None</option>';
428 }
429
430 foreach ($theme_names as $theme_name) {
431 if ($options['theme_game'] == $theme_name) {
432 $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
433 }
434 else {
435 $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
436 }
437 }
438 $html .= '</select>';
439 }
440 echo $html;
441 ?>
442 </td>
443 </tr>
444 </table>
445 </fieldset>
446
447 <fieldset id="UserAgent" class="options">
448 <h3 class="label"><?php _e( 'UserAgent', 'multi-device-switcher' ); ?></h3>
449 <table class="form-table">
450 <tr><th scope="row"><?php _e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
451 <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo $options['userAgent_smart']; ?></textarea></td>
452 </tr>
453 <tr><th scope="row"><?php _e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
454 <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo $options['userAgent_tablet']; ?></textarea></td>
455 </tr>
456 <tr><th scope="row"><?php _e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
457 <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo $options['userAgent_mobile']; ?></textarea></td>
458 </tr>
459 <tr><th scope="row"><?php _e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
460 <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo $options['userAgent_game']; ?></textarea></td>
461 </tr>
462 <tr><th></th>
463 <td><p class="submit"><input type="submit" name="multi_device_switcher_options[restore_UserAgent]" value="<?php _e( 'Reset Settings to Default UserAgent', 'multi-device-switcher' ); ?>"></p></td>
464 </tr>
465 </table>
466 </fieldset>
467 </div>
468 <?php submit_button(); ?>
469 </form>
470 </div>
471
472 <div id="donate" style="width: 36%; float: left; margin: 2em 0;padding: 0.4em;border: 1px solid #ddd;">
473 <h2><?php _e( 'Donationware', 'multi-device-switcher' ); ?></h2>
474 <p><?php _e( 'If you like this plugin, please donate to support development and maintenance.', 'multi-device-switcher' ); ?></p>
475 <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
476 <input type="hidden" name="cmd" value="_s-xclick">
477 <input type="hidden" name="hosted_button_id" value="9L53NELFMHTWW">
478 <table>
479 <tr><td><input type="hidden" name="on0" value="Donationware">Donationware</td></tr><tr><td><select name="os0">
480 <option value="1. Donate">1. Donate $3.00 USD</option>
481 <option value="2. Donate">2. Donate $5.00 USD</option>
482 <option value="3. Donate">3. Donate $7.00 USD</option>
483 <option value="4. Donate" selected="selected">4. Donate $10.00 USD</option>
484 <option value="5. Donate">5. Donate $20.00 USD</option>
485 <option value="6. Donate">6. Donate $30.00 USD</option>
486 <option value="7. Donate">7. Donate $40.00 USD</option>
487 <option value="8. Donate">8. Donate $50.00 USD</option>
488 <option value="9. Donate">9. Donate $60.00 USD</option>
489 <option value="10. Donate">10. Donate $70.00 USD</option>
490 </select> </td></tr>
491 </table>
492 <input type="hidden" name="currency_code" value="USD">
493 <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
494 <img alt="" border="0" src="https://www.paypalobjects.com/ja_JP/i/scr/pixel.gif" width="1" height="1">
495 </form>
496 </div>
497
498 <?php
499 }
500
501 /**
502 * Sanitize and validate form input. Accepts an array, return a sanitized array.
503 *
504 * @see multi_device_switcher_init()
505 *
506 * @since 1.0
507 */
508 function multi_device_switcher_validate( $input ) {
509 $output = $default_options = multi_device_switcher_get_default_options();
510
511 if ( isset( $input['theme_smartphone'] ) )
512 $output['theme_smartphone'] = $input['theme_smartphone'];
513 if ( isset( $input['theme_tablet'] ) )
514 $output['theme_tablet'] = $input['theme_tablet'];
515 if ( isset( $input['theme_mobile'] ) )
516 $output['theme_mobile'] = $input['theme_mobile'];
517 if ( isset( $input['theme_game'] ) )
518 $output['theme_game'] = $input['theme_game'];
519
520 if (isset( $input['restore_UserAgent'] )) {
521 $output['userAgent_smart'] = $default_options['userAgent_smart'];
522 $output['userAgent_tablet'] = $default_options['userAgent_tablet'];
523 $output['userAgent_mobile'] = $default_options['userAgent_mobile'];
524 $output['userAgent_game'] = $default_options['userAgent_game'];
525 }
526 else {
527 if ( isset( $input['userAgent_smart'] ) )
528 $output['userAgent_smart'] = $input['userAgent_smart'];
529 if ( isset( $input['userAgent_tablet'] ) )
530 $output['userAgent_tablet'] = $input['userAgent_tablet'];
531 if ( isset( $input['userAgent_mobile'] ) )
532 $output['userAgent_mobile'] = $input['userAgent_mobile'];
533 if ( isset( $input['userAgent_game'] ) )
534 $output['userAgent_game'] = $input['userAgent_game'];
535 }
536
537 return apply_filters( 'multi_device_switcher_validate', $output, $input, $default_options );
538 }
539
540 ?>
541