-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfast-woo-order-lookup.php
459 lines (393 loc) · 14.8 KB
/
fast-woo-order-lookup.php
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php /** @noinspection PhpUnusedParameterInspection */
/**
* Fast Woo Order Lookup
*
* @package fast-woo-order-lookup
* @author OllieJones
* @license gplv2
*
* @wordpress-plugin
* Plugin Name: Fast Woo Order Lookup
* Plugin URI: https://plumislandmedia.net/wordpress-plugins/fast-woo-order-lookup/
* Description: Look up orders faster in large WooCommerce stores with many orders.
* Version: 1.1.3
* Requires PHP: 5.6
* Requires at least: 5.8
* Tested up to: 6.7
* WC requires at least: 4.0
* WC tested up to: 9.1.4
* Requires Plugins: woocommerce
* Author: Ollie Jones
* Author URI: https://github.com/OllieJones
* Text Domain: fast-woo-order-lookup
* Domain Path: /languages
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* You should have received a copy of the GNU General Public License
* along with Fast Woo Order Lookup. If not, see <https://www.gnu.org/licenses/gpl-2.0.html/>.
*/
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlDialectInspection */
namespace Fast_Woo_Order_Lookup;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStoreMeta;
use WC_Order;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
const FAST_WOO_ORDER_LOOKUP_METAKEY_CACHE = 'fast_woo_order_lookup_metakey_cache';
class FastWooOrderLookup {
private static $instance = null;
private $textdex;
private $filtering = false;
private $term;
private $trigram_clause;
private $orders_to_update = array();
public static function declare_hpos_compatible() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
public static function woocommerce_changing_order( $order_id, $order ) {
$instance = self::getInstance();
$instance->orders_to_update[ $order_id ] = 1;
}
public static function woocommerce_deleting_order( $order_id ) {
$instance = self::getInstance();
$instance->orders_to_update[ $order_id ] = 1;
}
public static function woocommerce_order_object_updated_props( $order, $props ) {
$instance = self::getInstance();
$order_id = $order->get_id();
$instance->orders_to_update[ $order_id ] = 1;
}
public static function update_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
$instance = self::getInstance();
if ( $instance->textdex->is_order_meta_key( $meta_key ) ) {
$instance->orders_to_update[ $object_id ] = 1;
}
}
public static function textdex_batch_action() {
$instance = self::getInstance();
$instance->textdex->load_batch();
}
/**
* Filter: Data store name.
*
* @param string $store
*
* @return string
*/
public static function woocommerce_order_data_store( $store ) {
if ( 'WC_Order_Data_Store_CPT' === $store || 'WCS_Subscription_Data_Store_CPT' === $store ) {
self::getInstance();
}
return $store;
}
/** Singleton constructor interface.
*
* @return FastWooOrderLookup
*/
public static function getInstance() {
if ( self::$instance == null ) {
self::$instance = new FastWooOrderLookup();
}
return self::$instance;
}
/**
* Configure this plugin to intercept metadata searches for WooCommerce orders.
*/
private function __construct() {
/* Query manipulation */
add_filter( 'woocommerce_shop_order_search_fields', array( $this, 'filter_search_fields' ) );
add_filter( 'woocommerce_shop_subscription_search_fields', array( $this, 'filter_search_fields' ) );
add_filter( 'woocommerce_shop_order_search_results', array( $this, 'filter_search_results' ), 10, 3 );
add_filter( 'woocommerce_shop_subscription_search_results', array( $this, 'filter_search_results' ), 10, 3 );
add_filter( 'woocommerce_order_query_args', array( $this, 'woocommerce_order_query_args' ) );
add_filter( 'woocommerce_order_query', array( $this, 'woocommerce_order_query' ), 10, 2 );
add_filter( 'postmeta_form_keys', array( $this, 'postmeta_get_order_custom_field_names' ), 10, 2 );
require_once( plugin_dir_path( __FILE__ ) . 'code/class-textdex.php' );
$this->textdex = new Textdex();
$this->textdex->activate();
$this->textdex->load_textdex();
add_action( 'admin_notices', array( $this, 'show_status' ), 10, 0 );
add_action( 'shutdown', array( $this, 'update_textdex' ), 1, 0 );
}
public function show_status() {
if ( ! $this->textdex->is_ready( 10 ) ) {
load_plugin_textdomain( 'fast-woo-order-lookup', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
$sa1 = __( 'See the', 'fast-woo-order-lookup' );
$sa2 = __( 'Scheduled Actions status page', 'fast-woo-order-lookup' );
$sa3 = __( 'for details.', 'fast-woo-order-lookup' );
$frac = $this->textdex->fraction_complete();
if ( $frac < 0.01 ) {
$ms1 = __( 'Fast Woo Order Lookup indexing begins soon.', 'fast-woo-order-lookup' );
$ms2 = '';
} else {
$percent = number_format( 100 * $this->textdex->fraction_complete(), 1 );
$ms1 = __( 'Fast Woo Order Lookup indexing still in progress.', 'fast-woo-order-lookup' );
/* translators: 1: percent 12.3 */
$ms2 = __( '%1$s%% complete.', 'fast-woo-order-lookup' );
$ms2 = sprintf( $ms2, $percent );
}
?>
<div class="notice notice-info">
<p>
<?php echo esc_html( $ms1 ); ?>
<?php echo esc_html( $ms2 ); ?>
<?php echo esc_html( $sa1 ); ?>
<a href="admin.php?page=wc-status&tab=action-scheduler&s=fast_woo_order_lookup_textdex_action&status=pending"><?php echo esc_html( $sa2 ); ?></a>
<?php echo esc_html( $sa3 ); ?>
</p></div>
<?php
}
}
/**
* 'shutdown' action handler to update trigram indexes when orders change.
*
* @return void
*/
public function update_textdex() {
if ( count( $this->orders_to_update ) > 0 ) {
$this->update_meta_keys( array_keys( $this->orders_to_update ) );
$this->textdex->update( array_keys( $this->orders_to_update ) );
}
}
/**
* Filter. immediately before metadata search.
*
* This only works in legacy mode, not HPOS mode.
*
* @param array $search_fields
*
* @return array
*/
public function filter_search_fields( $search_fields ) {
if ( ! $this->textdex->is_ready() ) {
return $search_fields;
}
/* Hook to mung the queries. */
$this->filtering = true;
add_filter( 'query', array( $this, 'standard_query' ), 1 );
return $search_fields;
}
/**
* Filter. Immediately after metadata search.
*
* This only works in legacy mode, not HPOS mode.
*
* @param array $order_ids
* @param string $term
* @param array $search_fields
*
* @return array
*/
public function filter_search_results( $order_ids, $term, $search_fields ) {
if ( $this->filtering ) {
/* Discontinue filtering queries after the metadata search */
remove_filter( 'query', array( $this, 'standard_query' ), 1 );
$this->filtering = false;
}
return $order_ids;
}
/**
* Mung the order metadata search queries to include the trigram selection clause.
*
* This is a MISERABLE hack. There aren't any suitable hooks to do this more elegantly.
*
* @param string $query Database query.
*
* @return string
*/
public function standard_query( $query ) {
if ( ! $this->term ) {
$splits = explode( 'LIKE \'%', $query, 2 );
if ( 2 !== count( $splits ) ) {
return $query;
}
$splits = explode( '%\'', $splits[1] );
if ( 2 !== count( $splits ) ) {
return $query;
}
$this->term = $splits[0];
$this->trigram_clause = $this->textdex->trigram_clause( $this->term );
}
$newQuery = $query;
if ( str_contains( $newQuery, 'woocommerce_order_items as order_item' ) ) {
$newQuery = str_replace( 'WHERE order_item_name LIKE', 'WHERE order_id IN (' . $this->trigram_clause . ') AND order_item_name LIKE', $newQuery );
} else if ( str_contains( $newQuery, 'SELECT DISTINCT os.order_id FROM wp_wc_order_stats os' ) ) {
/* empty, intentionally */
} else {
$newQuery = str_replace( 'postmeta p1 WHERE ', 'postmeta p1 WHERE post_id IN (' . $this->trigram_clause . ') AND ', $newQuery );
}
return $newQuery;
}
/**
* HPOS filter. Fix up the args.
*
* We use this to know we're about to get some queries we need to mung.
*
* @param $args
*
* @return mixed
*/
public function woocommerce_order_query_args( $args ) {
$its_search = array_key_exists( 's', $args ) && is_string( $args['s'] ) && strlen( $args['s'] ) > 0;
$its_order_id_search = array_key_exists( 'search_filter', $args ) && 'order_id' === $args ['search_filter'];
if ( $its_search && ! $its_order_id_search && $this->textdex->is_ready() ) {
$this->term = $args['s'];
$this->trigram_clause = $this->textdex->trigram_clause( $this->term );
/* Hook to mung the queries. */
$this->filtering = true;
add_filter( 'query', array( $this, 'hpos_query' ), 1 );
}
return $args;
}
/**
* HPOS filter. Fix up results.
*
* We use this to know we're done munging queries.
*
* @param $results
* @param $args
*
* @return mixed
*/
public function woocommerce_order_query( $results, $args ) {
if ( $this->filtering ) {
/* Discontinue filtering queries after the search */
remove_filter( 'query', array( $this, 'hpos_query' ), 1 );
$this->filtering = false;
}
return $results;
}
/**
* Mung the order metadata search queries to include the trigram selection clause, in the HPOS queries
*
* This is a MISERABLE hack. There aren't any suitable hooks to do this more elegantly.
*
* Two queries use this. They're the same except the second
* counts the resultset, without the LIMIT clause, of the first,
* which is Oracle MySQL's replacement for SQL_CALC_FOUND_ROWS.
*
* @param string $query Database query.
*
* @return string
*/
public function hpos_query( $query ) {
global $wpdb;
$orders = $wpdb->prefix . 'wc_orders';
$orderitems = $wpdb->prefix . 'woocommerce_order_items';
/* Skip modifying the FULLTEXT search option choice. */
if ( str_contains( $query, 'IN BOOLEAN MODE' ) ) {
return $query;
}
if ( str_contains( $query, "$orderitems AS search_query_items ON search_query_items.order_id = $orders.id WHERE 1=1 AND" ) ||
str_contains( $query, "SELECT $orders.id FROM $orders WHERE 1=1 AND" ) ||
str_contains( $query, "SELECT COUNT(DISTINCT $orders.id) FROM $orders WHERE 1=1 AND" )
) {
return str_replace( 'WHERE 1=1 AND', "WHERE 1=1 AND $orders.id IN (" . $this->trigram_clause . ") AND ", $query );
}
return $query;
}
/** Filter for custom order fields.
*
* Here we implement necessary monkeypatching for the query,
* and a cache for the keys.
*
* https://github.com/woocommerce/woocommerce/issues/47212
*
* @param array|null $keys
* @param Automattic\WooCommerce\Admin\Overrides\Order $order
*
* @return array|null
*/
public function postmeta_get_order_custom_field_names( $keys, $order ) {
if ( ! @is_a( $order, WC_Order::class ) ) {
return $keys;
}
require_once( plugin_dir_path( __FILE__ ) . 'code/class-custom-fields.php' );
$cust = new Custom_Fields();
return $cust->get_order_custom_field_names();
}
private function update_meta_keys( array $orders ) {
$cached_keys = get_transient( FAST_WOO_ORDER_LOOKUP_METAKEY_CACHE );
if ( ! is_array( $cached_keys ) ) {
return;
}
$changed = false;
foreach ( $orders as $order_id ) {
$order = wc_get_order( $order_id );
$metas = wc_get_container()->get( OrdersTableDataStoreMeta::class )->read_meta( $order );
foreach ( $metas as $meta ) {
$meta_key = $meta->meta_key;
if ( ! str_starts_with( $meta_key, '_' ) ) {
if ( ! in_array( $meta_key, $cached_keys ) ) {
$changed = true;
$cached_keys [] = $meta_key;
}
}
}
}
if ( $changed ) {
set_transient( FAST_WOO_ORDER_LOOKUP_METAKEY_CACHE, $cached_keys );
}
}
}
// Plugin name
const FAST_WOO_ORDER_LOOKUP_NAME = 'Fast Woo Order Lookup';
// Plugin version
const FAST_WOO_ORDER_LOOKUP_VERSION = '1.0.1';
// Plugin Root File
const FAST_WOO_ORDER_LOOKUP_PLUGIN_FILE = __FILE__;
// Plugin base
define( 'FAST_WOO_ORDER_LOOKUP_PLUGIN_BASE', plugin_basename( FAST_WOO_ORDER_LOOKUP_PLUGIN_FILE ) );
// Plugin slug
define( 'FAST_WOO_ORDER_LOOKUP_SLUG', explode( DIRECTORY_SEPARATOR, FAST_WOO_ORDER_LOOKUP_PLUGIN_BASE )[0] );
// Plugin Folder Path
define( 'FAST_WOO_ORDER_LOOKUP_PLUGIN_DIR', plugin_dir_path( FAST_WOO_ORDER_LOOKUP_PLUGIN_FILE ) );
// Plugin Folder URL
define( 'FAST_WOO_ORDER_LOOKUP_PLUGIN_URL', plugin_dir_url( FAST_WOO_ORDER_LOOKUP_PLUGIN_FILE ) );
register_activation_hook( __FILE__, 'Fast_Woo_Order_Lookup\activate' );
register_deactivation_hook( __FILE__, 'Fast_Woo_Order_Lookup\deactivate' );
add_action( 'before_woocommerce_init', array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'declare_hpos_compatible' ) );
/* Don't do anything until WooCommerce does ->load( 'order' ). */
add_action( 'woocommerce_order_data_store',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_order_data_store' ) );
/* Hook anything that changes an order object */
add_action( 'woocommerce_new_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_changing_order' ), 10, 2 );
add_action( 'woocommerce_update_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_changing_order' ), 10, 2 );
add_action( 'woocommerce_order_object_updated_props',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_order_object_updated_props' ), 10, 2 );
/* Hook changes to order status. */
add_action( 'woocommerce_delete_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_deleting_order' ) );
add_action( 'woocommerce_trash_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_deleting_order' ) );
add_action( 'woocommerce_untrash_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_deleting_order' ) );
add_action( 'woocommerce_cancelled_order',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'woocommerce_deleting_order' ) );
add_action( 'update_post_meta',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'update_post_meta' ), 10, 4 );
/* ActionScheduler action for loading textdex. */
add_action( 'fast_woo_order_lookup_textdex_action',
array( 'Fast_Woo_Order_Lookup\FastWooOrderLookup', 'textdex_batch_action' ) );
function activate() {
register_uninstall_hook( __FILE__, 'Fast_Woo_Order_Lookup\uninstall' );
require_once( plugin_dir_path( __FILE__ ) . 'code/class-textdex.php' );
$textdex = new Textdex();
$textdex->activate();
$textdex->get_order_id_range();
$textdex->load_textdex();
}
function deactivate() {
require_once( plugin_dir_path( __FILE__ ) . 'code/class-textdex.php' );
$textdex = new Textdex();
$textdex->deactivate();
delete_transient( FAST_WOO_ORDER_LOOKUP_METAKEY_CACHE );
}
function uninstall() {
}