Skip to content

Commit

Permalink
Events: Set wporg_events charset to latin1 to fix Mojibake
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Sep 20, 2023
1 parent 71e30ef commit 6f4420b
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
<?php

namespace WordPressdotorg\Events_2023;
use WP_Post, DateTimeZone, DateTime;
use WP_Post, DateTimeZone, DateTime, hyperdb;

defined( 'WPINC' ) || die();

/**
* Query a table that's encoded with the `latin1` charset.
*
* Unlike wordpress.org, wordcamp.org has a `DB_CHARSET` of `utf8mb4`, so that's what WPDB uses when querying
* tables. w.org tables use `latin1`, so we need to switch to that when pulling from them. If you query it with
* `utf8mb4`, you'll get Mojibake.
*
* @param string $prepared_query ⚠️ This must have already be ran through `$wpdb->prepare()` if needed.
*/
function get_latin1_results_with_prepared_query( string $prepared_query ) {
global $wpdb;

// Local environments don't always use HyperDB, but production does.
$db_handle = $wpdb instanceof hyperdb ? $wpdb->db_connect( $prepared_query ) : $wpdb->dbh;
$wpdb->set_charset( $db_handle, 'latin1', 'latin1_swedish_ci' );

// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- This function doesn't have the context to prepare it, the caller must.
$results = $wpdb->get_results( $prepared_query );

// Revert to the default charset to avoid affecting other queries.
$wpdb->set_charset( $db_handle, DB_CHARSET, DB_COLLATE );

return $results;
}

/**
* Get a list of all upcoming events across all sites.
*/
function get_all_upcoming_events(): array {
global $wpdb;

$events = $wpdb->get_results( '
$events = get_latin1_results_with_prepared_query( '
SELECT
id, `type`, title, url, meetup, location, latitude, longitude, date_utc,
date_utc_offset AS tz_offset
Expand Down

0 comments on commit 6f4420b

Please sign in to comment.