Issues (4138)

classes/frontend/class-template-redirects.php (48 issues)

1
<?php
0 ignored issues
show
This file is missing a doc comment.
Loading history...
2
namespace lsx_health_plan\classes\frontend;
3
4
/**
5
 * Holds the functionality to for the template redirects.
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Template_Redirects {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\frontend\Template_Redirects()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * Constructor
22
	 */
23
	public function __construct() {
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
24
		add_filter( 'template_include', array( $this, 'archive_template_include' ), 99 );
25
		add_filter( 'template_include', array( $this, 'single_template_include' ), 99 );
26
		add_filter( 'template_include', array( $this, 'taxonomy_template_include' ), 99 );
27
		add_action( 'wp', array( $this, 'redirect_restrictions' ), 99 );
28
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
29
30
	/**
31
	 * Return an instance of this class.
32
	 *
33
	 * @since 1.0.0
34
	 *
35
	 * @return    object \lsx_health_plan\classes\frontend\Template_Redirects()    A single instance of this class.
36
	 */
37
	public static function get_instance() {
38
		// If the single instance hasn't been set, set it now.
39
		if ( null === self::$instance ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
40
			self::$instance = new self();
41
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
42
		return self::$instance;
43
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
44
45
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$template" missing
Loading history...
46
	 * Archive template.
47
	 */
48
	public function archive_template_include( $template ) {
49
		$applicable_post_types = apply_filters( 'lsx_health_plan_archive_template', array() );
50
		if ( ! empty( $applicable_post_types ) && is_main_query() && is_post_type_archive( $applicable_post_types ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
51
			$post_type = get_post_type();
52
			if ( empty( locate_template( array( 'archive-' . $post_type . '.php' ) ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/archive-' . $post_type . '.php' ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
53
				$template = LSX_HEALTH_PLAN_PATH . 'templates/archive-' . $post_type . '.php';
54
			}
55
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
56
		return $template;
57
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
58
59
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$template" missing
Loading history...
60
	 * Single template.
61
	 */
62
	public function single_template_include( $template ) {
63
		$applicable_post_types = apply_filters( 'lsx_health_plan_single_template', array() );
64
		if ( ! empty( $applicable_post_types ) && is_main_query() && is_singular( $applicable_post_types ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
65
			$post_type = get_post_type();
66
			if ( empty( locate_template( array( 'single-' . $post_type . '.php' ) ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/single-' . $post_type . '.php' ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
67
				$template = LSX_HEALTH_PLAN_PATH . 'templates/single-' . $post_type . '.php';
68
			}
69
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
70
		return $template;
71
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
72
73
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$template" missing
Loading history...
74
	 * Redirect WordPress to the taxonomy located in the plugin
75
	 *
76
	 * @param     $template string
0 ignored issues
show
Missing parameter name
Loading history...
77
	 * @return    string
78
	 */
79
	public function taxonomy_template_include( $template ) {
80
		$applicable_taxonomies = apply_filters( 'lsx_health_plan_taxonomies_template', array() );
81
		if ( is_main_query() && is_tax( $applicable_taxonomies ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
82
			$current_taxonomy = get_query_var( 'taxonomy' );
83
			if ( '' === locate_template( array( 'taxonomy-' . $current_taxonomy . '.php' ) ) && file_exists( LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php' ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
84
				$template = LSX_HEALTH_PLAN_PATH . 'templates/taxonomy-' . $current_taxonomy . '.php';
85
			}
86
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
87
		return $template;
88
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
89
90
	/**
91
	 * Disable WC Memberships restrictions for plan parents. We add our own custom
92
	 * restriction functionality elsewhere.
93
	 */
94
	public function redirect_restrictions() {
95
		if ( function_exists( 'WC' ) && ! is_user_logged_in() ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
96
			if ( is_post_type_archive( array( 'recipe', 'exercise', 'meal', 'workout' ) )
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
97
				|| is_tax( array( 'meal-type', 'workout-type', 'recipe-type', 'recipe-cuisine', 'exercise-type', 'equipment', 'muscle-group' ) )
98
				|| is_single( 'recipe', 'exercise' ) ) {
0 ignored issues
show
The call to is_single() has too many arguments starting with 'exercise'. ( Ignorable by Annotation )

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

98
				|| /** @scrutinizer ignore-call */ is_single( 'recipe', 'exercise' ) ) {

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...
Blank line found at start of control structure
Loading history...
99
100
				$redirect = \lsx_health_plan\functions\get_option( 'my_plan_slug', '/' );
101
				if ( function_exists( 'wc_memberships' ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
102
					$restriction_mode = wc_memberships()->get_restrictions_instance()->get_restriction_mode();
103
					if ( 'redirect' === $restriction_mode ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
104
						$page_id = wc_memberships()->get_restrictions_instance()->get_restricted_content_redirect_page_id();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
						$redirect = get_permalink( $page_id );
106
					}
107
				}
108
109
				wp_redirect( $redirect );
0 ignored issues
show
wp_redirect() found. Using wp_safe_redirect(), along with the allowed_redirect_hosts filter if needed, can help avoid any chances of malicious redirects within code. It is also important to remember to call exit() after a redirect so that no other unwanted code is executed.
Loading history...
110
				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...
111
			}
112
		}
113
	}
0 ignored issues
show
Expected 2 blank lines after function; 0 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
114
}
115