device = ''; if ( isset( $_COOKIE['disable-switcher'] ) ) { setcookie( 'disable-switcher', null, time() - 3600, '/' ); } if ( $this->get_disable() ) { setcookie( 'disable-switcher', 1, null, '/' ); return; } add_action( 'init', array( &$this, 'session' ) ); $userAgent = $this->get_options_userAgent(); $server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; foreach ( array_reverse( $userAgent ) as $key => $val ) { if ( ! preg_match( '/^custom_switcher_/', $key ) ) { continue; } if ( $userAgent[ $key ] && preg_match( '/' . implode( '|', $userAgent[ $key ] ) . '/i', $server_ua ) ) { $this->device = $key; break; } } if ( ! $this->device ) { if ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $server_ua ) ) { $this->device = 'game'; } elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $server_ua ) ) { $this->device = 'tablet'; } elseif ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $server_ua ) ) { $this->device = 'smart'; } elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $server_ua ) ) { $this->device = 'mobile'; } } if ( $this->device ) { load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' ); add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) ); add_filter( 'template', array( &$this, 'get_template' ) ); add_action( 'wp_footer', array( &$this, 'add_pc_switcher' ) ); setcookie( 'multi-device-switcher', preg_replace( '/^custom_switcher_/', '', $this->device ), null, '/' ); } else { setcookie( 'multi-device-switcher', null, time() - 3600, '/' ); } if ( isset( $_COOKIE['pc-switcher'] ) ) { remove_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) ); remove_filter( 'template', array( &$this, 'get_template' ) ); } } public function get_options_userAgent() { $options = multi_device_switcher_get_options(); $userAgent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'] ); $userAgent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'] ); $userAgent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'] ); $userAgent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'] ); foreach ( $options as $key => $val ) { if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) { continue; } $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key ); $userAgent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val ); } 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 ( 'publish' != $theme->get( 'Status' ) ) { 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 ( 'publish' != $theme->get( 'Status' ) ) { return $template; } return $theme['Template']; } public function get_device_theme() { $options = multi_device_switcher_get_options(); if ( 'smart' == $this->device ) { return $options['theme_smartphone']; } elseif ( 'tablet' == $this->device ) { return $options['theme_tablet']; } elseif ( 'mobile' == $this->device ) { return $options['theme_mobile']; } elseif ( 'game' == $this->device ) { return $options['theme_game']; } else { foreach ( $options as $key => $val ) { if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) { continue; } $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key ); if ( 'custom_switcher_' . $custom_switcher_name == $this->device ) { return $options[ $key ]; } } } return; } public function session() { if ( isset( $_GET['pc-switcher'] ) ) { setcookie( 'pc-switcher', $_GET['pc-switcher'] ? 1 : '', null, '/' ); $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] ); unset( $_GET['pc-switcher'] ); if ( ! empty( $_GET ) ) { $uri = $uri . '?' . http_build_query( $_GET ); } wp_redirect( esc_url( $uri ) ); exit; } } public function add_pc_switcher( $pc_switcher = 0 ) { $options = multi_device_switcher_get_options(); $name = $this->get_device_theme(); if ( $options['pc_switcher'] ) { $pc_switcher = 1; } if ( $pc_switcher && $name && 'None' != $name ) { if ( $options['default_css'] ) { wp_enqueue_style( 'pc-switcher-options', plugins_url() . '/multi-device-switcher/pc-switcher.css', false, '2013-03-20' ); } if ( isset( $_COOKIE['pc-switcher'] ) ) { $uri = get_home_url() . add_query_arg( 'pc-switcher', 0 ); ?>
device ) { return (boolean) 1; } if ( 'custom_switcher_' . $device == $this->device ) { return (boolean) 1; } return (boolean) 0; } public function get_disable( $disable = 0 ) { $options = multi_device_switcher_get_options(); $disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY ); foreach ( $disable_path as $path ) { if ( $options['enable_regex'] ) { if ( preg_match( '/' . $path . '/i', $_SERVER['REQUEST_URI'] ) ) { $disable = 1; break; } } else { if ( preg_match( '/^' . preg_quote( $path , '/' ) . '$/i', $_SERVER['REQUEST_URI'] ) ) { $disable = 1; break; } } } return (boolean) $disable; } } if ( ! is_admin() ) { $multi_device_switcher = new Multi_Device_Switcher(); } /** * Add HTTP/1.1 Vary header. * * @since 1.1.1 * */ function multi_device_switcher_add_header_vary( $headers ) { if ( ! is_admin() ) { $headers['Vary'] = 'User-Agent'; return $headers; } } add_filter( 'wp_headers', 'multi_device_switcher_add_header_vary' ); /** * Add PC Switcher. * * @since 1.2 * */ function multi_device_switcher_add_pc_switcher() { global $multi_device_switcher; $multi_device_switcher->add_pc_switcher( 1 ); } /** * Return boolean whether a particular device. * * @since 1.2.4 * */ if ( ! function_exists( 'is_multi_device' ) ) : function is_multi_device( $device = '' ) { global $multi_device_switcher; return $multi_device_switcher->is_multi_device( $device ); } endif; /** * 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( 'multi-device-switcher-options', plugins_url() . '/multi-device-switcher/multi-device-switcher.js', array( 'jquery', 'jquery-ui-tabs' ), '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', plugins_url() . '/multi-device-switcher/multi-device-switcher.css', false, '2011-08-22' ); } /** * 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' ); add_filter( 'plugin_action_links', 'multi_device_switcher_plugin_action_links', 10, 2 ); $page_hook = 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 ( ! $page_hook ) { return; } add_action( 'load-' . $page_hook , 'multi_device_switcher_page_hook_suffix' ); } add_action( 'admin_menu', 'multi_device_switcher_add_page' ); /** * Page Hook Suffix * * This function is attached to the load-** action hook. * * @since 1.2.4 */ function multi_device_switcher_page_hook_suffix() { add_action( 'admin_enqueue_scripts', 'multi_device_switcher_admin_enqueue_scripts' ); add_action( 'admin_enqueue_scripts', 'multi_device_switcher_admin_enqueue_styles' ); } /** * Add the settings link to the plugin page. * * @since 1.2 */ function multi_device_switcher_plugin_action_links( $links, $file ) { if ( $file != plugin_basename( __FILE__ ) ) { return $links; } $settings_link = '' . __( 'Settings', 'multi-device-switcher' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Returns the default options. * * @since 1.0 */ function multi_device_switcher_get_default_options() { $default_theme_options = array( 'pc_switcher' => 1, 'default_css' => 1, 'theme_smartphone' => 'None', 'theme_tablet' => 'None', 'theme_mobile' => 'None', 'theme_game' => 'None', 'userAgent_smart' => 'iPhone, iPod, Android, dream, CUPCAKE, Windows Phone, webOS, BB10, BlackBerry8707, BlackBerry9000, BlackBerry9300, BlackBerry9500, BlackBerry9530, BlackBerry9520, BlackBerry9550, BlackBerry9700, BlackBerry 93, BlackBerry 97, BlackBerry 99, BlackBerry 98', 'userAgent_tablet' => 'iPad, Kindle, Sony Tablet, Nexus 7', '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, PlayStation 4, Nitro, Nintendo 3DS, Nintendo Wii, Nintendo WiiU, Xbox', 'disable_path' => '', 'enable_regex' => 0, ); 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['pc_switcher'] ) ) { $options['pc_switcher'] = $default_options['pc_switcher']; } if ( ! isset( $options['default_css'] ) ) { $options['default_css'] = $default_options['default_css']; } 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']; } if ( ! isset( $options['disable_path'] ) ) { $options['disable_path'] = $default_options['disable_path']; } if ( ! isset( $options['enable_regex'] ) ) { $options['enable_regex'] = $default_options['enable_regex']; } return $options; } /** * Rendering Options Setting Page. * * @since 1.0 */ function multi_device_switcher_render_page() { ?>