Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Custom_CSS
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 create_item
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Custom CSS REST route
4 *
5 * @package automattic/jetpack-import
6 */
7
8namespace Automattic\Jetpack\Import\Endpoints;
9
10use WP_Error;
11use WP_REST_Request;
12use WP_REST_Response;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18require_once ABSPATH . 'wp-includes/theme.php';
19
20/**
21 * Class Custom_CSS
22 */
23class Custom_CSS extends \WP_REST_Posts_Controller {
24
25    /**
26     * Base class
27     */
28    use Import;
29
30    /**
31     * The Import ID add a new item to the schema.
32     */
33    use Import_ID;
34
35    /**
36     * Whether the controller supports batching.
37     *
38     * @var array
39     */
40    protected $allow_batch = array( 'v1' => true );
41
42    /**
43     * Constructor.
44     */
45    public function __construct() {
46        parent::__construct( 'custom_css' );
47
48        $this->rest_base = 'custom-css';
49    }
50
51    /**
52     * Update the custom CSS post.
53     *
54     * @param WP_REST_Request $request Full details about the request.
55     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
56     */
57    public function create_item( $request ) {
58        // Set the WP_IMPORTING constant to prevent sync notifications
59        $this->set_importing();
60        $args = array(
61            'stylesheet' => $request['title'],
62        );
63
64        $post = wp_update_custom_css_post( $request['content'], $args );
65
66        if ( is_wp_error( $post ) ) {
67            return $post;
68        }
69
70        $response = $this->prepare_item_for_response( $post, $request );
71        $response = rest_ensure_response( $response );
72
73        return $this->add_import_id_metadata( $request, $response );
74    }
75}