Passed
Branch add/multiplan (30efbe)
by Warwick
08:45 queued 04:00
created

Frontend   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 34
c 3
b 0
f 0
dl 0
loc 103
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 6 2
A load_classes() 0 18 1
A __construct() 0 4 2
B redirect() 0 13 9
1
<?php
2
namespace lsx_health_plan\classes;
3
4
/**
5
 * LSX Health Plan Frontend Class.
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Frontend {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\Frontend()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * @var object \lsx_health_plan\classes\frontend\Endpoints();
22
	 */
23
	public $endpoints;
24
25
	/**
26
	 * @var object \lsx_health_plan\classes\frontend\Gallery();
27
	 */
28
	public $gallery;
29
30
	/**
31
	 * @var object \lsx_health_plan\classes\frontend\Plan_Status();
32
	 */
33
	public $plan_status;
34
35
	/**
36
	 * @var object \lsx_health_plan\classes\frontend\General();
37
	 */
38
	public $general;
39
40
	/**
41
	 * @var object \lsx_health_plan\classes\frontend\Template_Redirects();
42
	 */
43
	public $template_redirects;
44
45
	/**
46
	 * Contructor
47
	 */
48
	public function __construct() {
49
		if ( ! is_admin() ) {
50
			$this->load_classes();
51
			add_action( 'template_redirect', array( $this, 'redirect' ) );
52
			
53
		}	
54
	}
55
56
	/**
57
	 * Return an instance of this class.
58
	 *
59
	 * @since 1.0.0
60
	 *
61
	 * @return    object \lsx_health_plan\classes\Frontend()    A single instance of this class.
62
	 */
63
	public static function get_instance() {
64
		// If the single instance hasn't been set, set it now.
65
		if ( null === self::$instance ) {
66
			self::$instance = new self();
67
		}
68
		return self::$instance;
69
	}
70
71
	/**
72
	 * Loads the variable classes and the static classes.
73
	 */
74
	private function load_classes() {
75
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-endpoints.php';
76
		$this->endpoints = frontend\Endpoints::get_instance();
77
78
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-modals.php';
79
		$this->modals = Modals::get_instance();
0 ignored issues
show
Bug Best Practice introduced by
The property modals does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
81
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-gallery.php';
82
		$this->gallery = frontend\Gallery::get_instance();
83
84
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-plan-status.php';
85
		$this->plan_status = frontend\Plan_Status::get_instance();
86
87
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-general.php';
88
		$this->general = frontend\General::get_instance();
89
90
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-template-redirects.php';
91
		$this->template_redirects = frontend\Template_Redirects::get_instance();
92
	}
93
94
	/**
95
	 * Redirect the user from the cart or checkout page if they have purchased the product already.
96
	 *
97
	 * @return void
98
	 */
99
	public function redirect() {
100
		if ( ! is_user_logged_in() || ! function_exists( 'wc_get_page_id' ) || is_home() ) {
101
			return;
102
		}
103
		if ( lsx_health_plan_user_has_purchase() && ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) ) ) {
104
			wp_redirect( get_permalink( wc_get_page_id( 'myaccount' ) ) );
0 ignored issues
show
Bug introduced by
It seems like get_permalink(wc_get_page_id('myaccount')) can also be of type false; however, parameter $location of wp_redirect() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
			wp_redirect( /** @scrutinizer ignore-type */ get_permalink( wc_get_page_id( 'myaccount' ) ) );
Loading history...
105
			die;
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...
106
		}
107
108
		$product_id = \lsx_health_plan\functions\get_option( 'membership_product', false );
109
		if ( false !== $product_id && is_single( $product_id ) ) {
110
			wp_redirect( home_url() );
111
			die;
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...
112
		}
113
	}
114
}
115