Making WordPress.org


Ignore:
Timestamp:
03/01/2017 11:13:13 PM (9 years ago)
Author:
iandunn
Message:

Events API: Modularize controller logic to faciliate automated tests

This allows the upcoming test suite to bypass undesired runtime behavior like outputting a JSON response. It also makes the code easier to read and maintain.

File:
1 edited

Legend:

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

    r4979 r5033  
    11<?php
    22
    3 $base_dir = dirname( dirname(__DIR__ ) );
    4 require( $base_dir . '/init.php' );
    5 require( $base_dir . '/includes/hyperdb/bb-10-hyper-db.php' );
    6 include( $base_dir . '/includes/object-cache.php' );
    7 include( $base_dir . '/includes/wp-json-encode.php' );
    8 
    9 wp_cache_init();
    10 
    11 $cache_group = 'events';
    12 $cache_life = 12 * 60 * 60;
    13 $ttl = 12 * 60 * 60; // Time the client should cache the document.
    14 
    15 $location_args = array();
    16 // 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.
    17 if ( isset( $_GET['latitude'] ) ) {
    18     $location_args['latitude'] = $_GET['latitude'];
    19     $location_args['longitude'] = $_GET['longitude'];
    20 }
    21 if ( isset( $_GET['country'] ) ) {
    22     $location_args['country'] = $_GET['country'];
    23 }
    24 
    25 // If a precide location is not known, create a POST request with a bunch of data which can be used to determine a precise location for future GET requests.
    26 if ( isset( $_POST['location_data'] ) ) {
    27     $location_args = $_POST['location_data'];
    28 }
    29 
    30 // Simplified parameters for lookup by location (city) name, with optional timezone and locale params for extra context.
    31 if ( isset( $_REQUEST['location'] ) )
    32     $location_args['location_name'] = $_REQUEST['location'];
    33 if ( isset( $_REQUEST['timezone'] ) && !isset( $location_args['timezone'] ) )
    34     $location_args['timezone'] = $_REQUEST['timezone'];
    35 if ( isset( $_REQUEST['locale'] ) && !isset( $location_args['locale'] ) )
    36     $location_args['locale'] = $_REQUEST['locale'];
    37 if ( isset( $_REQUEST['ip'] ) && !isset( $location_args['ip'] ) )
    38     $location_args['ip'] = $_REQUEST['ip'];
    39 
    40 $location = get_location( $location_args );
    41 
    42 if ( false === $location ) {
    43     // No location was determined for the request. Bail with an error.
    44     $events = array();
    45     $error = 'no_location_available';
    46 } else {
    47     $event_args = array();
    48     if ( isset( $_REQUEST['number'] ) ) {
    49         $event_args['number'] = $_REQUEST['number'];
    50     }
    51     if ( !empty( $location['latitude'] ) ) {
    52         $event_args['nearby'] = array(
    53             'latitude'  => $location['latitude'],
    54             'longitude' => $location['longitude'],
    55         );
    56     }
    57     if ( !empty( $location['country'] ) ) {
    58         $event_args['country'] = $location['country'];
    59     }
    60 
    61     $events = get_events( $event_args );
    62 }
    63 
    64 header( 'Expires: ' . gmdate( 'r', time() + $ttl ) );
    65 header( 'Access-Control-Allow-Origin: *' );
    66 header( 'Content-Type: application/json; charset=UTF-8' );
    67 echo wp_json_encode( compact( 'error', 'location', 'events', 'ttl' ) );
    68 
     3namespace Dotorg\API\Events;
     4
     5/**
     6 * Main entry point
     7 */
     8function main() {
     9    global $cache_group, $cache_life;
     10
     11    bootstrap();
     12
     13    // The test suite just needs the functions defined and doesn't want any headers or output
     14    if ( defined( 'RUNNING_TESTS' ) && RUNNING_TESTS ) {
     15        return;
     16    }
     17
     18    wp_cache_init();
     19
     20    $cache_group   = 'events';
     21    $cache_life    = 12 * 60 * 60;
     22    $ttl           = 12 * 60 * 60; // Time the client should cache the document.
     23    $location_args = parse_request();
     24    $location      = get_location( $location_args );
     25    $response      = build_response( $location );
     26
     27    send_response( $response, $ttl );
     28}
     29
     30/**
     31 * Include dependencies
     32 */
     33function bootstrap() {
     34    $base_dir = dirname( dirname(__DIR__ ) );
     35
     36    require( $base_dir . '/init.php' );
     37    require( $base_dir . '/includes/hyperdb/bb-10-hyper-db.php' );
     38    include( $base_dir . '/includes/object-cache.php' );
     39    include( $base_dir . '/includes/wp-json-encode.php' );
     40}
     41
     42/**
     43 * Parse and normalize the client's request
     44 *
     45 * @return array
     46 */
     47function parse_request() {
     48    $location_args = array();
     49
     50    // 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.
     51    if ( isset( $_GET['latitude'] ) ) {
     52        $location_args['latitude'] = $_GET['latitude'];
     53        $location_args['longitude'] = $_GET['longitude'];
     54    }
     55
     56    if ( isset( $_GET['country'] ) ) {
     57        $location_args['country'] = $_GET['country'];
     58    }
     59
     60    // If a precise location is not known, create a POST request with a bunch of data which can be used to determine a precise location for future GET requests.
     61    if ( isset( $_POST['location_data'] ) ) {
     62        $location_args = $_POST['location_data'];
     63    }
     64
     65    // Simplified parameters for lookup by location (city) name, with optional timezone and locale params for extra context.
     66    if ( isset( $_REQUEST['location'] ) ) {
     67        $location_args['location_name'] = $_REQUEST['location'];
     68    }
     69
     70    if ( isset( $_REQUEST['timezone'] ) && ! isset( $location_args['timezone'] ) ) {
     71        $location_args['timezone'] = $_REQUEST['timezone'];
     72    }
     73
     74    if ( isset( $_REQUEST['locale'] ) && ! isset( $location_args['locale'] ) ) {
     75        $location_args['locale'] = $_REQUEST['locale'];
     76    }
     77
     78    if ( isset( $_REQUEST['ip'] ) && ! isset( $location_args['ip'] ) ) {
     79        $location_args['ip'] = $_REQUEST['ip'];
     80    }
     81
     82    return $location_args;
     83}
     84
     85/**
     86 * Build the API's response to the client's request
     87 *
     88 * @param array $location
     89 *
     90 * @return array
     91 */
     92function build_response( $location ) {
     93    if ( false === $location ) {
     94        // No location was determined for the request. Bail with an error.
     95        $events = array();
     96        $error = 'no_location_available';
     97    } else {
     98        $event_args = array();
     99
     100        if ( isset( $_REQUEST['number'] ) ) {
     101            $event_args['number'] = $_REQUEST['number'];
     102        }
     103
     104        if ( ! empty( $location['latitude'] ) ) {
     105            $event_args['nearby'] = array(
     106                'latitude' => $location['latitude'],
     107                'longitude' => $location['longitude'],
     108            );
     109        }
     110
     111        if ( ! empty( $location['country'] ) ) {
     112            $event_args['country'] = $location['country'];
     113        }
     114
     115        $events = get_events( $event_args );
     116    }
     117
     118    return compact( 'error', 'location', 'events', 'ttl' );
     119}
     120
     121/**
     122 * Send the API's response to the client's request
     123 *
     124 * @param array $response
     125 * @param int   $ttl
     126 */
     127function send_response( $response, $ttl ) {
     128    header( 'Expires: ' . gmdate( 'r', time() + $ttl ) );
     129    header( 'Access-Control-Allow-Origin: *' );
     130    header( 'Content-Type: application/json; charset=UTF-8' );
     131
     132    echo wp_json_encode( $response );
     133}
    69134
    70135function guess_location_from_geonames( $location_name, $timezone, $country ) {
     
    319384    );
    320385}
     386
     387main();
    321388
    322389/*
Note: See TracChangeset for help on using the changeset viewer.