Completed
Push — master ( e1fcb8...b01a52 )
by Brian
26s queued 17s
created

GetPaid_Admin::init_admin_hooks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 10
rs 9.9666
cc 1
nc 1
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
	 * 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;
67
68
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
69
70
        if ( ! empty( $current_screen->post_type ) ) {
71
			$page = $current_screen->post_type;
72
        }
73
74
        // General styles.
75
        if ( false!== stripos( $page, 'wpi' ) ) {
76
77
            // Styles.
78
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
79
            wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version );
80
            wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' );
81
            wp_enqueue_style( 'wp_enqueue_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION );
82
            wp_enqueue_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui.min.css', array(), '1.8.16' );
83
84
            // Scripts.
85
            wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '4.0.13', true );
86
            wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION );
87
88
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
89
            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 );
90
            wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) );
91
92
        }
93
94
        // Payment form scripts.
95
		if ( 'wpi_payment_form' == $page ) {
96
            $this->load_payment_form_scripts();
97
        }
98
99
        if ( $page == 'wpinv-subscriptions' ) {
100
			wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ),  WPINV_VERSION );
101
			wp_enqueue_script( 'wpinv-sub-admin-script' );
102
		}
103
104
		if ( $page == 'wpinv-reports' ) {
105
			wp_enqueue_script( 'jquery-flot', WPINV_PLUGIN_URL . 'assets/js/jquery.flot.min.js', array( 'jquery' ), '0.7' );
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
        return 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'                  => wpinv_discount_amount(),
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
            'OneItemMin'                => __( 'Invoice must contain at least one item', 'invoicing' ),
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
            'emptyInvoice'              => __( 'Add at least one item to save invoice!', 'invoicing' ),
146
            'errDeleteItem'             => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ),
147
            'delete_subscription'       => __( 'Are you sure you want to delete this subscription?', 'invoicing' ),
148
            'action_edit'               => __( 'Edit', 'invoicing' ),
149
            'action_cancel'             => __( 'Cancel', 'invoicing' ),
150
            'item_description'          => __( 'Item Description', 'invoicing' ),
151
            'invoice_description'       => __( 'Invoice Description', 'invoicing' ),
152
            'discount_description'      => __( 'Discount Description', 'invoicing' ),
153
            'searching'                 => __( 'Searching', 'invoicing' ),
154
        );
155
156
    }
157
158
    /**
159
	 * Loads payment form js.
160
	 *
161
	 */
162
	protected function load_payment_form_scripts() {
163
        global $post;
164
165
        wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
166
		wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
167
		wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
168
169
		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
170
		wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
171
172
		wp_localize_script(
173
            'wpinv-admin-payment-form-script',
174
            'wpinvPaymentFormAdmin',
175
            array(
176
				'elements'      => $this->form_elements->get_elements(),
0 ignored issues
show
Bug Best Practice introduced by
The property form_elements does not exist on GetPaid_Admin. Did you maybe forget to declare it?
Loading history...
177
				'form_elements' => $this->form_elements->get_form_elements( $post->ID ),
178
				'all_items'     => $this->form_elements->get_published_items(),
179
				'currency'      => wpinv_currency_symbol(),
180
				'position'      => wpinv_currency_position(),
181
				'decimals'      => (int) wpinv_decimals(),
182
				'thousands_sep' => wpinv_thousands_separator(),
183
				'decimals_sep'  => wpinv_decimal_separator(),
184
				'form_items'    => $this->form_elements->get_form_items( $post->ID ),
185
				'is_default'    => $post->ID == wpinv_get_default_payment_form(),
186
            )
187
        );
188
189
        wp_enqueue_script( 'wpinv-admin-payment-form-script' );
190
191
    }
192
193
    /**
194
	 * Add our classes to admin pages.
195
     *
196
     * @param string $classes
197
     * @return string
198
	 *
199
	 */
200
    public function admin_body_class( $classes ) {
201
		global $pagenow, $post, $current_screen;
202
203
204
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
205
206
        if ( ! empty( $current_screen->post_type ) ) {
207
			$page = $current_screen->post_type;
208
        }
209
210
        if ( false !== stripos( $page, 'wpi' ) ) {
211
            $classes .= ' wpi-' . sanitize_key( $page );
212
        }
213
214
        if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) {
215
            $classes .= ' wpinv-cpt wpinv';
216
        }
217
218
		if ( $pagenow == 'post.php' && $page == 'wpi_item' && ! empty( $post ) && ! wpinv_item_is_editable( $post ) ) {
219
			$classes .= ' wpi-editable-n';
220
		}
221
222
		return $classes;
223
    }
224
225
    /**
226
	 * Maybe show the AyeCode Connect Notice.
227
	 */
228
	public function init_ayecode_connect_helper(){
229
230
        new AyeCode_Connect_Helper(
231
            array(
232
				'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
233
				'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
234
				'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>" ),
235
				'connect_button'    => __("Connect Site","invoicing"),
236
				'connecting_button'    => __("Connecting...","invoicing"),
237
				'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
238
				'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
239
            ),
240
            array( 'wpi-addons' )
241
        );
242
243
    }
244
245
    /**
246
     * Maybe redirect users to our admin settings page.
247
     */
248
    public function activation_redirect() {
249
250
		// Bail if no activation redirect.
251
		if ( ! get_transient( '_wpinv_activation_redirect' ) || is_ajax() ) {
0 ignored issues
show
Bug introduced by
The function is_ajax was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
		if ( ! get_transient( '_wpinv_activation_redirect' ) || /** @scrutinizer ignore-call */ is_ajax() ) {
Loading history...
252
			return;
253
		}
254
255
		// Delete the redirect transient.
256
		delete_transient( '_wpinv_activation_redirect' );
257
258
		// Bail if activating from network, or bulk
259
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
260
			return;
261
		}
262
263
		wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
264
		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...
265
    }
266
267
    /**
268
     * Fires an admin action after verifying that a user can fire them.
269
     */
270
    public function maybe_do_admin_action() {
271
272
        if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
273
            $key = sanitize_key( $_REQUEST['getpaid-admin-action'] );
274
            do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST );
275
        }
276
277
    }
278
279
	/**
280
     * Sends a payment reminder to a customer.
281
	 * 
282
	 * @param array $args
283
     */
284
    public function send_customer_invoice( $args ) {
285
		$sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ) );
286
287
		if ( $sent ) {
288
			$this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) );
289
		} else {
290
			$this->show_error( __( 'Could not sent the invoice to the customer', 'invoicing' ) );
291
		}
292
293
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
294
		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...
295
	}
296
297
	/**
298
     * Sends a payment reminder to a customer.
299
	 * 
300
	 * @param array $args
301
     */
302
    public function send_customer_payment_reminder( $args ) {
303
		$sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) );
304
305
		if ( $sent ) {
306
			$this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) );
307
		} else {
308
			$this->show_error( __( 'Could not sent payment reminder 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
	 * Returns an array of admin notices.
317
	 *
318
	 * @since       1.0.19
319
     * @return array
320
	 */
321
	public function get_notices() {
322
		$notices = get_option( 'wpinv_admin_notices' );
323
        return is_array( $notices ) ? $notices : array();
324
	}
325
326
	/**
327
	 * Clears all admin notices
328
	 *
329
	 * @access      public
330
	 * @since       1.0.19
331
	 */
332
	public function clear_notices() {
333
		delete_option( 'wpinv_admin_notices' );
334
	}
335
336
	/**
337
	 * Saves a new admin notice
338
	 *
339
	 * @access      public
340
	 * @since       1.0.19
341
	 */
342
	public function save_notice( $type, $message ) {
343
		$notices = $this->get_notices();
344
345
		if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) {
346
			$notices[ $type ] = array();
347
		}
348
349
		$notices[ $type ][] = $message;
350
351
		update_option( 'wpinv_admin_notices', $notices );
352
	}
353
354
	/**
355
	 * Displays a success notice
356
	 *
357
	 * @param       string $msg The message to qeue.
358
	 * @access      public
359
	 * @since       1.0.19
360
	 */
361
	public function show_success( $msg ) {
362
		$this->save_notice( 'success', $msg );
363
	}
364
365
	/**
366
	 * Displays a error notice
367
	 *
368
	 * @access      public
369
	 * @param       string $msg The message to qeue.
370
	 * @since       1.0.19
371
	 */
372
	public function show_error( $msg ) {
373
		$this->save_notice( 'error', $msg );
374
	}
375
376
	/**
377
	 * Displays a warning notice
378
	 *
379
	 * @access      public
380
	 * @param       string $msg The message to qeue.
381
	 * @since       1.0.19
382
	 */
383
	public function show_warning( $msg ) {
384
		$this->save_notice( 'warning', $msg );
385
	}
386
387
	/**
388
	 * Displays a info notice
389
	 *
390
	 * @access      public
391
	 * @param       string $msg The message to qeue.
392
	 * @since       1.0.19
393
	 */
394
	public function show_info( $msg ) {
395
		$this->save_notice( 'info', $msg );
396
	}
397
398
	/**
399
	 * Show notices
400
	 *
401
	 * @access      public
402
	 * @since       1.0.19
403
	 */
404
	public function show_notices() {
405
406
        $notices = $this->get_notices();
407
        $this->clear_notices();
408
409
		foreach ( $notices as $type => $messages ) {
410
411
			if ( ! is_array( $messages ) ) {
412
				continue;
413
			}
414
415
            $type  = sanitize_key( $type );
416
			foreach ( $messages as $message ) {
417
                $message = wp_kses_post( $message );
418
				echo "<div class='notice notice-$type is-dismissible'><p>$message</p></div>";
419
            }
420
421
        }
422
423
	}
424
425
}
426