Passed
Push — master ( 1efd62...4d9a77 )
by Brian
03:50
created

GetPaid_Admin::show_warning()   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_send_invoice', array( $this, 'send_customer_invoice' ) );
64
		add_action( 'getpaid_authenticated_admin_action_send_invoice_reminder', array( $this, 'send_customer_payment_reminder' ) );
65
        add_action( 'getpaid_authenticated_admin_action_reset_tax_rates', array( $this, 'admin_reset_tax_rates' ) );
66
		do_action( 'getpaid_init_admin_hooks', $this );
67
68
    }
69
70
    /**
71
	 * Register admin scripts
72
	 *
73
	 */
74
	public function enqeue_scripts() {
75
        global $current_screen, $pagenow;
76
77
		$page    = isset( $_GET['page'] ) ? $_GET['page'] : '';
78
		$editing = $pagenow == 'post.php' || $pagenow == 'post-new.php';
79
80
        if ( ! empty( $current_screen->post_type ) ) {
81
			$page = $current_screen->post_type;
82
        }
83
84
        // General styles.
85
        if ( false !== stripos( $page, 'wpi' ) ) {
86
87
            // Styles.
88
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
89
            wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version );
90
            wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' );
91
            wp_enqueue_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui.min.css', array(), '1.8.16' );
92
93
            // Scripts.
94
            wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '4.0.13', true );
95
            wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION );
96
97
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
98
            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 );
99
            wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) );
100
101
        }
102
103
        // Payment form scripts.
104
		if ( 'wpi_payment_form' == $page && $editing ) {
105
            $this->load_payment_form_scripts();
106
        }
107
108
		if ( $page == 'wpinv-subscriptions' ) {
109
			wp_enqueue_script( 'postbox' );
110
		}
111
112
    }
113
114
    /**
115
	 * Returns admin js translations.
116
	 *
117
	 */
118
	protected function get_admin_i18() {
119
        global $post;
120
121
        $i18n = array(
122
            'ajax_url'                  => admin_url( 'admin-ajax.php' ),
123
            'post_ID'                   => isset( $post->ID ) ? $post->ID : '',
124
            'wpinv_nonce'               => wp_create_nonce( 'wpinv-nonce' ),
125
            'add_invoice_note_nonce'    => wp_create_nonce( 'add-invoice-note' ),
126
            'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ),
127
            'invoice_item_nonce'        => wp_create_nonce( 'invoice-item' ),
128
            'billing_details_nonce'     => wp_create_nonce( 'get-billing-details' ),
129
            'tax'                       => wpinv_tax_amount(),
130
            'discount'                  => 0,
131
            'currency_symbol'           => wpinv_currency_symbol(),
132
            'currency_pos'              => wpinv_currency_position(),
133
            'thousand_sep'              => wpinv_thousands_separator(),
134
            'decimal_sep'               => wpinv_decimal_separator(),
135
            'decimals'                  => wpinv_decimals(),
136
            'save_invoice'              => __( 'Save Invoice', 'invoicing' ),
137
            'status_publish'            => wpinv_status_nicename( 'publish' ),
138
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
139
            'delete_tax_rate'           => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ),
140
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
141
            'FillBillingDetails'        => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ),
142
            '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' ),
143
            'AreYouSure'                => __( 'Are you sure?', 'invoicing' ),
144
            'errDeleteItem'             => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ),
145
            'delete_subscription'       => __( 'Are you sure you want to delete this subscription?', 'invoicing' ),
146
            'action_edit'               => __( 'Edit', 'invoicing' ),
147
            'action_cancel'             => __( 'Cancel', 'invoicing' ),
148
            'item_description'          => __( 'Item Description', 'invoicing' ),
149
            'invoice_description'       => __( 'Invoice Description', 'invoicing' ),
150
            'discount_description'      => __( 'Discount Description', 'invoicing' ),
151
            'searching'                 => __( 'Searching', 'invoicing' ),
152
        );
153
154
		if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) {
155
156
			$invoice              = new WPInv_Invoice( $post );
157
			$i18n['save_invoice'] = sprintf(
158
				__( 'Save %s', 'invoicing' ),
159
				ucfirst( $invoice->get_type() )
160
			);
161
162
			$i18n['invoice_description'] = sprintf(
163
				__( '%s Description', 'invoicing' ),
164
				ucfirst( $invoice->get_type() )
165
			);
166
167
		}
168
		return $i18n;
169
    }
170
171
    /**
172
	 * Loads payment form js.
173
	 *
174
	 */
175
	protected function load_payment_form_scripts() {
176
        global $post;
177
178
        wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
179
		wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
180
		wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
181
182
		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
183
		wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
184
185
		wp_localize_script(
186
            'wpinv-admin-payment-form-script',
187
            'wpinvPaymentFormAdmin',
188
            array(
189
				'elements'      => wpinv_get_data( 'payment-form-elements' ),
190
				'form_elements' => getpaid_get_payment_form_elements( $post->ID ),
191
				'currency'      => wpinv_currency_symbol(),
192
				'position'      => wpinv_currency_position(),
193
				'decimals'      => (int) wpinv_decimals(),
194
				'thousands_sep' => wpinv_thousands_separator(),
195
				'decimals_sep'  => wpinv_decimal_separator(),
196
				'form_items'    => gepaid_get_form_items( $post->ID ),
197
				'is_default'    => $post->ID == wpinv_get_default_payment_form(),
198
            )
199
        );
200
201
        wp_enqueue_script( 'wpinv-admin-payment-form-script' );
202
203
    }
204
205
    /**
206
	 * Add our classes to admin pages.
207
     *
208
     * @param string $classes
209
     * @return string
210
	 *
211
	 */
212
    public function admin_body_class( $classes ) {
213
		global $pagenow, $post, $current_screen;
214
215
216
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
217
218
        if ( ! empty( $current_screen->post_type ) ) {
219
			$page = $current_screen->post_type;
220
        }
221
222
        if ( false !== stripos( $page, 'wpi' ) ) {
223
            $classes .= ' wpi-' . sanitize_key( $page );
224
        }
225
226
        if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) {
227
            $classes .= ' wpinv-cpt wpinv';
228
		}
229
		
230
		if ( getpaid_is_invoice_post_type( $page ) ) {
231
            $classes .= ' getpaid-is-invoice-cpt';
232
        }
233
234
		return $classes;
235
    }
236
237
    /**
238
	 * Maybe show the AyeCode Connect Notice.
239
	 */
240
	public function init_ayecode_connect_helper(){
241
242
        new AyeCode_Connect_Helper(
243
            array(
244
				'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
245
				'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
246
				'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>" ),
247
				'connect_button'    => __("Connect Site","invoicing"),
248
				'connecting_button'    => __("Connecting...","invoicing"),
249
				'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
250
				'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
251
            ),
252
            array( 'wpi-addons' )
253
        );
254
255
    }
256
257
    /**
258
     * Maybe redirect users to our admin settings page.
259
     */
260
    public function activation_redirect() {
261
262
		// Bail if no activation redirect.
263
		if ( ! get_transient( '_wpinv_activation_redirect' ) || wp_doing_ajax() ) {
264
			return;
265
		}
266
267
		// Delete the redirect transient.
268
		delete_transient( '_wpinv_activation_redirect' );
269
270
		// Bail if activating from network, or bulk
271
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
272
			return;
273
		}
274
275
		wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
276
		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...
277
    }
278
279
    /**
280
     * Fires an admin action after verifying that a user can fire them.
281
     */
282
    public function maybe_do_admin_action() {
283
284
        if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
285
            $key = sanitize_key( $_REQUEST['getpaid-admin-action'] );
286
            do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST );
287
        }
288
289
    }
290
291
	/**
292
     * Sends a payment reminder to a customer.
293
	 * 
294
	 * @param array $args
295
     */
296
    public function send_customer_invoice( $args ) {
297
		$sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ) );
298
299
		if ( $sent ) {
300
			$this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) );
301
		} else {
302
			$this->show_error( __( 'Could not sent the invoice to the customer', 'invoicing' ) );
303
		}
304
305
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
306
		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...
307
	}
308
309
	/**
310
     * Sends a payment reminder to a customer.
311
	 * 
312
	 * @param array $args
313
     */
314
    public function send_customer_payment_reminder( $args ) {
315
		$sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) );
316
317
		if ( $sent ) {
318
			$this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) );
319
		} else {
320
			$this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) );
321
		}
322
323
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
324
		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...
325
	}
326
327
	/**
328
     * Resets tax rates.
329
	 * 
330
     */
331
    public function admin_reset_tax_rates() {
332
333
		update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
334
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) );
335
		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...
336
337
	}
338
339
    /**
340
	 * Returns an array of admin notices.
341
	 *
342
	 * @since       1.0.19
343
     * @return array
344
	 */
345
	public function get_notices() {
346
		$notices = get_option( 'wpinv_admin_notices' );
347
        return is_array( $notices ) ? $notices : array();
348
	}
349
350
	/**
351
	 * Clears all admin notices
352
	 *
353
	 * @access      public
354
	 * @since       1.0.19
355
	 */
356
	public function clear_notices() {
357
		delete_option( 'wpinv_admin_notices' );
358
	}
359
360
	/**
361
	 * Saves a new admin notice
362
	 *
363
	 * @access      public
364
	 * @since       1.0.19
365
	 */
366
	public function save_notice( $type, $message ) {
367
		$notices = $this->get_notices();
368
369
		if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) {
370
			$notices[ $type ] = array();
371
		}
372
373
		$notices[ $type ][] = $message;
374
375
		update_option( 'wpinv_admin_notices', $notices );
376
	}
377
378
	/**
379
	 * Displays a success notice
380
	 *
381
	 * @param       string $msg The message to qeue.
382
	 * @access      public
383
	 * @since       1.0.19
384
	 */
385
	public function show_success( $msg ) {
386
		$this->save_notice( 'success', $msg );
387
	}
388
389
	/**
390
	 * Displays a error notice
391
	 *
392
	 * @access      public
393
	 * @param       string $msg The message to qeue.
394
	 * @since       1.0.19
395
	 */
396
	public function show_error( $msg ) {
397
		$this->save_notice( 'error', $msg );
398
	}
399
400
	/**
401
	 * Displays a warning notice
402
	 *
403
	 * @access      public
404
	 * @param       string $msg The message to qeue.
405
	 * @since       1.0.19
406
	 */
407
	public function show_warning( $msg ) {
408
		$this->save_notice( 'warning', $msg );
409
	}
410
411
	/**
412
	 * Displays a info notice
413
	 *
414
	 * @access      public
415
	 * @param       string $msg The message to qeue.
416
	 * @since       1.0.19
417
	 */
418
	public function show_info( $msg ) {
419
		$this->save_notice( 'info', $msg );
420
	}
421
422
	/**
423
	 * Show notices
424
	 *
425
	 * @access      public
426
	 * @since       1.0.19
427
	 */
428
	public function show_notices() {
429
430
        $notices = $this->get_notices();
431
        $this->clear_notices();
432
433
		foreach ( $notices as $type => $messages ) {
434
435
			if ( ! is_array( $messages ) ) {
436
				continue;
437
			}
438
439
            $type  = sanitize_key( $type );
440
			foreach ( $messages as $message ) {
441
                $message = wp_kses_post( $message );
442
				echo "<div class='notice notice-$type is-dismissible'><p>$message</p></div>";
443
            }
444
445
        }
446
447
	}
448
449
}
450