get_show_admin_bar_weather() ) { return; } $coords = Admin_Settings::instance()->get_preview_coordinates(); $slug = Admin_Settings::instance()->get_credential_provider(); $service = new Weather_Service(); $result = $service->get_weather( $coords['lat'], $coords['lon'], $slug ); $wp_admin_bar->add_node( array( 'id' => 'forwp-weather-admin-bar', 'parent' => false, 'href' => admin_url( 'admin.php?page=forwp-weather' ), 'title' => self::build_title( $result ), 'meta' => array( 'class' => 'forwp-weather-admin-bar-root', 'title' => __( 'Current weather for admin preview coordinates. Opens 4WP Weather settings.', '4wp-weather' ), ), ) ); } /** * Build HTML title (admin bar allows limited markup from trusted code). * * @param array|\WP_Error $result Weather payload or error. */ private static function build_title( $result ): string { if ( is_wp_error( $result ) ) { $text = __( 'Weather unavailable', '4wp-weather' ); return self::wrap_title( $text ); } if ( ! is_array( $result ) ) { $text = __( 'Weather —', '4wp-weather' ); return self::wrap_title( $text ); } $location = ''; if ( ! empty( $result['locationName'] ) && is_string( $result['locationName'] ) ) { $location = $result['locationName']; } if ( ! empty( $result['country'] ) && is_string( $result['country'] ) ) { $location .= ( '' !== $location ? ', ' : '' ) . $result['country']; } if ( '' === $location ) { $location = __( 'Unknown place', '4wp-weather' ); } if ( isset( $result['temperature'] ) && is_numeric( $result['temperature'] ) ) { /* translators: %s: formatted temperature number (locale-aware). */ $temp = sprintf( __( '%s °C', '4wp-weather' ), number_format_i18n( (float) $result['temperature'], 1 ) ); } else { $temp = '—'; } $text = $location . ' · ' . $temp; return self::wrap_title( $text ); } /** * Icon + label wrapper consistent with core admin bar items. * * @param string $text Plain text (will be escaped). */ private static function wrap_title( string $text ): string { $icon = ''; return $icon . '' . esc_html( $text ) . ''; } }