| 1 |
<?php |
| 2 |
|
| 3 |
namespace EssentialBlocks\API; |
| 4 |
|
| 5 |
use WP_REST_Server; |
| 6 |
use EssentialBlocks\Traits\HasSingletone; |
| 7 |
|
| 8 |
abstract class Base { |
| 9 |
use HasSingletone; |
| 10 |
|
| 11 |
/** |
| 12 |
* Register REST Routes |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
abstract function register(); |
| 17 |
|
| 18 |
public function register_endpoint( $endpoint, $args = [] ) { |
| 19 |
register_rest_route( 'essential-blocks/v1', $endpoint, $args ); |
| 20 |
} |
| 21 |
|
| 22 |
public function get( $endpoint, $args = [] ) { |
| 23 |
$_args = wp_parse_args( |
| 24 |
$args, |
| 25 |
[ |
| 26 |
'methods' => WP_REST_Server::READABLE, |
| 27 |
'permission_callback' => '__return_true' |
| 28 |
] |
| 29 |
); |
| 30 |
|
| 31 |
$this->register_endpoint( $endpoint, $_args ); |
| 32 |
} |
| 33 |
|
| 34 |
public function post( $endpoint, $args = [] ) { |
| 35 |
$_args = wp_parse_args( |
| 36 |
$args, |
| 37 |
[ |
| 38 |
'methods' => WP_REST_Server::CREATABLE, |
| 39 |
'permission_callback' => [ $this, 'verify_post_permission' ] |
| 40 |
] |
| 41 |
); |
| 42 |
|
| 43 |
$this->register_endpoint( $endpoint, $_args ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Verify permission for POST requests |
| 48 |
* |
| 49 |
* @param WP_REST_Request $request |
| 50 |
* @return bool |
| 51 |
*/ |
| 52 |
public function verify_post_permission( $request ) { |
| 53 |
// For public endpoints, we can still allow access but with basic validation |
| 54 |
// You can add nonce verification here if needed |
| 55 |
return true; |
| 56 |
} |
| 57 |
} |
| 58 |
|