| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Controllers; |
| 4 |
|
| 5 |
use YayMail\Abstracts\BaseController; |
| 6 |
use YayMail\Models\PostModel; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
|
| 9 |
/** |
| 10 |
* Post Controller |
| 11 |
* |
| 12 |
* @method static PostController get_instance() |
| 13 |
*/ |
| 14 |
class PostController extends BaseController { |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var PostModel|null |
| 19 |
*/ |
| 20 |
private $model = null; |
| 21 |
|
| 22 |
protected function __construct() { |
| 23 |
$this->model = PostModel::get_instance(); |
| 24 |
$this->init_hooks(); |
| 25 |
} |
| 26 |
|
| 27 |
protected function init_hooks() { |
| 28 |
register_rest_route( |
| 29 |
YAYMAIL_REST_NAMESPACE, |
| 30 |
'/post/featured-post', |
| 31 |
[ |
| 32 |
[ |
| 33 |
'methods' => \WP_REST_Server::READABLE, |
| 34 |
'callback' => [ $this, 'exec_get_featured_posts' ], |
| 35 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 36 |
], |
| 37 |
] |
| 38 |
); |
| 39 |
|
| 40 |
register_rest_route( |
| 41 |
YAYMAIL_REST_NAMESPACE, |
| 42 |
'/post/categories', |
| 43 |
[ |
| 44 |
[ |
| 45 |
'methods' => \WP_REST_Server::READABLE, |
| 46 |
'callback' => [ $this, 'exec_get_categories' ], |
| 47 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 48 |
], |
| 49 |
] |
| 50 |
); |
| 51 |
|
| 52 |
register_rest_route( |
| 53 |
YAYMAIL_REST_NAMESPACE, |
| 54 |
'/post/tags', |
| 55 |
[ |
| 56 |
[ |
| 57 |
'methods' => \WP_REST_Server::READABLE, |
| 58 |
'callback' => [ $this, 'exec_get_tags' ], |
| 59 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 60 |
], |
| 61 |
] |
| 62 |
); |
| 63 |
|
| 64 |
register_rest_route( |
| 65 |
YAYMAIL_REST_NAMESPACE, |
| 66 |
'/post', |
| 67 |
[ |
| 68 |
[ |
| 69 |
'methods' => \WP_REST_Server::READABLE, |
| 70 |
'callback' => [ $this, 'exec_get_posts' ], |
| 71 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 72 |
], |
| 73 |
] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Handle get featured posts. |
| 79 |
* |
| 80 |
* @param \WP_REST_Request $request The request object. |
| 81 |
* @return \WP_REST_Response|\WP_Error |
| 82 |
*/ |
| 83 |
public function exec_get_featured_posts( \WP_REST_Request $request ) { |
| 84 |
return $this->exec( [ $this, 'get_featured_posts' ], $request ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get featured posts. |
| 89 |
* |
| 90 |
* @param \WP_REST_Request $request The request object. |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_featured_posts( \WP_REST_Request $request ) { |
| 94 |
$params['number_of_posts'] = sanitize_text_field( $request->get_param( 'number_of_posts' ) ); |
| 95 |
$params['post_type'] = sanitize_text_field( $request->get_param( 'post_type' ) ); |
| 96 |
$params['sorted_by'] = sanitize_text_field( $request->get_param( 'sorted_by' ) ); |
| 97 |
|
| 98 |
$params['category_ids'] = json_decode( sanitize_text_field( $request->get_param( 'category_ids' ) ) ); |
| 99 |
$params['tag_ids'] = json_decode( sanitize_text_field( $request->get_param( 'tag_ids' ) ) ); |
| 100 |
$params['post_ids'] = json_decode( sanitize_text_field( $request->get_param( 'post_ids' ) ) ); |
| 101 |
|
| 102 |
return $this->model->get_featured_posts( $params ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Handle get post categories. |
| 107 |
* |
| 108 |
* @param \WP_REST_Request $request The request object. |
| 109 |
* @return \WP_REST_Response|\WP_Error |
| 110 |
*/ |
| 111 |
public function exec_get_categories( \WP_REST_Request $request ) { |
| 112 |
return $this->exec( [ $this, 'get_categories' ], $request ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get post categories. |
| 117 |
* |
| 118 |
* @param \WP_REST_Request $request The request object. |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function get_categories( \WP_REST_Request $request ) { |
| 122 |
$params['search_string'] = sanitize_text_field( $request->get_param( 'search_string' ) ); |
| 123 |
$params['page_num'] = sanitize_text_field( $request->get_param( 'page_num' ) ); |
| 124 |
$params['page_size'] = sanitize_text_field( $request->get_param( 'page_size' ) ); |
| 125 |
$params['term_type'] = 'category'; |
| 126 |
|
| 127 |
return $this->model->get_terms( $params, [ 'id' => 'term_id' ] ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Handle get post tags. |
| 132 |
* |
| 133 |
* @param \WP_REST_Request $request The request object. |
| 134 |
* @return \WP_REST_Response|\WP_Error |
| 135 |
*/ |
| 136 |
public function exec_get_tags( \WP_REST_Request $request ) { |
| 137 |
return $this->exec( [ $this, 'get_tags' ], $request ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get post tags. |
| 142 |
* |
| 143 |
* @param \WP_REST_Request $request The request object. |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function get_tags( \WP_REST_Request $request ) { |
| 147 |
$params['search_string'] = sanitize_text_field( $request->get_param( 'search_string' ) ); |
| 148 |
$params['page_num'] = sanitize_text_field( $request->get_param( 'page_num' ) ); |
| 149 |
$params['page_size'] = sanitize_text_field( $request->get_param( 'page_size' ) ); |
| 150 |
$params['term_type'] = 'post_tag'; |
| 151 |
|
| 152 |
return $this->model->get_terms( $params, [ 'id' => 'term_id' ] ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Handle get posts list. |
| 157 |
* |
| 158 |
* @param \WP_REST_Request $request The request object. |
| 159 |
* @return \WP_REST_Response|\WP_Error |
| 160 |
*/ |
| 161 |
public function exec_get_posts( \WP_REST_Request $request ) { |
| 162 |
return $this->exec( [ $this, 'get_posts' ], $request ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get posts for entity selector. |
| 167 |
* |
| 168 |
* @param \WP_REST_Request $request The request object. |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function get_posts( \WP_REST_Request $request ) { |
| 172 |
$params['search_string'] = sanitize_text_field( $request->get_param( 'search_string' ) ); |
| 173 |
$params['page_num'] = sanitize_text_field( $request->get_param( 'page_num' ) ); |
| 174 |
$params['page_size'] = sanitize_text_field( $request->get_param( 'page_size' ) ); |
| 175 |
|
| 176 |
return $this->model->get_terms( $params ); |
| 177 |
} |
| 178 |
} |
| 179 |
|