PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Admin / API / DataDownloadIPs.php
DataDownloadIPs.php
168 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API Data Download IP Controller
4 *
5 * Handles requests to /data/download-ips
6 */
7
8 namespace Automattic\WooCommerce\Admin\API;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Data Download IP controller.
14 *
15 * @internal
16 * @extends WC_REST_Data_Controller
17 */
18 class DataDownloadIPs extends \WC_REST_Data_Controller {
19 /**
20 * Endpoint namespace.
21 *
22 * @var string
23 */
24 protected $namespace = 'wc-analytics';
25
26 /**
27 * Route base.
28 *
29 * @var string
30 */
31 protected $rest_base = 'data/download-ips';
32
33 /**
34 * Register routes.
35 *
36 * @since 3.5.0
37 */
38 public function register_routes() {
39 register_rest_route(
40 $this->namespace,
41 '/' . $this->rest_base,
42 array(
43 array(
44 'methods' => \WP_REST_Server::READABLE,
45 'callback' => array( $this, 'get_items' ),
46 'permission_callback' => array( $this, 'get_items_permissions_check' ),
47 ),
48 'schema' => array( $this, 'get_public_item_schema' ),
49 )
50 );
51 }
52
53 /**
54 * Return the download IPs matching the passed parameters.
55 *
56 * @since 3.5.0
57 * @param WP_REST_Request $request Request data.
58 * @return WP_Error|WP_REST_Response
59 */
60 public function get_items( $request ) {
61 global $wpdb;
62
63 if ( isset( $request['match'] ) ) {
64 $downloads = $wpdb->get_results(
65 $wpdb->prepare(
66 "SELECT DISTINCT( user_ip_address ) FROM {$wpdb->prefix}wc_download_log
67 WHERE user_ip_address LIKE %s
68 LIMIT 10",
69 $request['match'] . '%'
70 )
71 );
72 } else {
73 return new \WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce' ), array( 'status' => 400 ) );
74 }
75
76 $data = array();
77
78 if ( ! empty( $downloads ) ) {
79 foreach ( $downloads as $download ) {
80 $response = $this->prepare_item_for_response( $download, $request );
81 $data[] = $this->prepare_response_for_collection( $response );
82 }
83 }
84
85 return rest_ensure_response( $data );
86 }
87
88 /**
89 * Prepare the data object for response.
90 *
91 * @since 3.5.0
92 * @param object $item Data object.
93 * @param WP_REST_Request $request Request object.
94 * @return WP_REST_Response $response Response data.
95 */
96 public function prepare_item_for_response( $item, $request ) {
97 $data = $this->add_additional_fields_to_object( $item, $request );
98 $data = $this->filter_response_by_context( $data, 'view' );
99 $response = rest_ensure_response( $data );
100
101 $response->add_links( $this->prepare_links( $item ) );
102
103 /**
104 * Filter the list returned from the API.
105 *
106 * @param WP_REST_Response $response The response object.
107 * @param array $item The original item.
108 * @param WP_REST_Request $request Request used to generate the response.
109 */
110 return apply_filters( 'woocommerce_rest_prepare_data_download_ip', $response, $item, $request );
111 }
112
113 /**
114 * Prepare links for the request.
115 *
116 * @param object $item Data object.
117 * @return array Links for the given object.
118 */
119 protected function prepare_links( $item ) {
120 $links = array(
121 'collection' => array(
122 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
123 ),
124 );
125 return $links;
126 }
127
128 /**
129 * Get the query params for collections.
130 *
131 * @return array
132 */
133 public function get_collection_params() {
134 $params = array();
135 $params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
136 $params['match'] = array(
137 'description' => __( 'A partial IP address can be passed and matching results will be returned.', 'woocommerce' ),
138 'type' => 'string',
139 'validate_callback' => 'rest_validate_request_arg',
140 );
141 return $params;
142 }
143
144
145 /**
146 * Get the schema, conforming to JSON Schema.
147 *
148 * @return array
149 */
150 public function get_item_schema() {
151 $schema = array(
152 '$schema' => 'http://json-schema.org/draft-04/schema#',
153 'title' => 'data_download_ips',
154 'type' => 'object',
155 'properties' => array(
156 'user_ip_address' => array(
157 'type' => 'string',
158 'description' => __( 'IP address.', 'woocommerce' ),
159 'context' => array( 'view' ),
160 'readonly' => true,
161 ),
162 ),
163 );
164
165 return $this->add_additional_fields_schema( $schema );
166 }
167 }
168