get_options_userAgent(); if ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) { $this->device = 'smart'; } elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) { $this->device = 'tablet'; } elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) { $this->device = 'mobile'; } elseif ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) { $this->device = 'game'; } else { $this->device = ''; } if ($this->device) { add_filter('stylesheet', array(&$this, 'get_stylesheet')); add_filter('template', array(&$this, 'get_template')); } } public function get_options_userAgent() { $options = get_option('multi_device_switcher_options'); $default_options = multi_device_switcher_get_default_options(); if ( ! isset( $options['userAgent_smart'] ) ) $options['userAgent_smart'] = $default_options['userAgent_smart']; if ( ! isset( $options['userAgent_tablet'] ) ) $options['userAgent_tablet'] = $default_options['userAgent_tablet']; if ( ! isset( $options['userAgent_mobile'] ) ) $options['userAgent_mobile'] = $default_options['userAgent_mobile']; if ( ! isset( $options['userAgent_game'] ) ) $options['userAgent_game'] = $default_options['userAgent_game']; if ( $options['userAgent_smart'] ) $userAgent['smart'] = preg_split( "/,\s*/", $options['userAgent_smart'] ); if ( $options['userAgent_tablet'] ) $userAgent['tablet'] = preg_split( "/,\s*/", $options['userAgent_tablet'] ); if ( $options['userAgent_mobile'] ) $userAgent['mobile'] = preg_split( "/,\s*/", $options['userAgent_mobile'] ); if ( $options['userAgent_game'] ) $userAgent['game'] = preg_split( "/,\s*/", $options['userAgent_game'] ); return $userAgent; } public function get_stylesheet($stylesheet = '') { $name = $this->get_device_theme(); if ( empty($name) ) return $stylesheet; $themes = wp_get_themes(); foreach ( $themes as $t ) { if ($name == $t->get('Name')) { $theme = $t; break; } } if ( empty($theme) ) return $stylesheet; if ( $theme->get('Status') != 'publish' ) return $stylesheet; return $theme['Stylesheet']; } public function get_template($template = '') { $name = $this->get_device_theme(); if ( empty($name) ) return $template; $themes = wp_get_themes(); foreach ( $themes as $t ) { if ($name == $t->get('Name')) { $theme = $t; break; } } if ( empty( $theme ) ) return $template; if ( $theme->get('Status') != 'publish' ) return $template; return $theme['Template']; } public function get_device_theme() { $options = get_option('multi_device_switcher_options'); if ($this->device == 'smart') { return $options['theme_smartphone']; } elseif ($this->device == 'tablet') { return $options['theme_tablet']; } elseif ($this->device == 'mobile') { return $options['theme_mobile']; } elseif ($this->device == 'game') { return $options['theme_game']; } return; } } if ( ! is_admin() ) $multi_device_switcher = new Multi_Device_Switcher(); /** * Properly enqueue scripts for our multi_device_switcher options page. * * This function is attached to the admin_enqueue_scripts action hook. * * @since 1.0 * */ function multi_device_switcher_admin_enqueue_scripts( $hook_suffix ) { wp_enqueue_script('jquery-ui-tabs'); wp_enqueue_script( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.js', array( 'jquery' ), '2011-08-22' ); } /** * Properly enqueue styles for our multi_device_switcher options page. * * This function is attached to the admin_enqueue_styles action hook. * * @since 1.0 * */ function multi_device_switcher_admin_enqueue_styles( $hook_suffix ) { wp_enqueue_style( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.css', false, '2011-08-22' ); wp_enqueue_style( 'thickbox', includes_url() . '/js/thickbox/thickbox.css', false, '20090514' ); } /** * Register the form setting for our multi_device_switcher array. * * This function is attached to the admin_init action hook. * * This call to register_setting() registers a validation callback, multi_device_switcher_validate(), * which is used when the option is saved, to ensure that our option values are complete, properly * formatted, and safe. * * @since 1.0 */ function multi_device_switcher_init() { // If we have no options in the database, let's add them now. if ( false === multi_device_switcher_get_options() ) add_option( 'multi_device_switcher_options' ); register_setting( 'multi_device_switcher', // Options group, see settings_fields() call in multi_device_switcher_render_page() 'multi_device_switcher_options', // Database option, see multi_device_switcher_get_options() 'multi_device_switcher_validate' // The sanitization callback, see multi_device_switcher_validate() ); } add_action( 'admin_init', 'multi_device_switcher_init' ); /** * Change the capability required to save the 'multi_device_switcher' options group. * * @see multi_device_switcher_init() First parameter to register_setting() is the name of the options group. * @see multi_device_switcher_add_page() The edit_theme_options capability is used for viewing the page. * * By default, the options groups for all registered settings require the manage_options capability. * By default, only administrators have either of these capabilities, but the desire here is * to allow for finer-grained control for roles and users. * * @param string $capability The capability used for the page, which is manage_options by default. * @return string The capability to actually use. */ function multi_device_switcher_page_capability( $capability ) { return 'edit_theme_options'; } add_filter( 'option_page_capability_multi_device_switcher', 'multi_device_switcher_page_capability' ); /** * Add our options page to the admin menu, including some help documentation. * * This function is attached to the admin_menu action hook. * * @since 1.0 */ function multi_device_switcher_add_page() { load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages'); $theme_page = add_theme_page( __( 'Multi Device Switcher', 'multi-device-switcher' ), // Name of page __( 'Multi Device Switcher', 'multi-device-switcher' ), // Label in menu 'manage_options', // Capability required 'multi-device-switcher', // Menu slug, used to uniquely identify the page 'multi_device_switcher_render_page' // Function that renders the options page ); if ( ! $theme_page ) return; $help = ''; if ($help) add_contextual_help( $theme_page, $help ); add_action( "admin_print_scripts-$theme_page", 'multi_device_switcher_admin_enqueue_scripts' ); add_action( "admin_print_styles-$theme_page", 'multi_device_switcher_admin_enqueue_styles' ); } add_action( 'admin_menu', 'multi_device_switcher_add_page' ); /** * Returns the default options. * * @since 1.0 */ function multi_device_switcher_get_default_options() { $default_theme_options = array( 'theme_smartphone' => 'None', 'theme_tablet' => 'None', 'theme_mobile' => 'None', 'theme_game' => 'None', 'userAgent_smart' => 'iPhone, iPod, Android, dream, CUPCAKE, Windows Phone, webOS, BlackBerry8707, BlackBerry9000, BlackBerry9300, BlackBerry9500, BlackBerry9530, BlackBerry9520, BlackBerry9550, BlackBerry9700, BlackBerry9800', 'userAgent_tablet' => 'iPad', 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', 'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, Nitro, Nintendo 3DS, Nintendo Wii', ); return $default_theme_options; } /** * Returns the options array. * * @since 1.0 */ function multi_device_switcher_get_options() { $options = get_option( 'multi_device_switcher_options' ); $default_options = multi_device_switcher_get_default_options(); if ( ! isset( $options['theme_smartphone'] ) ) $options['theme_smartphone'] = $default_options['theme_smartphone']; if ( ! isset( $options['theme_tablet'] ) ) $options['theme_tablet'] = $default_options['theme_tablet']; if ( ! isset( $options['theme_mobile'] ) ) $options['theme_mobile'] = $default_options['theme_mobile']; if ( ! isset( $options['theme_game'] ) ) $options['theme_game'] = $default_options['theme_game']; if ( ! isset( $options['userAgent_smart'] ) ) $options['userAgent_smart'] = $default_options['userAgent_smart']; if ( ! isset( $options['userAgent_tablet'] ) ) $options['userAgent_tablet'] = $default_options['userAgent_tablet']; if ( ! isset( $options['userAgent_mobile'] ) ) $options['userAgent_mobile'] = $default_options['userAgent_mobile']; if ( ! isset( $options['userAgent_game'] ) ) $options['userAgent_game'] = $default_options['userAgent_game']; return $options; } /** * Rendering Options Setting Page. * * @since 1.0 */ function multi_device_switcher_render_page() { ?>

get('Name') ); ?>

get('Name'); $themes = wp_get_themes(); $theme_names = array(); if (count($themes)) { foreach ( $themes as $t ) { $theme_names[] = $t->get('Name'); } natcasesort($theme_names); } ?>

'; if (($options['theme_smartphone'] == 'None') || ($options['theme_smartphone'] == '')) { $html .= ''; } else { $html .= ''; } foreach ($theme_names as $theme_name) { if ($default_theme == $theme_name) continue; if ($options['theme_smartphone'] == $theme_name) { $html .= ''; } else { $html .= ''; } } $html .= ''; } echo $html; ?>
'; if (($options['theme_tablet'] == 'None') || ($options['theme_tablet'] == '')) { $html .= ''; } else { $html .= ''; } foreach ($theme_names as $theme_name) { if ($default_theme == $theme_name) continue; if ($options['theme_tablet'] == $theme_name) { $html .= ''; } else { $html .= ''; } } $html .= ''; } echo $html; ?>
'; if (($options['theme_mobile'] == 'None') || ($options['theme_mobile'] == '')) { $html .= ''; } else { $html .= ''; } foreach ($theme_names as $theme_name) { if ($default_theme == $theme_name) continue; if ($options['theme_mobile'] == $theme_name) { $html .= ''; } else { $html .= ''; } } $html .= ''; } echo $html; ?>
'; if (($options['theme_game'] == 'None') || ($options['theme_game'] == '')) { $html .= ''; } else { $html .= ''; } foreach ($theme_names as $theme_name) { if ($default_theme == $theme_name) continue; if ($options['theme_game'] == $theme_name) { $html .= ''; } else { $html .= ''; } } $html .= ''; } echo $html; ?>