LSX_Rest_Helper::check_event_request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
This file is missing a doc comment.
Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Holds functions for rest api specific actions.
9
 *
10
 * @author   LightSpeed
11
 * @category Widgets
12
 * @package  LSX
13
 * @return   LSX_Rest_Helper
14
 */
15
class LSX_Rest_Helper {
16
17
	/**
18
	 * Holds class instance
19
	 *
20
	 * @since 1.0.0
21
	 * @var      object
22
	 */
23
	protected static $instance = null;
24
25
	/**
26
	 * Holds the conditional.
27
	 *
28
	 * @var boolean
29
	 */
30
	protected $is_rest_request = false;
31
32
	/**
33
	 * Constructor.
34
	 */
35
	public function __construct() {
36
		add_filter( 'tribe_events_views_v2_rest_params', array( $this, 'check_event_request' ), 10, 2 );
37
	}
38
	/**
39
	 * Return an instance of this class.
40
	 *
41
	 * @since 1.0.0
42
	 * @return    object    A single instance of this class.
43
	 */
44
	public static function get_instance() {
45
		// If the single instance hasn't been set, set it now.
46
		if ( null === self::$instance ) {
47
			self::$instance = new self;
0 ignored issues
show
introduced by
Parenthesis should always be used when instantiating a new object.
Loading history...
48
		}
49
		return self::$instance;
50
	}
51
52
	/**
53
	 * This will set the 'is_rest_request' variable as true if it runs. Tribe has already done the checkes for us.
54
	 *
55
	 * @param array $params
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
56
	 * @param array $request
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
57
	 * @return void
0 ignored issues
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
58
	 */
59
	public function check_event_request( $params, $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

59
	public function check_event_request( $params, /** @scrutinizer ignore-unused */ $request ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
		$this->is_rest_request = true;
61
		return $params;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $params returns the type array which is incompatible with the documented return type void.
Loading history...
62
	}
63
64
	/**
65
	 * Determines if the request is an REST API request.
66
	 *
67
	 * @return bool True if it's a REST API request, false otherwise.
68
	 */
69
	public function is_rest_api_request() {
70
		if ( true === $this->is_rest_request ) {
71
			return $this->is_rest_request;
72
		}
73
74
		if ( empty( $_SERVER['REQUEST_URI'] ) ) {
75
			return false;
76
		}
77
		$rest_prefix         = trailingslashit( rest_get_url_prefix() );
78
		$this->is_rest_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) );
0 ignored issues
show
introduced by
$_SERVER data not unslashed before sanitization. Use wp_unslash() or similar
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_SERVER['REQUEST_URI']
Loading history...
79
		return $this->is_rest_request;
80
	}
81
}
82
$rest_helper = LSX_Rest_Helper::get_instance();
83