class.json-api-endpoints.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. <?php
  2. require_once( dirname( __FILE__ ) . '/json-api-config.php' );
  3. require_once( dirname( __FILE__ ) . '/sal/class.json-api-links.php' );
  4. require_once( dirname( __FILE__ ) . '/sal/class.json-api-metadata.php' );
  5. require_once( dirname( __FILE__ ) . '/sal/class.json-api-date.php' );
  6. // Endpoint
  7. abstract class WPCOM_JSON_API_Endpoint {
  8. // The API Object
  9. public $api;
  10. // The link-generating utility class
  11. public $links;
  12. public $pass_wpcom_user_details = false;
  13. // One liner.
  14. public $description;
  15. // Object Grouping For Documentation (Users, Posts, Comments)
  16. public $group;
  17. // Stats extra value to bump
  18. public $stat;
  19. // HTTP Method
  20. public $method = 'GET';
  21. // Minimum version of the api for which to serve this endpoint
  22. public $min_version = '0';
  23. // Maximum version of the api for which to serve this endpoint
  24. public $max_version = WPCOM_JSON_API__CURRENT_VERSION;
  25. // Path at which to serve this endpoint: sprintf() format.
  26. public $path = '';
  27. // Identifiers to fill sprintf() formatted $path
  28. public $path_labels = array();
  29. // Accepted query parameters
  30. public $query = array(
  31. // Parameter name
  32. 'context' => array(
  33. // Default value => description
  34. 'display' => 'Formats the output as HTML for display. Shortcodes are parsed, paragraph tags are added, etc..',
  35. // Other possible values => description
  36. 'edit' => 'Formats the output for editing. Shortcodes are left unparsed, significant whitespace is kept, etc..',
  37. ),
  38. 'http_envelope' => array(
  39. 'false' => '',
  40. 'true' => 'Some environments (like in-browser JavaScript or Flash) block or divert responses with a non-200 HTTP status code. Setting this parameter will force the HTTP status code to always be 200. The JSON response is wrapped in an "envelope" containing the "real" HTTP status code and headers.',
  41. ),
  42. 'pretty' => array(
  43. 'false' => '',
  44. 'true' => 'Output pretty JSON',
  45. ),
  46. 'meta' => "(string) Optional. Loads data from the endpoints found in the 'meta' part of the response. Comma-separated list. Example: meta=site,likes",
  47. 'fields' => '(string) Optional. Returns specified fields only. Comma-separated list. Example: fields=ID,title',
  48. // Parameter name => description (default value is empty)
  49. 'callback' => '(string) An optional JSONP callback function.',
  50. );
  51. // Response format
  52. public $response_format = array();
  53. // Request format
  54. public $request_format = array();
  55. // Is this endpoint still in testing phase? If so, not available to the public.
  56. public $in_testing = false;
  57. // Is this endpoint still allowed if the site in question is flagged?
  58. public $allowed_if_flagged = false;
  59. // Is this endpoint allowed if the site is red flagged?
  60. public $allowed_if_red_flagged = false;
  61. // Is this endpoint allowed if the site is deleted?
  62. public $allowed_if_deleted = false;
  63. /**
  64. * @var string Version of the API
  65. */
  66. public $version = '';
  67. /**
  68. * @var string Example request to make
  69. */
  70. public $example_request = '';
  71. /**
  72. * @var string Example request data (for POST methods)
  73. */
  74. public $example_request_data = '';
  75. /**
  76. * @var string Example response from $example_request
  77. */
  78. public $example_response = '';
  79. /**
  80. * @var bool Set to true if the endpoint implements its own filtering instead of the standard `fields` query method
  81. */
  82. public $custom_fields_filtering = false;
  83. /**
  84. * @var bool Set to true if the endpoint accepts all cross origin requests. You probably should not set this flag.
  85. */
  86. public $allow_cross_origin_request = false;
  87. /**
  88. * @var bool Set to true if the endpoint can recieve unauthorized POST requests.
  89. */
  90. public $allow_unauthorized_request = false;
  91. /**
  92. * @var bool Set to true if the endpoint should accept site based (not user based) authentication.
  93. */
  94. public $allow_jetpack_site_auth = false;
  95. /**
  96. * @var bool Set to true if the endpoint should accept auth from an upload token.
  97. */
  98. public $allow_upload_token_auth = false;
  99. function __construct( $args ) {
  100. $defaults = array(
  101. 'in_testing' => false,
  102. 'allowed_if_flagged' => false,
  103. 'allowed_if_red_flagged' => false,
  104. 'allowed_if_deleted' => false,
  105. 'description' => '',
  106. 'group' => '',
  107. 'method' => 'GET',
  108. 'path' => '/',
  109. 'min_version' => '0',
  110. 'max_version' => WPCOM_JSON_API__CURRENT_VERSION,
  111. 'force' => '',
  112. 'deprecated' => false,
  113. 'new_version' => WPCOM_JSON_API__CURRENT_VERSION,
  114. 'jp_disabled' => false,
  115. 'path_labels' => array(),
  116. 'request_format' => array(),
  117. 'response_format' => array(),
  118. 'query_parameters' => array(),
  119. 'version' => 'v1',
  120. 'example_request' => '',
  121. 'example_request_data' => '',
  122. 'example_response' => '',
  123. 'required_scope' => '',
  124. 'pass_wpcom_user_details' => false,
  125. 'custom_fields_filtering' => false,
  126. 'allow_cross_origin_request' => false,
  127. 'allow_unauthorized_request' => false,
  128. 'allow_jetpack_site_auth' => false,
  129. 'allow_upload_token_auth' => false,
  130. );
  131. $args = wp_parse_args( $args, $defaults );
  132. $this->in_testing = $args['in_testing'];
  133. $this->allowed_if_flagged = $args['allowed_if_flagged'];
  134. $this->allowed_if_red_flagged = $args['allowed_if_red_flagged'];
  135. $this->allowed_if_deleted = $args['allowed_if_deleted'];
  136. $this->description = $args['description'];
  137. $this->group = $args['group'];
  138. $this->stat = $args['stat'];
  139. $this->force = $args['force'];
  140. $this->jp_disabled = $args['jp_disabled'];
  141. $this->method = $args['method'];
  142. $this->path = $args['path'];
  143. $this->path_labels = $args['path_labels'];
  144. $this->min_version = $args['min_version'];
  145. $this->max_version = $args['max_version'];
  146. $this->deprecated = $args['deprecated'];
  147. $this->new_version = $args['new_version'];
  148. // Ensure max version is not less than min version
  149. if ( version_compare( $this->min_version, $this->max_version, '>' ) ) {
  150. $this->max_version = $this->min_version;
  151. }
  152. $this->pass_wpcom_user_details = $args['pass_wpcom_user_details'];
  153. $this->custom_fields_filtering = (bool) $args['custom_fields_filtering'];
  154. $this->allow_cross_origin_request = (bool) $args['allow_cross_origin_request'];
  155. $this->allow_unauthorized_request = (bool) $args['allow_unauthorized_request'];
  156. $this->allow_jetpack_site_auth = (bool) $args['allow_jetpack_site_auth'];
  157. $this->allow_upload_token_auth = (bool) $args['allow_upload_token_auth'];
  158. $this->version = $args['version'];
  159. $this->required_scope = $args['required_scope'];
  160. if ( $this->request_format ) {
  161. $this->request_format = array_filter( array_merge( $this->request_format, $args['request_format'] ) );
  162. } else {
  163. $this->request_format = $args['request_format'];
  164. }
  165. if ( $this->response_format ) {
  166. $this->response_format = array_filter( array_merge( $this->response_format, $args['response_format'] ) );
  167. } else {
  168. $this->response_format = $args['response_format'];
  169. }
  170. if ( false === $args['query_parameters'] ) {
  171. $this->query = array();
  172. } elseif ( is_array( $args['query_parameters'] ) ) {
  173. $this->query = array_filter( array_merge( $this->query, $args['query_parameters'] ) );
  174. }
  175. $this->api = WPCOM_JSON_API::init(); // Auto-add to WPCOM_JSON_API
  176. $this->links = WPCOM_JSON_API_Links::getInstance();
  177. /** Example Request/Response ******************************************/
  178. // Examples for endpoint documentation request
  179. $this->example_request = $args['example_request'];
  180. $this->example_request_data = $args['example_request_data'];
  181. $this->example_response = $args['example_response'];
  182. $this->api->add( $this );
  183. }
  184. // Get all query args. Prefill with defaults
  185. function query_args( $return_default_values = true, $cast_and_filter = true ) {
  186. $args = array_intersect_key( $this->api->query, $this->query );
  187. if ( !$cast_and_filter ) {
  188. return $args;
  189. }
  190. return $this->cast_and_filter( $args, $this->query, $return_default_values );
  191. }
  192. // Get POST body data
  193. function input( $return_default_values = true, $cast_and_filter = true ) {
  194. $input = trim( $this->api->post_body );
  195. $content_type = $this->api->content_type;
  196. if ( $content_type ) {
  197. list ( $content_type ) = explode( ';', $content_type );
  198. }
  199. $content_type = trim( $content_type );
  200. switch ( $content_type ) {
  201. case 'application/json' :
  202. case 'application/x-javascript' :
  203. case 'text/javascript' :
  204. case 'text/x-javascript' :
  205. case 'text/x-json' :
  206. case 'text/json' :
  207. $return = json_decode( $input, true );
  208. if ( function_exists( 'json_last_error' ) ) {
  209. if ( JSON_ERROR_NONE !== json_last_error() ) { // phpcs:ignore PHPCompatibility
  210. return null;
  211. }
  212. } else {
  213. if ( is_null( $return ) && json_encode( null ) !== $input ) {
  214. return null;
  215. }
  216. }
  217. break;
  218. case 'multipart/form-data' :
  219. $return = array_merge( stripslashes_deep( $_POST ), $_FILES );
  220. break;
  221. case 'application/x-www-form-urlencoded' :
  222. //attempt JSON first, since probably a curl command
  223. $return = json_decode( $input, true );
  224. if ( is_null( $return ) ) {
  225. wp_parse_str( $input, $return );
  226. }
  227. break;
  228. default :
  229. wp_parse_str( $input, $return );
  230. break;
  231. }
  232. if ( isset( $this->api->query['force'] )
  233. && 'secure' === $this->api->query['force']
  234. && isset( $return['secure_key'] ) ) {
  235. $this->api->post_body = $this->get_secure_body( $return['secure_key'] );
  236. $this->api->query['force'] = false;
  237. return $this->input( $return_default_values, $cast_and_filter );
  238. }
  239. if ( $cast_and_filter ) {
  240. $return = $this->cast_and_filter( $return, $this->request_format, $return_default_values );
  241. }
  242. return $return;
  243. }
  244. protected function get_secure_body( $secure_key ) {
  245. $response = Jetpack_Client::wpcom_json_api_request_as_blog(
  246. sprintf( '/sites/%d/secure-request', Jetpack_Options::get_option('id' ) ),
  247. '1.1',
  248. array( 'method' => 'POST' ),
  249. array( 'secure_key' => $secure_key )
  250. );
  251. if ( 200 !== $response['response']['code'] ) {
  252. return null;
  253. }
  254. return json_decode( $response['body'], true );
  255. }
  256. function cast_and_filter( $data, $documentation, $return_default_values = false, $for_output = false ) {
  257. $return_as_object = false;
  258. if ( is_object( $data ) ) {
  259. // @todo this should probably be a deep copy if $data can ever have nested objects
  260. $data = (array) $data;
  261. $return_as_object = true;
  262. } elseif ( !is_array( $data ) ) {
  263. return $data;
  264. }
  265. $boolean_arg = array( 'false', 'true' );
  266. $naeloob_arg = array( 'true', 'false' );
  267. $return = array();
  268. foreach ( $documentation as $key => $description ) {
  269. if ( is_array( $description ) ) {
  270. // String or boolean array keys only
  271. $whitelist = array_keys( $description );
  272. if ( $whitelist === $boolean_arg || $whitelist === $naeloob_arg ) {
  273. // Truthiness
  274. if ( isset( $data[$key] ) ) {
  275. $return[$key] = (bool) WPCOM_JSON_API::is_truthy( $data[$key] );
  276. } elseif ( $return_default_values ) {
  277. $return[$key] = $whitelist === $naeloob_arg; // Default to true for naeloob_arg and false for boolean_arg.
  278. }
  279. } elseif ( isset( $data[$key] ) && isset( $description[$data[$key]] ) ) {
  280. // String Key
  281. $return[$key] = (string) $data[$key];
  282. } elseif ( $return_default_values ) {
  283. // Default value
  284. $return[$key] = (string) current( $whitelist );
  285. }
  286. continue;
  287. }
  288. $types = $this->parse_types( $description );
  289. $type = array_shift( $types );
  290. // Explicit default - string and int only for now. Always set these reguardless of $return_default_values
  291. if ( isset( $type['default'] ) ) {
  292. if ( !isset( $data[$key] ) ) {
  293. $data[$key] = $type['default'];
  294. }
  295. }
  296. if ( !isset( $data[$key] ) ) {
  297. continue;
  298. }
  299. $this->cast_and_filter_item( $return, $type, $key, $data[$key], $types, $for_output );
  300. }
  301. if ( $return_as_object ) {
  302. return (object) $return;
  303. }
  304. return $return;
  305. }
  306. /**
  307. * Casts $value according to $type.
  308. * Handles fallbacks for certain values of $type when $value is not that $type
  309. * Currently, only handles fallback between string <-> array (two way), from string -> false (one way), and from object -> false (one way),
  310. * and string -> object (one way)
  311. *
  312. * Handles "child types" - array:URL, object:category
  313. * array:URL means an array of URLs
  314. * object:category means a hash of categories
  315. *
  316. * Handles object typing - object>post means an object of type post
  317. */
  318. function cast_and_filter_item( &$return, $type, $key, $value, $types = array(), $for_output = false ) {
  319. if ( is_string( $type ) ) {
  320. $type = compact( 'type' );
  321. }
  322. switch ( $type['type'] ) {
  323. case 'false' :
  324. $return[$key] = false;
  325. break;
  326. case 'url' :
  327. $return[$key] = (string) esc_url_raw( $value );
  328. break;
  329. case 'string' :
  330. // Fallback string -> array, or for string -> object
  331. if ( is_array( $value ) || is_object( $value ) ) {
  332. if ( !empty( $types[0] ) ) {
  333. $next_type = array_shift( $types );
  334. return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
  335. }
  336. }
  337. // Fallback string -> false
  338. if ( !is_string( $value ) ) {
  339. if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) {
  340. $next_type = array_shift( $types );
  341. return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
  342. }
  343. }
  344. $return[$key] = (string) $value;
  345. break;
  346. case 'html' :
  347. $return[$key] = (string) $value;
  348. break;
  349. case 'safehtml' :
  350. $return[$key] = wp_kses( (string) $value, wp_kses_allowed_html() );
  351. break;
  352. case 'zip' :
  353. case 'media' :
  354. if ( is_array( $value ) ) {
  355. if ( isset( $value['name'] ) && is_array( $value['name'] ) ) {
  356. // It's a $_FILES array
  357. // Reformat into array of $_FILES items
  358. $files = array();
  359. foreach ( $value['name'] as $k => $v ) {
  360. $files[$k] = array();
  361. foreach ( array_keys( $value ) as $file_key ) {
  362. $files[$k][$file_key] = $value[$file_key][$k];
  363. }
  364. }
  365. $return[$key] = $files;
  366. break;
  367. }
  368. } else {
  369. // no break - treat as 'array'
  370. }
  371. // nobreak
  372. case 'array' :
  373. // Fallback array -> string
  374. if ( is_string( $value ) ) {
  375. if ( !empty( $types[0] ) ) {
  376. $next_type = array_shift( $types );
  377. return $this->cast_and_filter_item( $return, $next_type, $key, $value, $types, $for_output );
  378. }
  379. }
  380. if ( isset( $type['children'] ) ) {
  381. $children = array();
  382. foreach ( (array) $value as $k => $child ) {
  383. $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output );
  384. }
  385. $return[$key] = (array) $children;
  386. break;
  387. }
  388. $return[$key] = (array) $value;
  389. break;
  390. case 'iso 8601 datetime' :
  391. case 'datetime' :
  392. // (string)s
  393. $dates = $this->parse_date( (string) $value );
  394. if ( $for_output ) {
  395. $return[$key] = $this->format_date( $dates[1], $dates[0] );
  396. } else {
  397. list( $return[$key], $return["{$key}_gmt"] ) = $dates;
  398. }
  399. break;
  400. case 'float' :
  401. $return[$key] = (float) $value;
  402. break;
  403. case 'int' :
  404. case 'integer' :
  405. $return[$key] = (int) $value;
  406. break;
  407. case 'bool' :
  408. case 'boolean' :
  409. $return[$key] = (bool) WPCOM_JSON_API::is_truthy( $value );
  410. break;
  411. case 'object' :
  412. // Fallback object -> false
  413. if ( is_scalar( $value ) || is_null( $value ) ) {
  414. if ( !empty( $types[0] ) && 'false' === $types[0]['type'] ) {
  415. return $this->cast_and_filter_item( $return, 'false', $key, $value, $types, $for_output );
  416. }
  417. }
  418. if ( isset( $type['children'] ) ) {
  419. $children = array();
  420. foreach ( (array) $value as $k => $child ) {
  421. $this->cast_and_filter_item( $children, $type['children'], $k, $child, array(), $for_output );
  422. }
  423. $return[$key] = (object) $children;
  424. break;
  425. }
  426. if ( isset( $type['subtype'] ) ) {
  427. return $this->cast_and_filter_item( $return, $type['subtype'], $key, $value, $types, $for_output );
  428. }
  429. $return[$key] = (object) $value;
  430. break;
  431. case 'post' :
  432. $return[$key] = (object) $this->cast_and_filter( $value, $this->post_object_format, false, $for_output );
  433. break;
  434. case 'comment' :
  435. $return[$key] = (object) $this->cast_and_filter( $value, $this->comment_object_format, false, $for_output );
  436. break;
  437. case 'tag' :
  438. case 'category' :
  439. $docs = array(
  440. 'ID' => '(int)',
  441. 'name' => '(string)',
  442. 'slug' => '(string)',
  443. 'description' => '(HTML)',
  444. 'post_count' => '(int)',
  445. 'feed_url' => '(string)',
  446. 'meta' => '(object)',
  447. );
  448. if ( 'category' === $type['type'] ) {
  449. $docs['parent'] = '(int)';
  450. }
  451. $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
  452. break;
  453. case 'post_reference' :
  454. case 'comment_reference' :
  455. $docs = array(
  456. 'ID' => '(int)',
  457. 'type' => '(string)',
  458. 'title' => '(string)',
  459. 'link' => '(URL)',
  460. );
  461. $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
  462. break;
  463. case 'geo' :
  464. $docs = array(
  465. 'latitude' => '(float)',
  466. 'longitude' => '(float)',
  467. 'address' => '(string)',
  468. );
  469. $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
  470. break;
  471. case 'author' :
  472. $docs = array(
  473. 'ID' => '(int)',
  474. 'user_login' => '(string)',
  475. 'login' => '(string)',
  476. 'email' => '(string|false)',
  477. 'name' => '(string)',
  478. 'first_name' => '(string)',
  479. 'last_name' => '(string)',
  480. 'nice_name' => '(string)',
  481. 'URL' => '(URL)',
  482. 'avatar_URL' => '(URL)',
  483. 'profile_URL' => '(URL)',
  484. 'is_super_admin' => '(bool)',
  485. 'roles' => '(array:string)',
  486. 'ip_address' => '(string|false)',
  487. );
  488. $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
  489. break;
  490. case 'role' :
  491. $docs = array(
  492. 'name' => '(string)',
  493. 'display_name' => '(string)',
  494. 'capabilities' => '(object:boolean)',
  495. );
  496. $return[$key] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
  497. break;
  498. case 'attachment' :
  499. $docs = array(
  500. 'ID' => '(int)',
  501. 'URL' => '(URL)',
  502. 'guid' => '(string)',
  503. 'mime_type' => '(string)',
  504. 'width' => '(int)',
  505. 'height' => '(int)',
  506. 'duration' => '(int)',
  507. );
  508. $return[$key] = (object) $this->cast_and_filter(
  509. $value,
  510. /**
  511. * Filter the documentation returned for a post attachment.
  512. *
  513. * @module json-api
  514. *
  515. * @since 1.9.0
  516. *
  517. * @param array $docs Array of documentation about a post attachment.
  518. */
  519. apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ),
  520. false,
  521. $for_output
  522. );
  523. break;
  524. case 'metadata' :
  525. $docs = array(
  526. 'id' => '(int)',
  527. 'key' => '(string)',
  528. 'value' => '(string|false|float|int|array|object)',
  529. 'previous_value' => '(string)',
  530. 'operation' => '(string)',
  531. );
  532. $return[$key] = (object) $this->cast_and_filter(
  533. $value,
  534. /** This filter is documented in class.json-api-endpoints.php */
  535. apply_filters( 'wpcom_json_api_attachment_cast_and_filter', $docs ),
  536. false,
  537. $for_output
  538. );
  539. break;
  540. case 'plugin' :
  541. $docs = array(
  542. 'id' => '(safehtml) The plugin\'s ID',
  543. 'slug' => '(safehtml) The plugin\'s Slug',
  544. 'active' => '(boolean) The plugin status.',
  545. 'update' => '(object) The plugin update info.',
  546. 'name' => '(safehtml) The name of the plugin.',
  547. 'plugin_url' => '(url) Link to the plugin\'s web site.',
  548. 'version' => '(safehtml) The plugin version number.',
  549. 'description' => '(safehtml) Description of what the plugin does and/or notes from the author',
  550. 'author' => '(safehtml) The plugin author\'s name',
  551. 'author_url' => '(url) The plugin author web site address',
  552. 'network' => '(boolean) Whether the plugin can only be activated network wide.',
  553. 'autoupdate' => '(boolean) Whether the plugin is auto updated',
  554. 'log' => '(array:safehtml) An array of update log strings.',
  555. 'action_links' => '(array) An array of action links that the plugin uses.',
  556. );
  557. $return[$key] = (object) $this->cast_and_filter(
  558. $value,
  559. /**
  560. * Filter the documentation returned for a plugin.
  561. *
  562. * @module json-api
  563. *
  564. * @since 3.1.0
  565. *
  566. * @param array $docs Array of documentation about a plugin.
  567. */
  568. apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
  569. false,
  570. $for_output
  571. );
  572. break;
  573. case 'plugin_v1_2' :
  574. $docs = class_exists( 'Jetpack_JSON_API_Get_Plugins_v1_2_Endpoint' )
  575. ? Jetpack_JSON_API_Get_Plugins_v1_2_Endpoint::$_response_format
  576. : Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2;
  577. $return[$key] = (object) $this->cast_and_filter(
  578. $value,
  579. /**
  580. * Filter the documentation returned for a plugin.
  581. *
  582. * @module json-api
  583. *
  584. * @since 3.1.0
  585. *
  586. * @param array $docs Array of documentation about a plugin.
  587. */
  588. apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
  589. false,
  590. $for_output
  591. );
  592. break;
  593. case 'file_mod_capabilities':
  594. $docs = array(
  595. 'reasons_modify_files_unavailable' => '(array) The reasons why files can\'t be modified',
  596. 'reasons_autoupdate_unavailable' => '(array) The reasons why autoupdates aren\'t allowed',
  597. 'modify_files' => '(boolean) true if files can be modified',
  598. 'autoupdate_files' => '(boolean) true if autoupdates are allowed',
  599. );
  600. $return[ $key ] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
  601. break;
  602. case 'jetpackmodule' :
  603. $docs = array(
  604. 'id' => '(string) The module\'s ID',
  605. 'active' => '(boolean) The module\'s status.',
  606. 'name' => '(string) The module\'s name.',
  607. 'description' => '(safehtml) The module\'s description.',
  608. 'sort' => '(int) The module\'s display order.',
  609. 'introduced' => '(string) The Jetpack version when the module was introduced.',
  610. 'changed' => '(string) The Jetpack version when the module was changed.',
  611. 'free' => '(boolean) The module\'s Free or Paid status.',
  612. 'module_tags' => '(array) The module\'s tags.',
  613. 'override' => '(string) The module\'s override. Empty if no override, otherwise \'active\' or \'inactive\'',
  614. );
  615. $return[$key] = (object) $this->cast_and_filter(
  616. $value,
  617. /** This filter is documented in class.json-api-endpoints.php */
  618. apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
  619. false,
  620. $for_output
  621. );
  622. break;
  623. case 'sharing_button' :
  624. $docs = array(
  625. 'ID' => '(string)',
  626. 'name' => '(string)',
  627. 'URL' => '(string)',
  628. 'icon' => '(string)',
  629. 'enabled' => '(bool)',
  630. 'visibility' => '(string)',
  631. );
  632. $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
  633. break;
  634. case 'sharing_button_service':
  635. $docs = array(
  636. 'ID' => '(string) The service identifier',
  637. 'name' => '(string) The service name',
  638. 'class_name' => '(string) Class name for custom style sharing button elements',
  639. 'genericon' => '(string) The Genericon unicode character for the custom style sharing button icon',
  640. 'preview_smart' => '(string) An HTML snippet of a rendered sharing button smart preview',
  641. 'preview_smart_js' => '(string) An HTML snippet of the page-wide initialization scripts used for rendering the sharing button smart preview'
  642. );
  643. $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
  644. break;
  645. case 'site_keyring':
  646. $docs = array(
  647. 'keyring_id' => '(int) Keyring ID',
  648. 'service' => '(string) The service name',
  649. 'external_user_id' => '(string) External user id for the service'
  650. );
  651. $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
  652. break;
  653. case 'taxonomy':
  654. $docs = array(
  655. 'name' => '(string) The taxonomy slug',
  656. 'label' => '(string) The taxonomy human-readable name',
  657. 'labels' => '(object) Mapping of labels for the taxonomy',
  658. 'description' => '(string) The taxonomy description',
  659. 'hierarchical' => '(bool) Whether the taxonomy is hierarchical',
  660. 'public' => '(bool) Whether the taxonomy is public',
  661. 'capabilities' => '(object) Mapping of current user capabilities for the taxonomy',
  662. );
  663. $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output );
  664. break;
  665. default :
  666. $method_name = $type['type'] . '_docs';
  667. if ( method_exists( 'WPCOM_JSON_API_Jetpack_Overrides', $method_name ) ) {
  668. $docs = WPCOM_JSON_API_Jetpack_Overrides::$method_name();
  669. }
  670. if ( ! empty( $docs ) ) {
  671. $return[$key] = (object) $this->cast_and_filter(
  672. $value,
  673. /** This filter is documented in class.json-api-endpoints.php */
  674. apply_filters( 'wpcom_json_api_plugin_cast_and_filter', $docs ),
  675. false,
  676. $for_output
  677. );
  678. } else {
  679. trigger_error( "Unknown API casting type {$type['type']}", E_USER_WARNING );
  680. }
  681. }
  682. }
  683. function parse_types( $text ) {
  684. if ( !preg_match( '#^\(([^)]+)\)#', ltrim( $text ), $matches ) ) {
  685. return 'none';
  686. }
  687. $types = explode( '|', strtolower( $matches[1] ) );
  688. $return = array();
  689. foreach ( $types as $type ) {
  690. foreach ( array( ':' => 'children', '>' => 'subtype', '=' => 'default' ) as $operator => $meaning ) {
  691. if ( false !== strpos( $type, $operator ) ) {
  692. $item = explode( $operator, $type, 2 );
  693. $return[] = array( 'type' => $item[0], $meaning => $item[1] );
  694. continue 2;
  695. }
  696. }
  697. $return[] = compact( 'type' );
  698. }
  699. return $return;
  700. }
  701. /**
  702. * Checks if the endpoint is publicly displayable
  703. */
  704. function is_publicly_documentable() {
  705. return '__do_not_document' !== $this->group && true !== $this->in_testing;
  706. }
  707. /**
  708. * Auto generates documentation based on description, method, path, path_labels, and query parameters.
  709. * Echoes HTML.
  710. */
  711. function document( $show_description = true ) {
  712. global $wpdb;
  713. $original_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : 'unset';
  714. unset( $GLOBALS['post'] );
  715. $doc = $this->generate_documentation();
  716. if ( $show_description ) :
  717. ?>
  718. <caption>
  719. <h1><?php echo wp_kses_post( $doc['method'] ); ?> <?php echo wp_kses_post( $doc['path_labeled'] ); ?></h1>
  720. <p><?php echo wp_kses_post( $doc['description'] ); ?></p>
  721. </caption>
  722. <?php endif; ?>
  723. <?php if ( true === $this->deprecated ) { ?>
  724. <p><strong>This endpoint is deprecated in favor of version <?php echo floatval( $this->new_version ); ?></strong></p>
  725. <?php } ?>
  726. <section class="resource-info">
  727. <h2 id="apidoc-resource-info">Resource Information</h2>
  728. <table class="api-doc api-doc-resource-parameters api-doc-resource">
  729. <thead>
  730. <tr>
  731. <th class="api-index-title" scope="column">&nbsp;</th>
  732. <th class="api-index-title" scope="column">&nbsp;</th>
  733. </tr>
  734. </thead>
  735. <tbody>
  736. <tr class="api-index-item">
  737. <th scope="row" class="parameter api-index-item-title">Method</th>
  738. <td class="type api-index-item-title"><?php echo wp_kses_post( $doc['method'] ); ?></td>
  739. </tr>
  740. <tr class="api-index-item">
  741. <th scope="row" class="parameter api-index-item-title">URL</th>
  742. <?php
  743. $version = WPCOM_JSON_API__CURRENT_VERSION;
  744. if ( !empty( $this->max_version ) ) {
  745. $version = $this->max_version;
  746. }
  747. ?>
  748. <td class="type api-index-item-title">https://public-api.wordpress.com/rest/v<?php echo floatval( $version ); ?><?php echo wp_kses_post( $doc['path_labeled'] ); ?></td>
  749. </tr>
  750. <tr class="api-index-item">
  751. <th scope="row" class="parameter api-index-item-title">Requires authentication?</th>
  752. <?php
  753. $requires_auth = $wpdb->get_row( $wpdb->prepare( "SELECT requires_authentication FROM rest_api_documentation WHERE `version` = %s AND `path` = %s AND `method` = %s LIMIT 1", $version, untrailingslashit( $doc['path_labeled'] ), $doc['method'] ) );
  754. ?>
  755. <td class="type api-index-item-title"><?php echo ( true === (bool) $requires_auth->requires_authentication ? 'Yes' : 'No' ); ?></td>
  756. </tr>
  757. </tbody>
  758. </table>
  759. </section>
  760. <?php
  761. foreach ( array(
  762. 'path' => 'Method Parameters',
  763. 'query' => 'Query Parameters',
  764. 'body' => 'Request Parameters',
  765. 'response' => 'Response Parameters',
  766. ) as $doc_section_key => $label ) :
  767. $doc_section = 'response' === $doc_section_key ? $doc['response']['body'] : $doc['request'][$doc_section_key];
  768. if ( !$doc_section ) {
  769. continue;
  770. }
  771. $param_label = strtolower( str_replace( ' ', '-', $label ) );
  772. ?>
  773. <section class="<?php echo $param_label; ?>">
  774. <h2 id="apidoc-<?php echo esc_attr( $doc_section_key ); ?>"><?php echo wp_kses_post( $label ); ?></h2>
  775. <table class="api-doc api-doc-<?php echo $param_label; ?>-parameters api-doc-<?php echo strtolower( str_replace( ' ', '-', $doc['group'] ) ); ?>">
  776. <thead>
  777. <tr>
  778. <th class="api-index-title" scope="column">Parameter</th>
  779. <th class="api-index-title" scope="column">Type</th>
  780. <th class="api-index-title" scope="column">Description</th>
  781. </tr>
  782. </thead>
  783. <tbody>
  784. <?php foreach ( $doc_section as $key => $item ) : ?>
  785. <tr class="api-index-item">
  786. <th scope="row" class="parameter api-index-item-title"><?php echo wp_kses_post( $key ); ?></th>
  787. <td class="type api-index-item-title"><?php echo wp_kses_post( $item['type'] ); // @todo auto-link? ?></td>
  788. <td class="description api-index-item-body"><?php
  789. $this->generate_doc_description( $item['description'] );
  790. ?></td>
  791. </tr>
  792. <?php endforeach; ?>
  793. </tbody>
  794. </table>
  795. </section>
  796. <?php endforeach; ?>
  797. <?php
  798. if ( 'unset' !== $original_post ) {
  799. $GLOBALS['post'] = $original_post;
  800. }
  801. }
  802. function add_http_build_query_to_php_content_example( $matches ) {
  803. $trimmed_match = ltrim( $matches[0] );
  804. $pad = substr( $matches[0], 0, -1 * strlen( $trimmed_match ) );
  805. $pad = ltrim( $pad, ' ' );
  806. $return = ' ' . str_replace( "\n", "\n ", $matches[0] );
  807. return " http_build_query({$return}{$pad})";
  808. }
  809. /**
  810. * Recursively generates the <dl>'s to document item descriptions.
  811. * Echoes HTML.
  812. */
  813. function generate_doc_description( $item ) {
  814. if ( is_array( $item ) ) : ?>
  815. <dl>
  816. <?php foreach ( $item as $description_key => $description_value ) : ?>
  817. <dt><?php echo wp_kses_post( $description_key . ':' ); ?></dt>
  818. <dd><?php $this->generate_doc_description( $description_value ); ?></dd>
  819. <?php endforeach; ?>
  820. </dl>
  821. <?php
  822. else :
  823. echo wp_kses_post( $item );
  824. endif;
  825. }
  826. /**
  827. * Auto generates documentation based on description, method, path, path_labels, and query parameters.
  828. * Echoes HTML.
  829. */
  830. function generate_documentation() {
  831. $format = str_replace( '%d', '%s', $this->path );
  832. $path_labeled = $format;
  833. if ( ! empty( $this->path_labels ) ) {
  834. $path_labeled = vsprintf( $format, array_keys( $this->path_labels ) );
  835. }
  836. $boolean_arg = array( 'false', 'true' );
  837. $naeloob_arg = array( 'true', 'false' );
  838. $doc = array(
  839. 'description' => $this->description,
  840. 'method' => $this->method,
  841. 'path_format' => $this->path,
  842. 'path_labeled' => $path_labeled,
  843. 'group' => $this->group,
  844. 'request' => array(
  845. 'path' => array(),
  846. 'query' => array(),
  847. 'body' => array(),
  848. ),
  849. 'response' => array(
  850. 'body' => array(),
  851. )
  852. );
  853. foreach ( array( 'path_labels' => 'path', 'query' => 'query', 'request_format' => 'body', 'response_format' => 'body' ) as $_property => $doc_item ) {
  854. foreach ( (array) $this->$_property as $key => $description ) {
  855. if ( is_array( $description ) ) {
  856. $description_keys = array_keys( $description );
  857. if ( $boolean_arg === $description_keys || $naeloob_arg === $description_keys ) {
  858. $type = '(bool)';
  859. } else {
  860. $type = '(string)';
  861. }
  862. if ( 'response_format' !== $_property ) {
  863. // hack - don't show "(default)" in response format
  864. reset( $description );
  865. $description_key = key( $description );
  866. $description[$description_key] = "(default) {$description[$description_key]}";
  867. }
  868. } else {
  869. $types = $this->parse_types( $description );
  870. $type = array();
  871. $default = '';
  872. if ( 'none' == $types ) {
  873. $types = array();
  874. $types[]['type'] = 'none';
  875. }
  876. foreach ( $types as $type_array ) {
  877. $type[] = $type_array['type'];
  878. if ( isset( $type_array['default'] ) ) {
  879. $default = $type_array['default'];
  880. if ( 'string' === $type_array['type'] ) {
  881. $default = "'$default'";
  882. }
  883. }
  884. }
  885. $type = '(' . join( '|', $type ) . ')';
  886. $noop = ''; // skip an index in list below
  887. list( $noop, $description ) = explode( ')', $description, 2 );
  888. $description = trim( $description );
  889. if ( $default ) {
  890. $description .= " Default: $default.";
  891. }
  892. }
  893. $item = compact( 'type', 'description' );
  894. if ( 'response_format' === $_property ) {
  895. $doc['response'][$doc_item][$key] = $item;
  896. } else {
  897. $doc['request'][$doc_item][$key] = $item;
  898. }
  899. }
  900. }
  901. return $doc;
  902. }
  903. function user_can_view_post( $post_id ) {
  904. $post = get_post( $post_id );
  905. if ( !$post || is_wp_error( $post ) ) {
  906. return false;
  907. }
  908. if ( 'inherit' === $post->post_status ) {
  909. $parent_post = get_post( $post->post_parent );
  910. $post_status_obj = get_post_status_object( $parent_post->post_status );
  911. } else {
  912. $post_status_obj = get_post_status_object( $post->post_status );
  913. }
  914. if ( !$post_status_obj->public ) {
  915. if ( is_user_logged_in() ) {
  916. if ( $post_status_obj->protected ) {
  917. if ( !current_user_can( 'edit_post', $post->ID ) ) {
  918. return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
  919. }
  920. } elseif ( $post_status_obj->private ) {
  921. if ( !current_user_can( 'read_post', $post->ID ) ) {
  922. return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
  923. }
  924. } elseif ( in_array( $post->post_status, array( 'inherit', 'trash' ) ) ) {
  925. if ( !current_user_can( 'edit_post', $post->ID ) ) {
  926. return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
  927. }
  928. } elseif ( 'auto-draft' === $post->post_status ) {
  929. //allow auto-drafts
  930. } else {
  931. return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
  932. }
  933. } else {
  934. return new WP_Error( 'unauthorized', 'User cannot view post', 403 );
  935. }
  936. }
  937. if (
  938. -1 == get_option( 'blog_public' ) &&
  939. /**
  940. * Filter access to a specific post.
  941. *
  942. * @module json-api
  943. *
  944. * @since 3.4.0
  945. *
  946. * @param bool current_user_can( 'read_post', $post->ID ) Can the current user access the post.
  947. * @param WP_Post $post Post data.
  948. */
  949. ! apply_filters(
  950. 'wpcom_json_api_user_can_view_post',
  951. current_user_can( 'read_post', $post->ID ),
  952. $post
  953. )
  954. ) {
  955. return new WP_Error( 'unauthorized', 'User cannot view post', array( 'status_code' => 403, 'error' => 'private_blog' ) );
  956. }
  957. if ( strlen( $post->post_password ) && !current_user_can( 'edit_post', $post->ID ) ) {
  958. return new WP_Error( 'unauthorized', 'User cannot view password protected post', array( 'status_code' => 403, 'error' => 'password_protected' ) );
  959. }
  960. return true;
  961. }
  962. /**
  963. * Returns author object.
  964. *
  965. * @param object $author user ID, user row, WP_User object, comment row, post row
  966. * @param bool $show_email_and_ip output the author's email address and IP address?
  967. *
  968. * @return object
  969. */
  970. function get_author( $author, $show_email_and_ip = false ) {
  971. $ip_address = isset( $author->comment_author_IP ) ? $author->comment_author_IP : '';
  972. if ( isset( $author->comment_author_email ) ) {
  973. $ID = 0;
  974. $login = '';
  975. $email = $author->comment_author_email;
  976. $name = $author->comment_author;
  977. $first_name = '';
  978. $last_name = '';
  979. $URL = $author->comment_author_url;
  980. $avatar_URL = $this->api->get_avatar_url( $author );
  981. $profile_URL = 'https://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
  982. $nice = '';
  983. $site_id = -1;
  984. // Comment author URLs and Emails are sent through wp_kses() on save, which replaces "&" with "&amp;"
  985. // "&" is the only email/URL character altered by wp_kses()
  986. foreach ( array( 'email', 'URL' ) as $field ) {
  987. $$field = str_replace( '&amp;', '&', $$field );
  988. }
  989. } else {
  990. if ( isset( $author->user_id ) && $author->user_id ) {
  991. $author = $author->user_id;
  992. } elseif ( isset( $author->user_email ) ) {
  993. $author = $author->ID;
  994. } elseif ( isset( $author->post_author ) ) {
  995. // then $author is a Post Object.
  996. if ( 0 == $author->post_author )
  997. return null;
  998. /**
  999. * Filter whether the current site is a Jetpack site.
  1000. *
  1001. * @module json-api
  1002. *
  1003. * @since 3.3.0
  1004. *
  1005. * @param bool false Is the current site a Jetpack site. Default to false.
  1006. * @param int get_current_blog_id() Blog ID.
  1007. */
  1008. $is_jetpack = true === apply_filters( 'is_jetpack_site', false, get_current_blog_id() );
  1009. $post_id = $author->ID;
  1010. if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
  1011. $ID = get_post_meta( $post_id, '_jetpack_post_author_external_id', true );
  1012. $email = get_post_meta( $post_id, '_jetpack_author_email', true );
  1013. $login = '';
  1014. $name = get_post_meta( $post_id, '_jetpack_author', true );
  1015. $first_name = '';
  1016. $last_name = '';
  1017. $URL = '';
  1018. $nice = '';
  1019. } else {
  1020. $author = $author->post_author;
  1021. }
  1022. }
  1023. if ( ! isset( $ID ) ) {
  1024. $user = get_user_by( 'id', $author );
  1025. if ( ! $user || is_wp_error( $user ) ) {
  1026. trigger_error( 'Unknown user', E_USER_WARNING );
  1027. return null;
  1028. }
  1029. $ID = $user->ID;
  1030. $email = $user->user_email;
  1031. $login = $user->user_login;
  1032. $name = $user->display_name;
  1033. $first_name = $user->first_name;
  1034. $last_name = $user->last_name;
  1035. $URL = $user->user_url;
  1036. $nice = $user->user_nicename;
  1037. }
  1038. if ( defined( 'IS_WPCOM' ) && IS_WPCOM && ! $is_jetpack ) {
  1039. $active_blog = get_active_blog_for_user( $ID );
  1040. $site_id = $active_blog->blog_id;
  1041. if ( $site_id > -1 ) {
  1042. $site_visible = (
  1043. -1 != $active_blog->public ||
  1044. is_private_blog_user( $site_id, get_current_user_id() )
  1045. );
  1046. }
  1047. $profile_URL = "https://en.gravatar.com/{$login}";
  1048. } else {
  1049. $profile_URL = 'https://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
  1050. $site_id = -1;
  1051. }
  1052. $avatar_URL = $this->api->get_avatar_url( $email );
  1053. }
  1054. if ( $show_email_and_ip ) {
  1055. $email = (string) $email;
  1056. $ip_address = (string) $ip_address;
  1057. } else {
  1058. $email = false;
  1059. $ip_address = false;
  1060. }
  1061. $author = array(
  1062. 'ID' => (int) $ID,
  1063. 'login' => (string) $login,
  1064. 'email' => $email, // (string|bool)
  1065. 'name' => (string) $name,
  1066. 'first_name' => (string) $first_name,
  1067. 'last_name' => (string) $last_name,
  1068. 'nice_name' => (string) $nice,
  1069. 'URL' => (string) esc_url_raw( $URL ),
  1070. 'avatar_URL' => (string) esc_url_raw( $avatar_URL ),
  1071. 'profile_URL' => (string) esc_url_raw( $profile_URL ),
  1072. 'ip_address' => $ip_address, // (string|bool)
  1073. );
  1074. if ( $site_id > -1 ) {
  1075. $author['site_ID'] = (int) $site_id;
  1076. $author['site_visible'] = $site_visible;
  1077. }
  1078. return (object) $author;
  1079. }
  1080. function get_media_item( $media_id ) {
  1081. $media_item = get_post( $media_id );
  1082. if ( !$media_item || is_wp_error( $media_item ) )
  1083. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  1084. $response = array(
  1085. 'id' => strval( $media_item->ID ),
  1086. 'date' => (string) $this->format_date( $media_item->post_date_gmt, $media_item->post_date ),
  1087. 'parent' => $media_item->post_parent,
  1088. 'link' => wp_get_attachment_url( $media_item->ID ),
  1089. 'title' => $media_item->post_title,
  1090. 'caption' => $media_item->post_excerpt,
  1091. 'description' => $media_item->post_content,
  1092. 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
  1093. );
  1094. if ( defined( 'IS_WPCOM' ) && IS_WPCOM && is_array( $response['metadata'] ) && ! empty( $response['metadata']['file'] ) ) {
  1095. remove_filter( '_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10 );
  1096. $response['metadata']['file'] = _wp_relative_upload_path( $response['metadata']['file'] );
  1097. add_filter( '_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10, 2 );
  1098. }
  1099. $response['meta'] = (object) array(
  1100. 'links' => (object) array(
  1101. 'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id ),
  1102. 'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ),
  1103. 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
  1104. ),
  1105. );
  1106. return (object) $response;
  1107. }
  1108. function get_media_item_v1_1( $media_id, $media_item = null, $file = null ) {
  1109. if ( ! $media_item ) {
  1110. $media_item = get_post( $media_id );
  1111. }
  1112. if ( ! $media_item || is_wp_error( $media_item ) ) {
  1113. return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
  1114. }
  1115. $attachment_file = get_attached_file( $media_item->ID );
  1116. $file = basename( $attachment_file ? $attachment_file : $file );
  1117. $file_info = pathinfo( $file );
  1118. $ext = isset( $file_info['extension'] ) ? $file_info['extension'] : null;
  1119. $response = array(
  1120. 'ID' => $media_item->ID,
  1121. 'URL' => wp_get_attachment_url( $media_item->ID ),
  1122. 'guid' => $media_item->guid,
  1123. 'date' => (string) $this->format_date( $media_item->post_date_gmt, $media_item->post_date ),
  1124. 'post_ID' => $media_item->post_parent,
  1125. 'author_ID' => (int) $media_item->post_author,
  1126. 'file' => $file,
  1127. 'mime_type' => $media_item->post_mime_type,
  1128. 'extension' => $ext,
  1129. 'title' => $media_item->post_title,
  1130. 'caption' => $media_item->post_excerpt,
  1131. 'description' => $media_item->post_content,
  1132. 'alt' => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
  1133. 'icon' => wp_mime_type_icon( $media_item->ID ),
  1134. 'thumbnails' => array()
  1135. );
  1136. if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) {
  1137. $metadata = wp_get_attachment_metadata( $media_item->ID );
  1138. if ( isset( $metadata['height'], $metadata['width'] ) ) {
  1139. $response['height'] = $metadata['height'];
  1140. $response['width'] = $metadata['width'];
  1141. }
  1142. if ( isset( $metadata['sizes'] ) ) {
  1143. /**
  1144. * Filter the thumbnail sizes available for each attachment ID.
  1145. *
  1146. * @module json-api
  1147. *
  1148. * @since 3.9.0
  1149. *
  1150. * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
  1151. * @param string $media_id Attachment ID.
  1152. */
  1153. $sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_item->ID );
  1154. if ( is_array( $sizes ) ) {
  1155. foreach ( $sizes as $size => $size_details ) {
  1156. $response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
  1157. }
  1158. }
  1159. }
  1160. if ( isset( $metadata['image_meta'] ) ) {
  1161. $response['exif'] = $metadata['image_meta'];
  1162. }
  1163. }
  1164. if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ) ) ) {
  1165. $metadata = wp_get_attachment_metadata( $media_item->ID );
  1166. $response['length'] = $metadata['length'];
  1167. $response['exif'] = $metadata;
  1168. }
  1169. $is_video = false;
  1170. if (
  1171. in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) )
  1172. ||
  1173. $response['mime_type'] === 'video/videopress'
  1174. ) {
  1175. $is_video = true;
  1176. }
  1177. if ( $is_video ) {
  1178. $metadata = wp_get_attachment_metadata( $media_item->ID );
  1179. if ( isset( $metadata['height'], $metadata['width'] ) ) {
  1180. $response['height'] = $metadata['height'];
  1181. $response['width'] = $metadata['width'];
  1182. }
  1183. if ( isset( $metadata['length'] ) ) {
  1184. $response['length'] = $metadata['length'];
  1185. }
  1186. // add VideoPress info
  1187. if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
  1188. $info = video_get_info_by_blogpostid( $this->api->get_blog_id_for_output(), $media_item->ID );
  1189. // If we failed to get VideoPress info, but it exists in the meta data (for some reason)
  1190. // then let's use that.
  1191. if ( false === $info && isset( $metadata['videopress'] ) ) {
  1192. $info = (object) $metadata['videopress'];
  1193. }
  1194. // Thumbnails
  1195. if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
  1196. $response['thumbnails'] = array( 'fmt_hd' => '', 'fmt_dvd' => '', 'fmt_std' => '' );
  1197. foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
  1198. if ( video_format_done( $info, $size ) ) {
  1199. $response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
  1200. } else {
  1201. unset( $response['thumbnails'][ $size ] );
  1202. }
  1203. }
  1204. }
  1205. // If we didn't get VideoPress information (for some reason) then let's
  1206. // not try and include it in the response.
  1207. if ( isset( $info->guid ) ) {
  1208. $response['videopress_guid'] = $info->guid;
  1209. $response['videopress_processing_done'] = true;
  1210. if ( '0000-00-00 00:00:00' === $info->finish_date_gmt ) {
  1211. $response['videopress_processing_done'] = false;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. $response['thumbnails'] = (object) $response['thumbnails'];
  1217. $response['meta'] = (object) array(
  1218. 'links' => (object) array(
  1219. 'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_item->ID ),
  1220. 'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_item->ID, 'help' ),
  1221. 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
  1222. ),
  1223. );
  1224. // add VideoPress link to the meta
  1225. if ( isset ( $response['videopress_guid'] ) ) {
  1226. if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
  1227. $response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
  1228. }
  1229. }
  1230. if ( $media_item->post_parent > 0 ) {
  1231. $response['meta']->links->parent = (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $media_item->post_parent );
  1232. }
  1233. return (object) $response;
  1234. }
  1235. function get_taxonomy( $taxonomy_id, $taxonomy_type, $context ) {
  1236. $taxonomy = get_term_by( 'slug', $taxonomy_id, $taxonomy_type );
  1237. /// keep updating this function
  1238. if ( !$taxonomy || is_wp_error( $taxonomy ) ) {
  1239. return new WP_Error( 'unknown_taxonomy', 'Unknown taxonomy', 404 );
  1240. }
  1241. return $this->format_taxonomy( $taxonomy, $taxonomy_type, $context );
  1242. }
  1243. function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
  1244. // Permissions
  1245. switch ( $context ) {
  1246. case 'edit' :
  1247. $tax = get_taxonomy( $taxonomy_type );
  1248. if ( !current_user_can( $tax->cap->edit_terms ) )
  1249. return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
  1250. break;
  1251. case 'display' :
  1252. if ( -1 == get_option( 'blog_public' ) && ! current_user_can( 'read' ) ) {
  1253. return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
  1254. }
  1255. break;
  1256. default :
  1257. return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
  1258. }
  1259. $response = array();
  1260. $response['ID'] = (int) $taxonomy->term_id;
  1261. $response['name'] = (string) $taxonomy->name;
  1262. $response['slug'] = (string) $taxonomy->slug;
  1263. $response['description'] = (string) $taxonomy->description;
  1264. $response['post_count'] = (int) $taxonomy->count;
  1265. $response['feed_url'] = get_term_feed_link( $taxonomy->term_id, $taxonomy_type );
  1266. if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
  1267. $response['parent'] = (int) $taxonomy->parent;
  1268. }
  1269. $response['meta'] = (object) array(
  1270. 'links' => (object) array(
  1271. 'self' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type ),
  1272. 'help' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type, 'help' ),
  1273. 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
  1274. ),
  1275. );
  1276. return (object) $response;
  1277. }
  1278. /**
  1279. * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
  1280. *
  1281. * @param $date_gmt (string) GMT datetime string.
  1282. * @param $date (string) Optional. Used to calculate the offset from GMT.
  1283. *
  1284. * @return string
  1285. */
  1286. function format_date( $date_gmt, $date = null ) {
  1287. return WPCOM_JSON_API_Date::format_date( $date_gmt, $date );
  1288. }
  1289. /**
  1290. * Parses a date string and returns the local and GMT representations
  1291. * of that date & time in 'YYYY-MM-DD HH:MM:SS' format without
  1292. * timezones or offsets. If the parsed datetime was not localized to a
  1293. * particular timezone or offset we will assume it was given in GMT
  1294. * relative to now and will convert it to local time using either the
  1295. * timezone set in the options table for the blog or the GMT offset.
  1296. *
  1297. * @param datetime string
  1298. *
  1299. * @return array( $local_time_string, $gmt_time_string )
  1300. */
  1301. function parse_date( $date_string ) {
  1302. $date_string_info = date_parse( $date_string );
  1303. if ( is_array( $date_string_info ) && 0 === $date_string_info['error_count'] ) {
  1304. // Check if it's already localized. Can't just check is_localtime because date_parse('oppossum') returns true; WTF, PHP.
  1305. if ( isset( $date_string_info['zone'] ) && true === $date_string_info['is_localtime'] ) {
  1306. $dt_local = clone $dt_utc = new DateTime( $date_string );
  1307. $dt_utc->setTimezone( new DateTimeZone( 'UTC' ) );
  1308. return array(
  1309. (string) $dt_local->format( 'Y-m-d H:i:s' ),
  1310. (string) $dt_utc->format( 'Y-m-d H:i:s' ),
  1311. );
  1312. }
  1313. // It's parseable but no TZ info so assume UTC
  1314. $dt_local = clone $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) );
  1315. } else {
  1316. // Could not parse time, use now in UTC
  1317. $dt_local = clone $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
  1318. }
  1319. // First try to use timezone as it's daylight savings aware.
  1320. $timezone_string = get_option( 'timezone_string' );
  1321. if ( $timezone_string ) {
  1322. $tz = timezone_open( $timezone_string );
  1323. if ( $tz ) {
  1324. $dt_local->setTimezone( $tz );
  1325. return array(
  1326. (string) $dt_local->format( 'Y-m-d H:i:s' ),
  1327. (string) $dt_utc->format( 'Y-m-d H:i:s' ),
  1328. );
  1329. }
  1330. }
  1331. // Fallback to GMT offset (in hours)
  1332. // NOTE: TZ of $dt_local is still UTC, we simply modified the timestamp with an offset.
  1333. $gmt_offset_seconds = intval( get_option( 'gmt_offset' ) * 3600 );
  1334. $dt_local->modify("+{$gmt_offset_seconds} seconds");
  1335. return array(
  1336. (string) $dt_local->format( 'Y-m-d H:i:s' ),
  1337. (string) $dt_utc->format( 'Y-m-d H:i:s' ),
  1338. );
  1339. }
  1340. // Load the functions.php file for the current theme to get its post formats, CPTs, etc.
  1341. function load_theme_functions() {
  1342. // bail if we've done this already (can happen when calling /batch endpoint)
  1343. if ( defined( 'REST_API_THEME_FUNCTIONS_LOADED' ) )
  1344. return;
  1345. // VIP context loading is handled elsewhere, so bail to prevent
  1346. // duplicate loading. See `switch_to_blog_and_validate_user()`
  1347. if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) {
  1348. return;
  1349. }
  1350. define( 'REST_API_THEME_FUNCTIONS_LOADED', true );
  1351. // the theme info we care about is found either within functions.php or one of the jetpack files.
  1352. $function_files = array( '/functions.php', '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php' );
  1353. $copy_dirs = array( get_template_directory() );
  1354. // Is this a child theme? Load the child theme's functions file.
  1355. if ( get_stylesheet_directory() !== get_template_directory() && wpcom_is_child_theme() ) {
  1356. foreach ( $function_files as $function_file ) {
  1357. if ( file_exists( get_stylesheet_directory() . $function_file ) ) {
  1358. require_once( get_stylesheet_directory() . $function_file );
  1359. }
  1360. }
  1361. $copy_dirs[] = get_stylesheet_directory();
  1362. }
  1363. foreach ( $function_files as $function_file ) {
  1364. if ( file_exists( get_template_directory() . $function_file ) ) {
  1365. require_once( get_template_directory() . $function_file );
  1366. }
  1367. }
  1368. // add inc/wpcom.php and/or includes/wpcom.php
  1369. wpcom_load_theme_compat_file();
  1370. // Enable including additional directories or files in actions to be copied
  1371. $copy_dirs = apply_filters( 'restapi_theme_action_copy_dirs', $copy_dirs );
  1372. // since the stuff we care about (CPTS, post formats, are usually on setup or init hooks, we want to load those)
  1373. $this->copy_hooks( 'after_setup_theme', 'restapi_theme_after_setup_theme', $copy_dirs );
  1374. /**
  1375. * Fires functions hooked onto `after_setup_theme` by the theme for the purpose of the REST API.
  1376. *
  1377. * The REST API does not load the theme when processing requests.
  1378. * To enable theme-based functionality, the API will load the '/functions.php',
  1379. * '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php files
  1380. * of the theme (parent and child) and copy functions hooked onto 'after_setup_theme' within those files.
  1381. *
  1382. * @module json-api
  1383. *
  1384. * @since 3.2.0
  1385. */
  1386. do_action( 'restapi_theme_after_setup_theme' );
  1387. $this->copy_hooks( 'init', 'restapi_theme_init', $copy_dirs );
  1388. /**
  1389. * Fires functions hooked onto `init` by the theme for the purpose of the REST API.
  1390. *
  1391. * The REST API does not load the theme when processing requests.
  1392. * To enable theme-based functionality, the API will load the '/functions.php',
  1393. * '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php files
  1394. * of the theme (parent and child) and copy functions hooked onto 'init' within those files.
  1395. *
  1396. * @module json-api
  1397. *
  1398. * @since 3.2.0
  1399. */
  1400. do_action( 'restapi_theme_init' );
  1401. }
  1402. function copy_hooks( $from_hook, $to_hook, $base_paths ) {
  1403. global $wp_filter;
  1404. foreach ( $wp_filter as $hook => $actions ) {
  1405. if ( $from_hook != $hook ) {
  1406. continue;
  1407. }
  1408. if ( ! has_action( $hook ) ) {
  1409. continue;
  1410. }
  1411. foreach ( $actions as $priority => $callbacks ) {
  1412. foreach( $callbacks as $callback_key => $callback_data ) {
  1413. $callback = $callback_data['function'];
  1414. // use reflection api to determine filename where function is defined
  1415. $reflection = $this->get_reflection( $callback );
  1416. if ( false !== $reflection ) {
  1417. $file_name = $reflection->getFileName();
  1418. foreach( $base_paths as $base_path ) {
  1419. // only copy hooks with functions which are part of the specified files
  1420. if ( 0 === strpos( $file_name, $base_path ) ) {
  1421. add_action(
  1422. $to_hook,
  1423. $callback_data['function'],
  1424. $priority,
  1425. $callback_data['accepted_args']
  1426. );
  1427. }
  1428. }
  1429. }
  1430. }
  1431. }
  1432. }
  1433. }
  1434. function get_reflection( $callback ) {
  1435. if ( is_array( $callback ) ) {
  1436. list( $class, $method ) = $callback;
  1437. return new ReflectionMethod( $class, $method );
  1438. }
  1439. if ( is_string( $callback ) && strpos( $callback, "::" ) !== false ) {
  1440. list( $class, $method ) = explode( "::", $callback );
  1441. return new ReflectionMethod( $class, $method );
  1442. }
  1443. if ( version_compare( PHP_VERSION, "5.3.0", ">=" ) && method_exists( $callback, "__invoke" ) ) {
  1444. return new ReflectionMethod( $callback, "__invoke" );
  1445. }
  1446. if ( is_string( $callback ) && strpos( $callback, "::" ) == false && function_exists( $callback ) ) {
  1447. return new ReflectionFunction( $callback );
  1448. }
  1449. return false;
  1450. }
  1451. /**
  1452. * Check whether a user can view or edit a post type
  1453. * @param string $post_type post type to check
  1454. * @param string $context 'display' or 'edit'
  1455. * @return bool
  1456. */
  1457. function current_user_can_access_post_type( $post_type, $context='display' ) {
  1458. $post_type_object = get_post_type_object( $post_type );
  1459. if ( ! $post_type_object ) {
  1460. return false;
  1461. }
  1462. switch( $context ) {
  1463. case 'edit':
  1464. return current_user_can( $post_type_object->cap->edit_posts );
  1465. case 'display':
  1466. return $post_type_object->public || current_user_can( $post_type_object->cap->read_private_posts );
  1467. default:
  1468. return false;
  1469. }
  1470. }
  1471. function is_post_type_allowed( $post_type ) {
  1472. // if the post type is empty, that's fine, WordPress will default to post
  1473. if ( empty( $post_type ) ) {
  1474. return true;
  1475. }
  1476. // allow special 'any' type
  1477. if ( 'any' == $post_type ) {
  1478. return true;
  1479. }
  1480. // check for allowed types
  1481. if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) ) {
  1482. return true;
  1483. }
  1484. if ( $post_type_object = get_post_type_object( $post_type ) ) {
  1485. if ( ! empty( $post_type_object->show_in_rest ) ) {
  1486. return $post_type_object->show_in_rest;
  1487. }
  1488. if ( ! empty( $post_type_object->publicly_queryable ) ) {
  1489. return $post_type_object->publicly_queryable;
  1490. }
  1491. }
  1492. return ! empty( $post_type_object->public );
  1493. }
  1494. /**
  1495. * Gets the whitelisted post types that JP should allow access to.
  1496. *
  1497. * @return array Whitelisted post types.
  1498. */
  1499. protected function _get_whitelisted_post_types() {
  1500. $allowed_types = array( 'post', 'page', 'revision' );
  1501. /**
  1502. * Filter the post types Jetpack has access to, and can synchronize with WordPress.com.
  1503. *
  1504. * @module json-api
  1505. *
  1506. * @since 2.2.3
  1507. *
  1508. * @param array $allowed_types Array of whitelisted post types. Default to `array( 'post', 'page', 'revision' )`.
  1509. */
  1510. $allowed_types = apply_filters( 'rest_api_allowed_post_types', $allowed_types );
  1511. return array_unique( $allowed_types );
  1512. }
  1513. function handle_media_creation_v1_1( $media_files, $media_urls, $media_attrs = array(), $force_parent_id = false ) {
  1514. add_filter( 'upload_mimes', array( $this, 'allow_video_uploads' ) );
  1515. $media_ids = $errors = array();
  1516. $user_can_upload_files = current_user_can( 'upload_files' ) || $this->api->is_authorized_with_upload_token();
  1517. $media_attrs = array_values( $media_attrs ); // reset the keys
  1518. $i = 0;
  1519. if ( ! empty( $media_files ) ) {
  1520. $this->api->trap_wp_die( 'upload_error' );
  1521. foreach ( $media_files as $media_item ) {
  1522. $_FILES['.api.media.item.'] = $media_item;
  1523. if ( ! $user_can_upload_files ) {
  1524. $media_id = new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
  1525. } else {
  1526. if ( $force_parent_id ) {
  1527. $parent_id = absint( $force_parent_id );
  1528. } elseif ( ! empty( $media_attrs[$i] ) && ! empty( $media_attrs[$i]['parent_id'] ) ) {
  1529. $parent_id = absint( $media_attrs[$i]['parent_id'] );
  1530. } else {
  1531. $parent_id = 0;
  1532. }
  1533. $media_id = media_handle_upload( '.api.media.item.', $parent_id );
  1534. }
  1535. if ( is_wp_error( $media_id ) ) {
  1536. $errors[$i]['file'] = $media_item['name'];
  1537. $errors[$i]['error'] = $media_id->get_error_code();
  1538. $errors[$i]['message'] = $media_id->get_error_message();
  1539. } else {
  1540. $media_ids[$i] = $media_id;
  1541. }
  1542. $i++;
  1543. }
  1544. $this->api->trap_wp_die( null );
  1545. unset( $_FILES['.api.media.item.'] );
  1546. }
  1547. if ( ! empty( $media_urls ) ) {
  1548. foreach ( $media_urls as $url ) {
  1549. if ( ! $user_can_upload_files ) {
  1550. $media_id = new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
  1551. } else {
  1552. if ( $force_parent_id ) {
  1553. $parent_id = absint( $force_parent_id );
  1554. } else if ( ! empty( $media_attrs[$i] ) && ! empty( $media_attrs[$i]['parent_id'] ) ) {
  1555. $parent_id = absint( $media_attrs[$i]['parent_id'] );
  1556. } else {
  1557. $parent_id = 0;
  1558. }
  1559. $media_id = $this->handle_media_sideload( $url, $parent_id );
  1560. }
  1561. if ( is_wp_error( $media_id ) ) {
  1562. $errors[$i] = array(
  1563. 'file' => $url,
  1564. 'error' => $media_id->get_error_code(),
  1565. 'message' => $media_id->get_error_message(),
  1566. );
  1567. } elseif ( ! empty( $media_id ) ) {
  1568. $media_ids[$i] = $media_id;
  1569. }
  1570. $i++;
  1571. }
  1572. }
  1573. if ( ! empty( $media_attrs ) ) {
  1574. foreach ( $media_ids as $index => $media_id ) {
  1575. if ( empty( $media_attrs[$index] ) )
  1576. continue;
  1577. $attrs = $media_attrs[$index];
  1578. $insert = array();
  1579. // Attributes: Title, Caption, Description
  1580. if ( isset( $attrs['title'] ) ) {
  1581. $insert['post_title'] = $attrs['title'];
  1582. }
  1583. if ( isset( $attrs['caption'] ) ) {
  1584. $insert['post_excerpt'] = $attrs['caption'];
  1585. }
  1586. if ( isset( $attrs['description'] ) ) {
  1587. $insert['post_content'] = $attrs['description'];
  1588. }
  1589. if ( ! empty( $insert ) ) {
  1590. $insert['ID'] = $media_id;
  1591. wp_update_post( (object) $insert );
  1592. }
  1593. // Attributes: Alt
  1594. if ( isset( $attrs['alt'] ) ) {
  1595. $alt = wp_strip_all_tags( $attrs['alt'], true );
  1596. update_post_meta( $media_id, '_wp_attachment_image_alt', $alt );
  1597. }
  1598. // Attributes: Artist, Album
  1599. $id3_meta = array();
  1600. foreach ( array( 'artist', 'album' ) as $key ) {
  1601. if ( isset( $attrs[ $key ] ) ) {
  1602. $id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
  1603. }
  1604. }
  1605. if ( ! empty( $id3_meta ) ) {
  1606. // Before updating metadata, ensure that the item is audio
  1607. $item = $this->get_media_item_v1_1( $media_id );
  1608. if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
  1609. wp_update_attachment_metadata( $media_id, $id3_meta );
  1610. }
  1611. }
  1612. }
  1613. }
  1614. return array( 'media_ids' => $media_ids, 'errors' => $errors );
  1615. }
  1616. function handle_media_sideload( $url, $parent_post_id = 0, $type = 'any' ) {
  1617. if ( ! function_exists( 'download_url' ) || ! function_exists( 'media_handle_sideload' ) )
  1618. return false;
  1619. // if we didn't get a URL, let's bail
  1620. $parsed = @parse_url( $url );
  1621. if ( empty( $parsed ) )
  1622. return false;
  1623. $tmp = download_url( $url );
  1624. if ( is_wp_error( $tmp ) ) {
  1625. return $tmp;
  1626. }
  1627. // First check to see if we get a mime-type match by file, otherwise, check to
  1628. // see if WordPress supports this file as an image. If neither, then it is not supported.
  1629. if ( ! $this->is_file_supported_for_sideloading( $tmp ) || 'image' === $type && ! file_is_displayable_image( $tmp ) ) {
  1630. @unlink( $tmp );
  1631. return new WP_Error( 'invalid_input', 'Invalid file type.', 403 );
  1632. }
  1633. // emulate a $_FILES entry
  1634. $file_array = array(
  1635. 'name' => basename( parse_url( $url, PHP_URL_PATH ) ),
  1636. 'tmp_name' => $tmp,
  1637. );
  1638. $id = media_handle_sideload( $file_array, $parent_post_id );
  1639. if ( file_exists( $tmp ) ) {
  1640. @unlink( $tmp );
  1641. }
  1642. if ( is_wp_error( $id ) ) {
  1643. return $id;
  1644. }
  1645. if ( ! $id || ! is_int( $id ) ) {
  1646. return false;
  1647. }
  1648. return $id;
  1649. }
  1650. /**
  1651. * Checks that the mime type of the specified file is among those in a filterable list of mime types.
  1652. *
  1653. * @param string $file Path to file to get its mime type.
  1654. *
  1655. * @return bool
  1656. */
  1657. protected function is_file_supported_for_sideloading( $file ) {
  1658. if ( class_exists( 'finfo' ) ) { // php 5.3+
  1659. $finfo = new finfo( FILEINFO_MIME );
  1660. $mime = explode( '; ', $finfo->file( $file ) );
  1661. $type = $mime[0];
  1662. } elseif ( function_exists( 'mime_content_type' ) ) { // PHP 5.2
  1663. $type = mime_content_type( $file );
  1664. } else {
  1665. return false;
  1666. }
  1667. /**
  1668. * Filter the list of supported mime types for media sideloading.
  1669. *
  1670. * @since 4.0.0
  1671. *
  1672. * @module json-api
  1673. *
  1674. * @param array $supported_mime_types Array of the supported mime types for media sideloading.
  1675. */
  1676. $supported_mime_types = apply_filters( 'jetpack_supported_media_sideload_types', array(
  1677. 'image/png',
  1678. 'image/jpeg',
  1679. 'image/gif',
  1680. 'image/bmp',
  1681. 'video/quicktime',
  1682. 'video/mp4',
  1683. 'video/mpeg',
  1684. 'video/ogg',
  1685. 'video/3gpp',
  1686. 'video/3gpp2',
  1687. 'video/h261',
  1688. 'video/h262',
  1689. 'video/h264',
  1690. 'video/x-msvideo',
  1691. 'video/x-ms-wmv',
  1692. 'video/x-ms-asf',
  1693. ) );
  1694. // If the type returned was not an array as expected, then we know we don't have a match.
  1695. if ( ! is_array( $supported_mime_types ) ) {
  1696. return false;
  1697. }
  1698. return in_array( $type, $supported_mime_types );
  1699. }
  1700. function allow_video_uploads( $mimes ) {
  1701. // if we are on Jetpack, bail - Videos are already allowed
  1702. if ( ! defined( 'IS_WPCOM' ) || !IS_WPCOM ) {
  1703. return $mimes;
  1704. }
  1705. // extra check that this filter is only ever applied during REST API requests
  1706. if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) {
  1707. return $mimes;
  1708. }
  1709. // bail early if they already have the upgrade..
  1710. if ( get_option( 'video_upgrade' ) == '1' ) {
  1711. return $mimes;
  1712. }
  1713. // lets whitelist to only specific clients right now
  1714. $clients_allowed_video_uploads = array();
  1715. /**
  1716. * Filter the list of whitelisted video clients.
  1717. *
  1718. * @module json-api
  1719. *
  1720. * @since 3.2.0
  1721. *
  1722. * @param array $clients_allowed_video_uploads Array of whitelisted Video clients.
  1723. */
  1724. $clients_allowed_video_uploads = apply_filters( 'rest_api_clients_allowed_video_uploads', $clients_allowed_video_uploads );
  1725. if ( !in_array( $this->api->token_details['client_id'], $clients_allowed_video_uploads ) ) {
  1726. return $mimes;
  1727. }
  1728. $mime_list = wp_get_mime_types();
  1729. $video_exts = explode( ' ', get_site_option( 'video_upload_filetypes', false, false ) );
  1730. /**
  1731. * Filter the video filetypes allowed on the site.
  1732. *
  1733. * @module json-api
  1734. *
  1735. * @since 3.2.0
  1736. *
  1737. * @param array $video_exts Array of video filetypes allowed on the site.
  1738. */
  1739. $video_exts = apply_filters( 'video_upload_filetypes', $video_exts );
  1740. $video_mimes = array();
  1741. if ( !empty( $video_exts ) ) {
  1742. foreach ( $video_exts as $ext ) {
  1743. foreach ( $mime_list as $ext_pattern => $mime ) {
  1744. if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1745. $video_mimes[$ext_pattern] = $mime;
  1746. }
  1747. }
  1748. $mimes = array_merge( $mimes, $video_mimes );
  1749. }
  1750. return $mimes;
  1751. }
  1752. function is_current_site_multi_user() {
  1753. $users = wp_cache_get( 'site_user_count', 'WPCOM_JSON_API_Endpoint' );
  1754. if ( false === $users ) {
  1755. $user_query = new WP_User_Query( array(
  1756. 'blog_id' => get_current_blog_id(),
  1757. 'fields' => 'ID',
  1758. ) );
  1759. $users = (int) $user_query->get_total();
  1760. wp_cache_set( 'site_user_count', $users, 'WPCOM_JSON_API_Endpoint', DAY_IN_SECONDS );
  1761. }
  1762. return $users > 1;
  1763. }
  1764. function allows_cross_origin_requests() {
  1765. return 'GET' == $this->method || $this->allow_cross_origin_request;
  1766. }
  1767. function allows_unauthorized_requests( $origin, $complete_access_origins ) {
  1768. return 'GET' == $this->method || ( $this->allow_unauthorized_request && in_array( $origin, $complete_access_origins ) );
  1769. }
  1770. function get_platform() {
  1771. return wpcom_get_sal_platform( $this->api->token_details );
  1772. }
  1773. /**
  1774. * Allows the endpoint to perform logic to allow it to decide whether-or-not it should force a
  1775. * response from the WPCOM API, or potentially go to the Jetpack blog.
  1776. *
  1777. * Override this method if you want to do something different.
  1778. *
  1779. * @param int $blog_id
  1780. * @return bool
  1781. */
  1782. function force_wpcom_request( $blog_id ) {
  1783. return false;
  1784. }
  1785. /**
  1786. * Return endpoint response
  1787. *
  1788. * @param ... determined by ->$path
  1789. *
  1790. * @return
  1791. * falsy: HTTP 500, no response body
  1792. * WP_Error( $error_code, $error_message, $http_status_code ): HTTP $status_code, json_encode( array( 'error' => $error_code, 'message' => $error_message ) ) response body
  1793. * $data: HTTP 200, json_encode( $data ) response body
  1794. */
  1795. abstract function callback( $path = '' );
  1796. }
  1797. require_once( dirname( __FILE__ ) . '/json-endpoints.php' );