ActiveSubscriber.php
1.12 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
<?php
namespace ACA\WC\Search\UserSubscription;
use ACP;
use ACP\Search\Operators;
use ACP\Search\Query\Bindings;
use ACP\Search\Value;
class ActiveSubscriber extends ACP\Search\Comparison {
public function __construct() {
$operators = new Operators( [
Operators::IS_EMPTY,
Operators::NOT_IS_EMPTY,
] );
parent::__construct( $operators );
}
protected function create_query_bindings( $operator, Value $value ) {
global $wpdb;
$ids = implode( ',', $this->get_active_subscribers_ids() );
if ( Operators::NOT_IS_EMPTY === $operator ) {
$where = $wpdb->users . '.ID IN( ' . $ids . ' )';
} else {
$where = $wpdb->users . '.ID NOT IN( ' . $ids . ' )';
}
$bindings = new Bindings();
$bindings->where( $where );
return $bindings;
}
private function get_active_subscribers_ids() {
global $wpdb;
return $wpdb->get_col( "
SELECT DISTINCT pm.meta_value
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_subscription'
AND p.post_status = 'wc-active'
AND pm.meta_key = '_customer_user'
" );
}
}