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