-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathProductFeedQueryHelper.php
More file actions
243 lines (210 loc) · 7.6 KB
/
ProductFeedQueryHelper.php
File metadata and controls
243 lines (210 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\DB;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\ContainerAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\Interfaces\ContainerAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantStatuses;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductRepository;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\MCStatus;
use WP_Query;
use WP_REST_Request;
use wpdb;
defined( 'ABSPATH' ) || exit;
/**
* Class ProductFeedQueryHelper
*
* ContainerAware used to access:
* - MerchantCenterService
* - MerchantStatuses
* - ProductHelper
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\DB
*/
class ProductFeedQueryHelper implements ContainerAwareInterface, Service {
use ContainerAwareTrait;
use PluginHelper;
/**
* @var wpdb
*/
protected $wpdb;
/**
* @var WP_REST_Request
*/
protected $request;
/**
* @var ProductRepository
*/
protected $product_repository;
/**
* Meta key for total sales.
*/
protected const META_KEY_TOTAL_SALES = 'total_sales';
/**
* ProductFeedQueryHelper constructor.
*
* @param wpdb $wpdb
* @param ProductRepository $product_repository
*/
public function __construct( wpdb $wpdb, ProductRepository $product_repository ) {
$this->wpdb = $wpdb;
$this->product_repository = $product_repository;
}
/**
* Retrieve an array of product information using the request params.
*
* @param WP_REST_Request $request
*
* @return array
*
* @throws InvalidValue If the orderby value isn't valid.
* @throws Exception If the status data can't be retrieved from Google.
*/
public function get( WP_REST_Request $request ): array {
$this->request = $request;
$products = [];
$args = $this->prepare_query_args();
$refresh_status_data_job = null;
list( $limit, $offset ) = $this->prepare_query_pagination();
$mc_service = $this->container->get( MerchantCenterService::class );
if ( $mc_service->is_connected() ) {
$refresh_status_data_job = $this->container->get( MerchantStatuses::class )->maybe_refresh_status_data();
}
/** @var ProductHelper $product_helper */
$product_helper = $this->container->get( ProductHelper::class );
add_filter( 'posts_where', [ $this, 'title_filter' ], 10, 2 );
foreach ( $this->product_repository->find( $args, $limit, $offset ) as $product ) {
$id = $product->get_id();
$errors = $product_helper->get_validation_errors( $product );
$mc_status = $product_helper->get_mc_status( $product )
?? ( $product_helper->is_product_synced( $product ) ? MCStatus::PENDING : MCStatus::NOT_SYNCED );
// If the refresh_status_data_job is scheduled, we don't know the status yet as it is being refreshed.
if ( $refresh_status_data_job && $refresh_status_data_job->is_scheduled() ) {
$mc_status = null;
}
$products[ $id ] = [
'id' => $id,
'title' => $product->get_name(),
'visible' => $product_helper->get_channel_visibility( $product ) !== ChannelVisibility::DONT_SYNC_AND_SHOW,
'status' => $mc_status,
'image_url' => wp_get_attachment_image_url( $product->get_image_id(), 'full' ),
'price' => $product->get_price(),
'errors' => array_values( $errors ),
];
}
remove_filter( 'posts_where', [ $this, 'title_filter' ] );
return array_values( $products );
}
/**
* Count the number of products (using title filter if present).
*
* @param WP_REST_Request $request
*
* @return int
*
* @throws InvalidValue If the orderby value isn't valid.
*/
public function count( WP_REST_Request $request ): int {
$this->request = $request;
$args = $this->prepare_query_args();
add_filter( 'posts_where', [ $this, 'title_filter' ], 10, 2 );
$ids = $this->product_repository->find_ids( $args );
remove_filter( 'posts_where', [ $this, 'title_filter' ] );
return count( $ids );
}
/**
* Prepare the args to be used to retrieve the products, namely orderby, meta_query and type.
*
* @return array
*
* @throws InvalidValue If the orderby value isn't valid.
*/
protected function prepare_query_args(): array {
$product_types = ProductSyncer::get_supported_product_types();
$product_types = array_diff( $product_types, [ 'variation' ] );
$args = [
'type' => $product_types,
'status' => 'publish',
'orderby' => [ 'title' => 'ASC' ],
];
if ( ! empty( $this->request['ids'] ) ) {
$args['include'] = explode( ',', $this->request['ids'] );
}
if ( ! empty( $this->request['search'] ) ) {
$args['gla_search'] = $this->request['search'];
}
if ( empty( $this->request['orderby'] ) ) {
return $args;
}
switch ( $this->request['orderby'] ) {
case 'title':
$args['orderby']['title'] = $this->get_order();
break;
case 'id':
$args['orderby'] = [ 'ID' => $this->get_order() ] + $args['orderby'];
break;
case 'visible':
$args['meta_key'] = $this->prefix_meta_key( ProductMetaHandler::KEY_VISIBILITY );
$args['orderby'] = [ 'meta_value' => $this->get_order() ] + $args['orderby'];
break;
case 'status':
$args['meta_key'] = $this->prefix_meta_key( ProductMetaHandler::KEY_MC_STATUS );
$args['orderby'] = [ 'meta_value' => $this->get_order() ] + $args['orderby'];
break;
case 'total_sales':
$args['meta_key'] = self::META_KEY_TOTAL_SALES;
$args['orderby'] = [ 'meta_value_num' => $this->get_order() ] + $args['orderby'];
break;
default:
throw InvalidValue::not_in_allowed_list( 'orderby', [ 'title', 'id', 'visible', 'status', 'total_sales' ] );
}
return $args;
}
/**
* Convert the per_page and page parameters into limit and offset values.
*
* @return array Containing limit and offset values.
*/
protected function prepare_query_pagination(): array {
$limit = -1;
$offset = 0;
if ( ! empty( $this->request['per_page'] ) ) {
$limit = intval( $this->request['per_page'] );
$page = max( 1, intval( $this->request['page'] ) );
$offset = $limit * ( $page - 1 );
}
return [ $limit, $offset ];
}
/**
* Filter for the posts_where hook, adds WHERE clause to search
* for the 'search' parameter in the product titles (when present).
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $wp_query The WP_Query instance (passed by reference).
*
* @return string The updated WHERE clause.
*/
public function title_filter( string $where, WP_Query $wp_query ): string {
$gla_search = $wp_query->get( 'gla_search' );
if ( $gla_search ) {
$title_search = '%' . $this->wpdb->esc_like( $gla_search ) . '%';
$where .= $this->wpdb->prepare( " AND `{$this->wpdb->posts}`.`post_title` LIKE %s", $title_search ); // phpcs:ignore WordPress.DB.PreparedSQL
}
return $where;
}
/**
* Return the ORDER BY order based on the order request parameter value.
*
* @return string
*/
protected function get_order(): string {
return strtoupper( $this->request['order'] ?? '' ) === 'DESC' ? 'DESC' : 'ASC';
}
}