Last active
April 3, 2023 05:20
-
-
Save bryceadams/faf96cde20c2fe63b4cb580f1bafa901 to your computer and use it in GitHub Desktop.
Woo API redaction filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Redact customer address data from orders in WooCommerce's REST API | |
*/ | |
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'mtk_snippet_redact_order_info', 10, 3 ); | |
function mtk_snippet_redact_order_info( $response, $object, $request ) { | |
$onlyMetorikRequests = true; | |
if (! $onlyMetorikRequests || (function_exists('metorik_check_headers_agent') && metorik_check_headers_agent($request->get_headers($_SERVER)))) { | |
// Check if the request is for the orders endpoint | |
if ( $object instanceof WC_Order ) { | |
$domain = 'site.test'; // @todo | |
// Redact Meta Keys | |
$meta_keys = ['_billing_address_index', '_shipping_address_index']; | |
$meta_data = $response->data['meta_data']; | |
$new_meta_data = []; | |
foreach ($meta_data as $md) { | |
if (! in_array($md->key, $meta_keys)) { | |
$new_meta_data[] = $md; | |
} | |
} | |
$response->data['meta_data'] = $new_meta_data; | |
// Get the customer data | |
$customer = $response->data['customer']; | |
// Get the customer data | |
$customer_id = $response->data['customer_id']; | |
$customer_email = $response->data['billing']['email']; | |
// Check if the customer is registered or a guest | |
if ( $customer_id > 0 ) { | |
// Registered customer - change order email to customer ID email | |
$customer_email_parts = explode( '@', $customer_email ); | |
$new_order_email = 'customer-' . $customer_id . '@' . $customer_email_parts[1]; | |
} else { | |
// Guest customer - change order email to order ID email | |
$new_order_email = 'order-' . $response->data['id'] . '@' . $domain; | |
} | |
// Update the response data with the new order email | |
$response->data['billing']['email'] = $new_order_email; | |
$response->data['customer']['email'] = $new_order_email; | |
// Redact the customer data | |
$response->data['customer_ip_address'] = 'REDACTED'; | |
// Get the customer's billing and shipping address data | |
$billing_address = $response->data['billing']; | |
$shipping_address = $response->data['shipping']; | |
// Redact the customer's billing and shipping address data | |
$billing_address['first_name'] = 'REDACTED'; | |
$billing_address['last_name'] = 'REDACTED'; | |
$billing_address['email'] = $new_order_email; | |
$billing_address['company'] = 'REDACTED'; | |
$billing_address['address_1'] = 'REDACTED'; | |
$billing_address['address_2'] = 'REDACTED'; | |
$billing_address['city'] = 'REDACTED'; | |
$billing_address['state'] = 'REDACTED'; | |
$billing_address['postcode'] = 'REDACTED'; | |
$billing_address['country'] = 'REDACTED'; | |
$billing_address['phone'] = 'REDACTED'; | |
$shipping_address['first_name'] = 'REDACTED'; | |
$shipping_address['last_name'] = 'REDACTED'; | |
$shipping_address['company'] = 'REDACTED'; | |
$shipping_address['address_1'] = 'REDACTED'; | |
$shipping_address['address_2'] = 'REDACTED'; | |
$shipping_address['city'] = 'REDACTED'; | |
$shipping_address['state'] = 'REDACTED'; | |
$shipping_address['postcode'] = 'REDACTED'; | |
$shipping_address['country'] = 'REDACTED'; | |
$shipping_address['phone'] = 'REDACTED'; | |
// Update the response data with the redacted address data | |
$response->data['billing'] = $billing_address; | |
$response->data['shipping'] = $shipping_address; | |
} | |
} | |
return $response; | |
} | |
/** | |
* Redact customer address data from orders in WooCommerce's REST API | |
*/ | |
add_filter( 'woocommerce_rest_prepare_shop_subscription', 'mtk_snippet_redact_sub_info', 10, 3 ); | |
add_filter( 'woocommerce_rest_prepare_shop_subscription_object', 'mtk_snippet_redact_sub_info', 10, 3 ); // WCS v3 API | |
function mtk_snippet_redact_sub_info( $response, $object, $request ) { | |
$onlyMetorikRequests = true; | |
if (! $onlyMetorikRequests || (function_exists('metorik_check_headers_agent') && metorik_check_headers_agent($request->get_headers($_SERVER)))) { | |
$domain = 'site.test'; // @todo | |
// Redact Meta Keys | |
$meta_keys = ['_billing_address_index', '_shipping_address_index']; | |
$meta_data = $response->data['meta_data']; | |
$new_meta_data = []; | |
foreach ($meta_data as $md) { | |
if (! in_array($md->key, $meta_keys)) { | |
$new_meta_data[] = $md; | |
} | |
} | |
$response->data['meta_data'] = $new_meta_data; | |
// Get the customer data | |
$customer = $response->data['customer']; | |
// Get the customer data | |
$customer_id = $response->data['customer_id']; | |
$customer_email = $response->data['billing']['email']; | |
// Check if the customer is registered or a guest | |
if ( $customer_id > 0 ) { | |
// Registered customer - change order email to customer ID email | |
$customer_email_parts = explode( '@', $customer_email ); | |
$new_order_email = 'customer-' . $customer_id . '@' . $customer_email_parts[1]; | |
} else { | |
// Guest customer - change order email to order ID email | |
$new_order_email = 'order-' . $response->data['id'] . '@' . $domain; | |
} | |
// Update the response data with the new order email | |
$response->data['billing']['email'] = $new_order_email; | |
$response->data['customer']['email'] = $new_order_email; | |
// Redact the customer data | |
$response->data['customer_ip_address'] = 'REDACTED'; | |
// Get the customer's billing and shipping address data | |
$billing_address = $response->data['billing']; | |
$shipping_address = $response->data['shipping']; | |
// Redact the customer's billing and shipping address data | |
$billing_address['first_name'] = 'REDACTED'; | |
$billing_address['last_name'] = 'REDACTED'; | |
$billing_address['email'] = $new_order_email; | |
$billing_address['company'] = 'REDACTED'; | |
$billing_address['address_1'] = 'REDACTED'; | |
$billing_address['address_2'] = 'REDACTED'; | |
$billing_address['city'] = 'REDACTED'; | |
$billing_address['state'] = 'REDACTED'; | |
$billing_address['postcode'] = 'REDACTED'; | |
$billing_address['country'] = 'REDACTED'; | |
$billing_address['phone'] = 'REDACTED'; | |
$shipping_address['first_name'] = 'REDACTED'; | |
$shipping_address['last_name'] = 'REDACTED'; | |
$shipping_address['company'] = 'REDACTED'; | |
$shipping_address['address_1'] = 'REDACTED'; | |
$shipping_address['address_2'] = 'REDACTED'; | |
$shipping_address['city'] = 'REDACTED'; | |
$shipping_address['state'] = 'REDACTED'; | |
$shipping_address['postcode'] = 'REDACTED'; | |
$shipping_address['country'] = 'REDACTED'; | |
$shipping_address['phone'] = 'REDACTED'; | |
// Update the response data with the redacted address data | |
$response->data['billing'] = $billing_address; | |
$response->data['shipping'] = $shipping_address; | |
} | |
return $response; | |
} | |
/** | |
Redact customer data from customers in WooCommerce's REST API | |
*/ | |
add_filter( 'woocommerce_rest_prepare_customer', 'mtk_snippet_redact_customer_info', 10, 3 ); | |
function mtk_snippet_redact_customer_info( $response, $object, $request ) { | |
$onlyMetorikRequests = true; | |
if (! $onlyMetorikRequests || (function_exists('metorik_check_headers_agent') && metorik_check_headers_agent($request->get_headers($_SERVER)))) { | |
// Check if the request is for the customers endpoint | |
if ( $object instanceof WP_User ) { | |
$domain = 'site.test'; // @todo | |
// Redact Meta Keys | |
$meta_keys = ['shipping_email']; | |
$meta_data = $response->data['meta_data']; | |
$new_meta_data = []; | |
foreach ($meta_data as $md) { | |
if (! in_array($md->key, $meta_keys)) { | |
$new_meta_data[] = $md; | |
} | |
} | |
$response->data['meta_data'] = $new_meta_data; | |
// Get the customer data | |
$customer_id = $response->data['id']; | |
$customer_email = $response->data['email']; | |
// Change customer email to customer ID email | |
$new_customer_email = 'customer-' . $customer_id . '@' . $domain; | |
// Update the response data with the new customer email | |
$response->data['email'] = $new_customer_email; | |
$response->data['first_name'] = 'REDACTED'; | |
$response->data['last_name'] = 'REDACTED'; | |
$response->data['username'] = 'REDACTED'; | |
// Redact the customer data | |
$response->data['ip_address'] = 'REDACTED'; | |
$response->data['avatar_url'] = ''; | |
// Get the customer's billing and shipping address data | |
$billing_address = $response->data['billing']; | |
$shipping_address = $response->data['shipping']; | |
// Redact the customer's billing and shipping address data | |
$billing_address['first_name'] = 'REDACTED'; | |
$billing_address['last_name'] = 'REDACTED'; | |
$billing_address['email'] = $new_customer_email; | |
$billing_address['company'] = 'REDACTED'; | |
$billing_address['address_1'] = 'REDACTED'; | |
$billing_address['address_2'] = 'REDACTED'; | |
$billing_address['city'] = 'REDACTED'; | |
$billing_address['state'] = 'REDACTED'; | |
$billing_address['postcode'] = 'REDACTED'; | |
$billing_address['country'] = 'REDACTED'; | |
$billing_address['phone'] = 'REDACTED'; | |
$shipping_address['first_name'] = 'REDACTED'; | |
$shipping_address['last_name'] = 'REDACTED'; | |
$shipping_address['company'] = 'REDACTED'; | |
$shipping_address['address_1'] = 'REDACTED'; | |
$shipping_address['address_2'] = 'REDACTED'; | |
$shipping_address['city'] = 'REDACTED'; | |
$shipping_address['state'] = 'REDACTED'; | |
$shipping_address['postcode'] = 'REDACTED'; | |
$shipping_address['country'] = 'REDACTED'; | |
$shipping_address['phone'] = 'REDACTED'; | |
// Update the response data with the redacted address data | |
$response->data['billing'] = $billing_address; | |
$response->data['shipping'] = $shipping_address; | |
} | |
} | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment