| 1 |
<?php |
| 2 |
namespace Templately\API; |
| 3 |
|
| 4 |
use Templately\Utils\Database; |
| 5 |
use WP_REST_Request; |
| 6 |
|
| 7 |
class Categories extends API { |
| 8 |
private $endpoint = 'categories'; |
| 9 |
|
| 10 |
public function permission_check( WP_REST_Request $request ) { |
| 11 |
$this->request = $request; |
| 12 |
return true; |
| 13 |
} |
| 14 |
|
| 15 |
public function register_routes() { |
| 16 |
$this->get( $this->endpoint, [ $this, 'get_categories' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
public function get_categories() { |
| 20 |
/** |
| 21 |
* Return if there is any cache data. |
| 22 |
*/ |
| 23 |
$types = Database::get_transient( $this->endpoint ); |
| 24 |
if( ! empty( $types ) ){ |
| 25 |
return $this->success( $types ); |
| 26 |
} |
| 27 |
|
| 28 |
/** @noinspection SpellCheckingInspection */ |
| 29 |
$id = $this->get_param( 'id', 0, 'intval' ); |
| 30 |
$funcArgs = []; |
| 31 |
|
| 32 |
if ( $id > 0 ) { |
| 33 |
$funcArgs['id'] = $id; |
| 34 |
} |
| 35 |
|
| 36 |
$response = $this->http()->query( $this->endpoint, 'id, name, slug, status', $funcArgs )->post(); |
| 37 |
/** |
| 38 |
* Caching the response in transient. |
| 39 |
*/ |
| 40 |
if( ! is_wp_error( $response ) ) { |
| 41 |
Database::set_transient( $this->endpoint, $response ); |
| 42 |
} |
| 43 |
|
| 44 |
return $response; |
| 45 |
} |
| 46 |
} |