22
33class WC_Custom_Order_Table {
44
5- protected $ table_name = null ;
6-
7- public function setup () {
8- global $ wpdb ;
9-
10- $ this ->table_name = $ wpdb ->prefix . 'woocommerce_orders ' ;
11-
12- add_filter ( 'woocommerce_order_data_store ' , array ( $ this , 'order_data_store ' ) );
13- add_filter ( 'posts_join ' , array ( $ this , 'wp_query_customer_query ' ), 10 , 2 );
14-
15- // Register the CLI command if we're running WP_CLI
16- if (defined ('WP_CLI ' ) && WP_CLI ) {
17- WP_CLI ::add_command ('wc-order-table ' , 'WC_Custom_Order_Table_CLI ' );
18- }
19- }
20-
21- public function get_table_name () {
22- return apply_filters ( 'wc_customer_order_table_name ' , $ this ->table_name );
23- }
24-
25- /**
26- * Init the order data store.
27- *
28- * @return string
29- */
30- public function order_data_store () {
31- return 'WC_Order_Data_Store_Custom_Table ' ;
32- }
33-
34- /**
35- * Filter WP_Query for wc_customer_query
36- *
37- * @return string
38- */
39- public function wp_query_customer_query ( $ join , $ wp_query ) {
40- global $ wpdb ;
41-
42- // If there is no wc_customer_query then no need to process anything
43- if ( ! isset ( $ wp_query ->query_vars ['wc_customer_query ' ] ) ) {
44- return $ join ;
45- }
46-
47- $ customer_query = $ this ->generate_wc_customer_query ( $ wp_query ->query_vars ['wc_customer_query ' ] );
48-
49-
50- $ query_parts = array ();
51-
52- if ( ! empty ( $ customer_query ['emails ' ] ) ) {
53- $ emails = '\'' . implode ( '\', \'' , array_unique ( $ customer_query ['emails ' ] ) ) . '\'' ;
54- $ query_parts [] = "{$ this ->get_table_name ()}.billing_email IN ( {$ emails } ) " ;
55- }
56-
57- if ( ! empty ( $ customer_query ['users ' ] ) ) {
58- $ users = implode ( ', ' , array_unique ( $ customer_query ['users ' ] ) );
59- $ query_parts [] = "{$ this ->get_table_name ()}.customer_id IN ( {$ users } ) " ;
60- }
61-
62- if ( ! empty ( $ query_parts ) ) {
63- $ query = '( ' . implode ( ') OR ( ' , $ query_parts ) . ' ) ' ;
64- $ join .= "
65- JOIN {$ this ->get_table_name ()} ON
66- ( {$ wpdb ->posts }.ID = {$ this ->get_table_name ()}.order_id )
67- AND ( {$ query } ) " ;
68- }
69-
70- return $ join ;
71- }
72-
73- public function generate_wc_customer_query ( $ values ) {
74- $ customer_query ['emails ' ] = array ();
75- $ customer_query ['users ' ] = array ();
76-
77- foreach ( $ values as $ value ) {
78- if ( is_array ( $ value ) ) {
79- $ query = $ this ->generate_wc_customer_query ( $ value );
80-
81- if ( is_array ( $ query ['emails ' ] ) ) {
82- $ customer_query ['emails ' ] = array_merge ( $ customer_query ['emails ' ], $ query ['emails ' ] );
83- }
84-
85- if ( is_array ( $ query ['users ' ] ) ) {
86- $ customer_query ['users ' ] = array_merge ( $ customer_query ['users ' ], $ query ['users ' ] );
87- }
88- } elseif ( is_email ( $ value ) ) {
89- $ customer_query ['emails ' ][] = sanitize_email ( $ value );
90- } else {
91- $ customer_query ['users ' ][] = strval ( absint ( $ value ) );
92- }
93- }
94-
95- return $ customer_query ;
96- }
97- }
5+ /**
6+ * The database table name.
7+ *
8+ * @var string
9+ */
10+ protected $ table_name = null ;
11+
12+ /**
13+ * Steps to run on plugin initialization.
14+ *
15+ * @global $wpdb
16+ */
17+ public function setup () {
18+ global $ wpdb ;
19+
20+ $ this ->table_name = $ wpdb ->prefix . 'woocommerce_orders ' ;
21+
22+ // Inject the plugin into order processing.
23+ add_filter ( 'woocommerce_order_data_store ' , array ( $ this , 'order_data_store ' ) );
24+ add_filter ( 'posts_join ' , array ( $ this , 'wp_query_customer_query ' ), 10 , 2 );
25+
26+ // Register the CLI command if we're running WP_CLI.
27+ if ( defined ( 'WP_CLI ' ) && WP_CLI ) {
28+ WP_CLI ::add_command ( 'wc-order-table ' , 'WC_Custom_Order_Table_CLI ' );
29+ }
30+ }
31+
32+ /**
33+ * Retrieve the WooCommerce order table name.
34+ *
35+ * @return string The database table name.
36+ */
37+ public function get_table_name () {
38+ /**
39+ * Filter the WooCommerce order table name.
40+ *
41+ * @param string $table The WooCommerce orders table name.
42+ */
43+ return apply_filters ( 'wc_customer_order_table_name ' , $ this ->table_name );
44+ }
45+
46+ /**
47+ * Retrieve the class name of the WooCommerce order data store.
48+ *
49+ * @return string The data store class name.
50+ */
51+ public function order_data_store () {
52+ return 'WC_Order_Data_Store_Custom_Table ' ;
53+ }
54+
55+ /**
56+ * Modify posts_join queries when the query includes wc_customer_query.
57+ *
58+ * @global $wpdb
59+ *
60+ * @param string $join The SQL JOIN statement.
61+ * @param WP_Query $query The current WP_Query object.
62+ *
63+ * @return string The [potentially] filtered JOIN statement.
64+ */
65+ public function wp_query_customer_query ( $ join , $ wp_query ) {
66+ global $ wpdb ;
67+
68+ // If there is no wc_customer_query then no need to process anything
69+ if ( ! isset ( $ wp_query ->query_vars ['wc_customer_query ' ] ) ) {
70+ return $ join ;
71+ }
72+
73+ $ customer_query = $ this ->generate_wc_customer_query ( $ wp_query ->query_vars ['wc_customer_query ' ] );
74+ $ query_parts = array ();
75+
76+ if ( ! empty ( $ customer_query ['emails ' ] ) ) {
77+ $ emails = '\'' . implode ( '\', \'' , array_unique ( $ customer_query ['emails ' ] ) ) . '\'' ;
78+ $ query_parts [] = "{$ this ->get_table_name ()}.billing_email IN ( {$ emails } ) " ;
79+ }
80+
81+ if ( ! empty ( $ customer_query ['users ' ] ) ) {
82+ $ users = implode ( ', ' , array_unique ( $ customer_query ['users ' ] ) );
83+ $ query_parts [] = "{$ this ->get_table_name ()}.customer_id IN ( {$ users } ) " ;
84+ }
85+
86+ if ( ! empty ( $ query_parts ) ) {
87+ $ query = '( ' . implode ( ') OR ( ' , $ query_parts ) . ' ) ' ;
88+ $ join .= "JOIN {$ this ->get_table_name ()} ON
89+ ( {$ wpdb ->posts }.ID = {$ this ->get_table_name ()}.order_id )
90+ AND ( {$ query } ) " ;
91+ }
92+
93+ return $ join ;
94+ }
95+
96+ /**
97+ * Given a wc_customer_query argument, construct an array of customers grouped by either email
98+ * address or user ID.
99+ *
100+ * @param array $values Query arguments from WP_Query->query_vars['wc_customer_query'].
101+ *
102+ * @return array A complex array with two keys: "emails" and "users".
103+ */
104+ public function generate_wc_customer_query ( $ values ) {
105+ $ customer_query = array (
106+ 'emails ' => array (),
107+ 'users ' => array (),
108+ );
109+
110+ foreach ( $ values as $ value ) {
111+ // If the value is an array, call this method recursively and merge the results.
112+ if ( is_array ( $ value ) ) {
113+ $ query = $ this ->generate_wc_customer_query ( $ value );
114+
115+ if ( is_array ( $ query ['emails ' ] ) ) {
116+ $ customer_query ['emails ' ] = array_merge ( $ customer_query ['emails ' ], $ query ['emails ' ] );
117+ }
118+
119+ if ( is_array ( $ query ['users ' ] ) ) {
120+ $ customer_query ['users ' ] = array_merge ( $ customer_query ['users ' ], $ query ['users ' ] );
121+ }
122+ } elseif ( is_email ( $ value ) ) {
123+ $ customer_query ['emails ' ][] = sanitize_email ( $ value );
124+
125+ } else {
126+ $ customer_query ['users ' ][] = strval ( absint ( $ value ) );
127+ }
128+ }
129+
130+ return $ customer_query ;
131+ }
132+ }
0 commit comments