| 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 |
|
| 147 |
if (dismissButton) { |
| 148 |
dismissButton.addEventListener('click', function() { |
| 149 |
notice.remove(); |
| 150 |
}); |
| 151 |
} |
| 152 |
}()); |
| 153 |
</script> |
| 154 |
<?php |
| 155 |
} |
| 156 |
|
| 157 |
public function handle_switch(): void { |
| 158 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 159 |
wp_die( esc_html__( 'You are not allowed to switch UI modes.', 'wpvr' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
check_admin_referer( 'wpvr_switch_ui_mode' ); |
| 163 |
|
| 164 |
$mode = isset( $_GET['mode'] ) && sanitize_key( $_GET['mode'] ) === 'latest' ? 'latest' : 'legacy'; |
| 165 |
$context = isset( $_GET['context'] ) ? sanitize_key( $_GET['context'] ) : 'listing'; |
| 166 |
$tour_id = isset( $_GET['tour_id'] ) ? absint( $_GET['tour_id'] ) : 0; |
| 167 |
|
| 168 |
update_option( 'wpvr_ui_mode', $mode ); |
| 169 |
|
| 170 |
wp_safe_redirect( $this->get_redirect_url( $mode, $context, $tour_id ) ); |
| 171 |
exit; |
| 172 |
} |
| 173 |
|
| 174 |
private function is_target_screen(): bool { |
| 175 |
if ( isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::PAGE_SLUG ) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
if ( ! function_exists( 'get_current_screen' ) ) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
$screen = get_current_screen(); |
| 184 |
|
| 185 |
if ( ! $screen ) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
return $screen->id === self::POST_TYPE; |
| 190 |
} |
| 191 |
|
| 192 |
private function get_current_mode(): string { |
| 193 |
return get_option( 'wpvr_ui_mode', 'legacy' ) === 'latest' ? 'latest' : 'legacy'; |
| 194 |
} |
| 195 |
|
| 196 |
private function get_current_context(): string { |
| 197 |
if ( isset( $_GET['page'] ) && sanitize_key( $_GET['page'] ) === self::PAGE_SLUG ) { |
| 198 |
return isset( $_GET['tour_id'] ) ? 'edit' : 'new'; |
| 199 |
} |
| 200 |
|
| 201 |
if ( isset( $_GET['post'] ) ) { |
| 202 |
return 'edit'; |
| 203 |
} |
| 204 |
|
| 205 |
if ( isset( $_GET['post_type'] ) && sanitize_key( $_GET['post_type'] ) === self::POST_TYPE && strpos( (string) $_SERVER['PHP_SELF'], 'post-new.php' ) !== false ) { |
| 206 |
return 'new'; |
| 207 |
} |
| 208 |
|
| 209 |
return 'listing'; |
| 210 |
} |
| 211 |
|
| 212 |
private function get_current_tour_id(): int { |
| 213 |
if ( isset( $_GET['tour_id'] ) ) { |
| 214 |
return absint( $_GET['tour_id'] ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( isset( $_GET['post'] ) ) { |
| 218 |
return absint( $_GET['post'] ); |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
private function get_switch_url( string $mode, string $context, int $tour_id = 0 ): string { |
| 225 |
$args = [ |
| 226 |
'action' => 'wpvr_switch_ui_mode', |
| 227 |
'mode' => $mode, |
| 228 |
'context' => $context, |
| 229 |
]; |
| 230 |
|
| 231 |
if ( $tour_id > 0 ) { |
| 232 |
$args['tour_id'] = $tour_id; |
| 233 |
} |
| 234 |
|
| 235 |
return wp_nonce_url( add_query_arg( $args, admin_url( 'admin-post.php' ) ), 'wpvr_switch_ui_mode' ); |
| 236 |
} |
| 237 |
|
| 238 |
private function get_redirect_url( string $mode, string $context, int $tour_id = 0 ): string { |
| 239 |
if ( $mode === 'latest' ) { |
| 240 |
if ( $context === 'edit' && $tour_id > 0 ) { |
| 241 |
return add_query_arg( |
| 242 |
[ |
| 243 |
'page' => self::PAGE_SLUG, |
| 244 |
'tour_id' => $tour_id, |
| 245 |
], |
| 246 |
admin_url( 'admin.php' ) |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
if ( $context === 'new' ) { |
| 251 |
return add_query_arg( [ 'page' => self::PAGE_SLUG ], admin_url( 'admin.php' ) ); |
| 252 |
} |
| 253 |
|
| 254 |
return admin_url( 'edit.php?post_type=' . self::POST_TYPE ); |
| 255 |
} |
| 256 |
|
| 257 |
if ( $context === 'edit' && $tour_id > 0 ) { |
| 258 |
return add_query_arg( |
| 259 |
[ |
| 260 |
'post' => $tour_id, |
| 261 |
'action' => 'edit', |
| 262 |
], |
| 263 |
admin_url( 'post.php' ) |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
if ( $context === 'new' ) { |
| 268 |
return admin_url( 'post-new.php?post_type=' . self::POST_TYPE ); |
| 269 |
} |
| 270 |
|
| 271 |
return admin_url( 'edit.php?post_type=' . self::POST_TYPE ); |
| 272 |
} |
| 273 |
} |
| 274 |
|