* REST API Data Download IP Controller
* Handles requests to /data/download-ips
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
* Data Download IP controller.
* @extends WC_REST_Data_Controller
class DataDownloadIPs extends \WC_REST_Data_Controller {
protected $namespace = 'wc-analytics';
protected $rest_base = 'data/download-ips';
public function register_routes() {
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'schema' => array( $this, 'get_public_item_schema' ),
* Return the download IPs matching the passed parameters.
* @param WP_REST_Request $request Request data.
* @return WP_Error|WP_REST_Response
public function get_items( $request ) {
if ( isset( $request['match'] ) ) {
$downloads = $wpdb->get_results(
"SELECT DISTINCT( user_ip_address ) FROM {$wpdb->prefix}wc_download_log
WHERE user_ip_address LIKE %s
return new \WP_Error( 'woocommerce_rest_data_download_ips_invalid_request', __( 'Invalid request. Please pass the match parameter.', 'woocommerce' ), array( 'status' => 400 ) );
if ( ! empty( $downloads ) ) {
foreach ( $downloads as $download ) {
$response = $this->prepare_item_for_response( $download, $request );
$data[] = $this->prepare_response_for_collection( $response );
return rest_ensure_response( $data );
* Prepare the data object for response.
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item ) );
* Filter the list returned from the API.
* @param WP_REST_Response $response The response object.
* @param array $item The original item.
* @param WP_REST_Request $request Request used to generate the response.
return apply_filters( 'woocommerce_rest_prepare_data_download_ip', $response, $item, $request );
* Prepare links for the request.
* @param object $item Data object.
* @return array Links for the given object.
protected function prepare_links( $item ) {
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
* Get the query params for collections.
public function get_collection_params() {
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['match'] = array(
'description' => __( 'A partial IP address can be passed and matching results will be returned.', 'woocommerce' ),
'validate_callback' => 'rest_validate_request_arg',
* Get the schema, conforming to JSON Schema.
public function get_item_schema() {
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'data_download_ips',
'user_ip_address' => array(
'description' => __( 'IP address.', 'woocommerce' ),
'context' => array( 'view' ),
return $this->add_additional_fields_schema( $schema );