Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active October 31, 2023 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/45e189a13cfdf00e1af5bcbdd86d5da2 to your computer and use it in GitHub Desktop.
Save andrasguseo/45e189a13cfdf00e1af5bcbdd86d5da2 to your computer and use it in GitHub Desktop.
TEC > Add the WordPress time zone and the TEC time zone mode to the admin bar
<?php
/*
* Add the WordPress time zone, the TEC time zone mode, and the TEC editor mode to the admin bar.
* Change the TEC time zone or the editor mode in the admin bar.
* Intended for use on test sites or staging sites only.
*
* Plugins required: The Events Calendar (for the TEC related part)
* Author: Andras Guseo
* Created: October 31, 2023
* Last updated: October 31, 2023
*/
add_action( 'admin_bar_menu', 'display_wordpress_timezone_in_admin_bar', 999 );
/**
* Add the WordPress time zone, the TEC time zone setting, and the TEC editor setting to the admin bar.
*
* @return void
*/
function display_wordpress_timezone_in_admin_bar() {
global $wp_admin_bar;
$timezone_string = get_option( 'timezone_string' );
if ( ! empty( $timezone_string ) ) {
$wp_admin_bar->add_node( [
'id' => 'display_wordpress_timezone',
'title' => 'WP Time Zone: ' . $timezone_string,
'parent' => 'top-secondary',
'meta' => [
'class' => 'display_wordpress_timezone_node'
]
] );
}
if ( class_exists( 'Tribe__Events__Main' ) ) {
$tzms = [
'site' => 'site-wide',
'event' => 'manual',
];
$tzm = tribe_get_option( 'tribe_events_timezone_mode' );
$new_tzm = $tzm == 'site' ? 'event' : 'site';
$wp_admin_bar->add_node( [
'id' => 'display_tribe_timezone',
'title' => 'TEC Time Zone Mode: ' . $tzms[ $tzm ],
'parent' => 'top-secondary',
'meta' => [
'class' => 'display_tribe_timezone_node'
]
] );
// Add a submenu item with the option to change the TEC time zone setting
$wp_admin_bar->add_node( [
'id' => 'change_tribe_timezone_mode',
'title' => 'Change to ' . $tzms[ $new_tzm ],
'parent' => 'display_tribe_timezone',
'href' => add_query_arg( [ 'change_timezone_mode' => $new_tzm ] ),
'meta' => [
'class' => 'change_tribe_timezone_mode_node'
]
] );
$editors = [
'0' => 'classic',
'1' => 'block',
];
$editor_value = tribe_get_option( 'toggle_blocks_editor' );
$editor = $editor_value ? 'block' : 'classic';
$new_editor = $editor_value ? 'classic' : 'block';
$wp_admin_bar->add_node( [
'id' => 'display_tec_editor',
'title' => 'TEC editor: ' . $editor,
'parent' => 'top-secondary',
'meta' => [
'class' => 'display_tec_editor_node'
]
] );
// Add a submenu item with the option to change the TEC editor setting
$wp_admin_bar->add_node( [
'id' => 'change_tec_editor',
'title' => 'Change to ' . $new_editor,
'parent' => 'display_tec_editor',
'href' => add_query_arg( [ 'change_tec_editor' => $editor_value ? '0' : '1' ] ),
'meta' => [
'class' => 'change_tec_editor_node'
]
] );
}
}
add_action( 'init', 'update_tribe_timezone_mode' );
/**
* Check if any of the parameters are set and update the option value accordingly
*
* @return void
*/
function update_tribe_timezone_mode() {
if ( ! class_exists( 'Tribe__Events__Main' ) ) {
return;
}
if (
isset( $_GET['change_timezone_mode'] )
&& (
$_GET['change_timezone_mode'] === 'site'
|| $_GET['change_timezone_mode'] === 'event'
)
) {
Tribe__Settings_Manager::set_option( 'tribe_events_timezone_mode', $_GET['change_timezone_mode'] );
}
if (
isset( $_GET['change_tec_editor'] )
&& (
$_GET['change_tec_editor'] === '0'
|| $_GET['change_tec_editor'] === '1'
)
) {
Tribe__Settings_Manager::set_option( 'toggle_blocks_editor', $_GET['change_tec_editor'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment