Commit 7a800fe1de for woocommerce
commit 7a800fe1deca04d837e86fcde9ea2c58213d368e
Author: Néstor Soriano <konamiman@konamiman.com>
Date: Fri Jan 16 16:21:01 2026 +0100
Add caching for the countries REST API endpoints (#62834)
diff --git a/plugins/woocommerce/changelog/pr-62834 b/plugins/woocommerce/changelog/pr-62834
new file mode 100644
index 0000000000..358ff06593
--- /dev/null
+++ b/plugins/woocommerce/changelog/pr-62834
@@ -0,0 +1,4 @@
+Significance: minor
+Type: update
+
+Add caching for the countries REST API endpoints
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-data-countries-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-data-countries-controller.php
index b9783bc2d3..d57a9df4a7 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-data-countries-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-data-countries-controller.php
@@ -8,6 +8,8 @@
* @since 3.5.0
*/
+use Automattic\WooCommerce\Internal\Traits\RestApiCache;
+
defined( 'ABSPATH' ) || exit;
/**
@@ -18,6 +20,8 @@ defined( 'ABSPATH' ) || exit;
*/
class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
+ use RestApiCache;
+
/**
* Endpoint namespace.
*
@@ -32,6 +36,13 @@ class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
*/
protected $rest_base = 'data/countries';
+ /**
+ * Constructor.
+ */
+ public function __construct() {
+ $this->initialize_rest_api_cache();
+ }
+
/**
* Register routes.
*
@@ -44,7 +55,7 @@ class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
array(
array(
'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_items' ),
+ 'callback' => $this->with_cache( array( $this, 'get_items' ) ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
@@ -56,7 +67,7 @@ class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
array(
array(
'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_item' ),
+ 'callback' => $this->with_cache( array( $this, 'get_item' ) ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'location' => array(
@@ -159,6 +170,8 @@ class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
*
* Allows modification of the location data right before it is returned.
*
+ * @since 3.5.0
+ *
* @param WP_REST_Response $response The response object.
* @param array $data The original country's states list.
* @param WP_REST_Request $request Request used to generate the response.
@@ -241,4 +254,65 @@ class WC_REST_Data_Countries_Controller extends WC_REST_Data_Controller {
return $this->add_additional_fields_schema( $schema );
}
+
+ /**
+ * Get the default entity type for response caching.
+ *
+ * @return string|null The entity type.
+ */
+ protected function get_default_response_entity_type(): ?string {
+ return 'country';
+ }
+
+ /**
+ * Get the files relevant to response caching.
+ *
+ * @param WP_REST_Request<array<string, mixed>> $request The request object.
+ * @param string|null $endpoint_id Optional endpoint identifier.
+ * @return array Array of file paths to track for cache invalidation.
+ */
+ protected function get_files_relevant_to_response_caching( WP_REST_Request $request, ?string $endpoint_id = null ): array { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
+ return array( 'i18n/countries.php', 'i18n/states.php' );
+ }
+
+ /**
+ * Get the hooks relevant to response caching.
+ *
+ * @param WP_REST_Request<array<string, mixed>> $request The request object.
+ * @param string|null $endpoint_id Optional endpoint identifier.
+ * @return array Array of hook names to track for cache invalidation.
+ */
+ protected function get_hooks_relevant_to_caching( WP_REST_Request $request, ?string $endpoint_id = null ): array { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
+ return array(
+ 'woocommerce_countries',
+ 'woocommerce_states',
+ 'woocommerce_sort_countries',
+ 'woocommerce_rest_prepare_data_country',
+ );
+ }
+
+ /**
+ * Whether the response cache should vary by user.
+ *
+ * @param WP_REST_Request<array<string, mixed>> $request The request object.
+ * @param string|null $endpoint_id Optional endpoint identifier.
+ * @return bool False since country data doesn't vary by user.
+ */
+ protected function response_cache_vary_by_user( WP_REST_Request $request, ?string $endpoint_id = null ): bool { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
+ return false;
+ }
+
+ /**
+ * Extract entity IDs from response data.
+ *
+ * Countries don't have entity IDs, cache invalidation is file-based.
+ *
+ * @param array $response_data Response data.
+ * @param WP_REST_Request<array<string, mixed>> $request The request object.
+ * @param string|null $endpoint_id Optional endpoint identifier.
+ * @return array Empty array since countries don't have entity IDs.
+ */
+ protected function extract_entity_ids_from_response( array $response_data, WP_REST_Request $request, ?string $endpoint_id = null ): array { // phpcs:ignore Squiz.Commenting.FunctionComment.IncorrectTypeHint
+ return array();
+ }
}