Passed
Push — master ( 0bc0f7...b64220 )
by Warwick
04:04
created

Frontend::get_the_archive_title()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
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\Endpoints();
22
	 */
23
	public $endpoints;
24
25
	/**
26
	 * @var object \lsx_health_plan\classes\Modals();
27
	 */
28
	public $modals;
29
30
	/**
31
	 * Contructor
32
	 */
33
	public function __construct() {
34
		add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 );
35
36
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-endpoints.php';
37
		$this->endpoints = Endpoints::get_instance();
38
39
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-modals.php';
40
		$this->modals = Modals::get_instance();
41
42
		require_once LSX_HEALTH_PLAN_PATH . 'classes/frontend/class-gallery.php';
43
		$this->gallery = frontend\Gallery::get_instance();
0 ignored issues
show
Bug Best Practice introduced by
The property gallery does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
		if ( is_admin() ) {
46
			add_filter( 'lsx_customizer_colour_selectors_body', array( $this, 'customizer_body_colours_handler' ), 15, 2 );
47
		}
48
49
		// Handle the template redirects.
50
		add_filter( 'template_include', array( $this, 'archive_template_include' ), 99 );
51
		add_filter( 'template_include', array( $this, 'single_template_include' ), 99 );
52
		add_filter( 'template_include', array( $this, 'taxonomy_template_include' ), 99 );
53
		add_action( 'template_redirect', array( $this, 'redirect' ) );
54
55
		add_action( 'init', array( $this, 'handle_day_action' ), 100 );
56
		add_filter( 'wp_kses_allowed_html', array( $this, 'wpkses_post_tags' ), 100, 2 );
57
58
		add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 99, 1 );
59
	}
60
61
	/**
62
	 * Return an instance of this class.
63
	 *
64
	 * @since 1.0.0
65
	 *
66
	 * @return    object \lsx_health_plan\classes\Frontend()    A single instance of this class.
67
	 */
68
	public static function get_instance() {
69
		// If the single instance hasn't been set, set it now.
70
		if ( null === self::$instance ) {
71
			self::$instance = new self();
72
		}
73
		return self::$instance;
74
	}
75
76
	/**
77
	 * Registers the plugin frontend assets
78
	 *
79
	 * @return void
80
	 */
81
	public function assets() {
82
		wp_enqueue_style( 'lsx-health-plan', LSX_HEALTH_PLAN_URL . 'assets/css/lsx-health-plan.css', array(), LSX_HEALTH_PLAN_VER );
83
		wp_style_add_data( 'lsx-health-plan', 'rtl', 'replace' );
84
		wp_enqueue_script( 'lsx-health-plan-scripts', LSX_HEALTH_PLAN_URL . 'assets/js/src/lsx-health-plan-admin.js', array( 'jquery' ) );
85
	}
86
87
	/**
88
	 * Handle body colours that might be change by LSX Customizer.
89
	 */
90
	public function customizer_body_colours_handler( $css, $colors ) {
91
		$css .= '
92
			@import "' . LSX_HEALTH_PLAN_PATH . '/assets/css/scss/partials/customizer-health-plan-body-colours";
93
94
			/**
95
			 * LSX Customizer - Body (LSX Health Plan)
96
			 */
97
			@include customizer-health-plan-body-colours (
98
				$bg: 		' . $colors['background_color'] . ',
99
				$breaker: 	' . $colors['body_line_color'] . ',
100
				$color:    	' . $colors['body_text_color'] . ',
101
				$link:    	' . $colors['body_link_color'] . ',
102
				$hover:    	' . $colors['body_link_hover_color'] . ',
103
				$small:    	' . $colors['body_text_small_color'] . '
104
			);
105
		';
106
107
		return $css;
108
	}
109
110
	/**
111
	 * Archive template.
112
	 */
113
	public function archive_template_include( $template ) {
114
		$applicable_post_types = apply_filters( 'lsx_health_plan_archive_template', array() );
115
		if ( ! empty( $applicable_post_types ) && is_main_query() && is_post_type_archive( $applicable_post_types ) ) {
116
			$post_type = get_post_type();
117
			if ( empty( locate_template( array( 'archive-' . $post_type . '.php' ) ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/archive-' . $post_type . '.php' ) ) {
118
				$template = LSX_HEALTH_PLAN_PATH . 'templates/archive-' . $post_type . '.php';
119
			}
120
		}
121
		return $template;
122
	}
123
124
	/**
125
	 * Single template.
126
	 */
127
	public function single_template_include( $template ) {
128
		$applicable_post_types = apply_filters( 'lsx_health_plan_single_template', array() );
129
		if ( ! empty( $applicable_post_types ) && is_main_query() && is_singular( $applicable_post_types ) ) {
130
			$post_type = get_post_type();
131
			if ( empty( locate_template( array( 'single-' . $post_type . '.php' ) ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/single-' . $post_type . '.php' ) ) {
132
				$template = LSX_HEALTH_PLAN_PATH . 'templates/single-' . $post_type . '.php';
133
			}
134
		}
135
		return $template;
136
	}
137
138
	/**
139
	 * Redirect WordPress to the taxonomy located in the plugin
140
	 *
141
	 * @param     $template string
142
	 * @return    string
143
	 */
144
	public function taxonomy_template_include( $template ) {
145
		$applicable_taxonomies = apply_filters( 'lsx_health_plan_taxonomies_template', array() );
146
		if ( is_main_query() && is_tax( $applicable_taxonomies ) ) {
147
			$current_taxonomy = get_query_var( 'taxonomy' );
148
			if ( '' === locate_template( array( 'taxonomy-' . $current_taxonomy . '.php' ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php' ) ) {
149
				$template = LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php';
150
			}
151
		}
152
		return $template;
153
	}
154
155
	/**
156
	 * Redirect the user from the cart or checkout page if they have purchased the product already.
157
	 *
158
	 * @return void
159
	 */
160
	public function redirect() {
161
		if ( ! is_user_logged_in() || ! function_exists( 'wc_get_page_id' ) || is_home() ) {
162
			return;
163
		}
164
		if ( lsx_health_plan_user_has_purchase() && ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) ) ) {
165
			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

165
			wp_redirect( /** @scrutinizer ignore-type */ get_permalink( wc_get_page_id( 'myaccount' ) ) );
Loading history...
166
			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...
167
		}
168
169
		$product_id = \lsx_health_plan\functions\get_option( 'membership_product', false );
170
		if ( false !== $product_id && is_single( $product_id ) ) {
171
			wp_redirect( home_url() );
172
			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...
173
		}
174
	}
175
176
	/**
177
	 * Registers the rewrites.
178
	 */
179
	public function handle_day_action() {
180
		if ( isset( $_POST['lsx-health-plan-actions'] ) && wp_verify_nonce( $_POST['lsx-health-plan-actions'], 'complete' ) ) {
181
			update_user_meta( get_current_user_id(), 'day_' . sanitize_key( $_POST['lsx-health-plan-id'] ) . '_complete', true );
182
			wp_safe_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_safe_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

182
			wp_safe_redirect( /** @scrutinizer ignore-type */ get_permalink( wc_get_page_id( 'myaccount' ) ) );
Loading history...
Bug introduced by
The function wc_get_page_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

182
			wp_safe_redirect( get_permalink( /** @scrutinizer ignore-call */ wc_get_page_id( 'myaccount' ) ) );
Loading history...
183
		}
184
185
		if ( isset( $_POST['lsx-health-plan-actions'] ) && wp_verify_nonce( $_POST['lsx-health-plan-actions'], 'unlock' ) ) {
186
			delete_user_meta( get_current_user_id(), 'day_' . sanitize_key( $_POST['lsx-health-plan-id'] ) . '_complete' );
187
		}
188
	}
189
190
	/**
191
	 * Registers the rewrites.
192
	 */
193
	public function wpkses_post_tags( $tags, $context ) {
194
		if ( 'post' === $context ) {
195
			$tags['iframe'] = array(
196
				'src'             => true,
197
				'height'          => true,
198
				'width'           => true,
199
				'frameborder'     => true,
200
				'allowfullscreen' => true,
201
			);
202
		}
203
		return $tags;
204
	}
205
	/**
206
	 * Remove the "Archives:" from the post type recipes.
207
	 *
208
	 * @param string $title the term title.
209
	 * @return string
210
	 */
211
	public function get_the_archive_title( $title ) {
212
		if ( is_tax() ) {
213
			$queried_object = get_queried_object();
214
			if ( isset( $queried_object->name ) ) {
215
				$title = $queried_object->name;
216
			}
217
		}
218
		return $title;
219
	}
220
}
221