Test Failed
Push — master ( c533c5...05ca04 )
by Devin
08:10 queued 14s
created

Give_Export_Donations_CSV::set_properties()   D

Complexity

Conditions 10
Paths 384

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 12
nc 384
nop 1
dl 0
loc 23
rs 4.0396
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Payments Export Class.
4
 *
5
 * This class handles payment export in batches.
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/Reports
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.1
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Give_Export_Donations_CSV Class
21
 *
22
 * @since 2.1
23
 */
24
class Give_Export_Donations_CSV extends Give_Batch_Export {
25
26
	/**
27
	 * Our export type. Used for export-type specific filters/actions.
28
	 *
29
	 * @since 2.1
30
	 *
31
	 * @var string
32
	 */
33
	public $export_type = 'payments';
34
35
	/**
36
	 * Form submission data.
37
	 *
38
	 * @since 2.1
39
	 *
40
	 * @var array
41
	 */
42
	public $data = array();
43
44
	/**
45
	 * Form submission data.
46
	 *
47
	 * @since 2.1
48
	 *
49
	 * @var array
50
	 */
51
	public $cols = array();
52
53
	/**
54
	 * Form ID.
55
	 *
56
	 * @since 2.1
57
	 *
58
	 * @var string
59
	 */
60
	public $form_id = '';
61
62
	/**
63
	 * Form tags ids.
64
	 *
65
	 * @since 2.1
66
	 *
67
	 * @var array
68
	 */
69
	public $tags = '';
70
71
72
	/**
73
	 * Form categories ids.
74
	 *
75
	 * @since 2.1
76
	 *
77
	 * @var array
78
	 */
79
	public $categories = '';
80
81
	/**
82
	 * Set the properties specific to the export.
83
	 *
84
	 * @since 2.1
85
	 *
86
	 * @param array $request The Form Data passed into the batch processing.
87
	 */
88
	public function set_properties( $request ) {
89
90
		// Set data from form submission
91
		if ( isset( $_POST['form'] ) ) {
92
			$this->data = give_clean( wp_parse_args( $_POST['form'] ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like give_clean(wp_parse_args($_POST['form'])) can also be of type string. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
93
		}
94
95
		$this->form       = $this->data['forms'];
96
		$this->categories = ! empty( $request['give_forms_categories'] ) ? (array) $request['give_forms_categories'] : array();
97
		$this->tags       = ! empty( $request['give_forms_tags'] ) ? (array) $request['give_forms_tags'] : array();
98
		$this->form_id    = $this->get_form_ids( $request );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->get_form_ids($request) can also be of type array or boolean. However, the property $form_id is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
99
		$this->price_id   = isset( $request['give_price_option'] ) && ( 'all' !== $request['give_price_option'] && '' !== $request['give_price_option'] ) ? absint( $request['give_price_option'] ) : null;
100
		$this->start      = isset( $request['start'] ) ? sanitize_text_field( $request['start'] ) : '';
101
		$this->end        = isset( $request['end'] ) ? sanitize_text_field( $request['end'] ) : '';
102
		$this->status     = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'complete';
103
104
		/**
105
		 * Hook to use after setting properties.
106
		 *
107
		 * @since 2.1.3
108
		 */
109
		do_action( 'give_export_donations_form_data', $this->data );
110
	}
111
112
	/**
113
	 * Get donation form id list
114
	 *
115
	 * @since 2.1
116
	 *
117
	 * @param array $request form data that need to be exported
118
	 *
119
	 * @return array|boolean|null $form get all the donation id that need to be exported
120
	 */
121
	public function get_form_ids( $request = array() ) {
122
		$form = ! empty( $request['forms'] ) && 0 !== $request['forms'] ? absint( $request['forms'] ) : null;
123
124
		$form_ids = ! empty( $request['form_ids'] ) ? sanitize_text_field( $request['form_ids'] ) : null;
125
126
		if ( empty( $form ) && ! empty( $form_ids ) && ( ! empty( $this->categories ) || ! empty( $this->tags ) ) ) {
127
			$form = explode( ',', $form_ids );
128
		}
129
130
		return $form;
131
	}
132
133
	/**
134
	 * Set the CSV columns.
135
	 *
136
	 * @access public
137
	 *
138
	 * @since  2.1
139
	 *
140
	 * @return array|bool $cols All the columns.
141
	 */
142
	public function csv_cols() {
143
144
		$columns = isset( $this->data['give_give_donations_export_option'] ) ? $this->data['give_give_donations_export_option'] : array();
145
146
		// We need columns.
147
		if ( empty( $columns ) ) {
148
			return false;
149
		}
150
151
		$this->cols = $this->get_cols( $columns );
152
153
		return $this->cols;
154
	}
155
156
157
	/**
158
	 * CSV file columns.
159
	 *
160
	 * @since  2.1
161
	 *
162
	 * @param array $columns
163
	 *
164
	 * @return array
165
	 */
166
	private function get_cols( $columns ) {
167
168
		$cols = array();
169
170
		foreach ( $columns as $key => $value ) {
171
172
			switch ( $key ) {
173
				case 'donation_id' :
174
					$cols['donation_id'] = __( 'Donation ID', 'give' );
175
					break;
176
				case 'seq_id' :
177
					$cols['seq_id'] = __( 'Donation Number', 'give' );
178
					break;
179
				case 'first_name' :
180
					$cols['first_name'] = __( 'First Name', 'give' );
181
					break;
182
				case 'last_name' :
183
					$cols['last_name'] = __( 'Last Name', 'give' );
184
					break;
185
				case 'email' :
186
					$cols['email'] = __( 'Email Address', 'give' );
187
					break;
188
				case 'company' :
189
					$cols['company'] = __( 'Company Name', 'give' );
190
					break;
191 View Code Duplication
				case 'address' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
					$cols['address_line1']   = __( 'Address 1', 'give' );
193
					$cols['address_line2']   = __( 'Address 2', 'give' );
194
					$cols['address_city']    = __( 'City', 'give' );
195
					$cols['address_state']   = __( 'State', 'give' );
196
					$cols['address_zip']     = __( 'Zip', 'give' );
197
					$cols['address_country'] = __( 'Country', 'give' );
198
					break;
199
				case 'donation_total' :
200
					$cols['donation_total'] = __( 'Donation Total', 'give' );
201
					break;
202
				case 'currency_code' :
203
					$cols['currency_code'] = __( 'Currency Code', 'give' );
204
					break;
205
				case 'currency_symbol' :
206
					$cols['currency_symbol'] = __( 'Currency Symbol', 'give' );
207
					break;
208
				case 'donation_status' :
209
					$cols['donation_status'] = __( 'Donation Status', 'give' );
210
					break;
211
				case 'payment_gateway' :
212
					$cols['payment_gateway'] = __( 'Payment Gateway', 'give' );
213
					break;
214
				case 'form_id' :
215
					$cols['form_id'] = __( 'Form ID', 'give' );
216
					break;
217
				case 'form_title' :
218
					$cols['form_title'] = __( 'Form Title', 'give' );
219
					break;
220
				case 'form_level_id' :
221
					$cols['form_level_id'] = __( 'Level ID', 'give' );
222
					break;
223
				case 'form_level_title' :
224
					$cols['form_level_title'] = __( 'Level Title', 'give' );
225
					break;
226
				case 'donation_date' :
227
					$cols['donation_date'] = __( 'Donation Date', 'give' );
228
					break;
229
				case 'donation_time' :
230
					$cols['donation_time'] = __( 'Donation Time', 'give' );
231
					break;
232
				case 'userid' :
233
					$cols['userid'] = __( 'User ID', 'give' );
234
					break;
235
				case 'donorid' :
236
					$cols['donorid'] = __( 'Donor ID', 'give' );
237
					break;
238
				case 'donor_ip' :
239
					$cols['donor_ip'] = __( 'Donor IP Address', 'give' );
240
					break;
241
				default:
242
					$cols[ $key ] = $key;
243
244
			}
245
		}
246
247
		/**
248
		 * Filter to get columns name when exporting donation
249
		 *
250
		 * @since 2.1
251
		 *
252
		 * @param array $cols columns name for CSV
253
		 * @param array $columns columns select by admin to export
254
		 */
255
		return (array) apply_filters( 'give_export_donation_get_columns_name', $cols, $columns );
256
	}
257
258
	/**
259
	 * Get the donation argument
260
	 *
261
	 * @since 2.1
262
	 *
263
	 * @param array $args donation argument
264
	 *
265
	 * @return array $args donation argument
266
	 */
267
	public function get_donation_argument( $args = array() ) {
268
		$defaults = array(
269
			'number' => 30,
270
			'page'   => $this->step,
271
			'status' => $this->status,
272
		);
273
		// Date query.
274
		if ( ! empty( $this->start ) || ! empty( $this->end ) ) {
275
			if ( ! empty( $this->start ) ) {
276
				$defaults['date_query'][0]['after'] = date( 'Y-n-d 00:00:00', strtotime( $this->start ) );
277
			}
278
			if ( ! empty( $this->end ) ) {
279
				$defaults['date_query'][0]['before'] = date( 'Y-n-d 00:00:00', strtotime( $this->end ) );
280
			}
281
		}
282
283
		if ( ! empty( $this->form_id ) ) {
284
			$defaults['give_forms'] = is_array( $this->form_id ) ? $this->form_id : array( $this->form_id );
285
		}
286
287
		/**
288
		 * Filter to modify Payment Query arguments for exporting
289
		 * donations.
290
		 *
291
		 * @since 2.1.3
292
		 */
293
		return apply_filters( 'give_export_donations_donation_query_args', wp_parse_args( $args, $defaults ) );
294
	}
295
296
	/**
297
	 * Get the Export Data.
298
	 *
299
	 * @access public
300
	 *
301
	 * @since  2.1
302
	 *
303
	 * @global object $wpdb Used to query the database using the WordPress database API.
304
	 *
305
	 * @return array $data The data for the CSV file.
306
	 */
307
	public function get_data() {
308
309
		$data = array();
310
		$i    = 0;
311
		// Payment query.
312
		$payments = give_get_payments( $this->get_donation_argument() );
313
314
		if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
315
316
			foreach ( $payments as $payment ) {
317
318
				$columns      = $this->csv_cols();
319
				$payment      = new Give_Payment( $payment->ID );
320
				$payment_meta = $payment->payment_meta;
321
				$address      = $payment->address;
322
323
				// Set columns
324
				if ( ! empty( $columns['donation_id'] ) ) {
325
					$data[ $i ]['donation_id'] = $payment->ID;
326
				}
327
328
				if ( ! empty( $columns['seq_id'] ) ) {
329
					$data[ $i ]['seq_id'] = Give()->seq_donation_number->get_serial_code( $payment->ID );
330
				}
331
332 View Code Duplication
				if ( ! empty( $columns['first_name'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
333
					$data[ $i ]['first_name'] = isset( $payment->first_name ) ? $payment->first_name : '';
334
				}
335
336 View Code Duplication
				if ( ! empty( $columns['last_name'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
337
					$data[ $i ]['last_name'] = isset( $payment->last_name ) ? $payment->last_name : '';
338
				}
339
340
				if ( ! empty( $columns['email'] ) ) {
341
					$data[ $i ]['email'] = $payment->email;
342
				}
343
344
				if ( ! empty( $columns['company'] ) ) {
345
					$data[ $i ]['company'] = empty( $payment_meta['_give_donation_company'] ) ? '' : str_replace( "\'", "'", $payment_meta['_give_donation_company'] );
346
				}
347
348 View Code Duplication
				if ( ! empty( $columns['address_line1'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
349
					$data[ $i ]['address_line1']   = isset( $address['line1'] ) ? $address['line1'] : '';
350
					$data[ $i ]['address_line2']   = isset( $address['line2'] ) ? $address['line2'] : '';
351
					$data[ $i ]['address_city']    = isset( $address['city'] ) ? $address['city'] : '';
352
					$data[ $i ]['address_state']   = isset( $address['state'] ) ? $address['state'] : '';
353
					$data[ $i ]['address_zip']     = isset( $address['zip'] ) ? $address['zip'] : '';
354
					$data[ $i ]['address_country'] = isset( $address['country'] ) ? $address['country'] : '';
355
				}
356
357
				if ( ! empty( $columns['donation_total'] ) ) {
358
					$data[ $i ]['donation_total'] = give_format_amount( give_donation_amount( $payment->ID ) );
359
				}
360
361
				if ( ! empty( $columns['currency_code'] ) ) {
362
					$data[ $i ]['currency_code'] = empty( $payment_meta['_give_payment_currency'] ) ? give_get_currency() : $payment_meta['_give_payment_currency'];
363
				}
364
365
				if ( ! empty( $columns['currency_symbol'] ) ) {
366
					$currency_code = $data[ $i ]['currency_code'];
367
					$data[ $i ]['currency_symbol'] =  give_currency_symbol( $currency_code, true );
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
368
				}
369
370
				if ( ! empty( $columns['donation_status'] ) ) {
371
					$data[ $i ]['donation_status'] = give_get_payment_status( $payment, true );
372
				}
373
374
				if ( ! empty( $columns['payment_gateway'] ) ) {
375
					$data[ $i ]['payment_gateway'] = $payment->gateway;
376
				}
377
378
				if ( ! empty( $columns['form_id'] ) ) {
379
					$data[ $i ]['form_id'] = $payment->form_id;
380
				}
381
382
				if ( ! empty( $columns['form_title'] ) ) {
383
					$data[ $i ]['form_title'] = get_the_title( $payment->form_id );
384
				}
385
386
				if ( ! empty( $columns['form_level_id'] ) ) {
387
					$data[ $i ]['form_level_id'] = $payment->price_id;
388
				}
389
390
				if ( ! empty( $columns['form_level_title'] ) ) {
391
					$var_prices = give_has_variable_prices( $payment_meta['form_id'] );
392
					if ( empty( $var_prices ) ) {
393
						$data[ $i ]['form_level_title'] = '';
394
					} else {
395
						$prices_atts = '';
396 View Code Duplication
						if ( $variable_prices = give_get_variable_prices( $payment_meta['form_id'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
397
							foreach ( $variable_prices as $variable_price ) {
398
								$prices_atts[ $variable_price['_give_id']['level_id'] ] = give_format_amount( $variable_price['_give_amount'] );
399
							}
400
						}
401
						$data[ $i ]['form_level_title'] = give_get_price_option_name( $payment->form_id, $payment->price_id );
402
					}
403
				}
404
405
				if ( ! empty( $columns['donation_date'] ) ) {
406
					$payment_date                = strtotime( $payment->date );
407
					$data[ $i ]['donation_date'] = date( give_date_format(), $payment_date );
408
				}
409
410
				if ( ! empty( $columns['donation_time'] ) ) {
411
					$payment_date                = strtotime( $payment->date );
412
					$data[ $i ]['donation_time'] = date_i18n( 'H', $payment_date ) . ':' . date( 'i', $payment_date );
413
				}
414
415
				if ( ! empty( $columns['userid'] ) ) {
416
					$data[ $i ]['userid'] = $payment->user_id;
417
				}
418
419
				if ( ! empty( $columns['donorid'] ) ) {
420
					$data[ $i ]['donorid'] = $payment->customer_id;
421
				}
422
423
				if ( ! empty( $columns['donor_ip'] ) ) {
424
					$data[ $i ]['donor_ip'] = give_get_payment_user_ip( $payment->ID );
425
				}
426
427
				// Add custom field data.
428
				// First we remove the standard included keys from above.
429
				$remove_keys = array(
430
					'donation_id',
431
					'seq_id',
432
					'first_name',
433
					'last_name',
434
					'email',
435
					'address_line1',
436
					'address_line2',
437
					'address_city',
438
					'address_state',
439
					'address_zip',
440
					'address_country',
441
					'donation_total',
442
					'payment_gateway',
443
					'form_id',
444
					'form_title',
445
					'form_level_id',
446
					'form_level_title',
447
					'donation_date',
448
					'donation_time',
449
					'userid',
450
					'donorid',
451
					'donor_ip',
452
				);
453
454
				// Removing above keys...
455
				foreach ( $remove_keys as $key ) {
456
					unset( $columns[ $key ] );
457
				}
458
459
				// Now loop through remaining meta fields.
460
				foreach ( $columns as $col ) {
0 ignored issues
show
Bug introduced by
The expression $columns of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
461
					$field_data         = get_post_meta( $payment->ID, $col, true );
462
					$data[ $i ][ $col ] = $field_data;
463
					unset( $columns[ $col ] );
464
				}
465
466
				/**
467
				 * Filter to modify Donation CSV data when exporting donation
468
				 *
469
				 * @since 2.1
470
				 *
471
				 * @param array Donation data
472
				 * @param Give_Payment $payment Instance of Give_Payment
473
				 * @param array $columns Donation data $columns that are not being merge
474
				 * @param Give_Export_Donations_CSV $this Instance of Give_Export_Donations_CSV
475
				 *
476
				 * @return array Donation data
477
				 */
478
				$data[ $i ] = apply_filters( 'give_export_donation_data', $data[ $i ], $payment, $columns, $this );
479
480
				$new_data = array();
481
				$old_data = $data[ $i ];
482
483
				// sorting the columns bas on row
484
				foreach ( $this->csv_cols() as $key => $value ) {
0 ignored issues
show
Bug introduced by
The expression $this->csv_cols() of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
485
					if ( array_key_exists( $key, $old_data ) ) {
486
						$new_data[ $key ] = $old_data[ $key ];
487
					}
488
				}
489
490
				$data[ $i ] = $new_data;
491
492
				// Increment iterator.
493
				$i ++;
494
495
			}
496
497
			$data = apply_filters( 'give_export_get_data', $data );
498
			$data = apply_filters( "give_export_get_data_{$this->export_type}", $data );
499
500
			return $data;
501
502
		}
503
504
		return array();
505
506
	}
507
508
	/**
509
	 * Return the calculated completion percentage.
510
	 *
511
	 * @since 2.1
512
	 *
513
	 * @return int
514
	 */
515
	public function get_percentage_complete() {
516
		$args = $this->get_donation_argument( array( 'number' => - 1 ) );
517
		if ( isset( $args['page'] ) ) {
518
			unset( $args['page'] );
519
		}
520
		$query      = give_get_payments( $args );
521
		$total      = count( $query );
522
		$percentage = 100;
523
		if ( $total > 0 ) {
524
			$percentage = ( ( 30 * $this->step ) / $total ) * 100;
525
		}
526
		if ( $percentage > 100 ) {
527
			$percentage = 100;
528
		}
529
530
		return $percentage;
531
	}
532
533
	/**
534
	 * Print the CSV rows for the current step.
535
	 *
536
	 * @access public
537
	 *
538
	 * @since  2.1
539
	 *
540
	 * @return string|false
541
	 */
542 View Code Duplication
	public function print_csv_rows() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
543
544
		$row_data = '';
545
		$data     = $this->get_data();
546
		$cols     = $this->get_csv_cols();
547
548
		if ( $data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
549
550
			// Output each row
551
			foreach ( $data as $row ) {
552
				$i = 1;
553
				foreach ( $row as $col_id => $column ) {
554
					// Make sure the column is valid
555
					if ( array_key_exists( $col_id, $cols ) ) {
556
						$row_data .= '"' . preg_replace( '/"/', "'", $column ) . '"';
557
						$row_data .= $i == count( $cols ) ? '' : ',';
558
						$i ++;
559
					}
560
				}
561
				$row_data .= "\r\n";
562
			}
563
564
			$this->stash_step_data( $row_data );
565
566
			return $row_data;
567
		}
568
569
		return false;
570
	}
571
}