Passed
Push — master ( 66833a...11ce7c )
by Brian
04:47
created

GetPaid_Admin::admin_footer_text()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 19
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 42
rs 9.0111
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_rate_plugin', array( $this, 'redirect_to_wordpress_rating_page' ) );
64
		add_action( 'getpaid_authenticated_admin_action_send_invoice', array( $this, 'send_customer_invoice' ) );
65
		add_action( 'getpaid_authenticated_admin_action_send_invoice_reminder', array( $this, 'send_customer_payment_reminder' ) );
66
        add_action( 'getpaid_authenticated_admin_action_reset_tax_rates', array( $this, 'admin_reset_tax_rates' ) );
67
		add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
68
		do_action( 'getpaid_init_admin_hooks', $this );
69
70
    }
71
72
    /**
73
	 * Register admin scripts
74
	 *
75
	 */
76
	public function enqeue_scripts() {
77
        global $current_screen, $pagenow;
78
79
		$page    = isset( $_GET['page'] ) ? $_GET['page'] : '';
80
		$editing = $pagenow == 'post.php' || $pagenow == 'post-new.php';
81
82
        if ( ! empty( $current_screen->post_type ) ) {
83
			$page = $current_screen->post_type;
84
        }
85
86
        // General styles.
87
        if ( false !== stripos( $page, 'wpi' ) ) {
88
89
            // Styles.
90
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
91
            wp_enqueue_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), $version );
92
            wp_enqueue_style( 'select2', WPINV_PLUGIN_URL . 'assets/css/select2/select2.min.css', array(), '4.0.13', 'all' );
93
            wp_enqueue_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui.min.css', array(), '1.8.16' );
94
95
            // Scripts.
96
            wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '4.0.13', true );
97
            wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full.min.js', array( 'jquery' ), WPINV_VERSION );
98
99
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
100
            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 );
101
            wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', apply_filters( 'wpinv_admin_js_localize', $this->get_admin_i18() ) );
102
103
        }
104
105
        // Payment form scripts.
106
		if ( 'wpi_payment_form' == $page && $editing ) {
107
            $this->load_payment_form_scripts();
108
        }
109
110
		if ( $page == 'wpinv-subscriptions' ) {
111
			wp_enqueue_script( 'postbox' );
112
		}
113
114
    }
115
116
    /**
117
	 * Returns admin js translations.
118
	 *
119
	 */
120
	protected function get_admin_i18() {
121
        global $post;
122
123
		$date_range = array(
124
			'period' => isset( $_GET['date_range'] ) ? sanitize_text_field( $_GET['date_range'] ) : '7_days'
125
		);
126
127
		if ( $date_range['period'] == 'custom' ) {
128
			
129
			if ( isset( $_GET['from'] ) ) {
130
				$date_range[ 'after' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['from'] ), current_time( 'timestamp' ) ) - DAY_IN_SECONDS );
131
			}
132
133
			if ( isset( $_GET['to'] ) ) {
134
				$date_range[ 'before' ] = date( 'Y-m-d', strtotime( sanitize_text_field( $_GET['to'] ), current_time( 'timestamp' ) ) + DAY_IN_SECONDS );
135
			}
136
137
		}
138
139
        $i18n = array(
140
            'ajax_url'                  => admin_url( 'admin-ajax.php' ),
141
            'post_ID'                   => isset( $post->ID ) ? $post->ID : '',
142
			'wpinv_nonce'               => wp_create_nonce( 'wpinv-nonce' ),
143
			'rest_nonce'                => wp_create_nonce( 'wp_rest' ),
144
			'rest_root'                 => esc_url_raw( rest_url() ),
145
			'date_range'                => $date_range,
146
            'add_invoice_note_nonce'    => wp_create_nonce( 'add-invoice-note' ),
147
            'delete_invoice_note_nonce' => wp_create_nonce( 'delete-invoice-note' ),
148
            'invoice_item_nonce'        => wp_create_nonce( 'invoice-item' ),
149
            'billing_details_nonce'     => wp_create_nonce( 'get-billing-details' ),
150
            'tax'                       => wpinv_tax_amount(),
151
            'discount'                  => 0,
152
			'currency_symbol'           => wpinv_currency_symbol(),
153
			'currency'                  => wpinv_get_currency(),
154
            'currency_pos'              => wpinv_currency_position(),
155
            'thousand_sep'              => wpinv_thousands_separator(),
156
            'decimal_sep'               => wpinv_decimal_separator(),
157
            'decimals'                  => wpinv_decimals(),
158
            'save_invoice'              => __( 'Save Invoice', 'invoicing' ),
159
            'status_publish'            => wpinv_status_nicename( 'publish' ),
160
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
161
            'delete_tax_rate'           => __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ),
162
            'status_pending'            => wpinv_status_nicename( 'wpi-pending' ),
163
            'FillBillingDetails'        => __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ),
164
            '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' ),
165
            'AreYouSure'                => __( 'Are you sure?', 'invoicing' ),
166
            'errDeleteItem'             => __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ),
167
            'delete_subscription'       => __( 'Are you sure you want to delete this subscription?', 'invoicing' ),
168
            'action_edit'               => __( 'Edit', 'invoicing' ),
169
            'action_cancel'             => __( 'Cancel', 'invoicing' ),
170
            'item_description'          => __( 'Item Description', 'invoicing' ),
171
            'invoice_description'       => __( 'Invoice Description', 'invoicing' ),
172
            'discount_description'      => __( 'Discount Description', 'invoicing' ),
173
            'searching'                 => __( 'Searching', 'invoicing' ),
174
        );
175
176
		if ( ! empty( $post ) && getpaid_is_invoice_post_type( $post->post_type ) ) {
177
178
			$invoice              = new WPInv_Invoice( $post );
179
			$i18n['save_invoice'] = sprintf(
180
				__( 'Save %s', 'invoicing' ),
181
				ucfirst( $invoice->get_type() )
182
			);
183
184
			$i18n['invoice_description'] = sprintf(
185
				__( '%s Description', 'invoicing' ),
186
				ucfirst( $invoice->get_type() )
187
			);
188
189
		}
190
		return $i18n;
191
	}
192
193
	/**
194
	 * Change the admin footer text on GetPaid admin pages.
195
	 *
196
	 * @since  2.0.0
197
	 * @param  string $footer_text
198
	 * @return string
199
	 */
200
	public function admin_footer_text( $footer_text ) {
201
		global $current_screen;
202
203
		$page    = isset( $_GET['page'] ) ? $_GET['page'] : '';
204
205
        if ( ! empty( $current_screen->post_type ) ) {
206
			$page = $current_screen->post_type;
207
        }
208
209
        // General styles.
210
        if ( apply_filters( 'getpaid_display_admin_footer_text', wpinv_current_user_can_manage_invoicing() ) && false !== stripos( $page, 'wpi' ) ) {
211
212
			// Change the footer text
213
			if ( ! get_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', true ) ) {
214
215
				$rating_url  = esc_url(
216
					wp_nonce_url(
217
						admin_url( 'admin.php?page=wpinv-reports&getpaid-admin-action=rate_plugin' ),
218
						'getpaid-nonce',
219
						'getpaid-nonce'
220
						)
221
				);
222
223
				$footer_text = sprintf(
224
					/* translators: %s: five stars */
225
					__( 'If you like <strong>GetPaid</strong>, please leave us a %s rating. A huge thanks in advance!', 'invoicing' ),
226
					"<a href='$rating_url'>&#9733;&#9733;&#9733;&#9733;&#9733;</a>"
227
				);
228
229
			} else {
230
231
				$footer_text = sprintf(
232
					/* translators: %s: GetPaid */
233
					__( 'Thank you for using %s!', 'invoicing' ),
234
					"<a href='https://wpgetpaid.com/' target='_blank'><strong>GetPaid</strong></a>"
235
				);
236
237
			}
238
239
		}
240
241
		return $footer_text;
242
	}
243
244
	/**
245
	 * Redirects to wp.org to rate the plugin.
246
	 *
247
	 * @since  2.0.0
248
	 */
249
	public function redirect_to_wordpress_rating_page() {
250
		update_user_meta( get_current_user_id(), 'getpaid_admin_footer_text_rated', 1 );
251
		wp_redirect( 'https://wordpress.org/support/plugin/invoicing/reviews?rate=5#new-post' );
252
		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...
253
	}
254
255
    /**
256
	 * Loads payment form js.
257
	 *
258
	 */
259
	protected function load_payment_form_scripts() {
260
        global $post;
261
262
        wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
263
		wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
264
		wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
265
266
		$version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
267
		wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
268
269
		wp_localize_script(
270
            'wpinv-admin-payment-form-script',
271
            'wpinvPaymentFormAdmin',
272
            array(
273
				'elements'      => wpinv_get_data( 'payment-form-elements' ),
274
				'form_elements' => getpaid_get_payment_form_elements( $post->ID ),
275
				'currency'      => wpinv_currency_symbol(),
276
				'position'      => wpinv_currency_position(),
277
				'decimals'      => (int) wpinv_decimals(),
278
				'thousands_sep' => wpinv_thousands_separator(),
279
				'decimals_sep'  => wpinv_decimal_separator(),
280
				'form_items'    => gepaid_get_form_items( $post->ID ),
281
				'is_default'    => $post->ID == wpinv_get_default_payment_form(),
282
            )
283
        );
284
285
        wp_enqueue_script( 'wpinv-admin-payment-form-script' );
286
287
    }
288
289
    /**
290
	 * Add our classes to admin pages.
291
     *
292
     * @param string $classes
293
     * @return string
294
	 *
295
	 */
296
    public function admin_body_class( $classes ) {
297
		global $pagenow, $post, $current_screen;
298
299
300
        $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
301
302
        if ( ! empty( $current_screen->post_type ) ) {
303
			$page = $current_screen->post_type;
304
        }
305
306
        if ( false !== stripos( $page, 'wpi' ) ) {
307
            $classes .= ' wpi-' . sanitize_key( $page );
308
        }
309
310
        if ( in_array( $page, wpinv_parse_list( 'wpi_invoice wpi_payment_form wpi_quote' ) ) ) {
311
            $classes .= ' wpinv-cpt wpinv';
312
		}
313
		
314
		if ( getpaid_is_invoice_post_type( $page ) ) {
315
            $classes .= ' getpaid-is-invoice-cpt';
316
        }
317
318
		return $classes;
319
    }
320
321
    /**
322
	 * Maybe show the AyeCode Connect Notice.
323
	 */
324
	public function init_ayecode_connect_helper(){
325
326
        new AyeCode_Connect_Helper(
327
            array(
328
				'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
329
				'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
330
				'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>" ),
331
				'connect_button'    => __("Connect Site","invoicing"),
332
				'connecting_button'    => __("Connecting...","invoicing"),
333
				'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
334
				'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
335
            ),
336
            array( 'wpi-addons' )
337
        );
338
339
    }
340
341
    /**
342
     * Maybe redirect users to our admin settings page.
343
     */
344
    public function activation_redirect() {
345
346
		// Bail if no activation redirect.
347
		if ( ! get_transient( '_wpinv_activation_redirect' ) || wp_doing_ajax() ) {
348
			return;
349
		}
350
351
		// Delete the redirect transient.
352
		delete_transient( '_wpinv_activation_redirect' );
353
354
		// Bail if activating from network, or bulk
355
		if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
356
			return;
357
		}
358
359
		wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
360
		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...
361
    }
362
363
    /**
364
     * Fires an admin action after verifying that a user can fire them.
365
     */
366
    public function maybe_do_admin_action() {
367
368
        if ( wpinv_current_user_can_manage_invoicing() && isset( $_REQUEST['getpaid-admin-action'] ) && isset( $_REQUEST['getpaid-nonce'] ) && wp_verify_nonce( $_REQUEST['getpaid-nonce'], 'getpaid-nonce' ) ) {
369
            $key = sanitize_key( $_REQUEST['getpaid-admin-action'] );
370
            do_action( "getpaid_authenticated_admin_action_$key", $_REQUEST );
371
        }
372
373
    }
374
375
	/**
376
     * Sends a payment reminder to a customer.
377
	 * 
378
	 * @param array $args
379
     */
380
    public function send_customer_invoice( $args ) {
381
		$sent = getpaid()->get( 'invoice_emails' )->user_invoice( new WPInv_Invoice( $args['invoice_id'] ) );
382
383
		if ( $sent ) {
384
			$this->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) );
385
		} else {
386
			$this->show_error( __( 'Could not sent the invoice to the customer', 'invoicing' ) );
387
		}
388
389
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
390
		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...
391
	}
392
393
	/**
394
     * Sends a payment reminder to a customer.
395
	 * 
396
	 * @param array $args
397
     */
398
    public function send_customer_payment_reminder( $args ) {
399
		$sent = getpaid()->get( 'invoice_emails' )->force_send_overdue_notice( new WPInv_Invoice( $args['invoice_id'] ) );
400
401
		if ( $sent ) {
402
			$this->show_success( __( 'Payment reminder was successfully sent to the customer', 'invoicing' ) );
403
		} else {
404
			$this->show_error( __( 'Could not sent payment reminder to the customer', 'invoicing' ) );
405
		}
406
407
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce', 'invoice_id' ) ) );
408
		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...
409
	}
410
411
	/**
412
     * Resets tax rates.
413
	 * 
414
     */
415
    public function admin_reset_tax_rates() {
416
417
		update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
418
		wp_safe_redirect( remove_query_arg( array( 'getpaid-admin-action', 'getpaid-nonce' ) ) );
419
		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...
420
421
	}
422
423
    /**
424
	 * Returns an array of admin notices.
425
	 *
426
	 * @since       1.0.19
427
     * @return array
428
	 */
429
	public function get_notices() {
430
		$notices = get_option( 'wpinv_admin_notices' );
431
        return is_array( $notices ) ? $notices : array();
432
	}
433
434
	/**
435
	 * Clears all admin notices
436
	 *
437
	 * @access      public
438
	 * @since       1.0.19
439
	 */
440
	public function clear_notices() {
441
		delete_option( 'wpinv_admin_notices' );
442
	}
443
444
	/**
445
	 * Saves a new admin notice
446
	 *
447
	 * @access      public
448
	 * @since       1.0.19
449
	 */
450
	public function save_notice( $type, $message ) {
451
		$notices = $this->get_notices();
452
453
		if ( empty( $notices[ $type ] ) || ! is_array( $notices[ $type ]) ) {
454
			$notices[ $type ] = array();
455
		}
456
457
		$notices[ $type ][] = $message;
458
459
		update_option( 'wpinv_admin_notices', $notices );
460
	}
461
462
	/**
463
	 * Displays a success notice
464
	 *
465
	 * @param       string $msg The message to qeue.
466
	 * @access      public
467
	 * @since       1.0.19
468
	 */
469
	public function show_success( $msg ) {
470
		$this->save_notice( 'success', $msg );
471
	}
472
473
	/**
474
	 * Displays a error notice
475
	 *
476
	 * @access      public
477
	 * @param       string $msg The message to qeue.
478
	 * @since       1.0.19
479
	 */
480
	public function show_error( $msg ) {
481
		$this->save_notice( 'error', $msg );
482
	}
483
484
	/**
485
	 * Displays a warning notice
486
	 *
487
	 * @access      public
488
	 * @param       string $msg The message to qeue.
489
	 * @since       1.0.19
490
	 */
491
	public function show_warning( $msg ) {
492
		$this->save_notice( 'warning', $msg );
493
	}
494
495
	/**
496
	 * Displays a info notice
497
	 *
498
	 * @access      public
499
	 * @param       string $msg The message to qeue.
500
	 * @since       1.0.19
501
	 */
502
	public function show_info( $msg ) {
503
		$this->save_notice( 'info', $msg );
504
	}
505
506
	/**
507
	 * Show notices
508
	 *
509
	 * @access      public
510
	 * @since       1.0.19
511
	 */
512
	public function show_notices() {
513
514
        $notices = $this->get_notices();
515
        $this->clear_notices();
516
517
		foreach ( $notices as $type => $messages ) {
518
519
			if ( ! is_array( $messages ) ) {
520
				continue;
521
			}
522
523
            $type  = sanitize_key( $type );
524
			foreach ( $messages as $message ) {
525
                $message = wp_kses_post( $message );
526
				echo "<div class='notice notice-$type is-dismissible'><p>$message</p></div>";
527
            }
528
529
        }
530
531
	}
532
533
}
534