| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Controllers; |
| 4 |
|
| 5 |
use YayMail\Abstracts\BaseController; |
| 6 |
use YayMail\TemplateLibrary\TemplateLibraryService; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
|
| 9 |
/** |
| 10 |
* Template Library Controller |
| 11 |
* |
| 12 |
* @method static TemplateLibraryController get_instance() |
| 13 |
*/ |
| 14 |
class TemplateLibraryController extends BaseController { |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
protected function __construct() { |
| 18 |
$this->init_hooks(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register REST routes. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
protected function init_hooks() { |
| 27 |
register_rest_route( |
| 28 |
YAYMAIL_REST_NAMESPACE, |
| 29 |
'/template-library', |
| 30 |
[ |
| 31 |
[ |
| 32 |
'methods' => \WP_REST_Server::READABLE, |
| 33 |
'callback' => [ $this, 'exec_get_templates' ], |
| 34 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 35 |
'args' => [ |
| 36 |
'email_type' => [ |
| 37 |
'type' => 'string', |
| 38 |
'required' => true, |
| 39 |
], |
| 40 |
], |
| 41 |
], |
| 42 |
] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Exec wrapper for getting list of templates. |
| 48 |
* |
| 49 |
* @param \WP_REST_Request $request Request. |
| 50 |
* |
| 51 |
* @return \WP_REST_Response|\WP_Error |
| 52 |
*/ |
| 53 |
public function exec_get_templates( \WP_REST_Request $request ) { |
| 54 |
return $this->exec( [ $this, 'get_templates' ], $request ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get list of template summaries for current email type. |
| 59 |
* |
| 60 |
* @param \WP_REST_Request $request Request. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function get_templates( \WP_REST_Request $request ) { |
| 65 |
$email_type = sanitize_text_field( $request->get_param( 'email_type' ) ); |
| 66 |
|
| 67 |
if ( empty( $email_type ) ) { |
| 68 |
return [ |
| 69 |
'success' => false, |
| 70 |
'message' => __( 'Template name is required.', 'yaymail' ), |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
$templates = TemplateLibraryService::get_instance()->get_list( $email_type ); |
| 75 |
|
| 76 |
return [ |
| 77 |
'success' => true, |
| 78 |
'templates' => $templates, |
| 79 |
]; |
| 80 |
} |
| 81 |
} |
| 82 |
|