Issues (4138)

classes/integrations/woocommerce/class-plans.php (8 issues)

1
<?php
2
namespace lsx_health_plan\classes\integrations\woocommerce;
3
4
/**
5
 * Contains the downloads functions post type
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Plans {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\integrations\woocommerce\Plans()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * Holds the current screen var if it is active.
22
	 *
23
	 * @var string
24
	 */
25
	public $screen = '';
26
27
	/**
28
	 * Holds the current array of product IDS.
29
	 *
30
	 * @var array
31
	 */
32
	public $product_ids = array();
33
34
	/**
35
	 * Holds the curret parent ID.
36
	 *
37
	 * @var int
38
	 */
39
	public $parent_id = 0;
40
41
	/**
42
	 * Constructor
43
	 */
44
	public function __construct() {
45
		// Remove the default restrictions, as we will add our own.
46
		add_action( 'wp', array( $this, 'set_screen' ), 1 );
47
		add_action( 'wp', array( $this, 'disable_parent_plan_restrictions' ), 2 );
48
		add_action( 'wp', array( $this, 'child_plan_redirect_restrictions' ), 2 );
49
50
		// Initiate the WP Head functions.
51
		add_action( 'wp_head', array( $this, 'set_screen' ) );
52
		add_action( 'lsx_content_top', 'lsx_hp_single_plan_products' );
53
54
		// Plan Archive Actions.
55
		add_action( 'lsx_entry_before', array( $this, 'set_product_ids' ) );
56
	}
57
58
	/**
59
	 * Return an instance of this class.
60
	 *
61
	 * @since 1.0.0
62
	 *
63
	 * @return    object \lsx_health_plan\classes\integrations\woocommerce\Plans()    A single instance of this class.
64
	 */
65
	public static function get_instance() {
66
		// If the single instance hasn't been set, set it now.
67
		if ( null === self::$instance ) {
68
			self::$instance = new self();
69
		}
70
		return self::$instance;
71
	}
72
73
	/**
74
	 * Define the product metabox on the plan post type
75
	 */
76
	public function set_screen() {
77
		if ( is_singular( array( 'plan' ) ) ) {
78
			$section = get_query_var( 'section' );
79
			if ( ! empty( $section ) ) {
80
				$this->screen = 'child_plan';
81
			} else {
82
				$this->screen = 'parent_plan';
83
			}
84
			$product_ids = get_post_meta( get_the_ID(), 'plan_product', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, 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

84
			$product_ids = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), 'plan_product', true );
Loading history...
85
			if ( false !== $product_ids && ! empty( $product_ids ) ) {
86
				$this->product_ids = $product_ids;
0 ignored issues
show
Documentation Bug introduced by
It seems like $product_ids can also be of type string. However, the property $product_ids is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87
			}
88
		}
89
		if ( is_singular( array( 'workout', 'meal' ) ) ) {
90
			$parent_id = wp_get_post_parent_id( get_the_ID() );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post of wp_get_post_parent_id() does only seem to accept WP_Post|integer|null, 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

90
			$parent_id = wp_get_post_parent_id( /** @scrutinizer ignore-type */ get_the_ID() );
Loading history...
91
			if ( 0 === $parent_id || false === $parent_id ) {
92
				$this->screen = 'parent_plan';
93
			} else {
94
				$this->screen = 'child_plan';
95
			}
96
		}
97
		if ( is_post_type_archive( array( 'plan', 'workout', 'meal' ) ) || is_tax( array( 'plan-type', 'workout-type', 'meal-type' ) ) ) {
98
			$this->screen = 'plan_archive';
99
		}
100
	}
101
102
	/**
103
	 * Sets the post type archive product ids.
104
	 *
105
	 * @return void
106
	 */
107
	public function set_product_ids() {
108
		$this->product_ids = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $product_ids.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
109
		if ( 'plan' === get_post_type() ) {
110
			$product_ids = get_post_meta( get_the_ID(), 'plan_product', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, 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

110
			$product_ids = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), 'plan_product', true );
Loading history...
111
			if ( false !== $product_ids && ! empty( $product_ids ) ) {
112
				$this->product_ids = $product_ids;
113
			}
114
		}
115
	}
116
117
	/**
118
	 * Disable WC Memberships restrictions for plan parents. We add our own custom
119
	 * restriction functionality elsewhere.
120
	 */
121
	public function disable_parent_plan_restrictions() {
122
		if ( '' === $this->screen || ! function_exists( 'wc_memberships' ) ) {
123
			return;
124
		}
125
126
		$restrictions = wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance();
127
		remove_action( 'wp', array( $restrictions, 'handle_restriction_modes' ) );
128
	}
129
130
	/**
131
	 * Disable WC Memberships restrictions for plan parents. We add our own custom
132
	 * restriction functionality elsewhere.
133
	 */
134
	public function child_plan_redirect_restrictions() {
135
		if ( ! function_exists( 'wc_memberships' ) || ! is_singular( 'plan' ) || 'child_plan' !== $this->screen || ! function_exists( 'wc_memberships_is_post_content_restricted' ) ) {
136
			return;
137
		}
138
		$restricted = wc_memberships_is_post_content_restricted() && ! current_user_can( 'wc_memberships_view_restricted_post_content', get_the_ID() );
139
		if ( true === $restricted ) {
140
			wp_redirect( get_permalink( get_the_ID() ) );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post of get_permalink() does only seem to accept WP_Post|integer, 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

140
			wp_redirect( get_permalink( /** @scrutinizer ignore-type */ get_the_ID() ) );
Loading history...
It seems like get_permalink(get_the_ID()) 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

140
			wp_redirect( /** @scrutinizer ignore-type */ get_permalink( get_the_ID() ) );
Loading history...
141
			exit;
0 ignored issues
show
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...
142
		}
143
	}
144
145
	/**
146
	 * Returns the ids of the attached products.
147
	 *
148
	 * @return array
149
	 */
150
	public function get_products() {
151
		return $this->product_ids;
152
	}
153
}
154