Skip to content

Commit

Permalink
Filterable charset in content-type headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ozh committed Jan 1, 2014
1 parent 43e21bb commit 7dfbe54
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion admin/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
yourls_maybe_require_auth();

// This file will output a JSON string
header( 'Content-type: application/json' );
yourls_content_type_header( 'application/json' );

if( !isset( $_REQUEST['action'] ) )
die();
Expand Down
2 changes: 1 addition & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
if( isset( $_GET['jsonp'] ) && $_GET['jsonp'] == 'yourls' ) {
$short = $return['shorturl'] ? $return['shorturl'] : '';
$message = $return['message'];
header( 'Content-type: application/json' );
yourls_content_type_header( 'application/javascript' );
echo yourls_apply_filter( 'bookmarklet_jsonp', "yourls_callback({'short_url':'$short','message':'$message'});" );

die();
Expand Down
7 changes: 4 additions & 3 deletions includes/functions-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,23 @@ function yourls_api_output( $mode, $return ) {

switch ( $mode ) {
case 'jsonp':
header( 'Content-type: application/javascript' );
yourls_content_type_header( 'application/javascript' );
echo $return['callback'] . '(' . json_encode( $return ) . ')';
break;

case 'json':
header( 'Content-type: application/json' );
yourls_content_type_header( 'application/json' );
echo json_encode( $return );
break;

case 'xml':
header( 'Content-type: application/xml' );
yourls_content_type_header( 'application/xml' );
echo yourls_xml_encode( $return );
break;

case 'simple':
default:
yourls_content_type_header( 'text/plain' );
if( isset( $simple ) )
echo $simple;
break;
Expand Down
19 changes: 18 additions & 1 deletion includes/functions-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function yourls_html_head( $context = 'index', $title = '' ) {
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
header( 'Pragma: no-cache' );
yourls_content_type_header( yourls_apply_filters( 'html_head_content-type', 'text/html' ) );
yourls_do_action( 'admin_headers', $context, $title );
}

Expand All @@ -82,7 +83,7 @@ function yourls_html_head( $context = 'index', $title = '' ) {
<head>
<title><?php echo $title ?></title>
<link rel="shortcut icon" href="<?php yourls_favicon(); ?>" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="<?php echo yourls_apply_filters( 'html_head_meta_content-type', 'text/html; charset=utf-8' ); ?>" />
<meta http-equiv="X-UA-Compatible" content="IE-9"/>
<meta name="author" content="Ozh RICHARD & Lester CHAN for http://yourls.org/" />
<meta name="generator" content="YOURLS <?php echo YOURLS_VERSION ?>" />
Expand Down Expand Up @@ -890,3 +891,19 @@ function yourls_new_core_version_notice() {
}
}

/**
* Send a filerable content type header
*
* @since 1.7
* @param string $type content type ('text/html', 'application/json', ...)
* @return bool whether header was sent
*/
function yourls_content_type_header( $type ) {
if( !headers_sent() ) {
$charset = yourls_apply_filters( 'content_type_header_charset', 'utf-8' );
header( "Content-Type: $type; charset=$charset" );
return true;
}
return false;
}

0 comments on commit 7dfbe54

Please sign in to comment.