WPInv_Privacy_Exporters::process_subscription()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 41
rs 9.584
1
<?php
2
/**
3
 * Personal data exporters.
4
 */
5
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * WPInv_Privacy_Exporters Class.
10
 */
11
class WPInv_Privacy_Exporters {
12
    /**
13
     * Finds and exports customer data by email address.
14
     *
15
     * @since 1.0.13
16
     * @param string $email_address The user email address.
17
     * @param int    $page  Page.
18
     * @return array An array of invoice data in name value pairs
19
     */
20
    public static function customer_invoice_data_exporter( $email_address, $page ) {
21
        $done           = false;
22
        $page           = (int) $page;
23
        $data_to_export = array();
24
25
        $user           = get_user_by( 'email', $email_address );
26
        if ( ! $user instanceof WP_User ) {
27
            return array(
28
                'data' => $data_to_export,
29
                'done' => true,
30
            );
31
        }
32
33
        $args    = array(
34
            'limit'    => get_option( 'posts_per_page' ),
35
            'page'     => $page,
36
            'user'     => $user->ID,
37
            'paginate' => false,
38
        );
39
40
        $invoices = wpinv_get_invoices( $args );
41
42
        if ( 0 < count( $invoices ) ) {
43
            foreach ( $invoices as $invoice ) {
44
                $data_to_export[] = array(
45
                    'group_id'          => 'customer_invoices',
46
                    'group_label'       => __( 'GetPaid: Invoices', 'invoicing' ),
47
                    'group_description' => __( 'Customer invoices.', 'invoicing' ),
48
                    'item_id'           => "wpinv-{$invoice->get_id()}",
49
                    'data'              => self::get_customer_invoice_data( $invoice ),
50
                );
51
            }
52
            $done = get_option( 'posts_per_page' ) > count( $invoices );
53
        } else {
54
            $done = true;
55
        }
56
57
        return array(
58
            'data' => $data_to_export,
59
            'done' => $done,
60
        );
61
    }
62
63
    /**
64
     * Get invoice data (key/value pairs) for a user.
65
     *
66
     * @since 1.0.13
67
     * @param WPInv_Invoice $invoice invoice object.
68
     * @return array
69
     */
70
    public static function get_customer_invoice_data( $invoice ) {
71
72
        // Prepare basic properties.
73
        $props_to_export = array(
74
            'number'       => array(
75
                'name'  => __( 'Invoice Number', 'invoicing' ),
76
                'value' => $invoice->get_number(),
77
            ),
78
            'created_date' => array(
79
                'name'  => __( 'Created Date', 'invoicing' ),
80
                'value' => $invoice->get_date_created(),
81
            ),
82
            'due_date'     => array(
83
                'name'  => __( 'Due Date', 'invoicing' ),
84
                'value' => $invoice->get_due_date(),
85
            ),
86
            'items'        => array(
87
                'name'  => __( 'Invoice Items', 'invoicing' ),
88
                'value' => self::process_invoice_items( $invoice ),
89
            ),
90
            'discount'     => array(
91
                'name'  => __( 'Invoice Discount', 'invoicing' ),
92
                'value' => wpinv_price( $invoice->get_total_discount(), $invoice->get_currency() ),
93
            ),
94
            'total'        => array(
95
                'name'  => __( 'Invoice Total', 'invoicing' ),
96
                'value' => wpinv_price( $invoice->get_total(), $invoice->get_currency() ),
97
            ),
98
            'status'       => array(
99
                'name'  => __( 'Invoice Status', 'invoicing' ),
100
                'value' => $invoice->get_status_nicename(),
101
            ),
102
            'first_name'   => array(
103
                'name'  => __( 'First Name', 'invoicing' ),
104
                'value' => $invoice->get_first_name(),
105
            ),
106
            'last_name'    => array(
107
                'name'  => __( 'Last Name', 'invoicing' ),
108
                'value' => $invoice->get_last_name(),
109
            ),
110
            'email'        => array(
111
                'name'  => __( 'Email Address', 'invoicing' ),
112
                'value' => $invoice->get_email(),
113
            ),
114
            'company'      => array(
115
                'name'  => __( 'Company', 'invoicing' ),
116
                'value' => $invoice->get_company(),
117
            ),
118
            'phone'        => array(
119
                'name'  => __( 'Phone Number', 'invoicing' ),
120
                'value' => $invoice->get_phone(),
121
            ),
122
            'address'      => array(
123
                'name'  => __( 'Address', 'invoicing' ),
124
                'value' => $invoice->get_address(),
125
            ),
126
            'city'         => array(
127
                'name'  => __( 'City', 'invoicing' ),
128
                'value' => $invoice->get_city(),
129
            ),
130
            'state'        => array(
131
                'name'  => __( 'State', 'invoicing' ),
132
                'value' => $invoice->get_state(),
133
            ),
134
            'zip'          => array(
135
                'name'  => __( 'Zip', 'invoicing' ),
136
                'value' => $invoice->get_zip(),
137
            ),
138
            'vat_number'   => array(
139
                'name'  => __( 'VAT Number', 'invoicing' ),
140
                'value' => $invoice->get_vat_number(),
141
            ),
142
            'description'  => array(
143
                'name'  => __( 'Description', 'invoicing' ),
144
                'value' => $invoice->get_description(),
145
            ),
146
        );
147
148
        // In case the invoice is paid, add the payment date and gateway.
149
        if ( $invoice->is_paid() ) {
150
151
            $props_to_export['completed_date'] = array(
152
                'name'  => __( 'Completed Date', 'invoicing' ),
153
                'value' => $invoice->get_completed_date(),
154
            );
155
156
            $props_to_export['gateway'] = array(
157
                'name'  => __( 'Paid Via', 'invoicing' ),
158
                'value' => $invoice->get_gateway(),
159
            );
160
161
        }
162
163
        // Maybe add subscription details.
164
        $props_to_export = self::process_subscription( $invoice, $props_to_export );
165
166
        // Add the ip address.
167
        $props_to_export['ip'] = array(
168
            'name'  => __( 'IP Address', 'invoicing' ),
169
            'value' => $invoice->get_ip(),
170
        );
171
172
        // Add the invoice url.
173
        $props_to_export['view_url'] = array(
174
            'name'  => __( 'Invoice URL', 'invoicing' ),
175
            'value' => $invoice->get_view_url(),
176
        );
177
178
        // Return the values.
179
        $items = apply_filters( 'getpaid_privacy_export_invoice_personal_data', array_values( $props_to_export ), $invoice );
180
181
        $data = array();
182
183
        // Unset null values to prevent PHP deprecated notice.
184
        foreach ( $items as $item ) {
185
            if ( isset( $item['value'] ) && ! is_null( $item['value'] ) ) {
186
                $data[] = $item;
187
            }
188
        }
189
190
        return $data;
191
    }
192
193
    /**
194
     * Processes invoice subscriptions.
195
     *
196
     * @since 2.0.7
197
     * @param WPInv_Invoice $invoice invoice object.
198
     * @param array $props invoice props.
199
     * @return array
200
     */
201
    public static function process_subscription( $invoice, $props ) {
202
203
        $subscription = wpinv_get_subscription( $invoice );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_get_subscription() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

203
        $subscription = /** @scrutinizer ignore-deprecated */ wpinv_get_subscription( $invoice );
Loading history...
204
        if ( ! empty( $subscription ) ) {
205
206
            $frequency    = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency() );
207
            $period       = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ) . ' / ' . $frequency;
208
            $initial_amt  = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() );
209
            $bill_times   = $subscription->get_times_billed() . ' / ' . ( ( $subscription->get_bill_times() == 0 ) ? __( 'Until Cancelled', 'invoicing' ) : $subscription->get_bill_times() );
210
            $renewal_date = getpaid_format_date_value( $subscription->get_expiration() );
211
212
            // Billing cycle.
213
            $props['period'] = array(
214
                'name'  => __( 'Billing Cycle', 'invoicing' ),
215
                'value' => $period,
216
            );
217
218
            // Initial amount.
219
            $props['initial_amount'] = array(
220
                'name'  => __( 'Initial Amount', 'invoicing' ),
221
                'value' => $initial_amt,
222
            );
223
224
            // Bill times.
225
            $props['bill_times'] = array(
226
                'name'  => __( 'Times Billed', 'invoicing' ),
227
                'value' => $bill_times,
228
            );
229
230
            // Add expiry date.
231
            if ( $subscription->is_active() ) {
232
233
                $props['renewal_date'] = array(
234
                    'name'  => __( 'Expires', 'invoicing' ),
235
                    'value' => $renewal_date,
236
                );
237
238
            }
239
}
240
241
        return $props;
242
243
    }
244
245
    /**
246
     * Processes invoice items.
247
     *
248
     * @since 2.0.7
249
     * @param WPInv_Invoice $invoice invoice object.
250
     * @return array
251
     */
252
    public static function process_invoice_items( $invoice ) {
253
254
        $item_names = array();
255
        foreach ( $invoice->get_items() as $cart_item ) {
256
            $item_names[] = sprintf(
257
                '%s x %s - %s',
258
                $cart_item->get_name(),
259
                $cart_item->get_quantity(),
260
                wpinv_price( $invoice->is_renewal() ? $cart_item->get_recurring_sub_total() : $cart_item->get_sub_total(), $invoice->get_currency() )
261
            );
262
        }
263
264
        return implode( ', ', $item_names );
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(', ', $item_names) returns the type string which is incompatible with the documented return type array.
Loading history...
265
266
    }
267
268
}
269