Passed
Pull Request — master (#444)
by Brian
05:33
created

GetPaid_Admin::enqeue_scripts()   B

Complexity

Conditions 9
Paths 128

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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