Completed
Push — master ( 3ad42c...16627f )
by Brian
25s queued 14s
created

WPInv_Plugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 4
c 2
b 0
f 1
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Manual payment gateway
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * Manual Payment Gateway class.
11
 *
12
 */
13
class WPInv_Plugin {
14
15
    /**
16
	 * GetPaid version.
17
	 *
18
	 * @var string
19
	 */
20
    public $version;
21
22
    /**
23
	 * Session instance.
24
	 *
25
	 * @var WPInv_Session|WPInv_Session_Handler
26
	 */
27
    public $session;
28
    
29
    /**
30
	 * Notes instance.
31
	 *
32
	 * @var WPInv_Notes
33
	 */
34
    public $notes;
35
    
36
    /**
37
	 * Reports instance.
38
	 *
39
	 * @var WPInv_Reports
40
	 */
41
    public $reports;
42
    
43
    /**
44
	 * API instance.
45
	 *
46
	 * @var WPInv_API
47
	 */
48
    public $api;
49
    
50
    /**
51
	 * Form elements instance.
52
	 *
53
	 * @var WPInv_Payment_Form_Elements
54
	 */
55
    public $form_elements;
56
    
57
    /**
58
	 * Tax instance.
59
	 *
60
	 * @var WPInv_EUVat
61
	 */
62
	public $tax;
63
64
    /**
65
     * @param array An array of payment gateways.
66
     */
67
    public $gateways;
68
69
    /**
70
     * @param array An array of options.
71
     */
72
    public $options;
73
74
    /**
75
	 * Class constructor.
76
	 */
77
    public function __construct() {
78
        $this->define_constants();
79
        $this->includes();
80
        $this->init_hooks();
81
        $this->set_properties();
82
    }
83
84
    /**
85
	 * Define class properties.
86
	 */
87
    public function set_properties() {
88
89
        $this->session       = new WPInv_Session_Handler();
90
        $GLOBALS['wpi_session'] = $this->session; // Backwards compatibility.
91
        $this->notes         = new WPInv_Notes();
92
        $this->reports       = new WPInv_Reports();
93
        $this->api           = new WPInv_API();
94
        $this->form_elements = new WPInv_Payment_Form_Elements();
95
        $this->tax           = new WPInv_EUVat();
96
        $this->tax->init();
97
        $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility.
98
99
    }
100
101
     /**
102
	 * Define plugin constants.
103
	 */
104
    public function define_constants() {
105
        define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
106
        define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
107
        $this->version = WPINV_VERSION;
108
    }
109
110
    /**
111
	 * Hook into actions and filters.
112
	 *
113
	 * @since 1.0.19
114
	 */
115
    protected function init_hooks() {
116
        /* Internationalize the text strings used. */
117
        add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
118
        
119
        /* Perform actions on admin initialization. */
120
        add_action( 'admin_init', array( &$this, 'admin_init') );
121
122
        // Init the plugin after WordPress inits.
123
        add_action( 'init', array( $this, 'init' ), 1 );
124
        add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 );
125
        add_action( 'init', array( &$this, 'wpinv_actions' ) );
126
        
127
        if ( class_exists( 'BuddyPress' ) ) {
128
            add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) );
129
        }
130
131
        add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
132
        add_action( 'wp_footer', array( &$this, 'wp_footer' ) );
133
        add_action( 'widgets_init', array( &$this, 'register_widgets' ) );
134
        add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) );
135
136
        if ( is_admin() ) {
137
            add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) );
138
            add_filter( 'admin_body_class', array( &$this, 'admin_body_class' ) );
139
            add_action( 'admin_init', array( &$this, 'init_ayecode_connect_helper' ) );
140
141
        } else {
142
            add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
143
        }
144
        
145
        /**
146
         * Fires after the setup of all WPInv_Plugin actions.
147
         *
148
         * @since 1.0.0
149
         *
150
         * @param WPInv_Plugin $this. Current WPInv_Plugin instance. Passed by reference.
151
         */
152
        do_action_ref_array( 'wpinv_actions', array( &$this ) );
153
154
        add_action( 'admin_init', array( &$this, 'activation_redirect') );
155
    }
156
157
    /**
158
     * Maybe show the AyeCode Connect Notice.
159
     */
160
    public function init_ayecode_connect_helper(){
161
        // AyeCode Connect notice
162
        if ( is_admin() ){
163
            // set the strings so they can be translated
164
            $strings = array(
165
                'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"),
166
                'connect_external'  => __( "Please confirm you wish to connect your site?","invoicing" ),
167
                '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>" ),
168
                'connect_button'    => __("Connect Site","invoicing"),
169
                'connecting_button'    => __("Connecting...","invoicing"),
170
                'error_localhost'   => __( "This service will only work with a live domain, not a localhost.","invoicing" ),
171
                'error'             => __( "Something went wrong, please refresh and try again.","invoicing" ),
172
            );
173
            new AyeCode_Connect_Helper($strings,array('wpi-addons'));
174
        }
175
    }
176
    
177
    public function plugins_loaded() {
178
        /* Internationalize the text strings used. */
179
        $this->load_textdomain();
180
181
        do_action( 'wpinv_loaded' );
182
183
        // Fix oxygen page builder conflict
184
        if ( function_exists( 'ct_css_output' ) ) {
185
            wpinv_oxygen_fix_conflict();
186
        }
187
    }
188
    
189
    /**
190
     * Load the translation of the plugin.
191
     *
192
     * @since 1.0
193
     */
194
    public function load_textdomain( $locale = NULL ) {
195
        if ( empty( $locale ) ) {
196
            $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
197
        }
198
199
        $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
0 ignored issues
show
Unused Code introduced by
The call to get_locale() has too many arguments starting with $locale. ( Ignorable by Annotation )

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

199
        $locale = /** @scrutinizer ignore-call */ apply_filters( 'plugin_locale', $locale, 'invoicing' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
200
        
201
        unload_textdomain( 'invoicing' );
202
        load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
203
        load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' );
204
        
205
        /**
206
         * Define language constants.
207
         */
208
        require_once( WPINV_PLUGIN_DIR . 'language.php' );
209
    }
210
211
    /**
212
	 * Include required core files used in admin and on the frontend.
213
	 */
214
    public function includes() {
215
216
        // Start with the settings.
217
        require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' );
218
        $this->options = wpinv_get_settings();
219
        $GLOBALS['wpinv_options'] = $this->options; // Backwards compatibility.
220
221
        // Packages/libraries.
222
        require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' );
223
        require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' );
224
        require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' );
225
226
        // Load functions.
227
        require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' );
228
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' );
229
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' );
230
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' );
231
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' );
232
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' );
233
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' );
234
        require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' );
235
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' );
236
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' );
237
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' );
238
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' );
239
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' );
240
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-error-functions.php' );
241
242
        // Register autoloader.
243
		try {
244
			spl_autoload_register( array( $this, 'autoload' ), true );
245
		} catch ( Exception $e ) {
246
			wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true );
247
        }
248
249
        require_once( WPINV_PLUGIN_DIR . 'includes/class-getpaid-post-types.php' );
250
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-post-types.php' );
251
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-invoice.php' );
252
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-discount.php' );
253
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-item.php' );
254
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-notes.php' );
255
        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' );
256
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' );
257
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' );
258
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' );
259
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' );
260
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' );
261
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' );
262
        require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
263
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' );
264
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions.php' );
265
        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' );
266
        require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-subscriptions-list-table.php' );
267
        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' );
268
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
269
        require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
270
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
271
	    require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
272
	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
273
	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
274
	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
275
	    require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
276
        require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
277
        require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' );
278
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' );
279
280
        /**
281
         * Load the tax class.
282
         */
283
        if ( ! class_exists( 'WPInv_EUVat' ) ) {
284
            require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' );
285
        }
286
287
        $gateways = array_keys( wpinv_get_enabled_payment_gateways() );
288
        if ( !empty( $gateways ) ) {
289
            foreach ( $gateways as $gateway ) {
290
                if ( $gateway == 'manual' ) {
291
                    continue;
292
                }
293
                
294
                $gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php';
295
                
296
                if ( file_exists( $gateway_file ) ) {
297
                    require_once( $gateway_file );
298
                }
299
            }
300
        }
301
        
302
        if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
0 ignored issues
show
Bug introduced by
The constant WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
303
            GetPaid_Post_Types_Admin::init();
304
305
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' );
306
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' );
307
            //require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-recurring-admin.php' );
308
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' );
309
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' );
310
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' );
311
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' );
312
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' );
313
            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' );
314
            //require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
315
            // load the user class only on the users.php page
316
            global $pagenow;
317
            if($pagenow=='users.php'){
318
                new WPInv_Admin_Users();
319
            }
320
        }
321
322
        // Register cli commands
323
        if ( defined( 'WP_CLI' ) && WP_CLI ) {
324
            require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' );
325
            WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
0 ignored issues
show
Bug introduced by
The type WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
326
        }
327
        
328
        // include css inliner
329
        if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
330
            include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' );
331
        }
332
        
333
        require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' );
334
    }
335
336
    /**
337
	 * Class autoloader
338
	 *
339
	 * @param       string $class_name The name of the class to load.
340
	 * @access      public
341
	 * @since       1.0.19
342
	 * @return      void
343
	 */
344
	public function autoload( $class_name ) {
345
346
		// Normalize the class name...
347
		$class_name  = strtolower( $class_name );
348
349
		// ... and make sure it is our class.
350
		if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) {
351
			return;
352
		}
353
354
		// Next, prepare the file name from the class.
355
		$file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php';
356
357
        // Base path of the classes.
358
        $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR );
359
360
		// And an array of possible locations in order of importance.
361
		$locations = array(
362
            "$plugin_path/includes",
363
            "$plugin_path/includes/data-stores",
364
            "$plugin_path/includes/gateways",
365
            "$plugin_path/includes/admin",
366
            "$plugin_path/includes/admin/meta-boxes",
367
		);
368
369
		foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) {
370
371
			if ( file_exists( trailingslashit( $location ) . $file_name ) ) {
372
				include trailingslashit( $location ) . $file_name;
373
				break;
374
			}
375
376
		}
377
378
	}
379
380
    /**
381
     * Inits hooks etc.
382
     */
383
    public function init() {
384
385
        // Fires before getpaid inits.
386
        do_action( 'before_getpaid_init', $this );
387
388
        // Load default gateways.
389
        $gateways = apply_filters(
390
            'getpaid_default_gateways',
391
            array(
392
                'manual'        => 'GetPaid_Manual_Gateway',
393
                'paypal'        => 'GetPaid_Paypal_Gateway',
394
                'worldpay'      => 'GetPaid_Worldpay_Gateway',
395
                'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway',
396
            )
397
        );
398
399
        foreach ( $gateways as $id => $class ) {
400
            $this->gateways[ $id ] = new $class();
401
        }
402
403
        // Fires after getpaid inits.
404
        do_action( 'getpaid_init', $this );
405
        
406
    }
407
408
    /**
409
     * Checks if this is an IPN request and processes it.
410
     */
411
    public function maybe_process_ipn() {
412
413
        // Ensure that this is an IPN request.
414
        if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) {
415
            return;
416
        }
417
418
        $gateway = wpinv_clean( $_GET['wpi-gateway'] );
419
420
        do_action( 'wpinv_verify_payment_ipn', $gateway );
421
        do_action( "wpinv_verify_{$gateway}_ipn" );
422
        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...
423
424
    }
425
426
    public function admin_init() {
427
        $this->default_payment_form = wpinv_get_default_payment_form();
0 ignored issues
show
Bug Best Practice introduced by
The property default_payment_form does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
428
        add_action( 'admin_print_scripts-edit.php', array( &$this, 'admin_print_scripts_edit_php' ) );
429
    }
430
431
    public function activation_redirect() {
432
        // Bail if no activation redirect
433
        if ( !get_transient( '_wpinv_activation_redirect' ) ) {
434
            return;
435
        }
436
437
        // Delete the redirect transient
438
        delete_transient( '_wpinv_activation_redirect' );
439
440
        // Bail if activating from network, or bulk
441
        if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
442
            return;
443
        }
444
445
        wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
446
        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...
447
    }
448
    
449
    public function enqueue_scripts() {
450
        $suffix       = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
451
        
452
        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' );
453
        wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version );
454
        wp_enqueue_style( 'wpinv_front_style' );
455
               
456
        // Register scripts
457
        wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
458
        wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ),  filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) );
459
460
        $localize                         = array();
461
        $localize['ajax_url']             = admin_url( 'admin-ajax.php' );
462
        $localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
463
        $localize['currency_symbol']      = wpinv_currency_symbol();
464
        $localize['currency_pos']         = wpinv_currency_position();
465
        $localize['thousand_sep']         = wpinv_thousands_separator();
466
        $localize['decimal_sep']          = wpinv_decimal_separator();
467
        $localize['decimals']             = wpinv_decimals();
468
        $localize['txtComplete']          = __( 'Continue', 'invoicing' );
469
        $localize['UseTaxes']             = wpinv_use_taxes();
470
        $localize['checkoutNonce']        = wp_create_nonce( 'wpinv_checkout_nonce' );
471
        $localize['formNonce']            = wp_create_nonce( 'getpaid_form_nonce' );
472
        $localize['connectionError']      = __( 'Could not establish a connection to the server.', 'invoicing' );
473
474
        $localize = apply_filters( 'wpinv_front_js_localize', $localize );
475
        
476
        wp_enqueue_script( 'jquery-blockui' );
477
        $autofill_api = wpinv_get_option('address_autofill_api');
478
        $autofill_active = wpinv_get_option('address_autofill_active');
479
        if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) {
480
            if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) {
481
                wp_dequeue_script( 'google-maps-api' );
482
            }
483
            wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false );
484
            wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true );
485
        }
486
487
        wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
488
        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
489
490
        wp_enqueue_script( 'wpinv-front-script' );
491
        wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
492
493
        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' );
494
        wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ),  $version, true );
495
    }
496
497
    public function admin_enqueue_scripts( $hook ) {
498
        global $post, $pagenow;
499
        
500
        $post_type  = wpinv_admin_post_type();
501
        $suffix     = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
502
        $page       = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : '';
503
504
        $jquery_ui_css = false;
505
        if ( ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $post_type == 'wpi_discount' ) && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) {
506
            $jquery_ui_css = true;
507
        } else if ( $page == 'wpinv-settings' || $page == 'wpinv-reports' ) {
508
            $jquery_ui_css = true;
509
        }
510
        if ( $jquery_ui_css ) {
511
            wp_register_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16' );
512
            wp_enqueue_style( 'jquery-ui-css' );
513
            wp_deregister_style( 'yoast-seo-select2' );
514
	        wp_deregister_style( 'yoast-seo-monorepo' );
515
        }
516
517
        wp_register_style( 'wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION );
518
        wp_enqueue_style( 'wpinv_meta_box_style' );
519
        
520
        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' );
521
        wp_register_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array(), $version );
522
        wp_enqueue_style( 'wpinv_admin_style' );
523
524
        $enqueue = ( $post_type == 'wpi_discount' || $post_type == 'wpi_invoice' && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) );
525
        if ( $page == 'wpinv-subscriptions' ) {
526
            wp_enqueue_script( 'jquery-ui-datepicker' );
527
            wp_deregister_style( 'yoast-seo-select2' );
528
	        wp_deregister_style( 'yoast-seo-monorepo' );
529
        }
530
        
531
        if ( $enqueue_datepicker = apply_filters( 'wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue ) ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $enqueue_datepicker is dead and can be removed.
Loading history...
532
            wp_enqueue_script( 'jquery-ui-datepicker' );
533
        }
534
535
        wp_enqueue_style( 'wp-color-picker' );
536
        wp_enqueue_script( 'wp-color-picker' );
537
        
538
        wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
539
540
        if (($post_type == 'wpi_invoice' || $post_type == 'wpi_quote') && ($pagenow == 'post-new.php' || $pagenow == 'post.php')) {
541
            $autofill_api = wpinv_get_option('address_autofill_api');
542
            $autofill_active = wpinv_get_option('address_autofill_active');
543
            if (isset($autofill_active) && 1 == $autofill_active && !empty($autofill_api)) {
544
                wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array('jquery'), '', false);
545
                wp_enqueue_script('google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array('jquery'), '', true);
546
            }
547
        }
548
549
        wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
550
        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
551
552
        $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' );
553
        wp_register_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-blockui','jquery-ui-tooltip' ),  $version );
554
        wp_enqueue_script( 'wpinv-admin-script' );
555
        
556
        $localize                               = array();
557
        $localize['ajax_url']                   = admin_url( 'admin-ajax.php' );
558
        $localize['post_ID']                    = isset( $post->ID ) ? $post->ID : '';
559
        $localize['wpinv_nonce']                = wp_create_nonce( 'wpinv-nonce' );
560
        $localize['add_invoice_note_nonce']     = wp_create_nonce( 'add-invoice-note' );
561
        $localize['delete_invoice_note_nonce']  = wp_create_nonce( 'delete-invoice-note' );
562
        $localize['invoice_item_nonce']         = wp_create_nonce( 'invoice-item' );
563
        $localize['billing_details_nonce']      = wp_create_nonce( 'get-billing-details' );
564
        $localize['tax']                        = wpinv_tax_amount();
565
        $localize['discount']                   = wpinv_discount_amount();
566
        $localize['currency_symbol']            = wpinv_currency_symbol();
567
        $localize['currency_pos']               = wpinv_currency_position();
568
        $localize['thousand_sep']               = wpinv_thousands_separator();
569
        $localize['decimal_sep']                = wpinv_decimal_separator();
570
        $localize['decimals']                   = wpinv_decimals();
571
        $localize['save_invoice']               = __( 'Save Invoice', 'invoicing' );
572
        $localize['status_publish']             = wpinv_status_nicename( 'publish' );
573
        $localize['status_pending']             = wpinv_status_nicename( 'wpi-pending' );
574
        $localize['delete_tax_rate']            = __( 'Are you sure you wish to delete this tax rate?', 'invoicing' );
575
        $localize['OneItemMin']                 = __( 'Invoice must contain at least one item', 'invoicing' );
576
        $localize['DeleteInvoiceItem']          = __( 'Are you sure you wish to delete this item?', 'invoicing' );
577
        $localize['FillBillingDetails']         = __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' );
578
        $localize['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' );
579
        $localize['AreYouSure']                 = __( 'Are you sure?', 'invoicing' );
580
        $localize['emptyInvoice']               = __( 'Add at least one item to save invoice!', 'invoicing' );
581
        $localize['errDeleteItem']              = __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' );
582
        $localize['delete_subscription']        = __( 'Are you sure you want to delete this subscription?', 'invoicing' );
583
        $localize['action_edit']                = __( 'Edit', 'invoicing' );
584
        $localize['action_cancel']              = __( 'Cancel', 'invoicing' );
585
        $localize['item_description']           = __( 'Item Description', 'invoicing' );
586
        $localize['discount_description']       = __( 'Discount Description', 'invoicing' );
587
        $localize['invoice_description']        = __( 'Invoice Description', 'invoicing' );
588
        $localize['searching']                  = __( 'Searching', 'invoicing' );
589
590
        $localize = apply_filters( 'wpinv_admin_js_localize', $localize );
591
592
        wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', $localize );
593
594
        // Load payment form scripts on our admin pages only.
595
        if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && 'wpi_payment_form' === $post->post_type ) {
596
597
            wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION );
598
            wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION );
599
            wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION );
600
601
            $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' );
602
            wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ),  $version );
603
        
604
            wp_localize_script( 'wpinv-admin-payment-form-script', 'wpinvPaymentFormAdmin', array(
605
                'elements'      => $this->form_elements->get_elements(),
606
                'form_elements' => $this->form_elements->get_form_elements( $post->ID ),
607
                'all_items'     => $this->form_elements->get_published_items(),
608
                'currency'      => wpinv_currency_symbol(),
609
                'position'      => wpinv_currency_position(),
610
                'decimals'      => (int) wpinv_decimals(),
611
                'thousands_sep' => wpinv_thousands_separator(),
612
                'decimals_sep'  => wpinv_decimal_separator(),
613
                'form_items'    => $this->form_elements->get_form_items( $post->ID ),
614
                'is_default'    => $post->ID == $this->default_payment_form,
615
            ) );
616
617
            wp_enqueue_script( 'wpinv-admin-payment-form-script' );
618
        }
619
620
        if ( $page == 'wpinv-subscriptions' ) {
621
            wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ),  WPINV_VERSION );
622
            wp_enqueue_script( 'wpinv-sub-admin-script' );
623
        }
624
625
        if ( $page == 'wpinv-reports' ) {
626
            wp_enqueue_script( 'jquery-flot', WPINV_PLUGIN_URL . 'assets/js/jquery.flot.min.js', array( 'jquery' ), '0.7' );
627
        }
628
629
    }
630
631
    public function admin_body_class( $classes ) {
632
        global $pagenow, $post, $current_screen;
633
        
634
        if ( !empty( $current_screen->post_type ) && ( $current_screen->post_type == 'wpi_invoice' || $current_screen->post_type == 'wpi_payment_form' || $current_screen->post_type == 'wpi_quote' ) ) {
635
            $classes .= ' wpinv-cpt';
636
        }
637
        
638
        $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false;
639
640
        $add_class = $page && $pagenow == 'admin.php' && strpos( $page, 'wpinv-' ) === 0 ? true : false;
641
        if ( $add_class ) {
642
            $classes .= ' wpi-' . wpinv_sanitize_key( $page );
643
        }
644
        
645
        $settings_class = array();
646
        if ( $page == 'wpinv-settings' ) {
647
            if ( !empty( $_REQUEST['tab'] ) ) {
648
                $settings_class[] = sanitize_text_field( $_REQUEST['tab'] );
649
            }
650
            
651
            if ( !empty( $_REQUEST['section'] ) ) {
652
                $settings_class[] = sanitize_text_field( $_REQUEST['section'] );
653
            }
654
            
655
            $settings_class[] = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' ? sanitize_text_field( $_REQUEST['wpi_sub'] ) : 'main';
656
        }
657
        
658
        if ( !empty( $settings_class ) ) {
659
            $classes .= ' wpi-' . wpinv_sanitize_key( implode( $settings_class, '-' ) );
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with '-'. ( Ignorable by Annotation )

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

659
            $classes .= ' wpi-' . wpinv_sanitize_key( /** @scrutinizer ignore-call */ implode( $settings_class, '-' ) );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
660
        }
661
        
662
        $post_type = wpinv_admin_post_type();
663
664
        if ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $add_class !== false ) {
665
            return $classes .= ' wpinv';
666
        }
667
        
668
        if ( $pagenow == 'post.php' && $post_type == 'wpi_item' && !empty( $post ) && !wpinv_item_is_editable( $post ) ) {
669
            $classes .= ' wpi-editable-n';
670
        }
671
672
        return $classes;
673
    }
674
    
675
    public function admin_print_scripts_edit_php() {
676
677
    }
678
    
679
    public function wpinv_actions() {
680
        if ( isset( $_REQUEST['wpi_action'] ) ) {
681
            do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
682
        }
683
    }
684
    
685
    public function pre_get_posts( $wp_query ) {
686
        if ( !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
687
            $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() );
688
        }
689
        
690
        return $wp_query;
691
    }
692
    
693
    public function bp_invoicing_init() {
694
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
695
    }
696
697
	/**
698
	 * Register widgets
699
	 *
700
	 */
701
	public function register_widgets() {
702
		register_widget( "WPInv_Checkout_Widget" );
703
		register_widget( "WPInv_History_Widget" );
704
		register_widget( "WPInv_Receipt_Widget" );
705
		register_widget( "WPInv_Subscriptions_Widget" );
706
		register_widget( "WPInv_Buy_Item_Widget" );
707
        register_widget( "WPInv_Messages_Widget" );
708
        register_widget( 'WPInv_GetPaid_Widget' );
709
	}
710
    
711
    /**
712
     * Remove our pages from yoast sitemaps.
713
     *
714
     * @since 1.0.19
715
     * @param int[] $excluded_posts_ids
716
     */
717
    public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){
718
719
        // Ensure that we have an array.
720
        if ( ! is_array( $excluded_posts_ids ) ) {
0 ignored issues
show
introduced by
The condition is_array($excluded_posts_ids) is always true.
Loading history...
721
            $excluded_posts_ids = array();
722
        }
723
724
        // Prepare our pages.
725
        $our_pages = array();
726
    
727
        // Checkout page.
728
        $our_pages[] = wpinv_get_option( 'checkout_page', false );
729
730
        // Success page.
731
        $our_pages[] = wpinv_get_option( 'success_page', false );
732
733
        // Failure page.
734
        $our_pages[] = wpinv_get_option( 'failure_page', false );
735
736
        // History page.
737
        $our_pages[] = wpinv_get_option( 'invoice_history_page', false );
738
739
        // Subscriptions page.
740
        $our_pages[] = wpinv_get_option( 'invoice_subscription_page', false );
741
742
        $our_pages   = array_map( 'intval', array_filter( $our_pages ) );
743
744
        $excluded_posts_ids = $excluded_posts_ids + $our_pages;
745
        return array_unique( $excluded_posts_ids );
746
747
    }
748
749
    public function wp_footer() {
750
        echo '
751
            <div class="bsui">
752
                <div  id="getpaid-payment-modal" class="modal" tabindex="-1" role="dialog">
753
                    <div class="modal-dialog modal-dialog-centered modal-lg" role="checkout" style="max-width: 650px;">
754
                        <div class="modal-content">
755
                            <div class="modal-body"></div>
756
                        </div>
757
                    </div>
758
                </div>
759
            </div>
760
        ';
761
    }
762
763
}
764