class.wpcom-json-api-update-customcss.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Custom Css update endpoint
  4. *
  5. * https://public-api.wordpress.com/rest/v1.1/sites/$site/customcss/
  6. */
  7. new WPCOM_JSON_API_Update_CustomCss_Endpoint( array (
  8. 'description' => 'Set custom-css data for a site.',
  9. 'group' => '__do_not_document',
  10. 'stat' => 'customcss:1:update',
  11. 'method' => 'POST',
  12. 'min_version' => '1.1',
  13. 'path' => '/sites/%s/customcss',
  14. 'path_labels' => array(
  15. '$site' => '(string) Site ID or domain.',
  16. ),
  17. 'request_format' => array(
  18. 'css' => '(string) Optional. The raw CSS.',
  19. 'preprocessor' => '(string) Optional. The name of the preprocessor if any.',
  20. 'add_to_existing' => '(bool) Optional. False to skip the existing styles.',
  21. ),
  22. 'response_format' => array(
  23. 'css' => '(string) The raw CSS.',
  24. 'preprocessor' => '(string) The name of the preprocessor if any.',
  25. 'add_to_existing' => '(bool) False to skip the existing styles.',
  26. ),
  27. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/12345678/customcss',
  28. 'example_request_data' => array(
  29. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  30. 'body' => array(
  31. 'css' => '.stie-title { color: #fff; }',
  32. 'preprocessor' => 'sass'
  33. ),
  34. ),
  35. 'example_response' => '
  36. {
  37. "css": ".site-title { color: #fff; }",
  38. "preprocessor": "sass",
  39. "add_to_existing": "true"
  40. }'
  41. ) );
  42. class WPCOM_JSON_API_Update_CustomCss_Endpoint extends WPCOM_JSON_API_Endpoint {
  43. /**
  44. * API callback.
  45. */
  46. function callback( $path = '', $blog_id = 0 ) {
  47. // Switch to the given blog.
  48. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  49. if ( is_wp_error( $blog_id ) ) {
  50. return $blog_id;
  51. }
  52. if ( ! current_user_can( 'edit_theme_options' ) ) {
  53. return new WP_Error( 'unauthorized', 'User is not authorized to access custom css', 403 );
  54. }
  55. $args = $this->input();
  56. if ( empty( $args ) || ! is_array( $args ) ) {
  57. return new WP_Error( 'no_data', 'No data was provided.', 400 );
  58. }
  59. $save_args = array(
  60. 'css' => $args['css'],
  61. 'preprocessor' => $args['preprocessor'],
  62. 'add_to_existing' => $args['add_to_existing'],
  63. );
  64. Jetpack_Custom_CSS::save( $save_args );
  65. $current = array(
  66. 'css' => Jetpack_Custom_CSS::get_css(),
  67. 'preprocessor' => Jetpack_Custom_CSS::get_preprocessor_key(),
  68. 'add_to_existing' => ! Jetpack_Custom_CSS::skip_stylesheet(),
  69. );
  70. $defaults = array(
  71. 'css' => '',
  72. 'preprocessor' => '',
  73. 'add_to_existing' => true,
  74. );
  75. return wp_parse_args( $current, $defaults );
  76. }
  77. }