Passed
Push — master ( b8bd86...c2d39d )
by Warwick
04:43 queued 02:29
created

LSX_Rest_Helper::is_rest_api_request()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
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;
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
56
	 * @param array $request
57
	 * @return void
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.

This check looks from 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;
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 ) );
79
		return $this->is_rest_request;
80
	}
81
}
82
$rest_helper = LSX_Rest_Helper::get_instance();
83