PluginProbe
YayMail – WooCommerce Email Customizer / trunk
YayMail – WooCommerce Email Customizer vtrunk
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / Abstracts / BaseController.php

BaseController.php in YayMail – WooCommerce Email Customizer trunk, at src/Abstracts/BaseController.php

77 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace YayMail\Abstracts;
3
4 /**
5 * Base Rest API Controller
6 */
7 abstract class BaseController {
8
9 /**
10 * Check if a user has permission to access a router
11 *
12 * @return bool
13 */
14 public function permission_callback() {
15 return current_user_can( 'manage_options' );
16 }
17
18 /**
19 * Returns response when verify nonce failed
20 *
21 * @return \WP_REST_Response
22 */
23 public function nonce_failure_response() {
24 // 403 so HTTP clients reject instead of treating this object as payload
25 // (a 200 here used to flow into array states and crash the customizer).
26 return new \WP_REST_Response(
27 [
28 'success' => false,
29 'isError' => true,
30 'code' => 'nonce_failure',
31 'message' => __( 'Verify nonce failed', 'yaymail' ),
32 ],
33 403
34 );
35 }
36
37 /**
38 * Verify nonce
39 *
40 * @return int|false
41 */
42 public function verify_nonce( $request ) {
43 $nonce = $request->get_header( 'x_wp_nonce' );
44 return wp_verify_nonce( $nonce, 'wp_rest' );
45 }
46
47 /**
48 * Function API exec
49 *
50 * @param callable $callable
51 * @param \WP_REST_Request $request
52 * @return \WP_REST_Response|\WP_Error
53 */
54 public function exec( $callable, \WP_REST_Request $request ) {
55
56 if ( ! $this->verify_nonce( $request ) ) {
57 return rest_ensure_response( $this->nonce_failure_response() );
58 }
59
60 try {
61 if ( is_callable( $callable ) ) {
62 $response = $callable( $request );
63 }
64 } catch ( \Throwable $ex ) {
65 return rest_ensure_response(
66 [
67 'isError' => true,
68 'code' => $ex->getCode(),
69 'message' => $ex->getMessage(),
70 ]
71 );
72 }
73
74 return rest_ensure_response( $response );
75 }
76 }
77