WordPress.org

Making WordPress.org

Changeset 5962


Ignore:
Timestamp:
09/25/2017 04:24:26 PM (2 years ago)
Author:
coffee2code
Message:

Browse Happy API: Add data field to indicate if a given browser is running on a mobile platform.

  • Adds 'mobile' field to data array
  • If no platform is discernible for a mobile browser, generically sets platform as 'Mobile'
  • Adds unit test
Location:
sites/trunk/api.wordpress.org/public_html/core/browse-happy/1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/core/browse-happy/1.0/parse.php

    r5961 r5962  
    2222 *     @type bool   $upgrade         Is there an update available for the browser?
    2323 *     @type bool   $insecure        Is the browser insecure?
     24 *     @type bool   $mobile          Is the browser on a mobile platform?
    2425 * }
    2526 */
     
    3536        'upgrade'         => false,
    3637        'insecure'        => false,
     38        'mobile'          => false,
    3739    );
    3840
     
    4850    if ( 'Linux' === $data['platform'] && false !== strpos( $user_agent, 'Android' ) ) {
    4951        $data['platform'] = 'Android';
     52    }
     53
     54    if ( in_array( $data['platform'], array( 'Android', 'iPad', 'iPhone', 'PlayBook', 'RIM Tablet OS' ) ) ) {
     55        $data['mobile'] = true;
    5056    }
    5157
     
    115121    }
    116122
    117     // Don't fetch additional browser data for mobile platform browsers.
    118     if ( in_array( $data['platform'], array( 'Android', 'iPad', 'iPhone' ) ) ) {
     123    if ( $data['mobile'] ) {
     124        // Generically set "Mobile" as the platform if a platform hasn't been set.
     125        if ( ! $data['platform'] ) {
     126            $data['platform'] = 'Mobile';
     127        }
     128
     129        // Don't fetch additional browser data for mobile platform browsers at this time.
    119130        return $data;
    120131    }
  • sites/trunk/api.wordpress.org/public_html/core/browse-happy/1.0/tests/phpunit/tests/browse-happy.php

    r5961 r5962  
    252252    }
    253253
     254    /**
     255     * @dataProvider data_browse_happy
     256     *
     257     * @param string $header 'User-Agent' header value.
     258     */
     259    function test_mobile_browsers( $header ) {
     260        $parsed = browsehappy_parse_user_agent( $header );
     261
     262        if ( in_array( $parsed['platform'], array( 'Android', 'iPad', 'iPhone', 'Mobile', 'PlayBook', 'RIM Tablet OS' ) ) ) {
     263            $this->assertTrue( $parsed['mobile'] );
     264        } else {
     265            $this->assertFalse( $parsed['mobile'] );
     266        }
     267    }
     268
    254269}
    255 
Note: See TracChangeset for help on using the changeset viewer.