Passed
Push — master ( 01e429...89614d )
by Brian
08:00 queued 03:45
created

GetPaid_Admin::show_success()   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
	 * 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
        do_action( 'getpaid_init_admin_hooks', $this );
56
    }
57
58
    /**
59
	 * Register admin scripts
60
	 *
61
	 */
62
	public function enqeue_scripts() {
63
        global $current_screen;
64
65
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
66
67
        if ( ! empty( $current_screen->post_type ) ) {
68
			$page = $current_screen->post_type;
69
        }
70
71
        // General styles.
72
        if ( false!== stripos( $page, 'wpi' ) ) {
73
74
            // Styles.
75
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
76
            wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version );
77
            wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' );
78
            wp_enqueue_style( 'wp_enqueue_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION );
79
            wp_enqueue_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui.min.css', array(), '1.8.16' );
80
81
            // Scripts.
82
            wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '4.0.13', true );
83
            wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION );
84
85
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
86
            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 );
87
            wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) );
88
89
        }
90
91
        // Payment form scripts.
92
		if ( 'wpi_payment_form' == $page ) {
93
            $this->load_payment_form_scripts();
94
        }
95
96
        if ( $page == 'wpinv-subscriptions' ) {
97
			wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ),  WPINV_VERSION );
98
			wp_enqueue_script( 'wpinv-sub-admin-script' );
99
		}
100
101
		if ( $page == 'wpinv-reports' ) {
102
			wp_enqueue_script( 'jquery-flot', WPINV_PLUGIN_URL . 'assets/js/jquery.flot.min.js', array( 'jquery' ), '0.7' );
103
		}
104
105
		if ( $page == 'wpinv-subscriptions' ) {
106
			wp_enqueue_script( 'postbox' );
107
		}
108
109
    }
110
111
    /**
112
	 * Returns admin js translations.
113
	 *
114
	 */
115
	protected function get_admin_i18() {
116
        global $post;
117
118
        return array(
119
            'ajax_url'                  => admin_url( 'admin-ajax.php' ),
120
            'post_ID'                   => isset( $post->ID ) ? $post->ID : '',
121
            'wpinv_nonce'               => wp_create_nonce( 'wpinv-nonce' ),
122
            'add_invoice_note_nonce'    => wp_create_nonce( 'add-invoice-note' ),
123
            'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ),
124
            'invoice_item_nonce'        => wp_create_nonce( 'invoice-item' ),
125
            'billing_details_nonce'     => wp_create_nonce( 'get-billing-details' ),
126
            'tax'                       => wpinv_tax_amount(),
127
            'discount'                  => wpinv_discount_amount(),
128
            'currency_symbol'           => wpinv_currency_symbol(),
129
            'currency_pos'              => wpinv_currency_position(),
130
            'thousand_sep'              => wpinv_thousands_separator(),
131
            'decimal_sep'               => wpinv_decimal_separator(),
132
            'decimals'                  => wpinv_decimals(),
133
            'save_invoice'              => __( 'Save Invoice', 'invoicing' ),
134
            'status_publish'            => wpinv_status_nicename( 'publish' ),
135
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
136
            'delete_tax_rate'           => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ),
137
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
138
            'OneItemMin'                => __( 'Invoice must contain at least one item', 'invoicing' ),
139
            'FillBillingDetails'        => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ),
140
            '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' ),
141
            'AreYouSure'                => __( 'Are you sure?', 'invoicing' ),
142
            'emptyInvoice'              => __( 'Add at least one item to save invoice!', 'invoicing' ),
143
            'errDeleteItem'             => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ),
144
            'delete_subscription'       => __( 'Are you sure you want to delete this subscription?', 'invoicing' ),
145
            'action_edit'               => __( 'Edit', 'invoicing' ),
146
            'action_cancel'             => __( 'Cancel', 'invoicing' ),
147
            'item_description'          => __( 'Item Description', 'invoicing' ),
148
            'invoice_description'       => __( 'Invoice Description', 'invoicing' ),
149
            'discount_description'      => __( 'Discount Description', 'invoicing' ),
150
            'searching'                 => __( 'Searching', 'invoicing' ),
151
        );
152
153
    }
154
155
    /**
156
	 * Loads payment form js.
157
	 *
158
	 */
159
	protected function load_payment_form_scripts() {
160
        global $post;
161
162
        wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
163
		wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
164
		wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
165
166
		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
167
		wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
168
169
		wp_localize_script(
170
            'wpinv-admin-payment-form-script',
171
            'wpinvPaymentFormAdmin',
172
            array(
173
				'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...
174
				'form_elements' => $this->form_elements->get_form_elements( $post->ID ),
175
				'all_items'     => $this->form_elements->get_published_items(),
176
				'currency'      => wpinv_currency_symbol(),
177
				'position'      => wpinv_currency_position(),
178
				'decimals'      => (int) wpinv_decimals(),
179
				'thousands_sep' => wpinv_thousands_separator(),
180
				'decimals_sep'  => wpinv_decimal_separator(),
181
				'form_items'    => $this->form_elements->get_form_items( $post->ID ),
182
				'is_default'    => $post->ID == wpinv_get_default_payment_form(),
183
            )
184
        );
185
186
        wp_enqueue_script( 'wpinv-admin-payment-form-script' );
187
188
    }
189
190
    /**
191
	 * Add our classes to admin pages.
192
     *
193
     * @param string $classes
194
     * @return string
195
	 *
196
	 */
197
    public function admin_body_class( $classes ) {
198
		global $pagenow, $post, $current_screen;
199
200
201
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
202
203
        if ( ! empty( $current_screen->post_type ) ) {
204
			$page = $current_screen->post_type;
205
        }
206
207
        if ( false !== stripos( $page, 'wpi' ) ) {
208
            $classes .= ' wpi-' . sanitize_key( $page );
209
        }
210
211
        if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) {
212
            $classes .= ' wpinv-cpt wpinv';
213
        }
214
215
		if ( $pagenow == 'post.php' && $page == 'wpi_item' && ! empty( $post ) && ! wpinv_item_is_editable( $post ) ) {
216
			$classes .= ' wpi-editable-n';
217
		}
218
219
		return $classes;
220
    }
221
222
    /**
223
	 * Maybe show the AyeCode Connect Notice.
224
	 */
225
	public function init_ayecode_connect_helper(){
226
227
        new AyeCode_Connect_Helper(
228
            array(
229
				'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
230
				'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
231
				'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>" ),
232
				'connect_button'    => __("Connect Site","invoicing"),
233
				'connecting_button'    => __("Connecting...","invoicing"),
234
				'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
235
				'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
236
            ),
237
            array( 'wpi-addons' )
238
        );
239
240
    }
241
242
    /**
243
     * Maybe redirect users to our admin settings page.
244
     */
245
    public function activation_redirect() {
246
247
		// Bail if no activation redirect.
248
		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

248
		if ( ! get_transient( '_wpinv_activation_redirect' ) || /** @scrutinizer ignore-call */ is_ajax() ) {
Loading history...
249
			return;
250
		}
251
252
		// Delete the redirect transient.
253
		delete_transient( '_wpinv_activation_redirect' );
254
255
		// Bail if activating from network, or bulk
256
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
257
			return;
258
		}
259
260
		wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
261
		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...
262
    }
263
264
    /**
265
     * Fires an admin action after verifying that a user can fire them.
266
     */
267
    public function maybe_do_admin_action() {
268
269
        if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
270
            $key = sanitize_key( $_REQUEST['getpaid-admin-action'] );
271
            do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST );
272
        }
273
274
    }
275
276
    /**
277
	 * Returns an array of admin notices.
278
	 *
279
	 * @since       1.0.19
280
     * @return array
281
	 */
282
	public function get_notices() {
283
		$notices = get_option( 'wpinv_admin_notices' );
284
        return is_array( $notices ) ? $notices : array();
285
	}
286
287
	/**
288
	 * Clears all admin notices
289
	 *
290
	 * @access      public
291
	 * @since       1.0.19
292
	 */
293
	public function clear_notices() {
294
		delete_option( 'wpinv_admin_notices' );
295
	}
296
297
	/**
298
	 * Saves a new admin notice
299
	 *
300
	 * @access      public
301
	 * @since       1.0.19
302
	 */
303
	public function save_notice( $type, $message ) {
304
		$notices = $this->get_notices();
305
306
		if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) {
307
			$notices[ $type ] = array();
308
		}
309
310
		$notices[ $type ][] = $message;
311
312
		update_option( 'wpinv_admin_notices', $notices );
313
	}
314
315
	/**
316
	 * Displays a success notice
317
	 *
318
	 * @param       string $msg The message to qeue.
319
	 * @access      public
320
	 * @since       1.0.19
321
	 */
322
	public function show_success( $msg ) {
323
		$this->save_notice( 'success', $msg );
324
	}
325
326
	/**
327
	 * Displays a error notice
328
	 *
329
	 * @access      public
330
	 * @param       string $msg The message to qeue.
331
	 * @since       1.0.19
332
	 */
333
	public function show_error( $msg ) {
334
		$this->save_notice( 'error', $msg );
335
	}
336
337
	/**
338
	 * Displays a warning notice
339
	 *
340
	 * @access      public
341
	 * @param       string $msg The message to qeue.
342
	 * @since       1.0.19
343
	 */
344
	public function show_warning( $msg ) {
345
		$this->save_notice( 'warning', $msg );
346
	}
347
348
	/**
349
	 * Displays a info notice
350
	 *
351
	 * @access      public
352
	 * @param       string $msg The message to qeue.
353
	 * @since       1.0.19
354
	 */
355
	public function show_info( $msg ) {
356
		$this->save_notice( 'info', $msg );
357
	}
358
359
	/**
360
	 * Show notices
361
	 *
362
	 * @access      public
363
	 * @since       1.0.19
364
	 */
365
	public function show_notices() {
366
367
        $notices = $this->get_notices();
368
        $this->clear_notices();
369
370
		foreach ( $notices as $type => $messages ) {
371
372
			if ( ! is_array( $messages ) ) {
373
				continue;
374
			}
375
376
            $type  = sanitize_key( $type );
377
			foreach ( $messages as $message ) {
378
                $message = wp_kses_post( $message );
379
				echo "<div class='notice notice-$type is-dismissible'><p>$message</p></div>";
380
            }
381
382
        }
383
384
	}
385
386
}
387