Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Import
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 register_routes
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 import_permissions_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_route_options
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 ensure_http_status
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 set_importing
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
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;
11
12if ( ! 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 */
21trait 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}