Test Failed
Push — master ( e52611...5d5118 )
by Paul
04:30
created

Router::routeAjaxRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\AdminController;
7
use GeminiLabs\SiteReviews\Controllers\AjaxController;
8
use GeminiLabs\SiteReviews\Controllers\PublicController;
9
use GeminiLabs\SiteReviews\Helper;
10
11
class Router
12
{
13
	/**
14
	 * @return void
15
	 */
16
	public function routeAdminPostRequest()
17
	{
18
		$request = $this->getRequest();
19
		if( !$this->isValidPostRequest( $request ))return;
20
		check_admin_referer( $request['action'] );
21
		$this->routeRequest( 'admin', $request['action'], $request );
22
	}
23
24
	/**
25
	 * @return void
26
	 */
27
	public function routeAjaxRequest()
28
	{
29
		$request = $this->getRequest();
30
		$this->checkAjaxRequest( $request );
31
		$this->checkAjaxNonce( $request );
32
		$this->routeRequest( 'ajax', $request['action'], $request );
33
		wp_die();
34
	}
35
36
	/**
37
	 * @return void
38
	 */
39
	public function routePublicPostRequest()
40
	{
41
		if( is_admin() )return;
42
		$request = $this->getRequest();
43
		if( !$this->isValidPostRequest( $request ))return;
44
		if( !$this->isValidPublicNonce( $request ))return;
45
		$this->routeRequest( 'public', $request['action'], $request );
46
	}
47
48
	/**
49
	 * @return void
50
	 */
51
	protected function checkAjaxNonce( array $request )
52
	{
53
		if( !is_user_logged_in() )return;
54
		if( !isset( $request['nonce'] )) {
55
			$this->sendAjaxError( 'The request is missing a nonce', $request );
56
		}
57
		if( !wp_verify_nonce( $request['nonce'], $request['action'] )) {
58
			$this->sendAjaxError( 'The request failed the nonce check', $request, 403 );
59
		}
60
	}
61
62
	/**
63
	 * @return void
64
	 */
65
	protected function checkAjaxRequest( array $request )
66
	{
67
		if( !isset( $request['action'] )) {
68
			$this->sendAjaxError( 'The request must include an action', $request );
69
		}
70
		if( empty( $request['ajax_request'] )) {
71
			$this->sendAjaxError( 'The request is invalid', $request );
72
		}
73
	}
74
75
	/**
76
	 * All ajax requests in the plugin are triggered by a single action hook: glsr_action,
77
	 * while each route is determined by $_POST[request][action]
78
	 * @return array
79
	 */
80
	protected function getRequest()
81
	{
82
		if( glsr( Helper::class )->filterInput( 'action' ) != Application::PREFIX.'action' ) {
83
			// not an ajax request
84
			return glsr( Helper::class )->filterInputArray( Application::ID );
85
		}
86
		$request = glsr( Helper::class )->filterInputArray( 'request' );
87
		if( empty( $request )) {
88
			$request = glsr( Helper::class )->filterInputArray( Application::ID );
89
		}
90
		return $this->normalizeRequest( $request );
91
	}
92
93
	/**
94
	 * @return bool
95
	 */
96
	protected function isValidPostRequest( array $request = [] )
97
	{
98
		return !empty( $request['action'] ) && empty( $request['ajax_request'] );
99
	}
100
101
	/**
102
	 * @return bool
103
	 */
104
	protected function isValidPublicNonce( array $request )
105
	{
106
		if( is_user_logged_in() && !wp_verify_nonce( $request['nonce'], $request['action'] )) {
107
			glsr_log()->error( 'Nonce check failed for public request' )->info( $request );
108
			return false;
109
		}
110
		return true;
111
	}
112
113
	/**
114
	 * @return array
115
	 */
116
	protected function normalizeRequest( array $request )
117
	{
118
		if( glsr( Helper::class )->filterInput( 'action' ) == Application::PREFIX.'action' ) {
119
			$request['ajax_request'] = true;
120
		}
121
		if( glsr( Helper::class )->filterInput( 'action', $request ) == 'submit-review' ) {
122
			$request['recaptcha-token'] = glsr( Helper::class )->filterInput( 'g-recaptcha-response' );
123
		}
124
		return $request;
125
	}
126
127
	/**
128
	 * @param string $type
129
	 * @param string $action
130
	 * @return void
131
	 */
132
	protected function routeRequest( $type, $action, array $request = [] )
133
	{
134
		$actionHook = 'site-reviews/route/'.$type.'/request';
135
		$controller = glsr( glsr( Helper::class )->buildClassName( $type.'-controller', 'Controllers' ));
136
		$method = glsr( Helper::class )->buildMethodName( $action, 'router' );
137
		$request = apply_filters( 'site-reviews/route/request', $request, $action, $type );
138
		do_action( $actionHook, $action, $request );
139
		if( is_callable( [$controller, $method] )) {
140
			call_user_func( [$controller, $method], $request );
141
			return;
142
		}
143
		if( did_action( $actionHook ) === 0 ) {
144
			glsr_log( 'Unknown '.$type.' router request: '.$action );
145
		}
146
	}
147
148
	/**
149
	 * @param string $error
150
	 * @param int $statusCode
151
	 * @return void
152
	 */
153
	protected function sendAjaxError( $error, array $request, $statusCode = 400 )
154
	{
155
		glsr_log()->error( $error )->info( $request );
156
		wp_send_json_error([
157
			'message' => __( 'The form could not be submitted. Please notify the site administrator.', 'site-reviews' ),
158
			'error' => $error,
159
		], $statusCode );
160
	}
161
}
162