Make WordPress Core

Ticket #33193: 33193.3.patch

File 33193.3.patch, 8.4 KB (added by johnbillion, 11 years ago)
  • src/wp-includes/vars.php

     
    1616 */
    1717
    1818global $pagenow,
    19         $is_lynx, $is_gecko, $is_winIE, $is_macIE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_IE,
     19        $is_lynx, $is_gecko, $is_winIE, $is_macIE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_IE, $is_edge,
    2020        $is_apache, $is_IIS, $is_iis7, $is_nginx;
    2121
    2222// On which page are we ?
     
    4848unset($self_matches);
    4949
    5050// Simple browser detection
    51 $is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_iphone = false;
     51$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_edge = $is_iphone = false;
    5252
    5353if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
    5454        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false ) {
    5555                $is_lynx = true;
     56        } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Edge') !== false ) {
     57                $is_edge = true;
    5658        } elseif ( stripos($_SERVER['HTTP_USER_AGENT'], 'chrome') !== false ) {
    5759                if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) {
    5860                        $is_admin = is_admin();
     
    114116 * @global bool $is_iis7
    115117 */
    116118$is_iis7 = $is_IIS && intval( substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/' ) + 14 ) ) >= 7;
    117 
    118 /**
    119  * Test if the current browser runs on a mobile device (smart phone, tablet, etc.)
    120  *
    121  * @staticvar bool $is_mobile
    122  *
    123  * @return bool
    124  */
    125 function wp_is_mobile() {
    126         static $is_mobile = null;
    127 
    128         if ( isset( $is_mobile ) ) {
    129                 return $is_mobile;
    130         }
    131 
    132         if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
    133                 $is_mobile = false;
    134         } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
    135                 || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
    136                 || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
    137                 || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
    138                 || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
    139                 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
    140                 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
    141                         $is_mobile = true;
    142         } else {
    143                 $is_mobile = false;
    144         }
    145 
    146         return $is_mobile;
    147 }
  • src/wp-includes/functions.php

     
    49954995        </script>
    49964996        <?php
    49974997}
     4998
     4999/**
     5000 * Test if the current browser runs on a mobile device (smart phone, tablet, etc.)
     5001 *
     5002 * @staticvar bool $is_mobile
     5003 *
     5004 * @return bool
     5005 */
     5006function wp_is_mobile() {
     5007        static $is_mobile = null;
     5008
     5009        if ( isset( $is_mobile ) ) {
     5010                return $is_mobile;
     5011        }
     5012
     5013        if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
     5014                $is_mobile = false;
     5015        } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
     5016                || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
     5017                || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
     5018                || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
     5019                || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
     5020                || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
     5021                || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
     5022                        $is_mobile = true;
     5023        } else {
     5024                $is_mobile = false;
     5025        }
     5026
     5027        return $is_mobile;
     5028}
  • tests/phpunit/tests/vars.php

     
     1<?php
     2
     3// test behaviour of wp-includes/vars.php
     4/**
     5 * @group vars
     6 */
     7class Tests_Vars extends WP_UnitTestCase {
     8
     9        /**
     10         * @ticket 20014
     11         * @ticket 33193
     12         * @dataProvider userAgentData
     13         */
     14        public function testUserAgentDetection( $name, $user_agent, array $expected ) {
     15
     16                $_SERVER['HTTP_USER_AGENT'] = $user_agent;
     17
     18                require ABSPATH . '/wp-includes/vars.php';
     19
     20                $conditionals = array(
     21                        'is_lynx',
     22                        'is_gecko',
     23                        'is_winIE',
     24                        'is_macIE',
     25                        'is_opera',
     26                        'is_NS4',
     27                        'is_safari',
     28                        'is_chrome',
     29                        'is_iphone',
     30                        'is_IE',
     31                        'is_edge',
     32                );
     33
     34                foreach ( $conditionals as $cond ) {
     35                        if ( in_array( $cond, $expected ) ) {
     36                                $this->assertTrue( $$cond, sprintf( '%s() should be true for %s', $cond, $name ) );
     37                        } else {
     38                                $this->assertFalse( $$cond, sprintf( '%s() should be false for %s', $cond, $name ) );
     39                        }
     40                }
     41
     42        }
     43
     44        public function userAgentData() {
     45                return array(
     46                        array(
     47                                // https://udger.com/resources/ua-list/browser-detail?browser=Microsoft%20Edge
     48                                'Edge - Windows',
     49                                'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136',
     50                                array( 'is_edge' ),
     51                        ),
     52                        array(
     53                                'IE11 - Windows',
     54                                'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
     55                                array( 'is_IE', 'is_winIE' ),
     56                        ),
     57                        array(
     58                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     59                                'Firefox - Windows',
     60                                'Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0',
     61                                array( 'is_gecko' ),
     62                        ),
     63                        array(
     64                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     65                                'Firefox - Linux',
     66                                'Mozilla/5.0 (X11; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
     67                                array( 'is_gecko' ),
     68                        ),
     69                        array(
     70                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     71                                'Firefox - OS X',
     72                                'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:39.0) Gecko/20100101 Firefox/39.0',
     73                                array( 'is_gecko' ),
     74                        ),
     75                        array(
     76                                // https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
     77                                'Firefox - Android phone',
     78                                'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0',
     79                                array( 'is_gecko' ),
     80                        ),
     81                        array(
     82                                // https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
     83                                'Firefox - Android tablet',
     84                                'Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0',
     85                                array( 'is_gecko' ),
     86                        ),
     87                        array(
     88                                // https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
     89                                'Firefox - Firefox OS phone',
     90                                'Mozilla/5.0 (Mobile; rv:26.0) Gecko/26.0 Firefox/26.0',
     91                                array( 'is_gecko' ),
     92                        ),
     93                        array(
     94                                // https://developer.mozilla.org/en-US/docs/Web/HTTP/Gecko_user_agent_string_reference
     95                                'Firefox - Firefox OS tablet',
     96                                'Mozilla/5.0 (Tablet; rv:26.0) Gecko/26.0 Firefox/26.0',
     97                                array( 'is_gecko' ),
     98                        ),
     99                        array(
     100                                // https://developer.chrome.com/multidevice/user-agent
     101                                'Chrome - Android phone',
     102                                'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19',
     103                                array( 'is_chrome' ),
     104                        ),
     105                        array(
     106                                // https://developer.chrome.com/multidevice/user-agent
     107                                'Chrome - Android tablet',
     108                                'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Safari/535.19',
     109                                array( 'is_chrome' ),
     110                        ),
     111                        array(
     112                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     113                                'Chrome - Windows',
     114                                'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
     115                                array( 'is_chrome' ),
     116                        ),
     117                        array(
     118                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     119                                'Chrome - OS X',
     120                                'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36',
     121                                array( 'is_chrome' ),
     122                        ),
     123                        array(
     124                                // https://github.com/gorhill/uMatrix/wiki/Latest-user-agent-strings
     125                                'Lynx',
     126                                'Lynx/2.8.3dev.6 libwww-FM/2.14',
     127                                array( 'is_lynx' ),
     128                        ),
     129                        array(
     130                                // https://udger.com/resources/ua-list/browser-detail?browser=Safari
     131                                'Safari - OS X',
     132                                'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/600.3.10 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.10',
     133                                array( 'is_safari' ),
     134                        ),
     135                        array(
     136                                // http://www.useragentstring.com/pages/Safari/
     137                                'Safari - iOS',
     138                                'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25',
     139                                array( 'is_safari', 'is_iphone' ),
     140                        ),
     141                );
     142        }
     143
     144}