| 1 |
<?php |
| 2 |
|
| 3 |
namespace RexTheme\WPVR\Api\Controllers; |
| 4 |
|
| 5 |
use RexTheme\WPVR\Api\Contracts\ControllerInterface; |
| 6 |
use RexTheme\WPVR\Api\Services\TourService; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use WP_Error; |
| 10 |
|
| 11 |
class TourController implements ControllerInterface { |
| 12 |
|
| 13 |
const NAMESPACE = 'wpvr/v1'; |
| 14 |
const BASE = 'tours'; |
| 15 |
|
| 16 |
private TourService $service; |
| 17 |
|
| 18 |
public function __construct( TourService $service ) { |
| 19 |
$this->service = $service; |
| 20 |
} |
| 21 |
|
| 22 |
public function register_routes(): void { |
| 23 |
register_rest_route( self::NAMESPACE, '/' . self::BASE, [ |
| 24 |
[ |
| 25 |
'methods' => 'POST', |
| 26 |
'callback' => [ $this, 'create' ], |
| 27 |
'permission_callback' => [ $this, 'permission_check' ], |
| 28 |
], |
| 29 |
] ); |
| 30 |
|
| 31 |
$id_arg = [ |
| 32 |
'id' => [ |
| 33 |
'type' => 'integer', |
| 34 |
'required' => true, |
| 35 |
'validate_callback' => 'rest_validate_request_arg', |
| 36 |
'sanitize_callback' => 'absint', |
| 37 |
], |
| 38 |
]; |
| 39 |
|
| 40 |
register_rest_route( self::NAMESPACE, '/ui-mode', [ |
| 41 |
[ |
| 42 |
'methods' => 'POST', |
| 43 |
'callback' => [ $this, 'switch_ui_mode' ], |
| 44 |
'permission_callback' => [ $this, 'permission_check' ], |
| 45 |
], |
| 46 |
] ); |
| 47 |
|
| 48 |
register_rest_route( self::NAMESPACE, '/' . self::BASE . '/(?P<id>[\d]+)', [ |
| 49 |
[ |
| 50 |
'methods' => 'GET', |
| 51 |
'callback' => [ $this, 'show' ], |
| 52 |
'permission_callback' => [ $this, 'permission_check' ], |
| 53 |
'args' => $id_arg, |
| 54 |
], |
| 55 |
[ |
| 56 |
'methods' => 'PUT', |
| 57 |
'callback' => [ $this, 'update' ], |
| 58 |
'permission_callback' => [ $this, 'permission_check' ], |
| 59 |
'args' => $id_arg, |
| 60 |
], |
| 61 |
[ |
| 62 |
'methods' => 'DELETE', |
| 63 |
'callback' => [ $this, 'delete' ], |
| 64 |
'permission_callback' => [ $this, 'delete_permission_check' ], |
| 65 |
'args' => $id_arg, |
| 66 |
], |
| 67 |
] ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
public function create( WP_REST_Request $request ): WP_REST_Response { |
| 72 |
$body = $request->get_json_params() ?? []; |
| 73 |
$post_id = $this->service->create( $body ); |
| 74 |
|
| 75 |
if ( is_wp_error( $post_id ) ) { |
| 76 |
return new WP_REST_Response( [ 'message' => $post_id->get_error_message() ], 500 ); |
| 77 |
} |
| 78 |
|
| 79 |
return new WP_REST_Response( [ 'id' => $post_id ], 201 ); |
| 80 |
} |
| 81 |
|
| 82 |
public function show( WP_REST_Request $request ): WP_REST_Response { |
| 83 |
$tour_id = (int) $request->get_param( 'id' ); |
| 84 |
$post = get_post( $tour_id ); |
| 85 |
|
| 86 |
if ( ! $post || $post->post_type !== 'wpvr_item' ) { |
| 87 |
return new WP_REST_Response( [ 'message' => __( 'Tour not found.', 'wpvr' ) ], 404 ); |
| 88 |
} |
| 89 |
|
| 90 |
$data = $this->service->get( $tour_id ); |
| 91 |
$data['tourId'] = $tour_id; |
| 92 |
$data['title'] = $post->post_title; |
| 93 |
$data['status'] = $post->post_status; |
| 94 |
|
| 95 |
return new WP_REST_Response( $data, 200 ); |
| 96 |
} |
| 97 |
|
| 98 |
public function update( WP_REST_Request $request ): WP_REST_Response { |
| 99 |
$tour_id = (int) $request->get_param( 'id' ); |
| 100 |
$post = get_post( $tour_id ); |
| 101 |
|
| 102 |
if ( ! $post || $post->post_type !== 'wpvr_item' ) { |
| 103 |
return new WP_REST_Response( [ 'message' => __( 'Tour not found.', 'wpvr' ) ], 404 ); |
| 104 |
} |
| 105 |
|
| 106 |
$body = $request->get_json_params(); |
| 107 |
$success = $this->service->update( $tour_id, $body ); |
| 108 |
|
| 109 |
if ( ! $success ) { |
| 110 |
return new WP_REST_Response( [ 'message' => __( 'Save failed.', 'wpvr' ) ], 500 ); |
| 111 |
} |
| 112 |
|
| 113 |
$post_update = [ 'ID' => $tour_id ]; |
| 114 |
$allowed_statuses = [ 'draft', 'publish' ]; |
| 115 |
if ( ! empty( $body['status'] ) && in_array( $body['status'], $allowed_statuses, true ) ) { |
| 116 |
$post_update['post_status'] = $body['status']; |
| 117 |
} |
| 118 |
if ( isset( $body['title'] ) ) { |
| 119 |
$post_update['post_title'] = sanitize_text_field( $body['title'] ); |
| 120 |
} |
| 121 |
if ( count( $post_update ) > 1 ) { |
| 122 |
$post_result = wp_update_post( $post_update, true ); |
| 123 |
if ( is_wp_error( $post_result ) ) { |
| 124 |
return new WP_REST_Response( [ 'message' => __( 'Tour title or status could not be saved.', 'wpvr' ) ], 500 ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return new WP_REST_Response( [ 'saved' => true, 'id' => $tour_id ], 200 ); |
| 129 |
} |
| 130 |
|
| 131 |
public function delete( WP_REST_Request $request ): WP_REST_Response { |
| 132 |
$tour_id = (int) $request->get_param( 'id' ); |
| 133 |
$post = get_post( $tour_id ); |
| 134 |
|
| 135 |
if ( ! $post || $post->post_type !== 'wpvr_item' ) { |
| 136 |
return new WP_REST_Response( [ 'message' => __( 'Tour not found.', 'wpvr' ) ], 404 ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! wp_trash_post( $tour_id ) ) { |
| 140 |
return new WP_REST_Response( [ 'message' => __( 'Tour could not be deleted.', 'wpvr' ) ], 500 ); |
| 141 |
} |
| 142 |
|
| 143 |
return new WP_REST_Response( [ 'deleted' => true, 'id' => $tour_id ], 200 ); |
| 144 |
} |
| 145 |
|
| 146 |
public function switch_ui_mode( WP_REST_Request $request ): WP_REST_Response { |
| 147 |
$body = $request->get_json_params() ?? []; |
| 148 |
$mode = isset( $body['mode'] ) && $body['mode'] === 'latest' ? 'latest' : 'legacy'; |
| 149 |
update_option( 'wpvr_ui_mode', $mode ); |
| 150 |
return new WP_REST_Response( [ 'mode' => $mode ], 200 ); |
| 151 |
} |
| 152 |
|
| 153 |
public function permission_check(): bool { |
| 154 |
return current_user_can( 'edit_posts' ); |
| 155 |
} |
| 156 |
|
| 157 |
public function delete_permission_check( WP_REST_Request $request ): bool { |
| 158 |
return current_user_can( 'delete_post', (int) $request->get_param( 'id' ) ); |
| 159 |
} |
| 160 |
} |
| 161 |
|