shipping_time_query = $shipping_time_query;
$this->attribute_manager = $attribute_manager;
}
/**
* The meta key used to filter the items.
*
* @var string
*/
public const KEY_VISIBILITY = '_wc_gla_visibility';
/**
* The Post types to be filtered.
*
* @var array
*/
public static $post_types_to_filter = [
'product' => [
'meta_query' => [
[
'key' => self::KEY_VISIBILITY,
'value' => ChannelVisibility::SYNC_AND_SHOW,
'compare' => '=',
],
],
],
'shop_coupon' => [
'meta_query' => [
[
'key' => self::KEY_VISIBILITY,
'value' => ChannelVisibility::SYNC_AND_SHOW,
'compare' => '=',
],
[
'key' => 'customer_email',
'compare' => 'NOT EXISTS',
],
],
],
'product_variation' => [
'meta_query' => null,
],
];
/**
* Register all filters.
*/
public function register(): void {
// Allow to filter by gla_syncable.
add_filter(
'woocommerce_rest_query_vars',
function ( $valid_vars ) {
$valid_vars[] = 'gla_syncable';
return $valid_vars;
}
);
$this->register_callbacks();
foreach ( array_keys( self::$post_types_to_filter ) as $object_type ) {
$this->register_object_types_filter( $object_type );
}
}
/**
* Register the filters for a specific object type.
*
* @param string $object_type The object type.
*/
protected function register_object_types_filter( string $object_type ): void {
add_filter(
'woocommerce_rest_prepare_' . $object_type . '_object',
[ $this, 'filter_response_by_syncable_item' ],
PHP_INT_MAX, // Run this filter last to override any other response.
3
);
add_filter(
'woocommerce_rest_prepare_' . $object_type . '_object',
[ $this, 'prepare_response' ],
PHP_INT_MAX - 1,
3
);
add_filter(
'woocommerce_rest_' . $object_type . '_object_query',
[ $this, 'filter_by_metaquery' ],
10,
2
);
}
/**
* Register the callbacks.
*/
protected function register_callbacks() {
add_filter(
'rest_request_after_callbacks',
/**
* Add the Google for WooCommerce and Ads settings to the settings/general response.
*
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response The response object.
* @param mixed $handler The handler.
* @param WP_REST_Request $request The request object.
*/
function ( $response, $handler, $request ) {
if ( ! $this->is_gla_request( $request ) || ! $response instanceof WP_REST_Response ) {
return $response;
}
$data = $response->get_data();
if ( $request->get_route() === '/wc/v3/settings/general' ) {
$data[] = [
'id' => 'gla_target_audience',
'label' => 'Google for WooCommerce: Target Audience',
'value' => $this->options->get( OptionsInterface::TARGET_AUDIENCE, [] ),
];
$data[] = [
'id' => 'gla_shipping_times',
'label' => 'Google for WooCommerce: Shipping Times',
'value' => $this->shipping_time_query->get_all_shipping_times(),
];
$data[] = [
'id' => 'gla_language',
'label' => 'Google for WooCommerce: Store language',
'value' => get_locale(),
];
$response->set_data( array_values( $data ) );
}
$response->set_data( $this->prepare_data( $response->get_data(), $request ) );
return $response;
},
10,
3
);
}
/**
* Prepares the data converting the empty arrays in objects for consistency.
*
* @param array $data The response data to parse
* @param WP_REST_Request $request The request object.
* @return mixed
*/
public function prepare_data( $data, $request ) {
if ( ! is_array( $data ) ) {
return $data;
}
foreach ( $data as $key => $value ) {
if ( preg_match( '/^\/wc\/v3\/shipping\/zones\/\d+\/methods/', $request->get_route() ) && isset( $value['settings'] ) && empty( $value['settings'] ) ) {
$data[ $key ]['settings'] = (object) $value['settings'];
}
}
return $data;
}
/**
* Whether the request is coming from the WPCOM proxy.
*
* @param WP_REST_Request $request The request object.
*
* @return bool
*/
protected function is_gla_request( WP_REST_Request $request ): bool {
// WPCOM proxy will set the gla_syncable to 1 if the request is coming from the proxy and it is the Google App.
return $request->get_param( 'gla_syncable' ) === '1';
}
/**
* Get route pieces: resource and id, if present.
*
* @param WP_REST_Request $request The request object.
*
* @return array The route pieces.
*/
protected function get_route_pieces( WP_REST_Request $request ): array {
$route = $request->get_route();
$pattern = '/(?P