Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Category
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
30
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
 add_additional_fields_schema
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 create_item
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Categories 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
18/**
19 * Class Category
20 */
21class Category extends \WP_REST_Terms_Controller {
22
23    /**
24     * Base class
25     */
26    use Import;
27
28    /**
29     * The Import ID add a new item to the schema.
30     */
31    use Import_ID;
32
33    /**
34     * Whether the controller supports batching. Default true.
35     *
36     * @var array
37     */
38    protected $allow_batch = array( 'v1' => true );
39
40    /**
41     * Constructor.
42     */
43    public function __construct() {
44        parent::__construct( 'category' );
45
46        // @see add_term_meta
47        $this->import_id_meta_type = 'term';
48    }
49
50    /**
51     * Adds the schema from additional fields to a schema array.
52     *
53     * The type of object is inferred from the passed schema.
54     *
55     * @param array $schema Schema array.
56     * @return array Modified Schema array.
57     */
58    public function add_additional_fields_schema( $schema ) {
59        // Parent term is saved like a slug in WXR so we have to rewrite the schema.
60        $schema['properties']['parent']['description'] = __( 'The parent category slug.', 'jetpack-import' );
61        $schema['properties']['parent']['type']        = 'string';
62
63        // Add the import unique ID to the schema.
64        return $this->add_unique_identifier_to_schema( $schema );
65    }
66
67    /**
68     * Creates a single category.
69     *
70     * @param WP_REST_Request $request Full details about the request.
71     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
72     */
73    public function create_item( $request ) {
74        if ( ! empty( $request['parent'] ) ) {
75            $parent = get_term_by( 'slug', $request['parent'], 'category' );
76
77            // Overwrite the parent ID with the parent term ID found using the slug.
78            $request['parent'] = $parent ? $parent->term_id : 0;
79        }
80
81        $response = parent::create_item( $request );
82
83        // Ensure that the HTTP status is a valid one.
84        $response = $this->ensure_http_status( $response, 'term_exists', 409 );
85
86        return $this->add_import_id_metadata( $request, $response );
87    }
88}