Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
End
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_route_options
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 get_item_schema
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
6
 cleanup_database
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
2/**
3 * End 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 End
20 *
21 * This class is used to start the import process.
22 */
23class End extends \WP_REST_Controller {
24
25    /**
26     * Base class
27     */
28    use Import;
29
30    /**
31     * The Import ID add a new item to the schema.
32     */
33    use Import_ID;
34
35    /**
36     * Constructor.
37     */
38    public function __construct() {
39        $this->rest_base = 'end';
40    }
41
42    /**
43     * Get the register route options.
44     *
45     * @see register_rest_route()
46     *
47     * @return array The options.
48     */
49    protected function get_route_options() {
50        return array(
51            array(
52                'methods'             => \WP_REST_Server::CREATABLE,
53                'callback'            => array( $this, 'cleanup_database' ),
54                'permission_callback' => array( $this, 'import_permissions_callback' ),
55                'args'                => array(),
56            ),
57            'schema' => array( $this, 'get_public_item_schema' ),
58        );
59    }
60
61    /**
62     * Retrieves the start values schema, conforming to JSON Schema.
63     *
64     * @return array Item schema data.
65     */
66    public function get_item_schema() {
67        if ( $this->schema ) {
68            return $this->add_additional_fields_schema( $this->schema );
69        }
70
71        $schema = array(
72            '$schema'    => 'http://json-schema.org/draft-04/schema#',
73            'title'      => 'import-end',
74            'type'       => 'object',
75            'properties' => array(
76                'commentmeta_count' => array(
77                    'description' => __( 'Comment meta deleted count.', 'jetpack-import' ),
78                    'type'        => 'integer',
79                    'context'     => array( 'view' ),
80                    'readonly'    => true,
81                ),
82                'postmeta_count'    => array(
83                    'description' => __( 'Post meta deleted count.', 'jetpack-import' ),
84                    'type'        => 'integer',
85                    'context'     => array( 'view' ),
86                    'readonly'    => true,
87                ),
88                'termmeta_count'    => array(
89                    'description' => __( 'Term meta deleted count.', 'jetpack-import' ),
90                    'type'        => 'integer',
91                    'context'     => array( 'view' ),
92                    'readonly'    => true,
93                ),
94            ),
95        );
96
97        $this->schema = $schema;
98
99        return $this->add_additional_fields_schema( $this->schema );
100    }
101
102    /**
103     * Delete all meta values from database.
104     *
105     * @param WP_REST_Request $request Full details about the request.
106     * @return WP_REST_Response|WP_Error Response object on success, or error object on failure.
107     *
108     * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
109     */
110    public function cleanup_database( $request ) {
111        global $wpdb;
112
113        $where = array( 'meta_key' => $this->import_id_field_name );
114
115        return array(
116            'commentmeta_count' => $wpdb->delete( $wpdb->commentmeta, $where ),
117            'postmeta_count'    => $wpdb->delete( $wpdb->postmeta, $where ),
118            'termmeta_count'    => $wpdb->delete( $wpdb->termmeta, $where ),
119        );
120    }
121}