Passed
Pull Request — master (#420)
by Brian
04:41
created

getpaid_get_payment_form_elements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
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
    $checkout_page    = wpinv_get_option( 'checkout_page' );
20
    $is_checkout      = ! empty( $checkout_page ) && is_page( $checkout_page );
21
22
    if ( !$is_object_set ) {
23
        unset( $wp_query->queried_object );
24
    }
25
26
    if ( !$is_object_id_set ) {
27
        unset( $wp_query->queried_object_id );
28
    }
29
30
    return apply_filters( 'wpinv_is_checkout', $is_checkout );
31
}
32
33
function wpinv_can_checkout() {
34
	$can_checkout = true; // Always true for now
35
36
	return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout );
37
}
38
39
function wpinv_get_success_page_uri() {
40
	$page_id = wpinv_get_option( 'success_page', 0 );
41
	$page_id = absint( $page_id );
42
43
	return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) );
44
}
45
46
function wpinv_get_history_page_uri() {
47
	$page_id = wpinv_get_option( 'invoice_history_page', 0 );
48
	$page_id = absint( $page_id );
49
50
	return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ) );
51
}
52
53
function wpinv_is_success_page() {
54
	$is_success_page = wpinv_get_option( 'success_page', false );
55
	$is_success_page = ! empty( $is_success_page ) ? is_page( $is_success_page ) : false;
56
57
	return apply_filters( 'wpinv_is_success_page', $is_success_page );
58
}
59
60
function wpinv_is_invoice_history_page() {
61
	$ret = wpinv_get_option( 'invoice_history_page', false );
62
	$ret = $ret ? is_page( $ret ) : false;
63
	return apply_filters( 'wpinv_is_invoice_history_page', $ret );
64
}
65
66
function wpinv_is_subscriptions_history_page() {
67
    $ret = wpinv_get_option( 'invoice_subscription_page', false );
68
    $ret = $ret ? is_page( $ret ) : false;
69
    return apply_filters( 'wpinv_is_subscriptions_history_page', $ret );
70
}
71
72
/**
73
 * Redirects a user the success page.
74
 */
75
function wpinv_send_to_success_page( $args = array() ) {
76
    $redirect = add_query_arg(
77
        wp_parse_args( $args ),
78
        wpinv_get_success_page_uri()
79
    );
80
81
    wp_redirect( $redirect );
82
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
83
}
84
85
function wpinv_send_to_failed_page( $args = null ) {
86
	$redirect = wpinv_get_failed_transaction_uri();
87
    
88
    if ( !empty( $args ) ) {
89
        // Check for backward compatibility
90
        if ( is_string( $args ) )
91
            $args = str_replace( '?', '', $args );
92
93
        $args = wp_parse_args( $args );
94
95
        $redirect = add_query_arg( $args, $redirect );
96
    }
97
98
    $gateway = isset( $_REQUEST['wpi-gateway'] ) ? $_REQUEST['wpi-gateway'] : '';
99
    
100
    $redirect = apply_filters( 'wpinv_failed_page_redirect', $redirect, $gateway, $args );
101
    wp_redirect( $redirect );
102
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
103
}
104
105
function wpinv_get_checkout_uri( $args = array() ) {
106
	$uri = wpinv_get_option( 'checkout_page', false );
107
	$uri = isset( $uri ) ? get_permalink( $uri ) : NULL;
108
109
	if ( !empty( $args ) ) {
110
		// Check for backward compatibility
111
		if ( is_string( $args ) )
112
			$args = str_replace( '?', '', $args );
113
114
		$args = wp_parse_args( $args );
115
116
		$uri = add_query_arg( $args, $uri );
117
	}
118
119
	$scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
120
121
	$ajax_url = admin_url( 'admin-ajax.php', $scheme );
122
123
	if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) {
0 ignored issues
show
Bug introduced by
It seems like $uri can also be of type false; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

123
	if ( ( ! preg_match( '/^https/', /** @scrutinizer ignore-type */ $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) {
Loading history...
124
		$uri = preg_replace( '/^http:/', 'https:', $uri );
125
	}
126
127
	return apply_filters( 'wpinv_get_checkout_uri', $uri );
128
}
129
130
function wpinv_get_success_page_url( $query_string = null ) {
131
	$success_page = wpinv_get_option( 'success_page', 0 );
132
	$success_page = get_permalink( $success_page );
133
134
	if ( $query_string )
135
		$success_page .= $query_string;
136
137
	return apply_filters( 'wpinv_success_page_url', $success_page );
138
}
139
140
function wpinv_get_failed_transaction_uri( $extras = false ) {
141
	$uri = wpinv_get_option( 'failure_page', '' );
142
	$uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url();
0 ignored issues
show
Bug introduced by
It seems like $uri can also be of type string; however, parameter $post of get_permalink() does only seem to accept WP_Post|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

142
	$uri = ! empty( $uri ) ? trailingslashit( get_permalink( /** @scrutinizer ignore-type */ $uri ) ) : home_url();
Loading history...
Bug introduced by
It seems like get_permalink($uri) can also be of type false; however, parameter $string of trailingslashit() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

142
	$uri = ! empty( $uri ) ? trailingslashit( /** @scrutinizer ignore-type */ get_permalink( $uri ) ) : home_url();
Loading history...
143
144
	if ( $extras )
145
		$uri .= $extras;
146
147
	return apply_filters( 'wpinv_get_failed_transaction_uri', $uri );
148
}
149
150
function wpinv_is_failed_transaction_page() {
151
	$ret = wpinv_get_option( 'failure_page', false );
152
	$ret = isset( $ret ) ? is_page( $ret ) : false;
153
154
	return apply_filters( 'wpinv_is_failure_page', $ret );
155
}
156
157
function wpinv_transaction_query( $type = 'start' ) {
158
    global $wpdb;
159
160
    $wpdb->hide_errors();
161
162
    if ( ! defined( 'WPINV_USE_TRANSACTIONS' ) ) {
163
        define( 'WPINV_USE_TRANSACTIONS', true );
164
    }
165
166
    if ( WPINV_USE_TRANSACTIONS ) {
167
        switch ( $type ) {
168
            case 'commit' :
169
                $wpdb->query( 'COMMIT' );
170
                break;
171
            case 'rollback' :
172
                $wpdb->query( 'ROLLBACK' );
173
                break;
174
            default :
175
                $wpdb->query( 'START TRANSACTION' );
176
            break;
177
        }
178
    }
179
}
180
181
function wpinv_get_prefix() {
182
    $invoice_prefix = 'INV-';
183
    
184
    return apply_filters( 'wpinv_get_prefix', $invoice_prefix );
185
}
186
187
function wpinv_get_business_logo() {
188
    $business_logo = wpinv_get_option( 'logo' );
189
    return apply_filters( 'wpinv_get_business_logo', $business_logo );
190
}
191
192
function wpinv_get_business_name() {
193
    $business_name = wpinv_get_option('store_name');
194
    return apply_filters( 'wpinv_get_business_name', $business_name );
195
}
196
197
function wpinv_get_blogname() {
198
    return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
0 ignored issues
show
Bug introduced by
It seems like get_option('blogname') can also be of type false; however, parameter $string of wp_specialchars_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

198
    return wp_specialchars_decode( /** @scrutinizer ignore-type */ get_option( 'blogname' ), ENT_QUOTES );
Loading history...
199
}
200
201
function wpinv_get_admin_email() {
202
    $admin_email = wpinv_get_option( 'admin_email', get_option( 'admin_email' ) );
203
    return apply_filters( 'wpinv_admin_email', $admin_email );
204
}
205
206
function wpinv_get_business_website() {
207
    $business_website = home_url( '/' );
208
    return apply_filters( 'wpinv_get_business_website', $business_website );
209
}
210
211
function wpinv_get_terms_text( $invoice_id = 0 ) {
212
    $terms_text = '';
213
    return apply_filters( 'wpinv_get_terms_text', $terms_text, $invoice_id );
214
}
215
216
function wpinv_get_business_footer() {
217
    $site_link = '<a target="_blank" href="' . esc_url( wpinv_get_business_website() ) . '">' . esc_html( wpinv_get_business_name() ) . '</a>';
0 ignored issues
show
Bug introduced by
It seems like wpinv_get_business_name() can also be of type false; however, parameter $text of esc_html() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

217
    $site_link = '<a target="_blank" href="' . esc_url( wpinv_get_business_website() ) . '">' . esc_html( /** @scrutinizer ignore-type */ wpinv_get_business_name() ) . '</a>';
Loading history...
218
    $business_footer = wp_sprintf( __( 'Thanks for using %s', 'invoicing' ), $site_link );
219
    return apply_filters( 'wpinv_get_business_footer', $business_footer );
220
}
221
222
function wpinv_checkout_required_fields() {
223
    $required_fields = array();
224
    
225
    // Let payment gateways and other extensions determine if address fields should be required
226
    $require_billing_details = apply_filters( 'wpinv_checkout_required_billing_details', wpinv_use_taxes() );
227
    
228
    if ( $require_billing_details ) {
229
		if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) {
230
			$required_fields['first_name'] = array(
231
				'error_id' => 'invalid_first_name',
232
				'error_message' => __( 'Please enter your first name', 'invoicing' )
233
			);
234
		}
235
		if ( (bool)wpinv_get_option( 'address_mandatory' ) ) {
236
			$required_fields['address'] = array(
237
				'error_id' => 'invalid_address',
238
				'error_message' => __( 'Please enter your address', 'invoicing' )
239
			);
240
		}
241
		if ( (bool)wpinv_get_option( 'city_mandatory' ) ) {
242
			$required_fields['city'] = array(
243
				'error_id' => 'invalid_city',
244
				'error_message' => __( 'Please enter your billing city', 'invoicing' )
245
			);
246
		}
247
		if ( (bool)wpinv_get_option( 'state_mandatory' ) ) {
248
			$required_fields['state'] = array(
249
				'error_id' => 'invalid_state',
250
				'error_message' => __( 'Please enter billing state / province', 'invoicing' )
251
			);
252
		}
253
		if ( (bool)wpinv_get_option( 'country_mandatory' ) ) {
254
			$required_fields['country'] = array(
255
				'error_id' => 'invalid_country',
256
				'error_message' => __( 'Please select your billing country', 'invoicing' )
257
			);
258
		}
259
    }
260
261
    return apply_filters( 'wpinv_checkout_required_fields', $required_fields );
262
}
263
264
function wpinv_is_ssl_enforced() {
265
    $ssl_enforced = wpinv_get_option( 'enforce_ssl', false );
266
    return (bool) apply_filters( 'wpinv_is_ssl_enforced', $ssl_enforced );
267
}
268
269
function wpinv_schedule_event_twicedaily() {
270
    wpinv_email_payment_reminders();
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_email_payment_reminders() 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

270
    /** @scrutinizer ignore-deprecated */ wpinv_email_payment_reminders();
Loading history...
271
}
272
add_action( 'wpinv_register_schedule_event_daily', 'wpinv_schedule_event_twicedaily' );
273
274
function wpinv_require_login_to_checkout() {
275
    $return = wpinv_get_option( 'login_to_checkout', false );
276
    return (bool) apply_filters( 'wpinv_require_login_to_checkout', $return );
277
}
278
279
function wpinv_sequential_number_active( $type = '' ) {
280
    $check = apply_filters( 'wpinv_pre_check_sequential_number_active', null, $type );
281
    if ( null !== $check ) {
282
        return $check;
283
    }
284
    
285
    return wpinv_get_option( 'sequential_invoice_number' );
286
}
287
288
function wpinv_switch_to_locale( $locale = NULL ) {
289
    global $invoicing, $wpi_switch_locale;
290
291
    if ( ! empty( $invoicing ) && function_exists( 'switch_to_locale' ) ) {
292
        $locale = empty( $locale ) ? get_locale() : $locale;
293
294
        switch_to_locale( $locale );
295
296
        $wpi_switch_locale = $locale;
297
298
        add_filter( 'plugin_locale', 'get_locale' );
299
300
        $invoicing->load_textdomain();
301
302
        do_action( 'wpinv_switch_to_locale', $locale );
303
    }
304
}
305
306
function wpinv_restore_locale() {
307
    global $invoicing, $wpi_switch_locale;
308
    
309
    if ( ! empty( $invoicing ) && function_exists( 'restore_previous_locale' ) && $wpi_switch_locale ) {
310
        restore_previous_locale();
311
312
        $wpi_switch_locale = NULL;
313
314
        remove_filter( 'plugin_locale', 'get_locale' );
315
316
        $invoicing->load_textdomain();
317
318
        do_action( 'wpinv_restore_locale' );
319
    }
320
}
321
322
/**
323
 * Returns the default form's id.
324
 */
325
function wpinv_get_default_payment_form() {
326
    $form = get_option( 'wpinv_default_payment_form' );
327
328
    if ( empty( $form ) || 'publish' != get_post_status( $form ) ) {
329
        $form = wp_insert_post(
330
            array(
331
                'post_type'   => 'wpi_payment_form',
332
                'post_title'  => __( 'Checkout (default)', 'invoicing' ),
333
                'post_status' => 'publish',
334
                'meta_input'  => array(
335
                    'wpinv_form_elements' => wpinv_get_data( 'default-payment-form' ),
336
                    'wpinv_form_items'    => array(),
337
                )
338
            )
339
        );
340
341
        update_option( 'wpinv_default_payment_form', $form );
342
    }
343
344
    return $form;
345
}
346
347
/**
348
 * Retrieves a given payment form's elements.
349
 * 
350
 * @param int $payment_form
351
 */
352
function getpaid_get_payment_form_elements( $payment_form ) {
353
354
    if ( empty( $payment_form ) ) {
355
        return wpinv_get_data( 'sample-payment-form' );
356
    }
357
358
    $form_elements = get_post_meta( $payment_form, 'wpinv_form_elements', true );
359
360
    if ( is_array( $form_elements ) ) {
361
        return $form_elements;
362
    }
363
364
    return wpinv_get_data( 'sample-payment-form' );
365
366
}
367
368
/**
369
 * Returns an array of items for the given form.
370
 * 
371
 * @param int $payment_form
372
 */
373
function gepaid_get_form_items( $id ) {
374
    $form = new GetPaid_Payment_Form( $id );
375
376
    // Is this a default form?
377
    if ( $form->is_default() ) {
378
        return array();
379
    }
380
381
    return $form->get_items( 'view', 'arrays' );
382
}
383