Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Get_Database_Object_Backup_Endpoint
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 validate_input
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 result
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * Get Database object backup endpoint class.
9 *
10 * /sites/%s/database-object/backup      -> $blog_id
11 *
12 * @phan-constructor-used-for-side-effects
13 */
14class Jetpack_JSON_API_Get_Database_Object_Backup_Endpoint extends Jetpack_JSON_API_Endpoint {
15
16    /**
17     * Needed capabilities.
18     *
19     * @var array
20     */
21    protected $needed_capabilities = array(); // This endpoint is only accessible using a site token
22
23    /**
24     * Object type.
25     *
26     * @var string
27     */
28    protected $object_type;
29
30    /**
31     * Object ID.
32     *
33     * @var int
34     */
35    protected $object_id;
36
37    /**
38     * Full list of database objects that can be retrieved via this endpoint.
39     *
40     * @var array
41     */
42    protected $object_types = array(
43        'woocommerce_attribute'                       => array(
44            'table'    => 'woocommerce_attribute_taxonomies',
45            'id_field' => 'attribute_id',
46        ),
47
48        'woocommerce_downloadable_product_permission' => array(
49            'table'    => 'woocommerce_downloadable_product_permissions',
50            'id_field' => 'permission_id',
51        ),
52
53        'woocommerce_order_item'                      => array(
54            'table'     => 'woocommerce_order_items',
55            'id_field'  => 'order_item_id',
56            'meta_type' => 'order_item',
57        ),
58
59        'woocommerce_payment_token'                   => array(
60            'table'     => 'woocommerce_payment_tokens',
61            'id_field'  => 'token_id',
62            'meta_type' => 'payment_token',
63        ),
64
65        'woocommerce_tax_rate'                        => array(
66            'table'          => 'woocommerce_tax_rates',
67            'id_field'       => 'tax_rate_id',
68            'child_table'    => 'woocommerce_tax_rate_locations',
69            'child_id_field' => 'tax_rate_id',
70        ),
71
72        'woocommerce_webhook'                         => array(
73            'table'    => 'wc_webhooks',
74            'id_field' => 'webhook_id',
75        ),
76    );
77
78    /**
79     * Validate input.
80     *
81     * @param object $object - unused.
82     *
83     * @return bool|WP_Error
84     */
85    public function validate_input( $object ) {  // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
86        $query_args = $this->query_args();
87
88        if ( empty( $query_args['object_type'] ) || empty( $query_args['object_id'] ) ) {
89            return new WP_Error( 'invalid_args', __( 'You must specify both an object type and id to fetch', 'jetpack' ), 400 );
90        }
91
92        if ( empty( $this->object_types[ $query_args['object_type'] ] ) ) {
93            return new WP_Error( 'invalid_args', __( 'Specified object_type not recognized', 'jetpack' ), 400 );
94        }
95
96        $this->object_type = $this->object_types[ $query_args['object_type'] ];
97        $this->object_id   = $query_args['object_id'];
98
99        return true;
100    }
101
102    /**
103     * The result.
104     *
105     * @return array|WP_Error
106     */
107    protected function result() {
108        global $wpdb;
109
110        // Disable Sync as this is a read-only operation and triggered by sync activity.
111        \Automattic\Jetpack\Sync\Actions::mark_sync_read_only();
112
113        $table    = $wpdb->prefix . $this->object_type['table'];
114        $id_field = $this->object_type['id_field'];
115
116        // Fetch the requested object
117        $query  = $wpdb->prepare( 'select * from `' . $table . '` where `' . $id_field . '` = %d', $this->object_id ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
118        $object = $wpdb->get_row( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
119
120        if ( empty( $object ) ) {
121            return new WP_Error( 'object_not_found', __( 'Object not found', 'jetpack' ), 404 );
122        }
123
124        $result = array( 'object' => $object );
125
126        // Fetch associated metadata (if this object type has any)
127        if ( ! empty( $this->object_type['meta_type'] ) ) {
128            $result['meta'] = get_metadata( $this->object_type['meta_type'], $this->object_id );
129        }
130
131        // If there is a child linked table (eg: woocommerce_tax_rate_locations), fetch linked records
132        if ( ! empty( $this->object_type['child_table'] ) ) {
133            $child_table    = $wpdb->prefix . $this->object_type['child_table'];
134            $child_id_field = $this->object_type['child_id_field'];
135
136            $query              = $wpdb->prepare( 'select * from `' . $child_table . '` where `' . $child_id_field . '` = %d', $this->object_id ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
137            $result['children'] = $wpdb->get_results( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
138        }
139
140        return $result;
141    }
142}