Skip to content

Commit

Permalink
Refactor function to be testable
Browse files Browse the repository at this point in the history
  • Loading branch information
ozh committed May 27, 2015
1 parent 1921d0f commit 6dc180c
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions includes/functions-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,55 +81,83 @@ function yourls_api_action_version() {
$return['db_version'] = YOURLS_DB_VERSION;
return yourls_apply_filter( 'api_result_version', $return );
}

/**
* Return API result. Dies after this
* Output and return API result
*
*/
function yourls_api_output( $mode, $return ) {
if( isset( $return['simple'] ) ) {
$simple = $return['simple'];
unset( $return['simple'] );
* This function will echo (or only return if asked) an array as JSON, JSONP or XML. If the array has a
* 'simple' key, it can also output that key as unformatted text if expected output mode is 'simple'
*
* Most likely, script should not do anything after outputting this
*
* @since 1.6
*
* @param string $mode Expected output mode ('json', 'jsonp', 'xml', 'simple')
* @param array $output Array of things to output
* @param bool $send_headers Optional, default true: Whether a headers (status, content type) should be sent or not
* @param bool $echo Optional, default true: Whether the output should be outputted or just returned
* @return string API output, as an XML / JSON / JSONP / raw text string
*/
function yourls_api_output( $mode, $output, $send_headers = true, $echo = true ) {
if( isset( $output['simple'] ) ) {
$simple = $output['simple'];
unset( $output['simple'] );
}

yourls_do_action( 'pre_api_output', $mode, $return );
yourls_do_action( 'pre_api_output', $mode, $output, $send_headers, $echo );

if( isset( $return['statusCode'] ) ) {
$code = $return['statusCode'];
} elseif ( isset( $return['errorCode'] ) ) {
$code = $return['errorCode'];
} else {
$code = 200;
}
yourls_status_header( $code );
if( $send_headers ) {
if( isset( $output['statusCode'] ) ) {
$code = $output['statusCode'];
} elseif ( isset( $output['errorCode'] ) ) {
$code = $output['errorCode'];
} else {
$code = 200;
}
yourls_status_header( $code );
}

$result = '';

switch ( $mode ) {
case 'jsonp':
yourls_content_type_header( 'application/javascript' );
echo $return['callback'] . '(' . json_encode( $return ) . ')';
if( $send_headers )
yourls_content_type_header( 'application/javascript' );

$callback = isset( $output['callback'] ) ? $output['callback'] : '';
$result = $callback . '(' . json_encode( $output ) . ')';
break;

case 'json':
yourls_content_type_header( 'application/json' );
echo json_encode( $return );
if( $send_headers )
yourls_content_type_header( 'application/json' );

$result = json_encode( $output );
break;

case 'xml':
yourls_content_type_header( 'application/xml' );
echo yourls_xml_encode( $return );
if( $send_headers )
yourls_content_type_header( 'application/xml' );

$result = yourls_xml_encode( $output );
break;

case 'simple':
default:
yourls_content_type_header( 'text/plain' );
if( isset( $simple ) )
echo $simple;
if( $send_headers )
yourls_content_type_header( 'text/plain' );

$result = isset( $simple ) ? $simple : '';
break;
}

yourls_do_action( 'api_output', $mode, $return );
if( $echo ) {
echo $result;
}

yourls_do_action( 'api_output', $mode, $output, $send_headers, $echo );

die();
return $result;
}

/**
Expand Down

0 comments on commit 6dc180c

Please sign in to comment.