WordPress.org

Making WordPress.org

Changeset 6276


Ignore:
Timestamp:
12/14/2017 11:45:18 PM (5 months ago)
Author:
iandunn
Message:

Events: Apply coding standards.

Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r6275 r6276  
    3232     */
    3333    define( 'THROTTLE_STICKY_WORDCAMPS', false );
    34     define( 'THROTTLE_GEONAMES',    0 );
    35     define( 'THROTTLE_IP2LOCATION', 0 );
    36 
    37     defined( 'DAY_IN_SECONDS' ) or define( 'DAY_IN_SECONDS', 60 * 60 * 24 );
     34    define( 'THROTTLE_GEONAMES',         0 );
     35    define( 'THROTTLE_IP2LOCATION',      0 );
     36
     37    defined( 'DAY_IN_SECONDS'  ) or define( 'DAY_IN_SECONDS', 60 * 60 * 24 );
    3838    defined( 'WEEK_IN_SECONDS' ) or define( 'WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS );
    3939
     
    5959 */
    6060function bootstrap() {
    61     $base_dir = dirname( dirname(__DIR__ ) );
     61    $base_dir = dirname( dirname( __DIR__ ) );
    6262
    6363    require( $base_dir . '/init.php' );
     
    8080    // If a precise location is known, use a GET request. The values here should come from the `location` key of the result of a POST request.
    8181    if ( isset( $_GET['latitude'] ) ) {
    82         $location_args['latitude'] = $_GET['latitude'];
     82        $location_args['latitude']  = $_GET['latitude'];
    8383        $location_args['longitude'] = $_GET['longitude'];
    8484    }
     
    113113         */
    114114        $public_ip = filter_var(
    115             $_REQUEST['ip'],
    116             FILTER_VALIDATE_IP,
    117             FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
     115            $_REQUEST['ip'],
     116            FILTER_VALIDATE_IP,
     117            FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
    118118        );
    119119
     
    153153        if ( ! empty( $location['latitude'] ) ) {
    154154            $event_args['nearby'] = array(
    155                 'latitude' => $location['latitude'],
     155                'latitude'  => $location['latitude'],
    156156                'longitude' => $location['longitude'],
    157157            );
     
    362362    if ( false === $long_ip || ! isset( $from, $where ) ) {
    363363        wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life );
     364
    364365        return false;
    365366    }
     
    377378    if ( ! $row || '-' == $row->country_short ) {
    378379        wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life );
     380
    379381        return false;
    380382    }
     
    476478
    477479        $country_code = get_country_code_from_locale( $args['locale'] ?? '' );
    478         $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code );
     480        $guess        = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code );
    479481
    480482        if ( $guess ) {
    481483            $location = array(
    482484                'description' => $guess->name,
    483                 'latitude' => $guess->latitude,
    484                 'longitude' => $guess->longitude,
    485                 'country' => $guess->country,
     485                'latitude'    => $guess->latitude,
     486                'longitude'   => $guess->longitude,
     487                'country'     => $guess->country,
    486488            );
    487489        } else {
     
    596598    if ( ! $country && $location_word_count >= 2 ) {
    597599        // Catch input like "Vancouver Canada"
    598         $country_id   = $location_name_parts[ $location_word_count - 1 ];
    599         $country      = get_country_from_name( $country_id );
     600        $country_id = $location_name_parts[ $location_word_count - 1 ];
     601        $country    = get_country_from_name( $country_id );
    600602    }
    601603
     
    684686
    685687    $wheres = array();
    686     if ( !empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) {
    687         $wheres[] = '`type` = %s';
     688    if ( ! empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) {
     689        $wheres[]     = '`type` = %s';
    688690        $sql_values[] = $args['type'];
    689691    }
    690692
    691693    // If we want nearby events, create a WHERE based on a bounded box of lat/long co-ordinates.
    692     if ( !empty( $args['nearby'] ) ) {
     694    if ( ! empty( $args['nearby'] ) ) {
    693695        $nearby_where = array();
    694696
    695697        foreach ( $event_distances as $type => $distance ) {
    696             if ( !empty( $args['type'] ) && $type != $args['type'] ) {
     698            if ( ! empty( $args['type'] ) && $type != $args['type'] ) {
    697699                continue;
    698700            }
    699             $bounded_box = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance );
     701
     702            $bounded_box    = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance );
    700703            $nearby_where[] = '( `type` = %s AND `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';
    701             $sql_values[] = $type;
    702             $sql_values[] = $bounded_box['latitude']['min'];
    703             $sql_values[] = $bounded_box['latitude']['max'];
    704             $sql_values[] = $bounded_box['longitude']['min'];
    705             $sql_values[] = $bounded_box['longitude']['max'];
     704            $sql_values[]   = $type;
     705            $sql_values[]   = $bounded_box['latitude']['min'];
     706            $sql_values[]   = $bounded_box['latitude']['max'];
     707            $sql_values[]   = $bounded_box['longitude']['min'];
     708            $sql_values[]   = $bounded_box['longitude']['max'];
    706709        }
    707710        // Build the nearby where as a OR as different event types have different distances.
     
    710713
    711714    // Allow queries for limiting to specific countries.
    712     if ( !empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {
    713         $wheres[] = '`country` = %s';
     715    if ( ! empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {
     716        $wheres[]     = '`country` = %s';
    714717        $sql_values[] = $args['country'];
    715718    }
     
    724727    // Limit
    725728    if ( isset( $args['number'] ) ) {
    726         $sql_limits = 'LIMIT %d';
     729        $sql_limits   = 'LIMIT %d';
    727730        $sql_values[] = $args['number'];
    728731    }
     
    758761    foreach ( $raw_events as $event ) {
    759762        $events[] = array(
    760             'type'  => $event->type,
    761             'title' => $event->title,
    762             'url'   => $event->url,
    763             'meetup' => $event->meetup,
     763            'type'       => $event->type,
     764            'title'      => $event->title,
     765            'url'        => $event->url,
     766            'meetup'     => $event->meetup,
    764767            'meetup_url' => $event->meetup_url,
    765             'date' => $event->date_utc, // TODO: DB stores a local date, not UTC.
    766             'location' => array(
    767                 'location' => $event->location,
    768                 'country' => $event->country,
    769                 'latitude' => (float) $event->latitude,
     768            'date'       => $event->date_utc, // TODO: DB stores a local date, not UTC.
     769            'location'   => array(
     770                'location'  => $event->location,
     771                'country'   => $event->country,
     772                'latitude'  => (float) $event->latitude,
    770773                'longitude' => (float) $event->longitude,
    771774            )
     
    775778    wp_cache_set( $cache_key, $events, $cache_group, $cache_life );
    776779
    777     return $events; 
     780    return $events;
    778781}
    779782
     
    858861    if ( ! empty( $request_args['nearby'] ) ) {
    859862        $bounded_box = get_bounded_coordinates( $request_args['nearby']['latitude'], $request_args['nearby']['longitude'], $distance );
    860         $where[]  = '( `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';
    861         $values[] = $bounded_box['latitude']['min'];
    862         $values[] = $bounded_box['latitude']['max'];
    863         $values[] = $bounded_box['longitude']['min'];
    864         $values[] = $bounded_box['longitude']['max'];
     863        $where[]     = '( `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';
     864        $values[]    = $bounded_box['latitude']['min'];
     865        $values[]    = $bounded_box['latitude']['max'];
     866        $values[]    = $bounded_box['longitude']['min'];
     867        $values[]    = $bounded_box['longitude']['max'];
    865868    }
    866869
     
    904907 */
    905908function add_regional_wordcamps( $local_events, $user_agent ) {
    906     $time = time();
     909    $time               = time();
    907910    $regional_wordcamps = array();
    908911
     
    968971 * @param float $lon            The longitude of the location.
    969972 * @param int   $distance_in_km The distance of the bounded box, in KM.
     973 *
    970974 * @return array of bounded box.
    971975 */
     
    974978
    975979    $angular_distance = $distance_in_km / 6371; // 6371 = radius of the earth in KM.
    976     $lat = deg2rad( $lat );
    977     $lon = deg2rad( $lon );
     980    $lat              = deg2rad( $lat );
     981    $lon              = deg2rad( $lon );
    978982
    979983    $earth_min_lat = -1.5707963267949; // = deg2rad(  -90 ) = -PI/2
     
    986990    $minimum_lon = $maximum_lon = 0;
    987991
    988     // Ensure that we're not within a pole-area of the world, weirdness will ensure.
     992    // Ensure that we're not within a pole-area of the world, weirdness will ensue.
    989993    if ( $minimum_lat > $earth_min_lat && $maximum_lat < $earth_max_lat ) {
    990 
    991         $lon_delta = asin( sin( $angular_distance ) / cos( $lat ) );
    992 
     994        $lon_delta   = asin( sin( $angular_distance ) / cos( $lat ) );
    993995        $minimum_lon = $lon - $lon_delta;
     996        $maximum_lon = $lon + $lon_delta;
     997
    994998        if ( $minimum_lon < $earth_min_lon ) {
    995999            $minimum_lon += 2 * pi();
    9961000        }
    9971001
    998         $maximum_lon = $lon + $lon_delta;
    9991002        if ( $maximum_lon > $earth_max_lon ) {
    10001003            $maximum_lon -= 2 * pi();
    10011004        }
    1002 
    10031005    } else {
    10041006        // Use a much simpler range in polar regions.
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r6275 r6276  
    1212function run_tests() {
    1313    global $wpdb;
     14
    1415    define( 'RUNNING_TESTS', true );
    1516    define( 'SAVEQUERIES',   true );
     
    2324    $tests_failed += test_build_response();
    2425    $tests_failed += test_is_client_core();
     26
    2527    $query_count  = count( $wpdb->queries );
    2628    $query_time   = array_sum( array_column( $wpdb->queries, 1 ) );
     
    98100        }
    99101
    100         $passed      = $case['expected'] === $actual_result;
     102        $passed = $case['expected'] === $actual_result;
    101103
    102104        output_results( $case_id, $passed, $case['expected'], $actual_result );
     
    116118 */
    117119function get_location_test_cases() {
    118      $cases = array(
     120    $cases = array(
    119121        /*
    120122         * Only the country code is given
     
    140142            ),
    141143            'expected' => array(
    142                 'country' => 'ID',
     144                'country'     => 'ID',
    143145                'description' => 'indonesia',
    144146            ),
     
    388390         * "Yaoundé", but it's correct in the database itself.
    389391         */
    390          'city-endonym-accents-africa' => array(
     392        'city-endonym-accents-africa' => array(
    391393            'input' => array(
    392394                'location_name' => 'Yaoundé',
     
    641643            ),
    642644            'expected' => array(
    643                 'country' => 'CA',
     645                'country'     => 'CA',
    644646                'description' => 'canada',
    645647            ),
     
    653655            ),
    654656            'expected' => array(
    655                 'country' => 'DO',
     657                'country'     => 'DO',
    656658                'description' => 'dominican republic',
    657659            ),
     
    665667            ),
    666668            'expected' => array(
    667                 'country' => 'CF',
     669                'country'     => 'CF',
    668670                'description' => 'central african republic',
    669671            ),
     
    677679            ),
    678680            'expected' => array(
    679                 'country' => 'GB',
     681                'country'     => 'GB',
    680682                'description' => 'united kingdom',
    681683            ),
     
    689691            ),
    690692            'expected' => array(
    691                 'country' => 'BI',
     693                'country'     => 'BI',
    692694                'description' => 'burundi',
    693695            ),
     
    890892
    891893        $passed = $case['expected']['count'] === count( $actual_result ) &&
    892             ! empty( $actual_result[0]['url'] ) &&
    893             strtotime( $actual_result[0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ) &&
    894             $case['expected']['country'] === strtoupper( $actual_result[0]['location']['country'] );
     894                  ! empty( $actual_result[0]['url'] ) &&
     895                  strtotime( $actual_result[0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ) &&
     896                  $case['expected']['country'] === strtoupper( $actual_result[0]['location']['country'] );
    895897
    896898        output_results( $case_id, $passed, $case['expected'], $actual_result );
     
    928930        '1-in-australia' => array(
    929931            'input' => array(
    930                 'number' => '1',
     932                'number'  => '1',
    931933                'country' => 'AU',
    932934            ),
     
    958960        $actual_result = build_response( $case['input']['location'], $case['input']['location_args'] );
    959961
    960         $passed = $case['expected']['location'] === $actual_result['location'] &&
    961             isset( $case['expected']['error'] ) === isset( $actual_result['error'] );
     962        $passed = $case['expected']['location']       === $actual_result['location'] &&
     963                  isset( $case['expected']['error'] ) === isset( $actual_result['error'] );
    962964
    963965        if ( $passed && $case['expected']['events'] ) {
    964966            $passed = ! empty( $actual_result['events'] ) &&
    965                 ! empty( $actual_result['events'][0]['url'] ) &&
    966                 strtotime( $actual_result['events'][0]['date'] ) > time() - ( 2 * 24 * 60 * 60 );
     967                      ! empty( $actual_result['events'][0]['url'] ) &&
     968                      strtotime( $actual_result['events'][0]['date'] ) > time() - ( 2 * 24 * 60 * 60 );
    967969        }
    968970
     
    10631065    foreach ( $cases as $user_agent => $expected_result ) {
    10641066        $actual_result = is_client_core( $user_agent );
    1065 
    1066         $passed = $expected_result === $actual_result;
     1067        $passed        = $expected_result === $actual_result;
    10671068
    10681069        output_results( $user_agent, $passed, $expected_result, $actual_result );
Note: See TracChangeset for help on using the changeset viewer.