PluginProbe
Multi Device Switcher / 1.0.4
Multi Device Switcher v1.0.4
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.4, at multi-device-switcher.php

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