Passed
Push — master ( dea42c...d59b87 )
by Brian
11:32
created

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