Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FavoritesController | |
0.00% |
0 / 25 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| add | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| delete | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Patterns\Api\Controllers; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Patterns\Library\Favorites; |
| 5 | |
| 6 | /** |
| 7 | * Controller for favorites. |
| 8 | */ |
| 9 | class FavoritesController { |
| 10 | |
| 11 | /** |
| 12 | * Return all favorites. |
| 13 | * |
| 14 | * @param WP_REST_Request $request Request object. |
| 15 | */ |
| 16 | public static function index( $request ) { |
| 17 | |
| 18 | $params = $request->get_query_params(); |
| 19 | |
| 20 | $data = Favorites::get( $params ); |
| 21 | |
| 22 | return new \WP_REST_Response( $data ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Add to Favorites. |
| 27 | * |
| 28 | * @param WP_REST_Request $request Request object. |
| 29 | * |
| 30 | * @return WP_REST_Response $data Response data. |
| 31 | */ |
| 32 | public static function add( $request ) { |
| 33 | |
| 34 | $body = $request->get_json_params(); |
| 35 | $type = \sanitize_text_field( $body['type'] ); |
| 36 | |
| 37 | if ( ! in_array( $type, array( 'patterns', 'templates' ), true ) ) { |
| 38 | return new \WP_REST_Response( __( 'Invalid request', 'nfd-wonder-blocks' ), 400 ); |
| 39 | } |
| 40 | |
| 41 | $item = array( |
| 42 | 'id' => \sanitize_text_field( $body['id'] ), |
| 43 | 'title' => \sanitize_text_field( $body['title'] ), |
| 44 | 'content' => $body['content'], |
| 45 | ); |
| 46 | |
| 47 | $data = Favorites::add( $item, $type ); |
| 48 | |
| 49 | return new \WP_REST_Response( $data ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Remove from Favorites. |
| 54 | * |
| 55 | * @param WP_REST_Request $request Request object. |
| 56 | * @return WP_REST_Response $data Response data. |
| 57 | */ |
| 58 | public static function delete( $request ) { |
| 59 | |
| 60 | $body = $request->get_json_params(); |
| 61 | $type = \sanitize_text_field( $body['type'] ); |
| 62 | |
| 63 | if ( ! in_array( $type, array( 'patterns', 'templates' ), true ) ) { |
| 64 | return new \WP_REST_Response( __( 'Invalid request', 'nfd-wonder-blocks' ), 400 ); |
| 65 | } |
| 66 | |
| 67 | $item = array( |
| 68 | 'id' => \sanitize_text_field( $body['id'] ), |
| 69 | 'title' => \sanitize_text_field( $body['title'] ), |
| 70 | 'content' => $body['content'], |
| 71 | ); |
| 72 | |
| 73 | $data = Favorites::delete( $item ); |
| 74 | |
| 75 | return new \WP_REST_Response( $data ); |
| 76 | } |
| 77 | } |