PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 9.1.2
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v9.1.2
9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 8.5.4 All 221 releases
wpvr / src / Api / Controllers / TourController.php

TourController.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 9.1.2, at src/Api/Controllers/TourController.php

179 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, 'create_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, 'ui_mode_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, 'item_permission_check' ],
53 'args' => $id_arg,
54 ],
55 [
56 'methods' => 'PUT',
57 'callback' => [ $this, 'update' ],
58 'permission_callback' => [ $this, 'item_permission_check' ],
59 'args' => $id_arg,
60 ],
61 [
62 'methods' => 'DELETE',
63 'callback' => [ $this, 'delete' ],
64 'permission_callback' => [ $this, 'item_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_type_obj = get_post_type_object( 'wpvr_item' );
117 $publish_cap = $post_type_obj ? $post_type_obj->cap->publish_posts : 'publish_wpvr_tours';
118 if ( 'publish' === $body['status'] && ! current_user_can( $publish_cap ) ) {
119 return new WP_REST_Response( [ 'message' => __( 'You do not have permission to publish tours.', 'wpvr' ) ], 403 );
120 }
121 $post_update['post_status'] = $body['status'];
122 }
123 if ( isset( $body['title'] ) ) {
124 $post_update['post_title'] = sanitize_text_field( $body['title'] );
125 }
126 if ( count( $post_update ) > 1 ) {
127 $post_result = wp_update_post( $post_update, true );
128 if ( is_wp_error( $post_result ) ) {
129 return new WP_REST_Response( [ 'message' => __( 'Tour title or status could not be saved.', 'wpvr' ) ], 500 );
130 }
131 }
132
133 return new WP_REST_Response( [ 'saved' => true, 'id' => $tour_id ], 200 );
134 }
135
136 public function delete( WP_REST_Request $request ): WP_REST_Response {
137 $tour_id = (int) $request->get_param( 'id' );
138 $post = get_post( $tour_id );
139
140 if ( ! $post || $post->post_type !== 'wpvr_item' ) {
141 return new WP_REST_Response( [ 'message' => __( 'Tour not found.', 'wpvr' ) ], 404 );
142 }
143
144 if ( ! wp_trash_post( $tour_id ) ) {
145 return new WP_REST_Response( [ 'message' => __( 'Tour could not be deleted.', 'wpvr' ) ], 500 );
146 }
147
148 return new WP_REST_Response( [ 'deleted' => true, 'id' => $tour_id ], 200 );
149 }
150
151 public function switch_ui_mode( WP_REST_Request $request ): WP_REST_Response {
152 $body = $request->get_json_params() ?? [];
153 $mode = isset( $body['mode'] ) && $body['mode'] === 'latest' ? 'latest' : 'legacy';
154 update_option( 'wpvr_ui_mode', $mode );
155 return new WP_REST_Response( [ 'mode' => $mode ], 200 );
156 }
157
158 public function create_permission_check(): bool {
159 $post_type_obj = get_post_type_object( 'wpvr_item' );
160 $create_cap = $post_type_obj ? $post_type_obj->cap->edit_posts : 'edit_wpvr_tours';
161 return current_user_can( $create_cap );
162 }
163
164 public function item_permission_check( WP_REST_Request $request ): bool {
165 $tour_id = (int) $request->get_param( 'id' );
166 $post = get_post( $tour_id );
167 if ( ! $post || $post->post_type !== 'wpvr_item' ) {
168 return false;
169 }
170
171 $capability = ( 'DELETE' === $request->get_method() ) ? 'delete_post' : 'edit_post';
172 return current_user_can( $capability, $tour_id );
173 }
174
175 public function ui_mode_permission_check(): bool {
176 return current_user_can( 'manage_options' );
177 }
178 }
179