1515use Exception ;
1616use Pronamic \WordPress \DateTime \DateTime ;
1717use Pronamic \WordPress \DateTime \DateTimeZone ;
18- use Pronamic \WordPress \Money \TaxedMoney ;
1918use Pronamic \WordPress \Pay \Address ;
20- use Pronamic \WordPress \Pay \Admin \AdminModule ;
21- use Pronamic \WordPress \Pay \ContactName ;
2219use Pronamic \WordPress \Pay \Core \Gateway ;
23- use Pronamic \WordPress \Pay \Core \PaymentMethods ;
2420use Pronamic \WordPress \Pay \Core \Recurring ;
2521use Pronamic \WordPress \Pay \Core \Server ;
2622use Pronamic \WordPress \Pay \Core \Statuses ;
2723use Pronamic \WordPress \Pay \Core \Util ;
2824use Pronamic \WordPress \Pay \Customer ;
29- use Pronamic \WordPress \Pay \Extensions \WooCommerce \DirectDebitIDealGateway ;
30- use Pronamic \WordPress \Pay \Extensions \WooCommerce \WooCommerce ;
3125use Pronamic \WordPress \Pay \Payments \Payment ;
3226use Pronamic \WordPress \Pay \Plugin ;
3327use UnexpectedValueException ;
3428use WP_CLI ;
3529use WP_Error ;
3630use WP_Query ;
37- use WP_User ;
3831
3932/**
4033 * Title: Subscriptions module
@@ -79,8 +72,6 @@ public function __construct( Plugin $plugin ) {
7972
8073 add_action ( 'plugins_loaded ' , array ( $ this , 'maybe_schedule_subscription_payments ' ), 6 );
8174
82- add_action ( 'pronamic_pay_admin_menu ' , array ( $ this , 'admin_menu ' ) );
83-
8475 // Exclude subscription notes.
8576 add_filter ( 'comments_clauses ' , array ( $ this , 'exclude_subscription_comment_notes ' ), 10 , 2 );
8677
@@ -104,249 +95,6 @@ public function __construct( Plugin $plugin ) {
10495 }
10596 }
10697
107- /**
108- * Add pages to the admin menu.
109- */
110- public function admin_menu () {
111- add_submenu_page (
112- null ,
113- __ ( 'Import ' , 'pronamic_ideal ' ),
114- __ ( 'Import ' , 'pronamic_ideal ' ),
115- 'manage_options ' ,
116- 'pronamic_pay_subscriptions_import ' ,
117- array ( $ this , 'page_import ' )
118- );
119- }
120-
121- /**
122- * Page reports.
123- */
124- public function page_import () {
125- global $ pronamic_pay_import_results ;
126-
127- if ( ! isset ( $ this ->plugin ->admin ) ) {
128- return false ;
129- }
130-
131- if ( ! ( $ this ->plugin ->admin instanceof AdminModule ) ) {
132- return false ;
133- }
134-
135- // File upload.
136- $ data = $ this ->maybe_process_import_file_upload ();
137-
138- if ( $ data ) {
139- $ pronamic_pay_import_results = $ this ->import_data ( $ data );
140- }
141-
142- return $ this ->plugin ->admin ->render_page ( 'subscriptions-import ' );
143- }
144-
145- /**
146- * Import data.
147- *
148- * @param array $data Data to import.
149- *
150- * @return array
151- */
152- private function import_data ( array $ data ) {
153- $ result = array ();
154-
155- // Keys.
156- $ keys = array_shift ( $ data );
157-
158- $ user_id_key = array_search ( 'user_id ' , $ keys , true );
159- $ config_id_key = array_search ( 'config_id ' , $ keys , true );
160- $ source_key = array_search ( 'source ' , $ keys , true );
161- $ source_id_key = array_search ( 'source_id ' , $ keys , true );
162- $ mollie_customer_id_key = array_search ( 'mollie_customer_id ' , $ keys , true );
163- $ subscription_source_id_key = array_search ( 'subscription_source_id ' , $ keys , true );
164-
165- // Defaults.
166- $ config_id = get_option ( 'pronamic_pay_config_id ' );
167-
168- // Loop data.
169- foreach ( $ data as $ item ) {
170- // User.
171- $ user = new WP_User ( $ item [ $ user_id_key ] );
172-
173- if ( ! $ user ) {
174- $ result [] = sprintf (
175- /* translators: 1: subscription id, 2: source, 3: source id, 4: user id */
176- __ ( 'User #%1$s does not exist, skipping. ' , 'pronamic_ideal ' ),
177- $ item [ $ user_id_key ]
178- );
179-
180- continue ;
181- }
182-
183- // Customer name.
184- $ name = new ContactName ();
185-
186- $ name ->set_first_name ( $ user ->first_name );
187- $ name ->set_last_name ( $ user ->last_name );
188-
189- if ( empty ( $ user ->first_name ) && empty ( $ user ->last_name ) ) {
190- $ name ->set_first_name ( $ user ->get ( 'display_name ' ) );
191- }
192-
193- // Payment.
194- $ payment = new Payment ();
195- $ subscription = new Subscription ();
196-
197- $ customer = new Customer ();
198- $ customer ->set_name ( $ name );
199- $ customer ->set_user_id ( $ item [ $ user_id_key ] );
200- $ customer ->set_email ( $ user ->get ( 'user_email ' ) );
201-
202- $ payment ->set_config_id ( ( false === $ config_id_key ? $ config_id : $ item [ $ config_id_key ] ) );
203- $ payment ->method = PaymentMethods::DIRECT_DEBIT_IDEAL ;
204- $ payment ->set_source ( $ item [ $ source_key ] );
205- $ payment ->set_source_id ( $ item [ $ source_id_key ] );
206- $ payment ->set_customer ( $ customer );
207- // $payment->email = $customer->get_email();
208-
209- $ subscription_source_id = $ item [ $ subscription_source_id_key ];
210-
211- switch ( $ payment ->get_source () ) {
212- case 'woocommerce ' :
213- $ payment ->subscription_source_id = $ subscription_source_id ;
214- $ subscription ->interval = get_post_meta (
215- $ subscription_source_id ,
216- '_billing_interval ' ,
217- true
218- );
219- $ subscription ->interval_period = Util::to_period (
220- get_post_meta ( $ subscription_source_id , '_billing_period ' , true )
221- );
222-
223- // Start date.
224- $ payment ->date = new DateTime ( get_the_date ( DATE_ATOM , $ subscription_source_id ) );
225-
226- // Description.
227- $ payment ->description = sprintf (
228- 'WooCommerce Subscription #%s ' ,
229- $ subscription_source_id
230- );
231-
232- // Amount.
233- $ subscription ->set_total_amount (
234- new TaxedMoney (
235- get_post_meta ( $ subscription_source_id , '_order_total ' , true ),
236- WooCommerce::get_currency ()
237- )
238- );
239-
240- // Update WooCommerce Subscription payment method.
241- update_post_meta ( $ subscription_source_id , '_payment_method ' , DirectDebitIDealGateway::ID );
242- update_post_meta (
243- $ subscription_source_id ,
244- '_payment_method_title ' ,
245- PaymentMethods::get_name (
246- $ subscription ->payment_method ,
247- __ ( 'Pronamic ' , 'pronamic_ideal ' )
248- )
249- );
250- delete_post_meta ( $ subscription_source_id , '_requires_manual_renewal ' );
251-
252- // Set WooCommerce subscription post parent.
253- wp_update_post (
254- array (
255- 'ID ' => $ subscription_source_id ,
256- 'post_parent ' => $ item [ $ source_id_key ],
257- )
258- );
259-
260- $ payment ->subscription = $ subscription ;
261-
262- break ;
263- default :
264- $ result [] = sprintf (
265- /* translators: %s: source name */
266- __ ( 'Source `%1$s` is unsupported, skipping. ' , 'pronamic_ideal ' ),
267- strval ( $ payment ->get_source () )
268- );
269- }
270-
271- // Check if subscription has been set by source case, otherwise go to next item.
272- if ( empty ( $ payment ->subscription ) ) {
273- continue ;
274- }
275-
276- // Set payment amount.
277- $ payment ->set_total_amount (
278- new TaxedMoney (
279- $ subscription ->get_total_amount ()->get_value (),
280- $ subscription ->get_total_amount ()->get_currency ()
281- )
282- );
283-
284- // Save.
285- $ payment ->set_status ( Statuses::SUCCESS );
286- $ payment ->save ();
287-
288- $ subscription = $ payment ->get_subscription ();
289-
290- // Add user meta Mollie customer ID.
291- if ( ! empty ( $ mollie_customer_id_key ) ) {
292- update_user_meta (
293- $ customer ->get_user_id (),
294- '_pronamic_pay_mollie_customer_id ' ,
295- $ item [ $ mollie_customer_id_key ]
296- );
297-
298- $ subscription ->set_meta ( 'mollie_customer_id ' , $ item [ $ mollie_customer_id_key ] );
299- }
300-
301- $ subscription ->set_status ( Statuses::ACTIVE );
302- $ subscription ->save ();
303-
304- $ result [] = sprintf (
305- /* translators: 1: subscription id, 2: source, 3: source id, 4: user id */
306- __ ( 'Subscription %1$s created for %2$s #%3$s, user #%4$s. ' , 'pronamic_ideal ' ),
307- strval ( $ subscription ->get_id () ),
308- strval ( $ subscription ->get_source () ),
309- strval ( $ subscription ->get_source_id () ),
310- strval ( $ subscription ->user_id )
311- );
312- }
313-
314- $ result [] = __ ( 'Done ' , 'pronamic_ideal ' );
315-
316- return $ result ;
317- }
318-
319- /**
320- * Maybe process import file upload.
321- *
322- * @return bool|string
323- */
324- public function maybe_process_import_file_upload () {
325- $ name = 'pronamic_subscriptions_import_file ' ;
326-
327- if ( ! isset ( $ _FILES [ $ name ] ) || UPLOAD_ERR_OK !== $ _FILES [ $ name ]['error ' ] ) { // WPCS: input var okay.
328- return false ;
329- }
330-
331- if ( ! is_readable ( $ _FILES [ $ name ]['tmp_name ' ] ) ) { // WPCS: input var okay. // WPCS: sanitization ok.
332- return false ;
333- }
334-
335- $ file = file (
336- $ _FILES [ $ name ]['tmp_name ' ],
337- FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
338- ); // WPCS: input var okay. // WPCS: sanitization ok.
339-
340- // Parse the CSV file into an array.
341- $ delimiter = ( false === strpos ( $ file [0 ], ', ' ) ? '; ' : ', ' );
342-
343- $ delimiters = array_fill ( 0 , count ( $ file ), $ delimiter );
344-
345- $ data = array_map ( 'str_getcsv ' , $ file , $ delimiters );
346-
347- return $ data ;
348- }
349-
35098 /**
35199 * Handle subscription actions.
352100 *
0 commit comments