PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 9.2.0
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v9.2.0
9.2.0 9.1.4 9.1.3 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 All 224 releases
wpvr / src / Admin / UiModeSwitcher.php

UiModeSwitcher.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 9.2.0, at src/Admin/UiModeSwitcher.php

288 lines 8.3 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\Admin;
4
5 class UiModeSwitcher {
6
7 const POST_TYPE = 'wpvr_item';
8 const PAGE_SLUG = 'wpvr-tour-editor';
9
10 public static function init(): void {
11 $instance = new self();
12
13 add_action( 'admin_post_wpvr_switch_ui_mode', [ $instance, 'handle_switch' ] );
14 add_action( 'admin_notices', [ $instance, 'render_notice' ] );
15 }
16
17 public function render_notice(): void {
18 if ( ! current_user_can( 'edit_posts' ) || ! $this->is_target_screen() ) {
19 return;
20 }
21
22 // New tour editor has its own "Classic View" button — no notice needed there.
23 if ( isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::PAGE_SLUG ) {
24 return;
25 }
26
27 $current_mode = $this->get_current_mode();
28 $target_mode = $current_mode === 'latest' ? 'legacy' : 'latest';
29 $context = $this->get_current_context();
30 $tour_id = $this->get_current_tour_id();
31
32 $button_label = $current_mode === 'latest'
33 ? __( 'Switch to Classic UI', 'wpvr' )
34 : __( 'Switch to New UI', 'wpvr' );
35
36 $message = $current_mode === 'latest'
37 ? __( 'You are using the latest WP VR editor.', 'wpvr' )
38 : __( 'You are using the classic WP VR editor.', 'wpvr' );
39 ?>
40 <style>
41 .wpvr-ui-mode-notice.notice {
42 box-sizing: border-box;
43 display: flex;
44 align-items: center;
45 justify-content: space-between;
46 gap: 24px;
47 min-height: 48px;
48 margin: 5px 0 2px 0;
49 padding: 6px 18px 6px 20px;
50 background: #fff;
51 border: 0;
52 border-left: 2px solid #3f04fe;
53 border-radius: 5px;
54 box-shadow: none;
55 }
56 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__message {
57 flex: 1 1 auto;
58 margin: 0;
59 color: #212121;
60 font-family: Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
61 font-size: 15px;
62 font-weight: 400;
63 line-height: 20px;
64 }
65 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__actions {
66 display: flex;
67 flex: 0 0 auto;
68 align-items: center;
69 gap: 64px;
70 }
71 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__switch {
72 box-sizing: border-box;
73 display: inline-flex;
74 align-items: center;
75 justify-content: center;
76 min-width: 142px;
77 height: 34px;
78 padding: 0 19px;
79 color: #3f04fe;
80 font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
81 font-size: 14px;
82 font-weight: 500;
83 line-height: 16px;
84 text-decoration: none;
85 white-space: nowrap;
86 background: #fff;
87 border: 1px solid #3f04fe;
88 border-radius: 6px;
89 }
90 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__switch:hover,
91 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__switch:focus {
92 color: #fff;
93 background: #3f04fe;
94 border-color: #3f04fe;
95 }
96 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__dismiss {
97 display: inline-flex;
98 align-items: center;
99 justify-content: center;
100 width: 32px;
101 height: 32px;
102 margin: 0 -2px 0 0;
103 padding: 0;
104 cursor: pointer;
105 background: transparent;
106 border: 0;
107 border-radius: 4px;
108 }
109 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__dismiss:hover,
110 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__dismiss:focus {
111 background: #f4f4f4;
112 outline: none;
113 }
114 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__dismiss:focus-visible {
115 box-shadow: 0 0 0 1px #3f04fe;
116 }
117 @media screen and (max-width: 782px) {
118 .wpvr-ui-mode-notice.notice {
119 align-items: flex-start;
120 margin-right: 10px;
121 padding: 10px 12px;
122 }
123 .wpvr-ui-mode-notice .wpvr-ui-mode-notice__actions {
124 gap: 8px;
125 }
126 }
127 </style>
128 <div id="wpvr-ui-mode-notice" class="notice wpvr-ui-mode-notice">
129 <p class="wpvr-ui-mode-notice__message"><?php echo esc_html( $message ); ?></p>
130 <div class="wpvr-ui-mode-notice__actions">
131 <a class="wpvr-ui-mode-notice__switch" href="<?php echo esc_url( $this->get_switch_url( $target_mode, $context, $tour_id ) ); ?>">
132 <?php echo esc_html( $button_label ); ?>
133 </a>
134 <button type="button" class="wpvr-ui-mode-notice__dismiss" aria-label="<?php esc_attr_e( 'Dismiss this notice', 'wpvr' ); ?>">
135 <svg width="9" height="9" viewBox="0 0 9 9" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
136 <path d="M7.77482 0.75L0.75 7.75" stroke="#7E7E7E" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
137 <path d="M7.77482 7.75L0.75 0.75" stroke="#7E7E7E" stroke-width="1.5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
138 </svg>
139 </button>
140 </div>
141 </div>
142 <script>
143 (function() {
144 var notice = document.getElementById('wpvr-ui-mode-notice');
145 var dismissButton = notice ? notice.querySelector('.wpvr-ui-mode-notice__dismiss') : null;
146 var switchButton = notice ? notice.querySelector('.wpvr-ui-mode-notice__switch') : null;
147
148 if (switchButton) {
149 switchButton.addEventListener('click', function() {
150 // Publishing updates the page URL without rebuilding this notice.
151 var tourId = parseInt(new URLSearchParams(window.location.search).get('post'), 10);
152 if (tourId > 0) {
153 var switchUrl = new URL(switchButton.href);
154 switchUrl.searchParams.set('context', 'edit');
155 switchUrl.searchParams.set('tour_id', tourId);
156 switchButton.href = switchUrl.toString();
157 }
158 });
159 }
160
161 if (dismissButton) {
162 dismissButton.addEventListener('click', function() {
163 notice.remove();
164 });
165 }
166 }());
167 </script>
168 <?php
169 }
170
171 public function handle_switch(): void {
172 if ( ! current_user_can( 'edit_posts' ) ) {
173 wp_die( esc_html__( 'You are not allowed to switch UI modes.', 'wpvr' ) );
174 }
175
176 check_admin_referer( 'wpvr_switch_ui_mode' );
177
178 $mode = isset( $_GET['mode'] ) && sanitize_key( $_GET['mode'] ) === 'latest' ? 'latest' : 'legacy';
179 $context = isset( $_GET['context'] ) ? sanitize_key( $_GET['context'] ) : 'listing';
180 $tour_id = isset( $_GET['tour_id'] ) ? absint( $_GET['tour_id'] ) : 0;
181
182 update_option( 'wpvr_ui_mode', $mode );
183
184 wp_safe_redirect( $this->get_redirect_url( $mode, $context, $tour_id ) );
185 exit;
186 }
187
188 private function is_target_screen(): bool {
189 if ( isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::PAGE_SLUG ) {
190 return true;
191 }
192
193 if ( ! function_exists( 'get_current_screen' ) ) {
194 return false;
195 }
196
197 $screen = get_current_screen();
198
199 if ( ! $screen ) {
200 return false;
201 }
202
203 return $screen->id === self::POST_TYPE;
204 }
205
206 private function get_current_mode(): string {
207 return get_option( 'wpvr_ui_mode', 'latest' ) === 'latest' ? 'latest' : 'legacy';
208 }
209
210 private function get_current_context(): string {
211 if ( isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::PAGE_SLUG ) {
212 return isset( $_GET['tour_id'] ) ? 'edit' : 'new';
213 }
214
215 if ( isset( $_GET['post'] ) ) {
216 return 'edit';
217 }
218
219 if ( isset( $_GET['post_type'] ) && sanitize_key( $_GET['post_type'] ) === self::POST_TYPE && strpos( (string) $_SERVER['PHP_SELF'], 'post-new.php' ) !== false ) {
220 return 'new';
221 }
222
223 return 'listing';
224 }
225
226 private function get_current_tour_id(): int {
227 if ( isset( $_GET['tour_id'] ) ) {
228 return absint( $_GET['tour_id'] );
229 }
230
231 if ( isset( $_GET['post'] ) ) {
232 return absint( $_GET['post'] );
233 }
234
235 return 0;
236 }
237
238 private function get_switch_url( string $mode, string $context, int $tour_id = 0 ): string {
239 $args = [
240 'action' => 'wpvr_switch_ui_mode',
241 'mode' => $mode,
242 'context' => $context,
243 ];
244
245 if ( $tour_id > 0 ) {
246 $args['tour_id'] = $tour_id;
247 }
248
249 return wp_nonce_url( add_query_arg( $args, admin_url( 'admin-post.php' ) ), 'wpvr_switch_ui_mode' );
250 }
251
252 private function get_redirect_url( string $mode, string $context, int $tour_id = 0 ): string {
253 if ( $mode === 'latest' ) {
254 if ( $context === 'edit' && $tour_id > 0 ) {
255 return add_query_arg(
256 [
257 'page' => self::PAGE_SLUG,
258 'tour_id' => $tour_id,
259 ],
260 admin_url( 'admin.php' )
261 );
262 }
263
264 if ( $context === 'new' ) {
265 return add_query_arg( [ 'page' => self::PAGE_SLUG ], admin_url( 'admin.php' ) );
266 }
267
268 return admin_url( 'edit.php?post_type=' . self::POST_TYPE );
269 }
270
271 if ( $context === 'edit' && $tour_id > 0 ) {
272 return add_query_arg(
273 [
274 'post' => $tour_id,
275 'action' => 'edit',
276 ],
277 admin_url( 'post.php' )
278 );
279 }
280
281 if ( $context === 'new' ) {
282 return admin_url( 'post-new.php?post_type=' . self::POST_TYPE );
283 }
284
285 return admin_url( 'edit.php?post_type=' . self::POST_TYPE );
286 }
287 }
288