@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | /** @var ServerRequestInterface */ |
35 | 35 | protected $server_request; |
36 | 36 | |
37 | - public function __construct( ServerRequestInterface $server_request ) { |
|
37 | + public function __construct(ServerRequestInterface $server_request) { |
|
38 | 38 | $this->server_request = $server_request; |
39 | 39 | } |
40 | 40 | |
@@ -44,22 +44,22 @@ discard block |
||
44 | 44 | * @param \PinkCrab\Ajax\Ajax $ajax |
45 | 45 | * @return bool |
46 | 46 | */ |
47 | - public function validate( Ajax $ajax ): bool { |
|
48 | - if ( ! $ajax->has_nonce() ) { |
|
47 | + public function validate(Ajax $ajax): bool { |
|
48 | + if ( ! $ajax->has_nonce()) { |
|
49 | 49 | return true; |
50 | 50 | } |
51 | 51 | |
52 | 52 | // Find nonce value in request |
53 | - $nonce_value = $this->find_nonce( $ajax->get_nonce_field() ); |
|
53 | + $nonce_value = $this->find_nonce($ajax->get_nonce_field()); |
|
54 | 54 | |
55 | 55 | // If no nonce value found in request. |
56 | - if ( is_null( $nonce_value ) ) { |
|
56 | + if (is_null($nonce_value)) { |
|
57 | 57 | return false; |
58 | 58 | } |
59 | 59 | |
60 | 60 | /* @phpstan-ignore-next-line, nonce handle checked at start of method*/ |
61 | - return ( new Nonce( $ajax->get_nonce_handle() ) ) |
|
62 | - ->validate( $nonce_value ); |
|
61 | + return (new Nonce($ajax->get_nonce_handle())) |
|
62 | + ->validate($nonce_value); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -68,11 +68,11 @@ discard block |
||
68 | 68 | * @param string $nonce_field |
69 | 69 | * @return string|null |
70 | 70 | */ |
71 | - protected function find_nonce( string $nonce_field ): ?string { |
|
72 | - $args = Ajax_Helper::extract_server_request_args( $this->server_request ); |
|
71 | + protected function find_nonce(string $nonce_field): ?string { |
|
72 | + $args = Ajax_Helper::extract_server_request_args($this->server_request); |
|
73 | 73 | |
74 | - return \array_key_exists( $nonce_field, $args ) |
|
75 | - ? $args[ $nonce_field ] |
|
74 | + return \array_key_exists($nonce_field, $args) |
|
75 | + ? $args[$nonce_field] |
|
76 | 76 | : null; |
77 | 77 | } |
78 | 78 |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | /** @var Ajax_Controller */ |
38 | 38 | protected $ajax_controller; |
39 | 39 | |
40 | - public function __construct( Ajax_Controller $ajax_controller ) { |
|
40 | + public function __construct(Ajax_Controller $ajax_controller) { |
|
41 | 41 | $this->loader = new Hook_Loader(); |
42 | 42 | $this->ajax_controller = $ajax_controller; |
43 | 43 | } |
@@ -49,15 +49,15 @@ discard block |
||
49 | 49 | * @return void |
50 | 50 | * @throws Ajax_Exception (code 101) If no action defined |
51 | 51 | */ |
52 | - public function add_ajax_call( Ajax $ajax ): void { |
|
52 | + public function add_ajax_call(Ajax $ajax): void { |
|
53 | 53 | |
54 | - if ( ! $ajax->has_valid_action() ) { |
|
55 | - throw Ajax_Exception::undefined_action( \get_class( $ajax ) ); |
|
54 | + if ( ! $ajax->has_valid_action()) { |
|
55 | + throw Ajax_Exception::undefined_action(\get_class($ajax)); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | $this->loader->ajax( |
59 | 59 | $ajax->get_action(), /* @phpstan-ignore-line, action existance checked above */ |
60 | - $this->ajax_controller->create_callback( $ajax ), |
|
60 | + $this->ajax_controller->create_callback($ajax), |
|
61 | 61 | $ajax->get_logged_out(), |
62 | 62 | $ajax->get_logged_in() |
63 | 63 | ); |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | /** @var HTTP */ |
36 | 36 | protected $http; |
37 | 37 | |
38 | - public function __construct( HTTP $http ) { |
|
38 | + public function __construct(HTTP $http) { |
|
39 | 39 | $this->http = $http; |
40 | 40 | } |
41 | 41 | |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | * @param int $code The HTTP status code. Defaults to 200. |
46 | 46 | * @param string $reasonPhrase |
47 | 47 | */ |
48 | - public function createResponse( int $code = 200, string $reasonPhrase = '' ): ResponseInterface { |
|
48 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface { |
|
49 | 49 | return $this->http->psr7_response() |
50 | - ->withStatus( $code ) |
|
51 | - ->withHeader( 'Content-Type', 'application/json; charset=' . get_option( 'blog_charset' ) ) |
|
52 | - ->withBody( Stream::create( $reasonPhrase ) ); |
|
50 | + ->withStatus($code) |
|
51 | + ->withHeader('Content-Type', 'application/json; charset=' . get_option('blog_charset')) |
|
52 | + ->withBody(Stream::create($reasonPhrase)); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * @param array<mixed> $payload |
59 | 59 | * @return ResponseInterface |
60 | 60 | */ |
61 | - public function success( array $payload = array() ): ResponseInterface { |
|
62 | - return $this->createResponse( 200, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
61 | + public function success(array $payload = array()): ResponseInterface { |
|
62 | + return $this->createResponse(200, json_encode($payload) ?: ''); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -69,8 +69,8 @@ discard block |
||
69 | 69 | * @param array<mixed> $payload |
70 | 70 | * @return ResponseInterface |
71 | 71 | */ |
72 | - public function unauthorised( array $payload = array( 'error' => 'unauthorised' ) ): ResponseInterface { |
|
73 | - return $this->createResponse( 401, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
72 | + public function unauthorised(array $payload = array('error' => 'unauthorised')): ResponseInterface { |
|
73 | + return $this->createResponse(401, json_encode($payload) ?: ''); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | * @param array<mixed> $payload |
81 | 81 | * @return ResponseInterface |
82 | 82 | */ |
83 | - public function failure( array $payload = array( 'error' => 'error' ) ): ResponseInterface { |
|
84 | - return $this->createResponse( 500, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
83 | + public function failure(array $payload = array('error' => 'error')): ResponseInterface { |
|
84 | + return $this->createResponse(500, json_encode($payload) ?: ''); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @param array<mixed> $payload |
92 | 92 | * @return ResponseInterface |
93 | 93 | */ |
94 | - public function not_found( array $payload = array( 'error' => 'not found' ) ): ResponseInterface { |
|
95 | - return $this->createResponse( 404, json_encode( $payload ) ?: '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
94 | + public function not_found(array $payload = array('error' => 'not found')): ResponseInterface { |
|
95 | + return $this->createResponse(404, json_encode($payload) ?: ''); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
|
96 | 96 | } |
97 | 97 | } |
@@ -41,18 +41,18 @@ |
||
41 | 41 | public static function use(): void { |
42 | 42 | add_filter( |
43 | 43 | Hooks::APP_INIT_SET_DI_RULES, |
44 | - function( array $rules ): array { |
|
44 | + function(array $rules): array { |
|
45 | 45 | |
46 | 46 | // Ensure the global rules exist. |
47 | - if ( ! \array_key_exists( '*', $rules ) ) { |
|
47 | + if ( ! \array_key_exists('*', $rules)) { |
|
48 | 48 | $rules['*'] = array(); |
49 | 49 | } |
50 | - if ( ! \array_key_exists( 'substitutions', $rules['*'] ) ) { |
|
50 | + if ( ! \array_key_exists('substitutions', $rules['*'])) { |
|
51 | 51 | $rules['*']['substitutions'] = array(); |
52 | 52 | } |
53 | 53 | |
54 | 54 | // Set the global rule for ServerRequestInterface |
55 | - $rules['*']['substitutions'][ ServerRequestInterface::class ] |
|
55 | + $rules['*']['substitutions'][ServerRequestInterface::class] |
|
56 | 56 | = HTTP_Helper::global_server_request(); |
57 | 57 | |
58 | 58 | return $rules; |
@@ -132,8 +132,8 @@ discard block |
||
132 | 132 | * @return bool |
133 | 133 | */ |
134 | 134 | public function has_valid_action(): bool { |
135 | - return is_string( $this->get_action() ) |
|
136 | - && \mb_strlen( $this->get_action() ) > 0; |
|
135 | + return is_string($this->get_action()) |
|
136 | + && \mb_strlen($this->get_action()) > 0; |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | /** |
@@ -142,6 +142,6 @@ discard block |
||
142 | 142 | * @return bool |
143 | 143 | */ |
144 | 144 | public function has_nonce(): bool { |
145 | - return ! is_null( $this->get_nonce_handle() ); |
|
145 | + return ! is_null($this->get_nonce_handle()); |
|
146 | 146 | } |
147 | 147 | } |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @return string |
50 | 50 | */ |
51 | 51 | public static function admin_ajax_url(): string { |
52 | - return admin_url( 'admin-ajax.php' ); |
|
52 | + return admin_url('admin-ajax.php'); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -60,17 +60,17 @@ discard block |
||
60 | 60 | * @return Ajax |
61 | 61 | * @throws Ajax_Exception (code 100) If non valid Ajax class passed. |
62 | 62 | */ |
63 | - private static function get_reflected( string $class ): Ajax { |
|
64 | - if ( ! \is_subclass_of( $class, Ajax::class ) ) { |
|
65 | - throw Ajax_Exception::non_ajax_model( 'get reflection' ); |
|
63 | + private static function get_reflected(string $class): Ajax { |
|
64 | + if ( ! \is_subclass_of($class, Ajax::class)) { |
|
65 | + throw Ajax_Exception::non_ajax_model('get reflection'); |
|
66 | 66 | } |
67 | 67 | |
68 | - if ( ! array_key_exists( $class, self::$class_cache ) ) { |
|
69 | - $reflection = new ReflectionClass( $class ); |
|
70 | - self::$class_cache[ $class ] = $reflection->newInstanceWithoutConstructor(); |
|
68 | + if ( ! array_key_exists($class, self::$class_cache)) { |
|
69 | + $reflection = new ReflectionClass($class); |
|
70 | + self::$class_cache[$class] = $reflection->newInstanceWithoutConstructor(); |
|
71 | 71 | } |
72 | 72 | |
73 | - return self::$class_cache[ $class ]; |
|
73 | + return self::$class_cache[$class]; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -82,11 +82,11 @@ discard block |
||
82 | 82 | * @throws Ajax_Exception (code 100) If non valid Ajax class passed. |
83 | 83 | * @throws Ajax_Exception (code 101) If no action defined |
84 | 84 | */ |
85 | - public static function get_action( string $class ):? string { |
|
86 | - $instance = self::get_reflected( $class ); |
|
85 | + public static function get_action(string $class): ? string { |
|
86 | + $instance = self::get_reflected($class); |
|
87 | 87 | |
88 | - if ( ! $instance->has_valid_action() ) { |
|
89 | - throw Ajax_Exception::undefined_action( $class ); |
|
88 | + if ( ! $instance->has_valid_action()) { |
|
89 | + throw Ajax_Exception::undefined_action($class); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | return $instance->get_action(); |
@@ -99,8 +99,8 @@ discard block |
||
99 | 99 | * @return boolean |
100 | 100 | * @throws Ajax_Exception (code 100) If non valid Ajax class passed. |
101 | 101 | */ |
102 | - public static function has_nonce( string $class ): bool { |
|
103 | - return self::get_reflected( $class )->has_nonce(); |
|
102 | + public static function has_nonce(string $class): bool { |
|
103 | + return self::get_reflected($class)->has_nonce(); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | * @return Nonce|null |
111 | 111 | * @throws Ajax_Exception (code 100) If non valid Ajax class passed. |
112 | 112 | */ |
113 | - public static function get_nonce( string $class ): ?Nonce { |
|
114 | - $instance = self::get_reflected( $class ); |
|
113 | + public static function get_nonce(string $class): ?Nonce { |
|
114 | + $instance = self::get_reflected($class); |
|
115 | 115 | |
116 | 116 | return $instance->has_nonce() |
117 | - ? new Nonce( $instance->get_nonce_handle() ?? '' ) // has_nonce conditional should catch null here |
|
117 | + ? new Nonce($instance->get_nonce_handle() ?? '') // has_nonce conditional should catch null here |
|
118 | 118 | : null; |
119 | 119 | } |
120 | 120 | |
@@ -125,8 +125,8 @@ discard block |
||
125 | 125 | * @return string |
126 | 126 | * @throws Ajax_Exception (code 100) If non valid Ajax class passed. |
127 | 127 | */ |
128 | - public static function get_nonce_field( string $class ): string { |
|
129 | - return self::get_reflected( $class )->get_nonce_field(); |
|
128 | + public static function get_nonce_field(string $class): string { |
|
129 | + return self::get_reflected($class)->get_nonce_field(); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | /** |
@@ -136,14 +136,14 @@ discard block |
||
136 | 136 | * @param ServerRequestInterface $request |
137 | 137 | * @return array<string, string> |
138 | 138 | */ |
139 | - public static function extract_server_request_args( ServerRequestInterface $request ): array { |
|
140 | - switch ( $request->getMethod() ) { |
|
139 | + public static function extract_server_request_args(ServerRequestInterface $request): array { |
|
140 | + switch ($request->getMethod()) { |
|
141 | 141 | case 'POST': |
142 | 142 | // Return different post types. |
143 | - if ( str_contains( $request->getHeaderLine( 'Content-Type' ), 'application/x-www-form-urlencoded;' ) ) { |
|
143 | + if (str_contains($request->getHeaderLine('Content-Type'), 'application/x-www-form-urlencoded;')) { |
|
144 | 144 | $params = (array) $request->getParsedBody(); |
145 | 145 | } else { |
146 | - $params = json_decode( (string) $request->getBody(), true ) ?? array(); |
|
146 | + $params = json_decode((string) $request->getBody(), true) ?? array(); |
|
147 | 147 | } |
148 | 148 | break; |
149 | 149 | case 'GET': |
@@ -34,9 +34,9 @@ discard block |
||
34 | 34 | * @param string $operation The operation being carries out. |
35 | 35 | * @return Ajax_Exception |
36 | 36 | */ |
37 | - public static function non_ajax_model( string $operation = 'unknown operation' ): Ajax_Exception { |
|
37 | + public static function non_ajax_model(string $operation = 'unknown operation'): Ajax_Exception { |
|
38 | 38 | $message = 'None Ajax Model passed to ' . $operation; |
39 | - return new Ajax_Exception( $message, 100 ); |
|
39 | + return new Ajax_Exception($message, 100); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | * @param string $class Ajax class |
46 | 46 | * @return Ajax_Exception |
47 | 47 | */ |
48 | - public static function undefined_action( string $class ): Ajax_Exception { |
|
48 | + public static function undefined_action(string $class): Ajax_Exception { |
|
49 | 49 | $message = "{$class} has no defined action property"; |
50 | - return new Ajax_Exception( $message, 101 ); |
|
50 | + return new Ajax_Exception($message, 101); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | } |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | * @param \PinkCrab\Ajax\Ajax $ajax_class |
65 | 65 | * @return bool |
66 | 66 | */ |
67 | - public function validate_request( Ajax $ajax_class ): bool { |
|
68 | - return $this->request_validator->validate( $ajax_class ); |
|
67 | + public function validate_request(Ajax $ajax_class): bool { |
|
68 | + return $this->request_validator->validate($ajax_class); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | * @return \Psr\Http\Message\ResponseInterface |
76 | 76 | * @filter Ajax_Hooks::CALLBACK_REQUEST_FILTER |
77 | 77 | */ |
78 | - public function invoke_callback( Ajax $ajax_class ): ResponseInterface { |
|
78 | + public function invoke_callback(Ajax $ajax_class): ResponseInterface { |
|
79 | 79 | return $ajax_class->callback( |
80 | - \apply_filters( Ajax_Hooks::CALLBACK_REQUEST_FILTER, $this->server_request, $ajax_class ), |
|
80 | + \apply_filters(Ajax_Hooks::CALLBACK_REQUEST_FILTER, $this->server_request, $ajax_class), |
|
81 | 81 | $this->response_factory |
82 | 82 | ); |
83 | 83 | } |
@@ -91,32 +91,32 @@ discard block |
||
91 | 91 | * @action Ajax_Hooks::CALLBACK_EXECUTION_EXCEPTION |
92 | 92 | * @filter Ajax_Hooks::CALLBACK_RESPONSE_FILTER |
93 | 93 | */ |
94 | - public function create_callback( Ajax $ajax_class ): Closure { |
|
94 | + public function create_callback(Ajax $ajax_class): Closure { |
|
95 | 95 | /** |
96 | 96 | * @param \PinkCrab\Ajax\Ajax $ajax_class |
97 | 97 | * @return noreturn |
98 | 98 | */ |
99 | - return function() use ( $ajax_class ): void { |
|
99 | + return function() use ($ajax_class): void { |
|
100 | 100 | |
101 | 101 | $valid_nonce = apply_filters( |
102 | 102 | Ajax_Hooks::REQUEST_NONCE_VERIFICATION, |
103 | - $this->validate_request( $ajax_class ), |
|
103 | + $this->validate_request($ajax_class), |
|
104 | 104 | $ajax_class, |
105 | 105 | $this->server_request |
106 | 106 | ); |
107 | 107 | |
108 | 108 | try { |
109 | 109 | $response = $valid_nonce |
110 | - ? $this->invoke_callback( $ajax_class ) |
|
110 | + ? $this->invoke_callback($ajax_class) |
|
111 | 111 | : $this->response_factory->unauthorised(); |
112 | - } catch ( Exception $th ) { |
|
112 | + } catch (Exception $th) { |
|
113 | 113 | |
114 | - do_action( Ajax_Hooks::CALLBACK_EXECUTION_EXCEPTION, $th, $ajax_class ); |
|
114 | + do_action(Ajax_Hooks::CALLBACK_EXECUTION_EXCEPTION, $th, $ajax_class); |
|
115 | 115 | |
116 | - $response = $this->response_factory->failure( array( 'error' => $th->getMessage() ) ); |
|
116 | + $response = $this->response_factory->failure(array('error' => $th->getMessage())); |
|
117 | 117 | } |
118 | 118 | $this->http_helper->emit_psr7_response( |
119 | - \apply_filters( Ajax_Hooks::CALLBACK_RESPONSE_FILTER, $response, $ajax_class, $this->server_request ) |
|
119 | + \apply_filters(Ajax_Hooks::CALLBACK_RESPONSE_FILTER, $response, $ajax_class, $this->server_request) |
|
120 | 120 | ); |
121 | 121 | |
122 | 122 | \wp_die(); |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | /** @var Ajax_Dispatcher */ |
24 | 24 | public $dispatcher; |
25 | 25 | |
26 | - public function __construct( Ajax_Dispatcher $dispatcher ) { |
|
26 | + public function __construct(Ajax_Dispatcher $dispatcher) { |
|
27 | 27 | $this->dispatcher = $dispatcher; |
28 | 28 | } |
29 | 29 | |
@@ -33,12 +33,12 @@ discard block |
||
33 | 33 | * @param object $class |
34 | 34 | * @return object |
35 | 35 | */ |
36 | - public function process( $class ) { |
|
37 | - if ( is_a( $class, Ajax::class ) |
|
36 | + public function process($class) { |
|
37 | + if (is_a($class, Ajax::class) |
|
38 | 38 | && is_admin() |
39 | 39 | && wp_doing_ajax() |
40 | 40 | ) { |
41 | - $this->dispatcher->add_ajax_call( $class ); |
|
41 | + $this->dispatcher->add_ajax_call($class); |
|
42 | 42 | } |
43 | 43 | return $class; |
44 | 44 | } |