Passed
Pull Request — master (#190)
by Stiofan
04:39
created

wpinv-general-functions.php ➔ wpinv_checkout_required_fields()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 33
nop 0
dl 0
loc 41
rs 8.3306
c 0
b 0
f 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
function wpinv_is_checkout() {
15
    global $wp_query;
16
17
    $is_object_set    = isset( $wp_query->queried_object );
18
    $is_object_id_set = isset( $wp_query->queried_object_id );
19
    $is_checkout      = is_page( wpinv_get_option( 'checkout_page' ) );
20
21
    if ( !$is_object_set ) {
22
        unset( $wp_query->queried_object );
23
    }
24
25
    if ( !$is_object_id_set ) {
26
        unset( $wp_query->queried_object_id );
27
    }
28
29
    return apply_filters( 'wpinv_is_checkout', $is_checkout );
30
}
31
32
function wpinv_can_checkout() {
33
	$can_checkout = true; // Always true for now
34
35
	return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout );
36
}
37
38
function wpinv_get_success_page_uri() {
39
	$page_id = wpinv_get_option( 'success_page', 0 );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
	$page_id = absint( $page_id );
41
42
	return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) );
43
}
44
45
function wpinv_get_history_page_uri() {
46
	$page_id = wpinv_get_option( 'invoice_history_page', 0 );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
	$page_id = absint( $page_id );
48
49
	return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ) );
50
}
51
52
function wpinv_is_success_page() {
53
	$is_success_page = wpinv_get_option( 'success_page', false );
54
	$is_success_page = isset( $is_success_page ) ? is_page( $is_success_page ) : false;
55
56
	return apply_filters( 'wpinv_is_success_page', $is_success_page );
57
}
58
59
function wpinv_is_invoice_history_page() {
60
	$ret = wpinv_get_option( 'invoice_history_page', false );
61
	$ret = $ret ? is_page( $ret ) : false;
62
	return apply_filters( 'wpinv_is_invoice_history_page', $ret );
63
}
64
65
function wpinv_is_subscriptions_history_page() {
66
    $ret = wpinv_get_option( 'invoice_subscription_page', false );
67
    $ret = $ret ? is_page( $ret ) : false;
68
    return apply_filters( 'wpinv_is_subscriptions_history_page', $ret );
69
}
70
71 View Code Duplication
function wpinv_send_to_success_page( $args = null ) {
0 ignored issues
show
Duplication introduced by
This function 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...
72
	$redirect = wpinv_get_success_page_uri();
73
    
74
    if ( !empty( $args ) ) {
75
        // Check for backward compatibility
76
        if ( is_string( $args ) )
77
            $args = str_replace( '?', '', $args );
78
79
        $args = wp_parse_args( $args );
80
81
        $redirect = add_query_arg( $args, $redirect );
82
    }
83
84
    $gateway = isset( $_REQUEST['wpi-gateway'] ) ? $_REQUEST['wpi-gateway'] : '';
85
    
86
    $redirect = apply_filters( 'wpinv_success_page_redirect', $redirect, $gateway, $args );
87
    wp_redirect( $redirect );
88
    exit;
89
}
90
91 View Code Duplication
function wpinv_send_to_failed_page( $args = null ) {
0 ignored issues
show
Duplication introduced by
This function 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...
92
	$redirect = wpinv_get_failed_transaction_uri();
93
    
94
    if ( !empty( $args ) ) {
95
        // Check for backward compatibility
96
        if ( is_string( $args ) )
97
            $args = str_replace( '?', '', $args );
98
99
        $args = wp_parse_args( $args );
100
101
        $redirect = add_query_arg( $args, $redirect );
102
    }
103
104
    $gateway = isset( $_REQUEST['wpi-gateway'] ) ? $_REQUEST['wpi-gateway'] : '';
105
    
106
    $redirect = apply_filters( 'wpinv_failed_page_redirect', $redirect, $gateway, $args );
107
    wp_redirect( $redirect );
108
    exit;
109
}
110
111
function wpinv_get_checkout_uri( $args = array() ) {
112
	$uri = wpinv_get_option( 'checkout_page', false );
113
	$uri = isset( $uri ) ? get_permalink( $uri ) : NULL;
114
115
	if ( !empty( $args ) ) {
116
		// Check for backward compatibility
117
		if ( is_string( $args ) )
118
			$args = str_replace( '?', '', $args );
119
120
		$args = wp_parse_args( $args );
121
122
		$uri = add_query_arg( $args, $uri );
123
	}
124
125
	$scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
126
127
	$ajax_url = admin_url( 'admin-ajax.php', $scheme );
128
129
	if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) {
130
		$uri = preg_replace( '/^http:/', 'https:', $uri );
131
	}
132
133
	return apply_filters( 'wpinv_get_checkout_uri', $uri );
134
}
135
136
function wpinv_send_back_to_checkout( $args = array() ) {
137
	$redirect = wpinv_get_checkout_uri();
138
139
	if ( ! empty( $args ) ) {
140
		// Check for backward compatibility
141
		if ( is_string( $args ) )
142
			$args = str_replace( '?', '', $args );
143
144
		$args = wp_parse_args( $args );
145
146
		$redirect = add_query_arg( $args, $redirect );
147
	}
148
149
	wp_redirect( apply_filters( 'wpinv_send_back_to_checkout', $redirect, $args ) );
150
	exit;
151
}
152
153
function wpinv_get_success_page_url( $query_string = null ) {
154
	$success_page = wpinv_get_option( 'success_page', 0 );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
	$success_page = get_permalink( $success_page );
156
157
	if ( $query_string )
158
		$success_page .= $query_string;
159
160
	return apply_filters( 'wpinv_success_page_url', $success_page );
161
}
162
163
function wpinv_get_failed_transaction_uri( $extras = false ) {
164
	$uri = wpinv_get_option( 'failure_page', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
	$uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url();
166
167
	if ( $extras )
168
		$uri .= $extras;
169
170
	return apply_filters( 'wpinv_get_failed_transaction_uri', $uri );
171
}
172
173
function wpinv_is_failed_transaction_page() {
174
	$ret = wpinv_get_option( 'failure_page', false );
175
	$ret = isset( $ret ) ? is_page( $ret ) : false;
176
177
	return apply_filters( 'wpinv_is_failure_page', $ret );
178
}
179
180
function wpinv_transaction_query( $type = 'start' ) {
181
    global $wpdb;
182
183
    $wpdb->hide_errors();
184
185
    if ( ! defined( 'WPINV_USE_TRANSACTIONS' ) ) {
186
        define( 'WPINV_USE_TRANSACTIONS', true );
187
    }
188
189
    if ( WPINV_USE_TRANSACTIONS ) {
190
        switch ( $type ) {
191
            case 'commit' :
192
                $wpdb->query( 'COMMIT' );
193
                break;
194
            case 'rollback' :
195
                $wpdb->query( 'ROLLBACK' );
196
                break;
197
            default :
198
                $wpdb->query( 'START TRANSACTION' );
199
            break;
200
        }
201
    }
202
}
203
204
function wpinv_create_invoice( $args = array(), $data = array(), $wp_error = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
205
    $default_args = array(
206
        'status'        => '',
207
        'user_id'       => null,
208
        'user_note'     => null,
209
        'invoice_id'    => 0,
210
        'created_via'   => '',
211
        'parent'        => 0
212
    );
213
214
    $args           = wp_parse_args( $args, $default_args );
215
    $invoice_data   = array();
216
217
    if ( $args['invoice_id'] > 0 ) {
218
        $updating           = true;
219
        $invoice_data['post_type']  = 'wpi_invoice';
220
        $invoice_data['ID']         = $args['invoice_id'];
221
    } else {
222
        $updating                       = false;
223
        $invoice_data['post_type']      = 'wpi_invoice';
224
        $invoice_data['post_status']    = apply_filters( 'wpinv_default_invoice_status', 'wpi-pending' );
225
        $invoice_data['ping_status']    = 'closed';
226
        $invoice_data['post_author']    = !empty( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
227
        $invoice_data['post_title']     = wpinv_format_invoice_number( '0' );
228
        $invoice_data['post_parent']    = absint( $args['parent'] );
229
        if ( !empty( $args['created_date'] ) ) {
230
            $invoice_data['post_date']      = $args['created_date'];
231
            $invoice_data['post_date_gmt']  = get_gmt_from_date( $args['created_date'] );
232
        }
233
    }
234
235
    if ( $args['status'] ) {
236
        if ( ! in_array( $args['status'], array_keys( wpinv_get_invoice_statuses() ) ) ) {
237
            return new WP_Error( 'wpinv_invalid_invoice_status', wp_sprintf( __( 'Invalid invoice status: %s', 'invoicing' ), $args['status'] ) );
238
        }
239
        $invoice_data['post_status']    = $args['status'];
240
    }
241
242
    if ( ! is_null( $args['user_note'] ) ) {
243
        $invoice_data['post_excerpt']   = $args['user_note'];
244
    }
245
246
    if ( $updating ) {
247
        $invoice_id = wp_update_post( $invoice_data, true );
248
    } else {
249
        $invoice_id = wp_insert_post( apply_filters( 'wpinv_new_invoice_data', $invoice_data ), true );
250
    }
251
252
    if ( is_wp_error( $invoice_id ) ) {
253
        return $wp_error ? $invoice_id : 0;
254
    }
255
    
256
    $invoice = wpinv_get_invoice( $invoice_id );
257
258
    if ( !$updating ) {
259
        update_post_meta( $invoice_id, '_wpinv_key', apply_filters( 'wpinv_generate_invoice_key', uniqid( 'wpinv_' ) ) );
260
        update_post_meta( $invoice_id, '_wpinv_currency', wpinv_get_currency() );
261
        update_post_meta( $invoice_id, '_wpinv_include_tax', get_option( 'wpinv_prices_include_tax' ) );
262
        update_post_meta( $invoice_id, '_wpinv_user_ip', wpinv_get_ip() );
263
        update_post_meta( $invoice_id, '_wpinv_user_agent', wpinv_get_user_agent() );
264
        update_post_meta( $invoice_id, '_wpinv_created_via', sanitize_text_field( $args['created_via'] ) );
265
        
266
        // Add invoice note
267
        $invoice->add_note( wp_sprintf( __( 'Invoice is created with status %s.', 'invoicing' ), wpinv_status_nicename( $invoice->status ) ) );
268
    }
269
270
    update_post_meta( $invoice_id, '_wpinv_version', WPINV_VERSION );
271
272
    return $invoice;
273
}
274
275
function wpinv_get_prefix() {
276
    $invoice_prefix = 'INV-';
277
    
278
    return apply_filters( 'wpinv_get_prefix', $invoice_prefix );
279
}
280
281
function wpinv_get_business_logo() {
282
    $business_logo = wpinv_get_option( 'logo' );
283
    return apply_filters( 'wpinv_get_business_logo', $business_logo );
284
}
285
286
function wpinv_get_business_name() {
287
    $business_name = wpinv_get_option('store_name');
288
    return apply_filters( 'wpinv_get_business_name', $business_name );
289
}
290
291
function wpinv_get_blogname() {
292
    return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
293
}
294
295
function wpinv_get_admin_email() {
296
    $admin_email = get_option( 'admin_email' );
297
    return apply_filters( 'wpinv_admin_email', $admin_email );
298
}
299
300
function wpinv_get_business_website() {
301
    $business_website = home_url( '/' );
302
    return apply_filters( 'wpinv_get_business_website', $business_website );
303
}
304
305
function wpinv_get_terms_text( $invoice_id = 0 ) {
306
    $terms_text = '';
307
    return apply_filters( 'wpinv_get_terms_text', $terms_text, $invoice_id );
308
}
309
310
function wpinv_get_business_footer() {
311
    $site_link = '<a target="_blank" href="' . esc_url( wpinv_get_business_website() ) . '">' . esc_html( wpinv_get_business_name() ) . '</a>';
312
    $business_footer = wp_sprintf( __( 'Thanks for using %s', 'invoicing' ), $site_link );
313
    return apply_filters( 'wpinv_get_business_footer', $business_footer );
314
}
315
316
function wpinv_checkout_required_fields() {
317
    $required_fields = array();
318
    
319
    // Let payment gateways and other extensions determine if address fields should be required
320
    $require_billing_details = apply_filters( 'wpinv_checkout_required_billing_details', wpinv_use_taxes() );
321
    
322
    if ( $require_billing_details ) {
323
		if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) {
324
			$required_fields['first_name'] = array(
325
				'error_id' => 'invalid_first_name',
326
				'error_message' => __( 'Please enter your first name', 'invoicing' )
327
			);
328
		}
329
		if ( (bool)wpinv_get_option( 'address_mandatory' ) ) {
330
			$required_fields['address'] = array(
331
				'error_id' => 'invalid_address',
332
				'error_message' => __( 'Please enter your address', 'invoicing' )
333
			);
334
		}
335
		if ( (bool)wpinv_get_option( 'city_mandatory' ) ) {
336
			$required_fields['city'] = array(
337
				'error_id' => 'invalid_city',
338
				'error_message' => __( 'Please enter your billing city', 'invoicing' )
339
			);
340
		}
341
		if ( (bool)wpinv_get_option( 'state_mandatory' ) ) {
342
			$required_fields['state'] = array(
343
				'error_id' => 'invalid_state',
344
				'error_message' => __( 'Please enter billing state / province', 'invoicing' )
345
			);
346
		}
347
		if ( (bool)wpinv_get_option( 'country_mandatory' ) ) {
348
			$required_fields['country'] = array(
349
				'error_id' => 'invalid_country',
350
				'error_message' => __( 'Please select your billing country', 'invoicing' )
351
			);
352
		}
353
    }
354
355
    return apply_filters( 'wpinv_checkout_required_fields', $required_fields );
356
}
357
358
function wpinv_is_ssl_enforced() {
359
    $ssl_enforced = wpinv_get_option( 'enforce_ssl', false );
360
    return (bool) apply_filters( 'wpinv_is_ssl_enforced', $ssl_enforced );
361
}
362
363
function wpinv_user_can_view_invoice( $post ) {
364
    $allow = false;
365
366
    $post = get_post( $post );
367
368
    if ( empty( $post->ID ) ) {
369
        return $allow;
370
    }
371
372
    $invoice = wpinv_get_invoice( $post->ID );
373
    if ( empty( $invoice->ID ) ) {
374
        return $allow;
375
    }
376
377
    // Don't allow trash, draft status
378
    if ( $invoice->has_status( array_keys( wpinv_get_invoice_statuses() ) ) ) {
379
        if ( current_user_can( 'manage_options' ) ) { // Admin user
380
            $allow = true;
381
        } else {
382
            if ( is_user_logged_in() ) {
383
                if ( (int)$invoice->get_user_id() === (int)get_current_user_id() ) {
384
                    $allow = true;
385 View Code Duplication
                } else if ( !wpinv_require_login_to_checkout() && isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
386
                    $allow = true;
387
                }
388 View Code Duplication
            } else {
389
                if ( !wpinv_require_login_to_checkout() && isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
390
                    $allow = true;
391
                }
392
            }
393
        }
394
    }
395
    
396
    return apply_filters( 'wpinv_can_print_invoice', $allow, $post, $invoice );
397
}
398
399
function wpinv_schedule_events() {
400
    // hourly, daily and twicedaily
401
    if ( !wp_next_scheduled( 'wpinv_register_schedule_event_twicedaily' ) ) {
402
        wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'wpinv_register_schedule_event_twicedaily' );
403
    }
404
}
405
add_action( 'wp', 'wpinv_schedule_events' );
406
407
function wpinv_schedule_event_twicedaily() {
408
    wpinv_email_payment_reminders();
409
}
410
add_action( 'wpinv_register_schedule_event_twicedaily', 'wpinv_schedule_event_twicedaily' );
411
412
function wpinv_require_login_to_checkout() {
413
    $return = wpinv_get_option( 'login_to_checkout', false );
414
    return (bool) apply_filters( 'wpinv_require_login_to_checkout', $return );
415
}
416
417
function wpinv_sequential_number_active( $type = '' ) {
418
    $check = apply_filters( 'wpinv_pre_check_sequential_number_active', null, $type );
419
    if ( null !== $check ) {
420
        return $check;
421
    }
422
    
423
    return wpinv_get_option( 'sequential_invoice_number' );
424
}
425
426
function wpinv_switch_to_locale( $locale = NULL ) {
427
    global $invoicing, $wpi_switch_locale;
428
429
    if ( ! empty( $invoicing ) && function_exists( 'switch_to_locale' ) ) {
430
        $locale = empty( $locale ) ? get_locale() : $locale;
431
432
        switch_to_locale( $locale );
433
434
        $wpi_switch_locale = $locale;
435
436
        add_filter( 'plugin_locale', 'get_locale' );
437
438
        $invoicing->load_textdomain();
439
440
        do_action( 'wpinv_switch_to_locale', $locale );
441
    }
442
}
443
444
function wpinv_restore_locale() {
445
    global $invoicing, $wpi_switch_locale;
446
    
447
    if ( ! empty( $invoicing ) && function_exists( 'restore_previous_locale' ) && $wpi_switch_locale ) {
448
        restore_previous_locale();
449
450
        $wpi_switch_locale = NULL;
451
452
        remove_filter( 'plugin_locale', 'get_locale' );
453
454
        $invoicing->load_textdomain();
455
456
        do_action( 'wpinv_restore_locale' );
457
    }
458
}