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