class.json-api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. defined( 'WPCOM_JSON_API__DEBUG' ) or define( 'WPCOM_JSON_API__DEBUG', false );
  3. require_once dirname( __FILE__ ) . '/sal/class.json-api-platform.php';
  4. class WPCOM_JSON_API {
  5. static $self = null;
  6. public $endpoints = array();
  7. public $token_details = array();
  8. public $method = '';
  9. public $url = '';
  10. public $path = '';
  11. public $version = null;
  12. public $query = array();
  13. public $post_body = null;
  14. public $files = null;
  15. public $content_type = null;
  16. public $accept = '';
  17. public $_server_https;
  18. public $exit = true;
  19. public $public_api_scheme = 'https';
  20. public $output_status_code = 200;
  21. public $trapped_error = null;
  22. public $did_output = false;
  23. public $extra_headers = array();
  24. /**
  25. * @return WPCOM_JSON_API instance
  26. */
  27. static function init( $method = null, $url = null, $post_body = null ) {
  28. if ( !self::$self ) {
  29. $class = function_exists( 'get_called_class' ) ? get_called_class() : __CLASS__; // phpcs:ignore PHPCompatibility
  30. self::$self = new $class( $method, $url, $post_body );
  31. }
  32. return self::$self;
  33. }
  34. function add( WPCOM_JSON_API_Endpoint $endpoint ) {
  35. $path_versions = serialize( array (
  36. $endpoint->path,
  37. $endpoint->min_version,
  38. $endpoint->max_version,
  39. ) );
  40. if ( !isset( $this->endpoints[$path_versions] ) ) {
  41. $this->endpoints[$path_versions] = array();
  42. }
  43. $this->endpoints[$path_versions][$endpoint->method] = $endpoint;
  44. }
  45. static function is_truthy( $value ) {
  46. switch ( strtolower( (string) $value ) ) {
  47. case '1' :
  48. case 't' :
  49. case 'true' :
  50. return true;
  51. }
  52. return false;
  53. }
  54. static function is_falsy( $value ) {
  55. switch ( strtolower( (string) $value ) ) {
  56. case '0' :
  57. case 'f' :
  58. case 'false' :
  59. return true;
  60. }
  61. return false;
  62. }
  63. function __construct() {
  64. $args = func_get_args();
  65. call_user_func_array( array( $this, 'setup_inputs' ), $args );
  66. }
  67. function setup_inputs( $method = null, $url = null, $post_body = null ) {
  68. if ( is_null( $method ) ) {
  69. $this->method = strtoupper( $_SERVER['REQUEST_METHOD'] );
  70. } else {
  71. $this->method = strtoupper( $method );
  72. }
  73. if ( is_null( $url ) ) {
  74. $this->url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  75. } else {
  76. $this->url = $url;
  77. }
  78. $parsed = parse_url( $this->url );
  79. $this->path = $parsed['path'];
  80. if ( !empty( $parsed['query'] ) ) {
  81. wp_parse_str( $parsed['query'], $this->query );
  82. }
  83. if ( isset( $_SERVER['HTTP_ACCEPT'] ) && $_SERVER['HTTP_ACCEPT'] ) {
  84. $this->accept = $_SERVER['HTTP_ACCEPT'];
  85. }
  86. if ( 'POST' === $this->method ) {
  87. if ( is_null( $post_body ) ) {
  88. $this->post_body = file_get_contents( 'php://input' );
  89. if ( isset( $_SERVER['HTTP_CONTENT_TYPE'] ) && $_SERVER['HTTP_CONTENT_TYPE'] ) {
  90. $this->content_type = $_SERVER['HTTP_CONTENT_TYPE'];
  91. } elseif ( isset( $_SERVER['CONTENT_TYPE'] ) && $_SERVER['CONTENT_TYPE'] ) {
  92. $this->content_type = $_SERVER['CONTENT_TYPE'] ;
  93. } elseif ( '{' === $this->post_body[0] ) {
  94. $this->content_type = 'application/json';
  95. } else {
  96. $this->content_type = 'application/x-www-form-urlencoded';
  97. }
  98. if ( 0 === strpos( strtolower( $this->content_type ), 'multipart/' ) ) {
  99. $this->post_body = http_build_query( stripslashes_deep( $_POST ) );
  100. $this->files = $_FILES;
  101. $this->content_type = 'multipart/form-data';
  102. }
  103. } else {
  104. $this->post_body = $post_body;
  105. $this->content_type = '{' === isset( $this->post_body[0] ) && $this->post_body[0] ? 'application/json' : 'application/x-www-form-urlencoded';
  106. }
  107. } else {
  108. $this->post_body = null;
  109. $this->content_type = null;
  110. }
  111. $this->_server_https = array_key_exists( 'HTTPS', $_SERVER ) ? $_SERVER['HTTPS'] : '--UNset--';
  112. }
  113. function initialize() {
  114. $this->token_details['blog_id'] = Jetpack_Options::get_option( 'id' );
  115. }
  116. function serve( $exit = true ) {
  117. ini_set( 'display_errors', false );
  118. $this->exit = (bool) $exit;
  119. // This was causing problems with Jetpack, but is necessary for wpcom
  120. // @see https://github.com/Automattic/jetpack/pull/2603
  121. // @see r124548-wpcom
  122. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  123. add_filter( 'home_url', array( $this, 'ensure_http_scheme_of_home_url' ), 10, 3 );
  124. }
  125. add_filter( 'user_can_richedit', '__return_true' );
  126. add_filter( 'comment_edit_pre', array( $this, 'comment_edit_pre' ) );
  127. $initialization = $this->initialize();
  128. if ( 'OPTIONS' == $this->method ) {
  129. /**
  130. * Fires before the page output.
  131. * Can be used to specify custom header options.
  132. *
  133. * @module json-api
  134. *
  135. * @since 3.1.0
  136. */
  137. do_action( 'wpcom_json_api_options' );
  138. return $this->output( 200, '', 'text/plain' );
  139. }
  140. if ( is_wp_error( $initialization ) ) {
  141. $this->output_error( $initialization );
  142. return;
  143. }
  144. // Normalize path and extract API version
  145. $this->path = untrailingslashit( $this->path );
  146. preg_match( '#^/rest/v(\d+(\.\d+)*)#', $this->path, $matches );
  147. $this->path = substr( $this->path, strlen( $matches[0] ) );
  148. $this->version = $matches[1];
  149. $allowed_methods = array( 'GET', 'POST' );
  150. $four_oh_five = false;
  151. $is_help = preg_match( '#/help/?$#i', $this->path );
  152. $matching_endpoints = array();
  153. if ( $is_help ) {
  154. $origin = get_http_origin();
  155. if ( !empty( $origin ) && 'GET' == $this->method ) {
  156. header( 'Access-Control-Allow-Origin: ' . esc_url_raw( $origin ) );
  157. }
  158. $this->path = substr( rtrim( $this->path, '/' ), 0, -5 );
  159. // Show help for all matching endpoints regardless of method
  160. $methods = $allowed_methods;
  161. $find_all_matching_endpoints = true;
  162. // How deep to truncate each endpoint's path to see if it matches this help request
  163. $depth = substr_count( $this->path, '/' ) + 1;
  164. if ( false !== stripos( $this->accept, 'javascript' ) || false !== stripos( $this->accept, 'json' ) ) {
  165. $help_content_type = 'json';
  166. } else {
  167. $help_content_type = 'html';
  168. }
  169. } else {
  170. if ( in_array( $this->method, $allowed_methods ) ) {
  171. // Only serve requested method
  172. $methods = array( $this->method );
  173. $find_all_matching_endpoints = false;
  174. } else {
  175. // We don't allow this requested method - find matching endpoints and send 405
  176. $methods = $allowed_methods;
  177. $find_all_matching_endpoints = true;
  178. $four_oh_five = true;
  179. }
  180. }
  181. // Find which endpoint to serve
  182. $found = false;
  183. foreach ( $this->endpoints as $endpoint_path_versions => $endpoints_by_method ) {
  184. $endpoint_path_versions = unserialize( $endpoint_path_versions );
  185. $endpoint_path = $endpoint_path_versions[0];
  186. $endpoint_min_version = $endpoint_path_versions[1];
  187. $endpoint_max_version = $endpoint_path_versions[2];
  188. // Make sure max_version is not less than min_version
  189. if ( version_compare( $endpoint_max_version, $endpoint_min_version, '<' ) ) {
  190. $endpoint_max_version = $endpoint_min_version;
  191. }
  192. foreach ( $methods as $method ) {
  193. if ( !isset( $endpoints_by_method[$method] ) ) {
  194. continue;
  195. }
  196. // Normalize
  197. $endpoint_path = untrailingslashit( $endpoint_path );
  198. if ( $is_help ) {
  199. // Truncate path at help depth
  200. $endpoint_path = join( '/', array_slice( explode( '/', $endpoint_path ), 0, $depth ) );
  201. }
  202. // Generate regular expression from sprintf()
  203. $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path );
  204. if ( !preg_match( "#^$endpoint_path_regex\$#", $this->path, $path_pieces ) ) {
  205. // This endpoint does not match the requested path.
  206. continue;
  207. }
  208. if ( version_compare( $this->version, $endpoint_min_version, '<' ) || version_compare( $this->version, $endpoint_max_version, '>' ) ) {
  209. // This endpoint does not match the requested version.
  210. continue;
  211. }
  212. $found = true;
  213. if ( $find_all_matching_endpoints ) {
  214. $matching_endpoints[] = array( $endpoints_by_method[$method], $path_pieces );
  215. } else {
  216. // The method parameters are now in $path_pieces
  217. $endpoint = $endpoints_by_method[$method];
  218. break 2;
  219. }
  220. }
  221. }
  222. if ( !$found ) {
  223. return $this->output( 404, '', 'text/plain' );
  224. }
  225. if ( $four_oh_five ) {
  226. $allowed_methods = array();
  227. foreach ( $matching_endpoints as $matching_endpoint ) {
  228. $allowed_methods[] = $matching_endpoint[0]->method;
  229. }
  230. header( 'Allow: ' . strtoupper( join( ',', array_unique( $allowed_methods ) ) ) );
  231. return $this->output( 405, array( 'error' => 'not_allowed', 'error_message' => 'Method not allowed' ) );
  232. }
  233. if ( $is_help ) {
  234. /**
  235. * Fires before the API output.
  236. *
  237. * @since 1.9.0
  238. *
  239. * @param string help.
  240. */
  241. do_action( 'wpcom_json_api_output', 'help' );
  242. $proxied = function_exists( 'wpcom_is_proxied_request' ) ? wpcom_is_proxied_request() : false;
  243. if ( 'json' === $help_content_type ) {
  244. $docs = array();
  245. foreach ( $matching_endpoints as $matching_endpoint ) {
  246. if ( $matching_endpoint[0]->is_publicly_documentable() || $proxied || WPCOM_JSON_API__DEBUG )
  247. $docs[] = call_user_func( array( $matching_endpoint[0], 'generate_documentation' ) );
  248. }
  249. return $this->output( 200, $docs );
  250. } else {
  251. status_header( 200 );
  252. foreach ( $matching_endpoints as $matching_endpoint ) {
  253. if ( $matching_endpoint[0]->is_publicly_documentable() || $proxied || WPCOM_JSON_API__DEBUG )
  254. call_user_func( array( $matching_endpoint[0], 'document' ) );
  255. }
  256. }
  257. exit;
  258. }
  259. if ( $endpoint->in_testing && !WPCOM_JSON_API__DEBUG ) {
  260. return $this->output( 404, '', 'text/plain' );
  261. }
  262. /** This action is documented in class.json-api.php */
  263. do_action( 'wpcom_json_api_output', $endpoint->stat );
  264. $response = $this->process_request( $endpoint, $path_pieces );
  265. if ( !$response && !is_array( $response ) ) {
  266. return $this->output( 500, '', 'text/plain' );
  267. } elseif ( is_wp_error( $response ) ) {
  268. return $this->output_error( $response );
  269. }
  270. $output_status_code = $this->output_status_code;
  271. $this->set_output_status_code();
  272. return $this->output( $output_status_code, $response, 'application/json', $this->extra_headers );
  273. }
  274. function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) {
  275. $this->endpoint = $endpoint;
  276. return call_user_func_array( array( $endpoint, 'callback' ), $path_pieces );
  277. }
  278. function output_early( $status_code, $response = null, $content_type = 'application/json' ) {
  279. $exit = $this->exit;
  280. $this->exit = false;
  281. if ( is_wp_error( $response ) )
  282. $this->output_error( $response );
  283. else
  284. $this->output( $status_code, $response, $content_type );
  285. $this->exit = $exit;
  286. if ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) {
  287. $this->finish_request();
  288. }
  289. }
  290. function set_output_status_code( $code = 200 ) {
  291. $this->output_status_code = $code;
  292. }
  293. function output( $status_code, $response = null, $content_type = 'application/json', $extra = array() ) {
  294. // In case output() was called before the callback returned
  295. if ( $this->did_output ) {
  296. if ( $this->exit )
  297. exit;
  298. return $content_type;
  299. }
  300. $this->did_output = true;
  301. // 400s and 404s are allowed for all origins
  302. if ( 404 == $status_code || 400 == $status_code )
  303. header( 'Access-Control-Allow-Origin: *' );
  304. if ( is_null( $response ) ) {
  305. $response = new stdClass;
  306. }
  307. if ( 'text/plain' === $content_type ) {
  308. status_header( (int) $status_code );
  309. header( 'Content-Type: text/plain' );
  310. foreach( $extra as $key => $value ) {
  311. header( "$key: $value" );
  312. }
  313. echo $response;
  314. if ( $this->exit ) {
  315. exit;
  316. }
  317. return $content_type;
  318. }
  319. $response = $this->filter_fields( $response );
  320. if ( isset( $this->query['http_envelope'] ) && self::is_truthy( $this->query['http_envelope'] ) ) {
  321. $headers = array(
  322. array(
  323. 'name' => 'Content-Type',
  324. 'value' => $content_type,
  325. )
  326. );
  327. foreach( $extra as $key => $value ) {
  328. $headers[] = array( 'name' => $key, 'value' => $value );
  329. }
  330. $response = array(
  331. 'code' => (int) $status_code,
  332. 'headers' => $headers,
  333. 'body' => $response,
  334. );
  335. $status_code = 200;
  336. $content_type = 'application/json';
  337. }
  338. status_header( (int) $status_code );
  339. header( "Content-Type: $content_type" );
  340. if ( isset( $this->query['callback'] ) && is_string( $this->query['callback'] ) ) {
  341. $callback = preg_replace( '/[^a-z0-9_.]/i', '', $this->query['callback'] );
  342. } else {
  343. $callback = false;
  344. }
  345. if ( $callback ) {
  346. // Mitigate Rosetta Flash [1] by setting the Content-Type-Options: nosniff header
  347. // and by prepending the JSONP response with a JS comment.
  348. // [1] http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
  349. echo "/**/$callback(";
  350. }
  351. echo $this->json_encode( $response );
  352. if ( $callback ) {
  353. echo ");";
  354. }
  355. if ( $this->exit ) {
  356. exit;
  357. }
  358. return $content_type;
  359. }
  360. public static function serializable_error ( $error ) {
  361. $status_code = $error->get_error_data();
  362. if ( is_array( $status_code ) ) {
  363. $status_code = $status_code['status_code'];
  364. }
  365. if ( !$status_code ) {
  366. $status_code = 400;
  367. }
  368. $response = array(
  369. 'error' => $error->get_error_code(),
  370. 'message' => $error->get_error_message(),
  371. );
  372. if ( $additional_data = $error->get_error_data( 'additional_data' ) ) {
  373. $response['data'] = $additional_data;
  374. }
  375. return array(
  376. 'status_code' => $status_code,
  377. 'errors' => $response
  378. );
  379. }
  380. function output_error( $error ) {
  381. $error_response = $this->serializable_error( $error );
  382. return $this->output( $error_response[ 'status_code'], $error_response['errors'] );
  383. }
  384. function filter_fields( $response ) {
  385. if ( empty( $this->query['fields'] ) || ( is_array( $response ) && ! empty( $response['error'] ) ) || ! empty( $this->endpoint->custom_fields_filtering ) )
  386. return $response;
  387. $fields = array_map( 'trim', explode( ',', $this->query['fields'] ) );
  388. if ( is_object( $response ) ) {
  389. $response = (array) $response;
  390. }
  391. $has_filtered = false;
  392. if ( is_array( $response ) && empty( $response['ID'] ) ) {
  393. $keys_to_filter = array(
  394. 'categories',
  395. 'comments',
  396. 'connections',
  397. 'domains',
  398. 'groups',
  399. 'likes',
  400. 'media',
  401. 'notes',
  402. 'posts',
  403. 'services',
  404. 'sites',
  405. 'suggestions',
  406. 'tags',
  407. 'themes',
  408. 'topics',
  409. 'users',
  410. );
  411. foreach ( $keys_to_filter as $key_to_filter ) {
  412. if ( ! isset( $response[ $key_to_filter ] ) || $has_filtered )
  413. continue;
  414. foreach ( $response[ $key_to_filter ] as $key => $values ) {
  415. if ( is_object( $values ) ) {
  416. if ( is_object( $response[ $key_to_filter ] ) ) {
  417. $response[ $key_to_filter ]->$key = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) );
  418. } elseif ( is_array( $response[ $key_to_filter ] ) ) {
  419. $response[ $key_to_filter ][ $key ] = (object) array_intersect_key( ( (array) $values ), array_flip( $fields ) );
  420. }
  421. } elseif ( is_array( $values ) ) {
  422. $response[ $key_to_filter ][ $key ] = array_intersect_key( $values, array_flip( $fields ) );
  423. }
  424. }
  425. $has_filtered = true;
  426. }
  427. }
  428. if ( ! $has_filtered ) {
  429. if ( is_object( $response ) ) {
  430. $response = (object) array_intersect_key( (array) $response, array_flip( $fields ) );
  431. } else if ( is_array( $response ) ) {
  432. $response = array_intersect_key( $response, array_flip( $fields ) );
  433. }
  434. }
  435. return $response;
  436. }
  437. function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) {
  438. if ( $original_scheme ) {
  439. return $url;
  440. }
  441. return preg_replace( '#^https:#', 'http:', $url );
  442. }
  443. function comment_edit_pre( $comment_content ) {
  444. return htmlspecialchars_decode( $comment_content, ENT_QUOTES );
  445. }
  446. function json_encode( $data ) {
  447. return json_encode( $data );
  448. }
  449. function ends_with( $haystack, $needle ) {
  450. return $needle === substr( $haystack, -strlen( $needle ) );
  451. }
  452. // Returns the site's blog_id in the WP.com ecosystem
  453. function get_blog_id_for_output() {
  454. return $this->token_details['blog_id'];
  455. }
  456. // Returns the site's local blog_id
  457. function get_blog_id( $blog_id ) {
  458. return $GLOBALS['blog_id'];
  459. }
  460. function switch_to_blog_and_validate_user( $blog_id = 0, $verify_token_for_blog = true ) {
  461. if ( $this->is_restricted_blog( $blog_id ) ) {
  462. return new WP_Error( 'unauthorized', 'User cannot access this restricted blog', 403 );
  463. }
  464. if ( -1 == get_option( 'blog_public' ) && !current_user_can( 'read' ) ) {
  465. return new WP_Error( 'unauthorized', 'User cannot access this private blog.', 403 );
  466. }
  467. return $blog_id;
  468. }
  469. // Returns true if the specified blog ID is a restricted blog
  470. function is_restricted_blog( $blog_id ) {
  471. /**
  472. * Filters all REST API access and return a 403 unauthorized response for all Restricted blog IDs.
  473. *
  474. * @module json-api
  475. *
  476. * @since 3.4.0
  477. *
  478. * @param array $array Array of Blog IDs.
  479. */
  480. $restricted_blog_ids = apply_filters( 'wpcom_json_api_restricted_blog_ids', array() );
  481. return true === in_array( $blog_id, $restricted_blog_ids );
  482. }
  483. function post_like_count( $blog_id, $post_id ) {
  484. return 0;
  485. }
  486. function is_liked( $blog_id, $post_id ) {
  487. return false;
  488. }
  489. function is_reblogged( $blog_id, $post_id ) {
  490. return false;
  491. }
  492. function is_following( $blog_id ) {
  493. return false;
  494. }
  495. function add_global_ID( $blog_id, $post_id ) {
  496. return '';
  497. }
  498. function get_avatar_url( $email, $avatar_size = null ) {
  499. if ( function_exists( 'wpcom_get_avatar_url' ) ) {
  500. return null === $avatar_size
  501. ? wpcom_get_avatar_url( $email )
  502. : wpcom_get_avatar_url( $email, $avatar_size );
  503. } else {
  504. return null === $avatar_size
  505. ? get_avatar_url( $email )
  506. : get_avatar_url( $email, $avatar_size );
  507. }
  508. }
  509. /**
  510. * traps `wp_die()` calls and outputs a JSON response instead.
  511. * The result is always output, never returned.
  512. *
  513. * @param string|null $error_code Call with string to start the trapping. Call with null to stop.
  514. * @param int $http_status HTTP status code, 400 by default.
  515. */
  516. function trap_wp_die( $error_code = null, $http_status = 400 ) {
  517. if ( is_null( $error_code ) ) {
  518. $this->trapped_error = null;
  519. // Stop trapping
  520. remove_filter( 'wp_die_handler', array( $this, 'wp_die_handler_callback' ) );
  521. return;
  522. }
  523. // If API called via PHP, bail: don't do our custom wp_die(). Do the normal wp_die().
  524. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  525. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
  526. return;
  527. }
  528. } else {
  529. if ( ! defined( 'XMLRPC_REQUEST' ) || ! XMLRPC_REQUEST ) {
  530. return;
  531. }
  532. }
  533. $this->trapped_error = array(
  534. 'status' => $http_status,
  535. 'code' => $error_code,
  536. 'message' => '',
  537. );
  538. // Start trapping
  539. add_filter( 'wp_die_handler', array( $this, 'wp_die_handler_callback' ) );
  540. }
  541. function wp_die_handler_callback() {
  542. return array( $this, 'wp_die_handler' );
  543. }
  544. function wp_die_handler( $message, $title = '', $args = array() ) {
  545. // Allow wp_die calls to override HTTP status code...
  546. $args = wp_parse_args( $args, array(
  547. 'response' => $this->trapped_error['status'],
  548. ) );
  549. // ... unless it's 500 ( see http://wp.me/pMz3w-5VV )
  550. if ( (int) $args['response'] !== 500 ) {
  551. $this->trapped_error['status'] = $args['response'];
  552. }
  553. if ( $title ) {
  554. $message = "$title: $message";
  555. }
  556. $this->trapped_error['message'] = wp_kses( $message, array() );
  557. switch ( $this->trapped_error['code'] ) {
  558. case 'comment_failure' :
  559. if ( did_action( 'comment_duplicate_trigger' ) ) {
  560. $this->trapped_error['code'] = 'comment_duplicate';
  561. } else if ( did_action( 'comment_flood_trigger' ) ) {
  562. $this->trapped_error['code'] = 'comment_flood';
  563. }
  564. break;
  565. }
  566. // We still want to exit so that code execution stops where it should.
  567. // Attach the JSON output to the WordPress shutdown handler
  568. add_action( 'shutdown', array( $this, 'output_trapped_error' ), 0 );
  569. exit;
  570. }
  571. function output_trapped_error() {
  572. $this->exit = false; // We're already exiting once. Don't do it twice.
  573. $this->output( $this->trapped_error['status'], (object) array(
  574. 'error' => $this->trapped_error['code'],
  575. 'message' => $this->trapped_error['message'],
  576. ) );
  577. }
  578. function finish_request() {
  579. if ( function_exists( 'fastcgi_finish_request' ) )
  580. return fastcgi_finish_request();
  581. }
  582. }