PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-import / src / endpoints / trait-import-id.php

trait-import-id.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-import/src/endpoints/trait-import-id.php

129 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Jetpack Import unique import ID.
4 *
5 * @package automattic/jetpack-import
6 */
7
8 namespace Automattic\Jetpack\Import\Endpoints;
9
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Response;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 0 );
16 }
17
18 /**
19 * Import ID trait. Add a unique import ID to the items schema.
20 */
21 trait 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 }
129