Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 136 lines (106 sloc) 3.12 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
<?php
/**
* JSON API support for WordPress
*
* @package WordPress
*
* @author Scott Taylor
*/

/*
Plugin Name: JSON dot PHP
Plugin URI: http://emusic.com
Description: Because XML-RPC sucks, and WordPress is backwards-compatible
Author: Scott Taylor
Author URI: http://scotty-t.com
Version: 1.0
*/

if ( ! defined( 'WP_CACHE' ) )
define( 'WP_CACHE', false );

$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
$protocol = 'HTTP/1.0';

$server_params = $_GET + $_POST;

if ( empty( $server_params ) || empty( $server_params['method'] ) ) {
header( "$protocol 400 Bad Request" );
exit();
}

/**
* Let's guess where WordPress is!
*/
if ( file_exists( 'wp-load.php' ) )
require_once( './wp-load.php' );
elseif ( file_exists( 'wordpress/wp-load.php' ) )
require_once( './wordpress/wp-load.php' );

include_once( ABSPATH . 'wp-admin/includes/admin.php' );

$headers = headers_list();
if ( ! empty( $headers ) )
header_remove( 'Set-Cookie' );

$_COOKIE = array();

$method = $server_params['method'];

if ( defined( 'XMLRPC_USES_JSON' ) && XMLRPC_USES_JSON ):

/**
* Load this bad boy to check the methods it contains
*
*/
$wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' );
$wp_xmlrpc_server = new $wp_xmlrpc_server_class;

if ( array_key_exists( $method, $wp_xmlrpc_server->methods ) ):

unset( $server_params['method'] );

/**
* XML-RPC callbacks expect a weirdly-arranged
* set of parameters
*
*/
$params = null;
if ( ! empty( $server_params ) )
$params = 1 === count( $server_params ) ? reset( $server_params ) : array_values( $server_params );

$unthis = $wp_xmlrpc_server->methods[$method];
if ( is_string( $unthis ) && 'this:' === substr( $unthis, 0, 5 ) )
$unthis = array( $wp_xmlrpc_server, str_replace( 'this:', '', $unthis ) );

$response = $params ? call_user_func( $unthis, $params ) : call_user_func( $unthis );

header( 'Connection: close' );
header( 'Content-Type: application/json' );
header( 'Date: ' . date( 'r' ) );

echo json_encode( $response );
exit();

endif;

endif;

/**
* Example callback methods.
* Return whatever you want, it'll get passed to json_encode
*
* @return mixed
*/
function json_hello() {
return array( 'hello' => 'world' );
}

function json_hello_object() {
$obj = new stdClass;
$obj->name = 'wonderboymusic';
return $obj;
}

function json_hello_int() {
return 33;
}

function json_hello_string() {
return 'I haz text only.';
}

$methods = apply_filters( 'json_methods', array(
'hello.world' => 'json_hello',
'hello.object' => 'json_hello_object',
'hello.int' => 'json_hello_int',
'hello.string' => 'json_hello_string',
) );

/**
* You're a big boy now, you can handle $_GET, $_POST, whatever on your own
*/
if ( ! array_key_exists( $method, $methods ) ) {
header( "$protocol 405 Method Not Allowed" );
exit();
}

$response = call_user_func( $methods[$method] );

header( 'Connection: close' );
header( 'Content-Type: application/json' );
header( 'Date: ' . date( 'r' ) );

echo json_encode( $response );
exit();
Something went wrong with that request. Please try again.