WordPress.org

Plugin Directory

Changeset 627845


Ignore:
Timestamp:
11/20/12 19:24:53 (17 months ago)
Author:
ethitter
Message:

Jetpack JSON API

Format dates differently for input and output.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jetpack/trunk/class.json-api-endpoints.php

    r622508 r627845  
    200200    } 
    201201 
    202     function cast_and_filter( $data, $documentation, $return_default_values = false ) { 
     202    function cast_and_filter( $data, $documentation, $return_default_values = false, $for_output = false ) { 
    203203        $return_as_object = false; 
    204204        if ( is_object( $data ) ) { 
     
    248248            } 
    249249 
    250             $this->cast_and_filter_item( $return, $type, $key, $data[$key], $types ); 
     250            $this->cast_and_filter_item( $return, $type, $key, $data[$key], $types, $for_output ); 
    251251        } 
    252252 
     
    269269     * Handles object typing - object>post means an object of type post 
    270270     */ 
    271     function cast_and_filter_item( &$return, $type, $key, $value, $types = array() ) { 
     271    function cast_and_filter_item( &$return, $type, $key, $value, $types = array(), $for_output = false ) { 
    272272        if ( is_string( $type ) ) { 
    273273            $type = compact( 'type' ); 
     
    286286                if ( !empty( $types[0] ) ) { 
    287287                    $next_type = array_shift( $types ); 
    288                     return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types ); 
     288                    return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); 
    289289                } 
    290290            } 
     
    294294                if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) { 
    295295                    $next_type = array_shift( $types ); 
    296                     return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types ); 
     296                    return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); 
    297297                } 
    298298            } 
     
    328328                if ( !empty( $types[0] ) ) { 
    329329                    $next_type = array_shift( $types ); 
    330                     return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types ); 
     330                    return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output ); 
    331331                } 
    332332            } 
     
    335335                $children = array(); 
    336336                foreach ( (array) $value as $k => $child ) { 
    337                     $this->cast_and_filter_item( $children, $type['children'], $k, $child ); 
     337                    $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output ); 
    338338                } 
    339339                $return[$key] = (array) $children; 
     
    346346        case 'datetime' : 
    347347            // (string)s 
    348             list( $return[$key], $return["{$key}_gmt"] ) = $this->parse_date( (string) $value ); 
     348            $dates = $this->parse_date( (string) $value ); 
     349            if ( $for_output ) { 
     350                $return[$key] = $this->format_date( $dates[1], $dates[0] ); 
     351            } else { 
     352                list( $return[$key], $return["{$key}_gmt"] ) = $dates; 
     353            } 
    349354            break; 
    350355        case 'float' : 
     
    363368            if ( is_scalar( $value ) || is_null( $value ) ) { 
    364369                if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) { 
    365                     return $this->cast_and_filter_item( $return, 'false', $key, $value, $types ); 
     370                    return $this->cast_and_filter_item( $return, 'false', $key, $value, $types, $for_output ); 
    366371                } 
    367372            } 
     
    370375                $children = array(); 
    371376                foreach ( (array) $value as $k => $child ) { 
    372                     $this->cast_and_filter_item( $children, $type['children'], $k, $child ); 
     377                    $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output ); 
    373378                } 
    374379                $return[$key] = (object) $children; 
     
    377382 
    378383            if ( isset( $type['subtype'] ) ) { 
    379                 return $this->cast_and_filter_item( $return, $type['subtype'], $key, $value, $types ); 
     384                return $this->cast_and_filter_item( $return, $type['subtype'], $key, $value, $types, $for_output ); 
    380385            } 
    381386 
     
    383388            break; 
    384389        case 'post' : 
    385             $return[$key] = (object) $this->cast_and_filter( $value, $this->post_object_format ); 
     390            $return[$key] = (object) $this->cast_and_filter( $value, $this->post_object_format, false, $for_output ); 
    386391            break; 
    387392        case 'comment' : 
    388             $return[$key] = (object) $this->cast_and_filter( $value, $this->comment_object_format ); 
     393            $return[$key] = (object) $this->cast_and_filter( $value, $this->comment_object_format, false, $for_output ); 
    389394            break; 
    390395        case 'tag' : 
     
    400405                $docs['parent'] = '(int)'; 
    401406            } 
    402             $return[$key] = (object) $this->cast_and_filter( $value, $docs ); 
     407            $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); 
    403408            break; 
    404409        case 'post_reference' : 
     
    409414                'link' => '(URL)', 
    410415            ); 
    411             $return[$key] = (object) $this->cast_and_filter( $value, $docs ); 
     416            $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); 
    412417            break; 
    413418        case 'geo' : 
     
    417422                'address'   => '(string)', 
    418423            ); 
    419             $return[$key] = (object) $this->cast_and_filter( $value, $docs ); 
     424            $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); 
    420425            break; 
    421426        case 'author' : 
     
    428433                'profile_URL' => '(URL)', 
    429434            ); 
    430             $return[$key] = (object) $this->cast_and_filter( $value, $docs ); 
     435            $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output ); 
    431436            break; 
    432437        case 'attachment' : 
     
    440445                'duration'  => '(int)', 
    441446            ); 
    442             $return[$key] = (object) $this->cast_and_filter( $value, apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ) ); 
     447            $return[$key] = (object) $this->cast_and_filter( $value, apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ), false, $for_output ); 
    443448            break; 
    444449        default : 
Note: See TracChangeset for help on using the changeset viewer.