Skip to content

Commit

Permalink
Merge branch 'release-1.6.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
thingsym committed Aug 17, 2018
2 parents 6f7cb3e + 54756a8 commit 261cdbc
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ Small patches and bug reports can be submitted a issue tracker in Github. Forkin

## Changelog

* Version 1.6.2
* add prefix into hook tag, change tag name from add_header_vary to multi_device_switcher_add_header_vary
* for php5.3, change called function via hook from anonymous function to public function
* Version 1.6.1
* add test cases
* add add_header_vary filter
Expand Down
39 changes: 23 additions & 16 deletions multi-device-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Multi Device Switcher
* Plugin URI: https://github.com/thingsym/multi-device-switcher
* Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
* Version: 1.6.1
* Version: 1.6.2
* Author: thingsym
* Author URI: http://www.thingslabo.com/
* License: GPL2
Expand Down Expand Up @@ -83,16 +83,11 @@ public function init() {

public function switch_theme() {
if ( isset( $_COOKIE[ $this->cookie_name_disable_switcher ] ) ) {
add_action( 'wp_headers', function () {
setcookie( $this->cookie_name_disable_switcher, null, time() - 3600, '/', '', is_ssl(), false );
} );
add_action( 'wp_headers', array( $this, 'set_cookie_rest_disable_switcher' ));
}

if ( $this->is_disable_switcher() ) {
add_action( 'wp_headers', function () {
setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
setcookie( $this->cookie_name_disable_switcher, 1, null, '/', '', is_ssl(), false );
} );
add_action( 'wp_headers', array( $this, 'set_cookie_enable_disable_switcher' ));
return;
}

Expand Down Expand Up @@ -130,15 +125,10 @@ public function switch_theme() {
add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
add_filter( 'template', array( $this, 'get_template' ) );
add_action( 'wp_footer', array( $this, 'add_pc_switcher' ) );

add_action( 'wp_headers', function () {
setcookie( $this->cookie_name_multi_device_switcher, preg_replace( '/^custom_switcher_/', '', $this->device ), null, '/', '', is_ssl(), false );
} );
add_action( 'wp_headers', array( $this, 'set_cookie_switch_theme' ));
}
else {
add_action( 'wp_headers', function () {
setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
} );
add_action( 'wp_headers', array( $this, 'set_cookie_normal_theme' ));
}

if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
Expand Down Expand Up @@ -251,6 +241,23 @@ public function get_device_theme() {
return;
}

public function set_cookie_rest_disable_switcher() {
setcookie( $this->cookie_name_disable_switcher, null, time() - 3600, '/', '', is_ssl(), false );
}

public function set_cookie_enable_disable_switcher() {
setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
setcookie( $this->cookie_name_disable_switcher, 1, null, '/', '', is_ssl(), false );
}

public function set_cookie_switch_theme() {
setcookie( $this->cookie_name_multi_device_switcher, preg_replace( '/^custom_switcher_/', '', $this->device ), null, '/', '', is_ssl(), false );
}

public function set_cookie_normal_theme() {
setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
}

public function session() {
if ( isset( $_GET['pc-switcher'] ) ) {
setcookie( $this->cookie_name_pc_switcher, $_GET['pc-switcher'] ? 1 : '', null, '/', '', is_ssl(), false );
Expand Down Expand Up @@ -362,7 +369,7 @@ public function shortcode_display_switcher( $atts, $content = '' ) {
*
*/
public function add_header_vary( $headers ) {
$headers['Vary'] = apply_filters( 'add_header_vary', 'User-Agent' );
$headers['Vary'] = apply_filters( 'multi_device_switcher_add_header_vary', 'User-Agent' );
return $headers;
}

Expand Down
2 changes: 1 addition & 1 deletion pc-switcher-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Widget Name: PC Switcher Widget
* Plugin URI: https://github.com/thingsym/multi-device-switcher
* Description: PC Switcher Widget add-on for the Multi Device Switcher. Use this widget to add the PC Switcher to a widget.
* Version: 1.6.1
* Version: 1.6.2
* Author: thingsym
* Author URI: http://www.thingslabo.com/
* License: GPL2
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tags: switcher, theme, ipad, iphone, android, tablet, mobile, game
Requires at least: 3.7
Requires PHP: 5.4
Tested up to: 4.9
Stable tag: 1.6.1
Stable tag: 1.6.2
License: GPL2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -356,6 +356,10 @@ For more information about the Multi Device Switcher Command, see `wp help multi

== Changelog ==

= 1.6.2 =
* add prefix into hook tag, change tag name from add_header_vary to multi_device_switcher_add_header_vary
* for php5.3, change called function via hook from anonymous function to public function

= 1.6.1 =
* add test cases
* add add_header_vary filter
Expand Down
39 changes: 39 additions & 0 deletions tests/test-functions-switch-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function switch_theme_no_match_device() {

$this->assertEquals( 10, has_action( 'init', array( $this->multi_device_switcher, 'session' ) ) );
$this->assertEmpty( $this->multi_device_switcher->device );
$this->assertEquals( 10, has_action( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_normal_theme' ) ) );
}

/**
Expand All @@ -54,6 +55,7 @@ function switch_theme_match_device() {
$this->assertEquals( 10, has_filter( 'template', array( $this->multi_device_switcher, 'get_template' ) ) );
$this->assertEquals( 10, has_action( 'wp_footer', array( $this->multi_device_switcher, 'add_pc_switcher' ) ) );
$this->assertEquals( 'smart', $this->multi_device_switcher->device );
$this->assertEquals( 10, has_action( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_switch_theme' ) ) );

$GLOBALS['_SERVER']['HTTP_USER_AGENT'] = 'test1';
$this->multi_device_switcher->switch_theme();
Expand All @@ -62,6 +64,7 @@ function switch_theme_match_device() {
$this->assertEquals( 10, has_filter( 'template', array( $this->multi_device_switcher, 'get_template' ) ) );
$this->assertEquals( 10, has_action( 'wp_footer', array( $this->multi_device_switcher, 'add_pc_switcher' ) ) );
$this->assertEquals( 'custom_switcher_test', $this->multi_device_switcher->device );
$this->assertEquals( 10, has_action( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_switch_theme' ) ) );

unset($GLOBALS['_SERVER']['HTTP_USER_AGENT']);
}
Expand All @@ -76,10 +79,46 @@ function switch_theme_enable_disable_switcher() {

$this->assertFalse( has_filter( 'stylesheet', array( $this->multi_device_switcher, 'get_stylesheet' ) ) );
$this->assertFalse( has_filter( 'template', array( $this->multi_device_switcher, 'get_template' ) ) );
$this->assertEquals( 10, has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_rest_disable_switcher' ) ) );

unset($GLOBALS['_COOKIE']['disable-switcher']);
}

/**
* @test
* @group switch_theme
*/
function switch_theme_match_disable_switcher() {
$options = array(
'pc_switcher' => 1,
'default_css' => 1,
'theme_smartphone' => 'Twenty Sixteen',
'theme_tablet' => 'Twenty Sixteen',
'theme_mobile' => 'None',
'theme_game' => 'None',
'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko',
'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko',
'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' => "/test\n/abc\n",
'enable_regex' => 0,
'custom_switcher_theme_test' => 'Twenty Sixteen',
'custom_switcher_userAgent_test' => 'test1,test2',
);

update_option( 'multi_device_switcher_options', $options );
$GLOBALS['_SERVER']['REQUEST_URI'] = '/test';
$this->multi_device_switcher->switch_theme();

$this->assertTrue( $this->multi_device_switcher->is_disable_switcher() );
$this->assertFalse( has_action( 'init', array( $this->multi_device_switcher, 'session' ) ) );
$this->assertFalse( has_filter( 'stylesheet', array( $this->multi_device_switcher, 'get_stylesheet' ) ) );
$this->assertFalse( has_filter( 'template', array( $this->multi_device_switcher, 'get_template' ) ) );
$this->assertEquals( 10, has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_enable_disable_switcher' ) ) );

unset($GLOBALS['_SERVER']['REQUEST_URI']);
}

/**
* @test
* @group switch_theme
Expand Down
13 changes: 10 additions & 3 deletions tests/test-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ function shortcode_display_switcher() {
$atts = array(
'device' => '',
);
$this->assertEquals( '', $this->multi_device_switcher->shortcode_display_switcher( $atts ) );
$content = '';
$this->assertEquals( $content, $this->multi_device_switcher->shortcode_display_switcher( $atts, $content ) );

$atts = array(
'device' => '',
);
$content = "test";
$this->assertEquals( $content, $this->multi_device_switcher->shortcode_display_switcher( $atts, $content ) );

$this->multi_device_switcher->device = 'smart';
$atts = array(
Expand All @@ -233,14 +240,14 @@ function add_header_vary() {
$headers = $this->multi_device_switcher->add_header_vary( array() );
$this->assertEquals( 'User-Agent', $headers['Vary'] );

add_filter( 'add_header_vary', function( $value ) {
add_filter( 'multi_device_switcher_add_header_vary', function( $value ) {
return 'Accept-Encoding';
} );

$headers = $this->multi_device_switcher->add_header_vary( array() );
$this->assertEquals( 'Accept-Encoding', $headers['Vary'] );

add_filter( 'add_header_vary', function( $value ) {
add_filter( 'multi_device_switcher_add_header_vary', function( $value ) {
return null;
} );

Expand Down
2 changes: 1 addition & 1 deletion tests/test-wp-cli.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Test_Multi_Device_Switcher_wp_cli extends WP_UnitTestCase {
class Test_Multi_Device_Switcher_WP_Cli extends WP_UnitTestCase {

public function setUp() {
parent::setUp();
Expand Down

0 comments on commit 261cdbc

Please sign in to comment.