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 ); |
|
|
|
|
85
|
|
|
if ( false !== $product_ids && ! empty( $product_ids ) ) { |
86
|
|
|
$this->product_ids = $product_ids; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if ( is_singular( array( 'workout', 'meal' ) ) ) { |
90
|
|
|
$parent_id = wp_get_post_parent_id( get_the_ID() ); |
|
|
|
|
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; |
|
|
|
|
109
|
|
|
if ( 'plan' === get_post_type() ) { |
110
|
|
|
$product_ids = get_post_meta( get_the_ID(), 'plan_product', true ); |
|
|
|
|
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() ) ); |
|
|
|
|
141
|
|
|
exit; |
|
|
|
|
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
|
|
|
|