Passed
Push — master ( 43660e...962628 )
by Brian
10:35
created

GetPaid_Admin::admin_body_class()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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