Passed
Push — master ( 57b9dd...cc2e7c )
by Brian
04:45
created

GetPaid_Admin::show_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Contains the admin class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * The main admin class.
11
 *
12
 * @since       1.0.19
13
 */
14
class GetPaid_Admin {
15
16
    /**
17
	 * Local path to this plugins admin directory
18
	 *
19
	 * @var         string
20
	 */
21
	public $admin_path;
22
23
	/**
24
	 * Web path to this plugins admin directory
25
	 *
26
	 * @var         string
27
	 */
28
	public $admin_url;
29
	
30
	/**
31
	 * Reports components.
32
	 *
33
	 * @var GetPaid_Reports
34
	 */
35
    public $reports;
36
37
    /**
38
	 * Class constructor.
39
	 */
40
	public function __construct(){
41
42
        $this->admin_path  = plugin_dir_path( __FILE__ );
43
		$this->admin_url   = plugins_url( '/', __FILE__ );
44
		$this->reports     = new GetPaid_Reports();
45
46
        if ( is_admin() ) {
47
			$this->init_admin_hooks();
48
        }
49
50
    }
51
52
    /**
53
	 * Init action and filter hooks
54
	 *
55
	 */
56
	private function init_admin_hooks() {
57
        add_action( 'admin_enqueue_scripts', array( $this, 'enqeue_scripts' ) );
58
        add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
59
        add_action( 'admin_init', array( $this, 'init_ayecode_connect_helper' ) );
60
        add_action( 'admin_init', array( $this, 'activation_redirect') );
61
        add_action( 'admin_init', array( $this, 'maybe_do_admin_action') );
62
		add_action( 'admin_notices', array( $this, 'show_notices' ) );
63
		add_action( 'getpaid_authenticated_admin_action_rate_plugin', array( $this, 'redirect_to_wordpress_rating_page' ) );
64
		add_action( 'getpaid_authenticated_admin_action_send_invoice', array( $this, 'send_customer_invoice' ) );
65
		add_action( 'getpaid_authenticated_admin_action_send_invoice_reminder', array( $this, 'send_customer_payment_reminder' ) );
66
        add_action( 'getpaid_authenticated_admin_action_reset_tax_rates', array( $this, 'admin_reset_tax_rates' ) );
67
		add_action( 'getpaid_authenticated_admin_action_create_missing_pages', array( $this, 'admin_create_missing_pages' ) );
68
		add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
69
		do_action( 'getpaid_init_admin_hooks', $this );
70
71
    }
72
73
    /**
74
	 * Register admin scripts
75
	 *
76
	 */
77
	public function enqeue_scripts() {
78
        global $current_screen, $pagenow;
79
80
		$page    = isset( $_GET['page'] ) ? $_GET['page'] : '';
81
		$editing = $pagenow == 'post.php' || $pagenow == 'post-new.php';
82
83
        if ( ! empty( $current_screen->post_type ) ) {
84
			$page = $current_screen->post_type;
85
        }
86
87
        // General styles.
88
        if ( false !== stripos( $page, 'wpi' ) ) {
89
90
            // Styles.
91
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
92
            wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version );
93
            wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' );
94
            wp_enqueue_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui.min.css', array(), '1.8.16' );
95
96
            // Scripts.
97
            wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '4.0.13', true );
98
            wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION );
99
100
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
101
            wp_enqueue_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-blockui','jquery-ui-tooltip', 'wp-color-picker', 'jquery-ui-datepicker' ),  $version );
102
            wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) );
103
104
        }
105
106
        // Payment form scripts.
107
		if ( 'wpi_payment_form' == $page && $editing ) {
108
            $this->load_payment_form_scripts();
109
        }
110
111
		if ( $page == 'wpinv-subscriptions' ) {
112
			wp_enqueue_script( 'postbox' );
113
		}
114
115
    }
116
117
    /**
118
	 * Returns admin js translations.
119
	 *
120
	 */
121
	protected function get_admin_i18() {
122
        global $post;
123
124
		$date_range = array(
125
			'period' => isset( $_GET['date_range'] ) ? sanitize_text_field( $_GET['date_range'] ) : '7_days'
126
		);
127
128
		if ( $date_range['period'] == 'custom' ) {
129
			
130
			if ( isset( $_GET['from'] ) ) {
131
				$date_range[ 'after' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['from'] ), current_time( 'timestamp' ) ) - DAY_IN_SECONDS );
132
			}
133
134
			if ( isset( $_GET['to'] ) ) {
135
				$date_range[ 'before' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['to'] ), current_time( 'timestamp' ) ) + DAY_IN_SECONDS );
136
			}
137
138
		}
139
140
        $i18n = array(
141
            'ajax_url'                  => admin_url( 'admin-ajax.php' ),
142
            'post_ID'                   => isset( $post->ID ) ? $post->ID : '',
143
			'wpinv_nonce'               => wp_create_nonce( 'wpinv-nonce' ),
144
			'rest_nonce'                => wp_create_nonce( 'wp_rest' ),
145
			'rest_root'                 => esc_url_raw( rest_url() ),
146
			'date_range'                => $date_range,
147
            'add_invoice_note_nonce'    => wp_create_nonce( 'add-invoice-note' ),
148
            'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ),
149
            'invoice_item_nonce'        => wp_create_nonce( 'invoice-item' ),
150
            'billing_details_nonce'     => wp_create_nonce( 'get-billing-details' ),
151
            'tax'                       => wpinv_tax_amount(),
152
            'discount'                  => 0,
153
			'currency_symbol'           => wpinv_currency_symbol(),
154
			'currency'                  => wpinv_get_currency(),
155
            'currency_pos'              => wpinv_currency_position(),
156
            'thousand_sep'              => wpinv_thousands_separator(),
157
            'decimal_sep'               => wpinv_decimal_separator(),
158
            'decimals'                  => wpinv_decimals(),
159
            'save_invoice'              => __( 'Save Invoice', 'invoicing' ),
160
            'status_publish'            => wpinv_status_nicename( 'publish' ),
161
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
162
            'delete_tax_rate'           => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ),
163
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
164
            'FillBillingDetails'        => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ),
165
            'confirmCalcTotals'         => __( 'Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing' ),
166
            'AreYouSure'                => __( 'Are you sure?', 'invoicing' ),
167
            'errDeleteItem'             => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ),
168
            'delete_subscription'       => __( 'Are you sure you want to delete this subscription?', 'invoicing' ),
169
            'action_edit'               => __( 'Edit', 'invoicing' ),
170
            'action_cancel'             => __( 'Cancel', 'invoicing' ),
171
            'item_description'          => __( 'Item Description', 'invoicing' ),
172
            'invoice_description'       => __( 'Invoice Description', 'invoicing' ),
173
            'discount_description'      => __( 'Discount Description', 'invoicing' ),
174
            'searching'                 => __( 'Searching', 'invoicing' ),
175
        );
176
177
		if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) {
178
179
			$invoice              = new WPInv_Invoice( $post );
180
			$i18n['save_invoice'] = sprintf(
181
				__( 'Save %s', 'invoicing' ),
182
				ucfirst( $invoice->get_invoice_quote_type() )
183
			);
184
185
			$i18n['invoice_description'] = sprintf(
186
				__( '%s Description', 'invoicing' ),
187
				ucfirst( $invoice->get_invoice_quote_type() )
188
			);
189
190
		}
191
		return $i18n;
192
	}
193
194
	/**
195
	 * Change the admin footer text on GetPaid admin pages.
196
	 *
197
	 * @since  2.0.0
198
	 * @param  string $footer_text
199
	 * @return string
200
	 */
201
	public function admin_footer_text( $footer_text ) {
202
		global $current_screen;
203
204
		$page    = isset( $_GET['page'] ) ? $_GET['page'] : '';
205
206
        if ( ! empty( $current_screen->post_type ) ) {
207
			$page = $current_screen->post_type;
208
        }
209
210
        // General styles.
211
        if ( apply_filters( 'getpaid_display_admin_footer_text', wpinv_current_user_can_manage_invoicing() ) && false !== stripos( $page, 'wpi' ) ) {
212
213
			// Change the footer text
214
			if ( ! get_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', true ) ) {
215
216
				$rating_url  = esc_url(
217
					wp_nonce_url(
218
						admin_url( 'admin.php?page=wpinv-reports&getpaid-admin-action=rate_plugin' ),
219
						'getpaid-nonce',
220
						'getpaid-nonce'
221
						)
222
				);
223
224
				$footer_text = sprintf(
225
					/* translators: %s: five stars */
226
					__( 'If you like <strong>GetPaid</strong>, please leave us a %s rating. A huge thanks in advance!', 'invoicing' ),
227
					"<a href='$rating_url'>&#9733;&#9733;&#9733;&#9733;&#9733;</a>"
228
				);
229
230
			} else {
231
232
				$footer_text = sprintf(
233
					/* translators: %s: GetPaid */
234
					__( 'Thank you for using %s!', 'invoicing' ),
235
					"<a href='https://wpgetpaid.com/' target='_blank'><strong>GetPaid</strong></a>"
236
				);
237
238
			}
239
240
		}
241
242
		return $footer_text;
243
	}
244
245
	/**
246
	 * Redirects to wp.org to rate the plugin.
247
	 *
248
	 * @since  2.0.0
249
	 */
250
	public function redirect_to_wordpress_rating_page() {
251
		update_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', 1 );
252
		wp_redirect( 'https://wordpress.org/support/plugin/invoicing/reviews?rate=5#new-post' );
253
		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...
254
	}
255
256
    /**
257
	 * Loads payment form js.
258
	 *
259
	 */
260
	protected function load_payment_form_scripts() {
261
        global $post;
262
263
        wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
264
		wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
265
		wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
266
267
		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
268
		wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
269
270
		wp_localize_script(
271
            'wpinv-admin-payment-form-script',
272
            'wpinvPaymentFormAdmin',
273
            array(
274
				'elements'      => wpinv_get_data( 'payment-form-elements' ),
275
				'form_elements' => getpaid_get_payment_form_elements( $post->ID ),
276
				'currency'      => wpinv_currency_symbol(),
277
				'position'      => wpinv_currency_position(),
278
				'decimals'      => (int) wpinv_decimals(),
279
				'thousands_sep' => wpinv_thousands_separator(),
280
				'decimals_sep'  => wpinv_decimal_separator(),
281
				'form_items'    => gepaid_get_form_items( $post->ID ),
282
				'is_default'    => $post->ID == wpinv_get_default_payment_form(),
283
            )
284
        );
285
286
        wp_enqueue_script( 'wpinv-admin-payment-form-script' );
287
288
    }
289
290
    /**
291
	 * Add our classes to admin pages.
292
     *
293
     * @param string $classes
294
     * @return string
295
	 *
296
	 */
297
    public function admin_body_class( $classes ) {
298
		global $pagenow, $post, $current_screen;
299
300
301
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
302
303
        if ( ! empty( $current_screen->post_type ) ) {
304
			$page = $current_screen->post_type;
305
        }
306
307
        if ( false !== stripos( $page, 'wpi' ) ) {
308
            $classes .= ' wpi-' . sanitize_key( $page );
309
        }
310
311
        if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) {
312
            $classes .= ' wpinv-cpt wpinv';
313
		}
314
		
315
		if ( getpaid_is_invoice_post_type( $page ) ) {
316
            $classes .= ' getpaid-is-invoice-cpt';
317
        }
318
319
		return $classes;
320
    }
321
322
    /**
323
	 * Maybe show the AyeCode Connect Notice.
324
	 */
325
	public function init_ayecode_connect_helper(){
326
327
        new AyeCode_Connect_Helper(
328
            array(
329
				'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
330
				'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
331
				'connect'           => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s","invoicing" ),"<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>","</a>" ),
332
				'connect_button'    => __("Connect Site","invoicing"),
333
				'connecting_button'    => __("Connecting...","invoicing"),
334
				'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
335
				'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
336
            ),
337
            array( 'wpi-addons' )
338
        );
339
340
    }
341
342
	/**
343
	 * Redirect users to settings on activation.
344
	 *
345
	 * @return void
346
	 */
347
	public function activation_redirect() {
348
349
		$redirected = get_option( 'wpinv_redirected_to_settings' );
350
351
		if ( ! empty( $redirected ) || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) {
352
			return;
353
		}
354
355
		// Bail if activating from network, or bulk
356
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
357
			return;
358
		}
359
360
	    update_option( 'wpinv_redirected_to_settings', 1 );
361
362
        wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
363
        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...
364
365
	}
366
367
    /**
368
     * Fires an admin action after verifying that a user can fire them.
369
     */
370
    public function maybe_do_admin_action() {
371
372
        if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
373
            $key = sanitize_key( $_REQUEST['getpaid-admin-action'] );
374
            do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST );
375
        }
376
377
    }
378
379
	/**
380
     * Sends a payment reminder to a customer.
381
	 * 
382
	 * @param array $args
383
     */
384
    public function send_customer_invoice( $args ) {
385
		$sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ) );
386
387
		if ( $sent ) {
388
			$this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) );
389
		} else {
390
			$this->show_error( __( 'Could not sent the invoice to the customer', 'invoicing' ) );
391
		}
392
393
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
394
		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...
395
	}
396
397
	/**
398
     * Sends a payment reminder to a customer.
399
	 * 
400
	 * @param array $args
401
     */
402
    public function send_customer_payment_reminder( $args ) {
403
		$sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) );
404
405
		if ( $sent ) {
406
			$this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) );
407
		} else {
408
			$this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) );
409
		}
410
411
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
412
		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...
413
	}
414
415
	/**
416
     * Resets tax rates.
417
	 * 
418
     */
419
    public function admin_reset_tax_rates() {
420
421
		update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
422
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) );
423
		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...
424
425
	}
426
427
	/**
428
     * Resets admin pages.
429
	 * 
430
     */
431
    public function admin_create_missing_pages() {
432
		$installer = new GetPaid_Installer();
433
		$installer->create_pages();
434
		$this->show_success( __( 'GetPaid pages updated.', 'invoicing' ) );
435
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) );
436
		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...
437
	}
438
439
    /**
440
	 * Returns an array of admin notices.
441
	 *
442
	 * @since       1.0.19
443
     * @return array
444
	 */
445
	public function get_notices() {
446
		$notices = get_option( 'wpinv_admin_notices' );
447
        return is_array( $notices ) ? $notices : array();
448
	}
449
450
	/**
451
	 * Clears all admin notices
452
	 *
453
	 * @access      public
454
	 * @since       1.0.19
455
	 */
456
	public function clear_notices() {
457
		delete_option( 'wpinv_admin_notices' );
458
	}
459
460
	/**
461
	 * Saves a new admin notice
462
	 *
463
	 * @access      public
464
	 * @since       1.0.19
465
	 */
466
	public function save_notice( $type, $message ) {
467
		$notices = $this->get_notices();
468
469
		if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) {
470
			$notices[ $type ] = array();
471
		}
472
473
		$notices[ $type ][] = $message;
474
475
		update_option( 'wpinv_admin_notices', $notices );
476
	}
477
478
	/**
479
	 * Displays a success notice
480
	 *
481
	 * @param       string $msg The message to qeue.
482
	 * @access      public
483
	 * @since       1.0.19
484
	 */
485
	public function show_success( $msg ) {
486
		$this->save_notice( 'success', $msg );
487
	}
488
489
	/**
490
	 * Displays a error notice
491
	 *
492
	 * @access      public
493
	 * @param       string $msg The message to qeue.
494
	 * @since       1.0.19
495
	 */
496
	public function show_error( $msg ) {
497
		$this->save_notice( 'error', $msg );
498
	}
499
500
	/**
501
	 * Displays a warning notice
502
	 *
503
	 * @access      public
504
	 * @param       string $msg The message to qeue.
505
	 * @since       1.0.19
506
	 */
507
	public function show_warning( $msg ) {
508
		$this->save_notice( 'warning', $msg );
509
	}
510
511
	/**
512
	 * Displays a info notice
513
	 *
514
	 * @access      public
515
	 * @param       string $msg The message to qeue.
516
	 * @since       1.0.19
517
	 */
518
	public function show_info( $msg ) {
519
		$this->save_notice( 'info', $msg );
520
	}
521
522
	/**
523
	 * Show notices
524
	 *
525
	 * @access      public
526
	 * @since       1.0.19
527
	 */
528
	public function show_notices() {
529
530
        $notices = $this->get_notices();
531
        $this->clear_notices();
532
533
		foreach ( $notices as $type => $messages ) {
534
535
			if ( ! is_array( $messages ) ) {
536
				continue;
537
			}
538
539
            $type  = sanitize_key( $type );
540
			foreach ( $messages as $message ) {
541
                $message = wp_kses_post( $message );
542
				echo "<div class='notice notice-$type is-dismissible'><p>$message</p></div>";
543
            }
544
545
        }
546
547
		foreach ( array( 'checkout_page', 'invoice_history_page', 'success_page', 'failure_page', 'invoice_subscription_page' ) as $page ) {
548
549
			if ( ! is_numeric( wpinv_get_option( $page, false ) ) ) {
550
				$url     = esc_url(
551
					wp_nonce_url(
552
						add_query_arg( 'getpaid-admin-action', 'create_missing_pages' ),
553
						'getpaid-nonce',
554
						'getpaid-nonce'
555
					)
556
				);
557
				$message  = __( 'Some GetPaid pages are missing. To use GetPaid without any issues, click the button below to generate the missing pages.', 'invoicing' );
558
				$message2 = __( 'Generate Pages', 'invoicing' );
559
				echo "<div class='notice notice-warning is-dismissible'><p>$message<br><br><a href='$url' class='button button-primary'>$message2</a></p></div>";
560
				break;
561
			}
562
563
		}
564
565
	}
566
567
}
568