PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
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 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-import / src / endpoints / trait-import.php

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

119 lines 2.6 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
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit( 0 );
14 }
15
16 /**
17 * Import trait. Base class for all import endpoints.
18 *
19 * @property array|false $allow_batch Batching details.
20 */
21 trait Import {
22 /**
23 * REST API namespace.
24 *
25 * @var string
26 */
27 private static $rest_namespace = 'jetpack/v4/import';
28
29 /**
30 * REST base path.
31 *
32 * @var ?string
33 */
34 protected $rest_base;
35
36 /**
37 * Registers the routes for the objects of the controller.
38 *
39 * @see WP_REST_Controller::register_rest_route()
40 */
41 public function register_routes() {
42 register_rest_route(
43 self::$rest_namespace,
44 '/' . $this->rest_base,
45 $this->get_route_options()
46 );
47 }
48
49 /**
50 * Ensure that the user has permissions to import.
51 *
52 * @return bool|WP_Error
53 */
54 public function import_permissions_callback() {
55 // The permission check is done in the REST API authentication. It's the same
56 // as the one used in wp-admin/import.php.
57 if ( \current_user_can( 'import' ) ) {
58 return true;
59 }
60
61 $error_msg = \esc_html__(
62 'You are not allowed to perform this action.',
63 'jetpack-import'
64 );
65
66 return new WP_Error( 'rest_forbidden', $error_msg, array( 'status' => \rest_authorization_required_code() ) );
67 }
68
69 /**
70 * Get the register route options.
71 *
72 * @see register_rest_route()
73 *
74 * @return array The options.
75 */
76 protected function get_route_options() {
77 return array(
78 array(
79 'methods' => \WP_REST_Server::CREATABLE,
80 'callback' => array( $this, 'create_item' ),
81 'permission_callback' => array( $this, 'import_permissions_callback' ),
82 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ),
83 ),
84 'allow_batch' => $this->allow_batch,
85 'schema' => array( $this, 'get_public_item_schema' ),
86 );
87 }
88
89 /**
90 * Ensure that the HTTP status is correct.
91 *
92 * @param WP_Error $response Response error object.
93 * @param int $error_code Error code.
94 * @param int $status HTTP status.
95 */
96 protected function ensure_http_status( $response, $error_code, $status ) {
97 if ( is_wp_error( $response ) && in_array( $error_code, $response->get_error_codes(), true ) ) {
98 $data = $response->get_error_data( $error_code );
99
100 if ( isset( $data['status'] ) ) {
101 $data['status'] = $status;
102
103 $response->add_data( $data );
104 }
105 }
106
107 return $response;
108 }
109
110 /**
111 * Set the importing constant.
112 */
113 public function set_importing() {
114 if ( ! defined( 'WP_IMPORTING' ) ) {
115 define( 'WP_IMPORTING', true );
116 }
117 }
118 }
119