1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CLI |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2020 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Mollie |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Title: CLI |
15
|
|
|
* Description: |
16
|
|
|
* Copyright: 2005-2020 Pronamic |
17
|
|
|
* Company: Pronamic |
18
|
|
|
* |
19
|
|
|
* @author Remco Tolsma |
20
|
|
|
* @version 2.1.0 |
21
|
|
|
* @since 2.1.0 |
22
|
|
|
* @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-cli.php |
23
|
|
|
*/ |
24
|
|
|
class CLI { |
25
|
|
|
/** |
26
|
|
|
* Construct CLI. |
27
|
|
|
*/ |
28
|
|
|
public function __construct() { |
29
|
|
|
\WP_CLI::add_command( |
|
|
|
|
30
|
|
|
'pronamic-pay mollie organizations synchronize', |
31
|
|
|
function( $args, $assoc_args ) { |
32
|
|
|
$this->wp_cli_organizations_synchronize( $args, $assoc_args ); |
33
|
|
|
}, |
34
|
|
|
array( |
35
|
|
|
'shortdesc' => 'Synchronize Mollie organizations to WordPress (not implemented yet).', |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
\WP_CLI::add_command( |
40
|
|
|
'pronamic-pay mollie customers synchronize', |
41
|
|
|
function( $args, $assoc_args ) { |
42
|
|
|
$this->wp_cli_customers_synchronize( $args, $assoc_args ); |
43
|
|
|
}, |
44
|
|
|
array( |
45
|
|
|
'shortdesc' => 'Synchronize Mollie customers to WordPress.', |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
\WP_CLI::add_command( |
50
|
|
|
'pronamic-pay mollie customers connect-wp-users', |
51
|
|
|
function( $args, $assoc_args ) { |
52
|
|
|
$this->wp_cli_customers_connect_wp_users( $args, $assoc_args ); |
53
|
|
|
}, |
54
|
|
|
array( |
55
|
|
|
'shortdesc' => 'Connect Mollie customers to WordPress users by email.', |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
// Data Stores. |
60
|
|
|
$this->profile_data_store = new ProfileDataStore(); |
|
|
|
|
61
|
|
|
$this->customer_data_store = new CustomerDataStore(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* CLI organizations synchronize. |
66
|
|
|
* |
67
|
|
|
* @link https://docs.mollie.com/reference/v2/organizations-api/current-organization |
68
|
|
|
* @param array<string> $args Arguments. |
69
|
|
|
* @param array<string> $assoc_args Associative arguments. |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function wp_cli_organizations_synchronize( $args, $assoc_args ) { |
|
|
|
|
73
|
|
|
\WP_CLI::error( 'Command not implemented yet.' ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* CLI customers synchronize. |
78
|
|
|
* |
79
|
|
|
* @link https://docs.mollie.com/reference/v2/customers-api/list-customers |
80
|
|
|
* @param array<string> $args Arguments. |
81
|
|
|
* @param array<string> $assoc_args Associative arguments. |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function wp_cli_customers_synchronize( $args, $assoc_args ) { |
|
|
|
|
85
|
|
|
global $post; |
86
|
|
|
global $wpdb; |
87
|
|
|
|
88
|
|
|
$query = new \WP_Query( |
89
|
|
|
array( |
90
|
|
|
'post_type' => 'pronamic_gateway', |
91
|
|
|
'post_status' => 'publish', |
92
|
|
|
'nopaging' => true, |
93
|
|
|
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Slow query allowed on CLI. |
94
|
|
|
'meta_query' => array( |
95
|
|
|
array( |
96
|
|
|
'key' => '_pronamic_gateway_id', |
97
|
|
|
'value' => 'mollie', |
98
|
|
|
), |
99
|
|
|
array( |
100
|
|
|
'key' => '_pronamic_gateway_mollie_api_key', |
101
|
|
|
'compare' => 'EXISTS', |
102
|
|
|
), |
103
|
|
|
), |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
if ( $query->have_posts() ) { |
108
|
|
|
while ( $query->have_posts() ) { |
109
|
|
|
$query->the_post(); |
110
|
|
|
|
111
|
|
|
$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true ); |
112
|
|
|
|
113
|
|
|
\WP_CLI::log( $post->post_title ); |
114
|
|
|
\WP_CLI::log( $api_key ); |
115
|
|
|
\WP_CLI::log( '' ); |
116
|
|
|
|
117
|
|
|
$client = new Client( $api_key ); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$urls = array( |
120
|
|
|
'https://api.mollie.com/v2/customers?limit=250', |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$profile = Profile::from_object( $client->get_current_profile() ); |
124
|
|
|
|
125
|
|
|
$profile_id = $this->profile_data_store->save_profile( |
126
|
|
|
$profile, |
127
|
|
|
array( |
128
|
|
|
'api_key_live' => ( 'live_' === substr( $api_key, 0, 5 ) ) ? $api_key : null, |
|
|
|
|
129
|
|
|
'api_key_test' => ( 'test_' === substr( $api_key, 0, 5 ) ) ? $api_key : null, |
130
|
|
|
), |
131
|
|
|
array( |
132
|
|
|
'api_key_live' => '%s', |
133
|
|
|
'api_key_test' => '%s', |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
while ( ! empty( $urls ) ) { |
138
|
|
|
$url = array_shift( $urls ); |
139
|
|
|
|
140
|
|
|
\WP_CLI::log( $url ); |
141
|
|
|
|
142
|
|
|
$response = $client->send_request( $url ); |
143
|
|
|
|
144
|
|
|
if ( isset( $response->count ) ) { |
145
|
|
|
\WP_CLI::log( |
146
|
|
|
\sprintf( |
147
|
|
|
'Found %d customer(s).', |
148
|
|
|
$response->count |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( isset( $response->_embedded->customers ) ) { |
154
|
|
|
\WP_CLI\Utils\format_items( |
|
|
|
|
155
|
|
|
'table', |
156
|
|
|
$response->_embedded->customers, |
157
|
|
|
array( |
158
|
|
|
'id', |
159
|
|
|
'mode', |
160
|
|
|
'name', |
161
|
|
|
'email', |
162
|
|
|
'locale', |
163
|
|
|
) |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
foreach ( $response->_embedded->customers as $object ) { |
167
|
|
|
$customer = Customer::from_object( $object ); |
168
|
|
|
|
169
|
|
|
$customer_id = $this->customer_data_store->save_customer( |
|
|
|
|
170
|
|
|
$customer, |
171
|
|
|
array( |
172
|
|
|
'profile_id' => $profile_id, |
173
|
|
|
), |
174
|
|
|
array( |
175
|
|
|
'profile_id' => '%d', |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ( isset( $response->_links->next->href ) ) { |
182
|
|
|
$urls[] = $response->_links->next->href; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
\wp_reset_postdata(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* CLI connect Mollie customers to WordPress users. |
193
|
|
|
* |
194
|
|
|
* @link https://docs.mollie.com/reference/v2/customers-api/list-customers |
195
|
|
|
* @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/ |
196
|
|
|
* @link https://developer.wordpress.org/reference/classes/wpdb/query/ |
197
|
|
|
* @param array<string> $args Arguments. |
198
|
|
|
* @param array<string> $assoc_args Associative arguments. |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) { |
|
|
|
|
202
|
|
|
global $wpdb; |
203
|
|
|
|
204
|
|
|
$query = " |
205
|
|
|
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( |
206
|
|
|
customer_id, |
207
|
|
|
user_id |
208
|
|
|
) |
209
|
|
|
SELECT |
210
|
|
|
mollie_customer.id AS mollie_customer_id, |
211
|
|
|
wp_user.ID AS wp_user_id |
212
|
|
|
FROM |
213
|
|
|
$wpdb->pronamic_pay_mollie_customers AS mollie_customer |
214
|
|
|
INNER JOIN |
215
|
|
|
$wpdb->users AS wp_user |
216
|
|
|
ON mollie_customer.email = wp_user.user_email |
217
|
|
|
; |
218
|
|
|
"; |
219
|
|
|
|
220
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepare is OK. |
221
|
|
|
$result = $wpdb->query( $query ); |
222
|
|
|
|
223
|
|
|
if ( false === $result ) { |
224
|
|
|
\WP_CLI::error( |
225
|
|
|
sprintf( |
226
|
|
|
'Database error: %s.', |
227
|
|
|
$wpdb->last_error |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
\WP_CLI::log( |
233
|
|
|
sprintf( |
234
|
|
|
'Connected %d users and Mollie customers.', |
235
|
|
|
$result |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths