Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Import_ID
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 add_additional_fields_schema
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create_item
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 add_unique_identifier_to_schema
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 add_import_id_metadata
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 get_import_db_query
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jetpack Import unique import ID.
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 * Import ID trait. Add a unique import ID to the items schema.
20 */
21trait Import_ID {
22
23    /**
24     * Meta and REST property name used for storing the WXR import ID.
25     *
26     * @var string
27     */
28    protected $import_id_field_name = 'unified_importer_id';
29
30    /**
31     * Import ID meta name.
32     *
33     * @var string
34     */
35    protected $import_id_meta_type;
36
37    /**
38     * Adds the schema from additional fields to a schema array.
39     *
40     * The type of object is inferred from the passed schema.
41     *
42     * @param array $schema Schema array.
43     * @return array Modified Schema array.
44     */
45    public function add_additional_fields_schema( $schema ) {
46        // Add the import unique ID to the schema.
47        return $this->add_unique_identifier_to_schema( $schema );
48    }
49
50    /**
51     * Create a resource.
52     *
53     * @param WP_REST_Request $request Full details about the request.
54     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
55     */
56    public function create_item( $request ) {
57        $response = parent::create_item( $request );
58
59        return $this->add_import_id_metadata( $request, $response );
60    }
61
62    /**
63     * Adds the unique identifier to the schema array.
64     *
65     * @param array $schema Schema array.
66     * @param bool  $required Whether the field is required.
67     * @return array Modified Schema array.
68     */
69    protected function add_unique_identifier_to_schema( $schema, $required = true ) {
70        // Add the import unique ID to the schema.
71        $schema['properties'][ $this->import_id_field_name ] = array(
72            'description' => __( 'Jetpack Import unique identifier for the term.', 'jetpack-import' ),
73            'type'        => 'integer',
74            'context'     => array( 'view', 'embed', 'edit' ),
75            'required'    => $required,
76        );
77
78        return $schema;
79    }
80
81    /**
82     * Add the import unique ID to the resource metadata.
83     *
84     * @param WP_REST_Request  $request Full details about the request.
85     * @param WP_REST_Response $response Response object.
86     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
87     */
88    protected function add_import_id_metadata( $request, $response ) {
89        // Skip on error.
90        if ( is_wp_error( $response ) ) {
91            return $response;
92        }
93
94        $data   = $response->get_data();
95        $status = $response->get_status();
96
97        // Skip if the resource has not been added or modified.
98        if ( ! ( $status === 200 || $status === 201 ) ) {
99            return $response;
100        }
101
102        // Add the import unique ID to the resource metadata.
103        \add_metadata( $this->import_id_meta_type, $data['id'], $this->import_id_field_name, $request[ $this->import_id_field_name ], true );
104
105        return $response;
106    }
107
108    /**
109     * Get the import DB query. This is used to get the items with a specific
110     * meta key that have been imported.
111     *
112     * @param int $parent_import_id The parent import ID.
113     * @return array The query.
114     */
115    protected function get_import_db_query( $parent_import_id ) {
116        // Get the only one item with the parent import ID.
117        return array(
118            'number'     => 1,
119            'fields'     => 'ids',
120            'meta_query' => array(
121                array(
122                    'key'   => $this->import_id_field_name,
123                    'value' => $parent_import_id,
124                ),
125            ),
126        );
127    }
128}