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