add_rewrite_rules();
}
flush_rewrite_rules();
}
/**
* Enqueue admin assets.
*/
public function enqueue_assets( string $hook ): void {
$allowed_hooks = [
'toplevel_page_webcalendar',
'webcalendar_page_webcalendar-categories',
'webcalendar_page_webcalendar-remote',
'webcalendar_page_webcalendar-holidays',
'webcalendar_page_webcalendar-shared',
'webcalendar_page_webcalendar-layers',
'webcalendar_page_webcalendar-submissions',
];
if ( ! in_array( $hook, $allowed_hooks, true ) ) {
return;
}
wp_enqueue_media();
$asset_file = WEBCALENDAR_WP_PATH . 'build/index.asset.php';
if ( ! file_exists( $asset_file ) ) {
return;
}
$assets = require $asset_file;
wp_enqueue_script(
'webcalendar-admin',
WEBCALENDAR_WP_URL . 'build/index.js',
$assets['dependencies'],
$assets['version'],
true
);
// Build layer data for admin color matching (mirrors Plugin.php frontend logic)
$layers_data = [];
if ( is_user_logged_in() ) {
$layer_service = \WebCalendar\WP\Container::get_instance()->get_layer_service();
$layers = $layer_service->getLayersForUser( wp_get_current_user()->user_login );
foreach ( $layers as $layer ) {
$layer_name = $layer->layerUser();
$wp_layer_user = get_user_by( 'login', $layer->layerUser() );
if ( $wp_layer_user ) {
$layer_name = $wp_layer_user->display_name;
} else {
$resource_repo = \WebCalendar\WP\Container::get_instance()->get_resource_repository();
$resource = $resource_repo->findByLogin( $layer->layerUser() );
if ( $resource !== null ) {
$layer_name = $resource->name();
}
}
$layers_data[] = [
'layerUser' => $layer->layerUser(),
'color' => $layer->color(),
'showDuplicates' => $layer->showDuplicates(),
'name' => $layer_name,
];
}
}
wp_localize_script(
'webcalendar-admin',
'webcalApiSettings',
[
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'timeFormat' => get_option( 'webcal_config_TIME_FORMAT', '12' ),
'mapProvider' => get_option( 'webcal_map_provider', 'osm' ),
'googleMapsApiKey' => get_option( 'webcal_google_maps_api_key', '' ),
'currentUser' => wp_get_current_user()->user_login,
'canEditOthers' => current_user_can( 'webcal_can_edit_others' ),
'layers' => $layers_data,
'features' => [
'conflict_detection' => webcal_feature_available( 'conflict_detection' ),
'remote_calendars' => webcal_feature_available( 'remote_calendars' ),
'email_notifications' => webcal_feature_available( 'email_notifications' ),
'multi_user_filter' => webcal_feature_available( 'multi_user_filter' ),
'category_merge' => webcal_feature_available( 'category_merge' ),
'frontend_submission' => webcal_feature_available( 'frontend_submission' ),
],
]
);
wp_enqueue_style(
'webcalendar-admin',
WEBCALENDAR_WP_URL . 'build/index.css',
[],
$assets['version']
);
}
/**
* Add the menu page.
*/
public function add_menu_page(): void {
add_menu_page(
__( 'WebCalendar', 'agenticdaisy-calendar' ),
__( 'WebCalendar', 'agenticdaisy-calendar' ),
'webcal_can_view_calendar',
'webcalendar',
[ $this, 'render_calendar_page' ],
'dashicons-calendar-alt'
);
// Rename the auto-created first submenu item to "Calendar".
add_submenu_page(
'webcalendar',
__( 'Calendar', 'agenticdaisy-calendar' ),
__( 'Calendar', 'agenticdaisy-calendar' ),
'webcal_can_view_calendar',
'webcalendar',
[ $this, 'render_calendar_page' ]
);
add_submenu_page(
'webcalendar',
__( 'Categories', 'agenticdaisy-calendar' ),
__( 'Categories', 'agenticdaisy-calendar' ),
'webcal_can_manage_categories',
'webcalendar-categories',
[ $this, 'render_categories_page' ]
);
// Submissions submenu with pending count badge
if ( webcal_feature_available( 'frontend_submission' ) ) {
$raw_opt = get_option( 'webcal_frontend_submission', 'no' );
if ( is_scalar( $raw_opt ) && (string) $raw_opt === 'yes' ) {
$count = \WebCalendar\WP\REST\SubmissionsController::get_pending_count();
$badge = $count > 0
? ' ' . $count . ' '
: '';
add_submenu_page(
'webcalendar',
__( 'Submissions', 'agenticdaisy-calendar' ),
/* translators: %s: pending count badge HTML */
sprintf( __( 'Submissions%s', 'agenticdaisy-calendar' ), $badge ),
'webcal_can_approve',
'webcalendar-submissions',
[ $this, 'render_submissions_page' ]
);
}
}
}
/**
* Add the Settings and Go Pro submenus at a later priority so they appear last.
*/
public function add_settings_submenu(): void {
add_submenu_page(
'webcalendar',
__( 'Settings', 'agenticdaisy-calendar' ),
__( 'Settings', 'agenticdaisy-calendar' ),
'webcal_can_manage_settings',
'webcalendar-settings',
[ $this, 'render_settings_page' ]
);
// "Go Pro" link — opens the WebCalendar Pro page in a new tab.
global $submenu;
if ( isset( $submenu['webcalendar'] ) ) {
$submenu['webcalendar'][] = [ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- intentional submenu registration
'' . esc_html__( 'Go Pro', 'agenticdaisy-calendar' ) . ' ',
'webcal_can_view_calendar',
'https://agenticdaisy.com/webcalendar/',
];
}
}
/**
* Set the "Go Pro" submenu link to open in a new tab via wp_add_inline_script().
*
* @param string $hook_suffix The current admin page hook suffix.
*/
public function go_pro_new_tab_script( string $hook_suffix = '' ): void {
unset( $hook_suffix );
$url = esc_url( 'https://agenticdaisy.com/webcalendar/' );
$js = 'document.querySelectorAll(\'#adminmenu a[href="' . $url . '"]\').forEach(function(a){a.target="_blank";a.rel="noopener noreferrer";});';
wp_register_script( 'webcalendar-go-pro-tab', false, [], WEBCALENDAR_WP_VERSION, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
wp_enqueue_script( 'webcalendar-go-pro-tab' );
wp_add_inline_script( 'webcalendar-go-pro-tab', $js );
}
/**
* Render the full calendar page.
*/
public function render_calendar_page(): void {
?>
'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '24',
]
);
register_setting(
'webcalendar_settings',
'webcal_config_DATE_FORMAT',
[
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => 'Ymd',
]
);
register_setting(
'webcalendar_settings',
'webcal_single_event_pages',
[
'type' => 'string',
'sanitize_callback' => [ $this, 'sanitize_single_event_pages' ],
'default' => 'off',
]
);
register_setting(
'webcalendar_settings',
'webcal_map_provider',
[
'type' => 'string',
'sanitize_callback' => [ $this, 'sanitize_map_provider' ],
'default' => 'osm',
]
);
register_setting(
'webcalendar_settings',
'webcal_google_maps_api_key',
[
'type' => 'string',
'sanitize_callback' => [ $this, 'sanitize_google_maps_key' ],
'default' => '',
]
);
register_setting(
'webcalendar_settings',
'webcal_import_max_events',
[
'type' => 'integer',
'sanitize_callback' => [ $this, 'sanitize_import_max_events' ],
'default' => 5000,
]
);
register_setting(
'webcalendar_settings',
'webcal_delete_data_on_uninstall',
[
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => 'no',
]
);
register_setting(
'webcalendar_settings',
'webcal_frontend_submission',
[
'type' => 'string',
'sanitize_callback' => [ $this, 'sanitize_yes_no' ],
'default' => 'no',
]
);
register_setting(
'webcalendar_settings',
'webcal_submission_requires_approval',
[
'type' => 'string',
'sanitize_callback' => [ $this, 'sanitize_yes_no' ],
'default' => 'yes',
]
);
register_setting(
'webcalendar_settings',
'webcal_rate_limit',
[
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 100,
]
);
register_setting(
'webcalendar_settings',
'webcal_rate_limit_period',
[
'type' => 'integer',
'sanitize_callback' => 'absint',
'default' => 60,
]
);
add_settings_section(
'webcalendar_general',
__( 'General Settings', 'agenticdaisy-calendar' ),
[ $this, 'render_general_section' ],
'webcalendar-settings'
);
add_settings_section(
'webcalendar_maps',
__( 'Maps', 'agenticdaisy-calendar' ),
[ $this, 'render_maps_section' ],
'webcalendar-settings'
);
add_settings_field(
'webcal_time_format',
__( 'Time Format', 'agenticdaisy-calendar' ),
[ $this, 'render_time_format_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_field(
'webcal_date_format',
__( 'Date Format', 'agenticdaisy-calendar' ),
[ $this, 'render_date_format_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_field(
'webcal_single_event_pages',
__( 'Single Event Pages', 'agenticdaisy-calendar' ),
[ $this, 'render_single_event_pages_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_field(
'webcal_import_max_events',
__( 'Import Event Limit', 'agenticdaisy-calendar' ),
[ $this, 'render_import_max_events_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_field(
'webcal_rate_limit',
__( 'API Rate Limit', 'agenticdaisy-calendar' ),
[ $this, 'render_rate_limit_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_field(
'webcal_delete_data_on_uninstall',
__( 'Delete Data on Uninstall', 'agenticdaisy-calendar' ),
[ $this, 'render_delete_data_field' ],
'webcalendar-settings',
'webcalendar_general'
);
add_settings_section(
'webcalendar_submission',
__( 'Frontend Event Submission', 'agenticdaisy-calendar' ),
[ $this, 'render_submission_section' ],
'webcalendar-settings'
);
add_settings_field(
'webcal_frontend_submission',
__( 'Enable Frontend Submission', 'agenticdaisy-calendar' ),
[ $this, 'render_frontend_submission_field' ],
'webcalendar-settings',
'webcalendar_submission'
);
add_settings_field(
'webcal_submission_requires_approval',
__( 'Require Approval', 'agenticdaisy-calendar' ),
[ $this, 'render_submission_approval_field' ],
'webcalendar-settings',
'webcalendar_submission'
);
add_settings_field(
'webcal_map_provider',
__( 'Map Provider', 'agenticdaisy-calendar' ),
[ $this, 'render_map_provider_field' ],
'webcalendar-settings',
'webcalendar_maps'
);
add_settings_field(
'webcal_google_maps_api_key',
__( 'Google Maps API Key', 'agenticdaisy-calendar' ),
[ $this, 'render_google_maps_key_field' ],
'webcalendar-settings',
'webcalendar_maps'
);
add_settings_section(
'webcalendar_feed',
__( 'iCal Subscription Feed', 'agenticdaisy-calendar' ),
[ $this, 'render_feed_section' ],
'webcalendar-settings'
);
add_settings_field(
'webcal_feed_urls',
__( 'Feed URLs', 'agenticdaisy-calendar' ),
[ $this, 'render_feed_urls_field' ],
'webcalendar-settings',
'webcalendar_feed'
);
add_settings_section(
'webcalendar_system_info',
__( 'System Info', 'agenticdaisy-calendar' ),
[ $this, 'render_system_info_section' ],
'webcalendar-settings'
);
}
/**
* Render general section description.
*/
public function render_general_section(): void {
echo '' . esc_html__( 'Configure basic calendar behavior.', 'agenticdaisy-calendar' ) . '
';
}
/**
* Render maps section description.
*/
public function render_maps_section(): void {
echo '' . esc_html__( 'Configure how event location maps are displayed on single event pages and calendar popups.', 'agenticdaisy-calendar' ) . '
';
}
/**
* Render the map provider dropdown.
*/
public function render_map_provider_field(): void {
$value = get_option( 'webcal_map_provider', 'osm' );
$value = is_scalar( $value ) ? (string) $value : 'osm';
?>
>
>
__( 'Please enter a Google Maps API key, or switch to OpenStreetMap.', 'agenticdaisy-calendar' ),
]
);
}
/**
* Render Google Maps API key field.
*/
public function render_google_maps_key_field(): void {
$raw_val = get_option( 'webcal_google_maps_api_key', '' );
$value = is_scalar( $raw_val ) ? (string) $raw_val : '';
?>
Google Cloud Console'
);
?>
>
' . esc_html__( 'Allow visitors to submit events from the frontend. Requires Business plan or higher.', 'agenticdaisy-calendar' ) . '';
return;
}
echo '' . esc_html__( 'Allow logged-in users to submit events from the frontend using the [webcalendar-submit] shortcode or Submit Event block.', 'agenticdaisy-calendar' ) . '
';
}
/**
* Render the frontend submission enable checkbox.
*/
public function render_frontend_submission_field(): void {
$available = webcal_feature_available( 'frontend_submission' );
$raw_val = get_option( 'webcal_frontend_submission', 'no' );
$checked = is_scalar( $raw_val ) && (string) $raw_val === 'yes';
?>
>
>
>
>
>
>
>
render_status_panel();
?>
get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $wpdb->prefix . 'webcal_entry' ) );
$category_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $wpdb->prefix . 'webcal_categories' ) );
$user_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(DISTINCT cal_login) FROM %i', $wpdb->prefix . 'webcal_entry_user' ) );
// Only count rows that are actual remote subscriptions (have a URL).
// webcal_nonuser_cals also holds internal shared/team calendars with
// cal_url = NULL, which would otherwise inflate the count and not
// match the Remote Calendars admin page (which filters the same way).
/** @psalm-suppress TooManyArguments -- wpdb::prepare is variadic at runtime; Psalm's WP stub has the legacy 2-arg signature */
$remote_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE cal_url IS NOT NULL AND cal_url <> %s', $wpdb->prefix . 'webcal_nonuser_cals', '' ) );
// phpcs:enable
$next_sync = wp_next_scheduled( 'webcal_sync_remote_calendars' );
$last_sync_raw = get_option( 'webcal_last_sync_time', '' );
$last_sync = is_string( $last_sync_raw ) ? $last_sync_raw : '';
?>
render_status_card(
__( 'Total Events', 'agenticdaisy-calendar' ),
number_format_i18n( $event_count ),
'calendar-alt'
);
$this->render_status_card(
__( 'Categories', 'agenticdaisy-calendar' ),
number_format_i18n( $category_count ),
'category'
);
$this->render_status_card(
__( 'Active Users', 'agenticdaisy-calendar' ),
number_format_i18n( $user_count ),
'groups'
);
$this->render_status_card(
__( 'Remote Calendars', 'agenticdaisy-calendar' ),
number_format_i18n( $remote_count ),
'update'
);
if ( $remote_count > 0 ) {
if ( ! empty( $last_sync ) ) {
$sync_label = __( 'Last Sync', 'agenticdaisy-calendar' );
$sync_display = sprintf(
/* translators: %s is a human-readable time difference like "2 hours ago". */
__( '%s ago', 'agenticdaisy-calendar' ),
human_time_diff( intval( $last_sync ), time() )
);
} elseif ( $next_sync !== false ) {
$sync_label = __( 'Next Sync', 'agenticdaisy-calendar' );
$sync_display = sprintf(
/* translators: %s is a human-readable time until next sync like "45 minutes". */
__( 'in %s', 'agenticdaisy-calendar' ),
human_time_diff( time(), $next_sync )
);
} else {
$sync_label = __( 'Sync', 'agenticdaisy-calendar' );
$sync_display = __( 'Not scheduled', 'agenticdaisy-calendar' );
}
$this->render_status_card( $sync_label, $sync_display, 'clock' );
}
?>
' . esc_html__( 'Subscribe to your calendar from external apps like Google Calendar, Apple Calendar, or Outlook.', 'agenticdaisy-calendar' ) . '';
}
/**
* Render the system info section showing dependency versions.
*/
public function render_system_info_section(): void {
$health = new SiteHealth();
$fields = $health->get_version_fields();
echo '';
echo '';
foreach ( $fields as $field ) {
echo '';
echo '' . esc_html( $field['label'] ) . ' ';
echo '' . esc_html( $field['value'] ) . ' ';
echo ' ';
}
echo ' ';
echo '
';
}
/**
* Render the feed URLs field with public/personal URLs and regenerate button.
*/
public function render_feed_urls_field(): void {
$public_url = rest_url( 'webcal/v1/feed' );
$token_manager = new \WebCalendar\WP\FeedTokenManager();
$user_id = get_current_user_id();
$token = $token_manager->get_token( $user_id );
$personal_url = add_query_arg( 'token', $token, $public_url );
$webcal_public = preg_replace( '/^https?:/', 'webcal:', $public_url );
$webcal_personal = preg_replace( '/^https?:/', 'webcal:', $personal_url );
?>
regenerate_token( $user_id );
$redirect_url = add_query_arg(
'webcal_token_regenerated',
'1',
admin_url( 'admin.php?page=webcalendar-settings' )
);
wp_safe_redirect( (string) $redirect_url );
exit;
}
}