fix(domain-mapping): avoid get_option(blog_charset) in early bootstrap (#1164)#1165
fix(domain-mapping): avoid get_option(blog_charset) in early bootstrap (#1164)#1165superdav42 wants to merge 1 commit intomainfrom
Conversation
#1164) `Domain_Mapping::verify_dns_mapping()` is hooked on `pre_get_site_by_path` and `ms_site_not_found`, both of which fire from inside `ms_load_current_site_and_network()` at `wp-includes/ms-settings.php:77`. At that point `$wpdb->set_prefix()` (line 102 of the same file) has not yet run, so `$wpdb->options` is the empty string. The async DNS-check path called `wp_send_json($mapping->to_array())`, and `wp_send_json()` calls `get_option('blog_charset')` before sending the `Content-Type` header. With an empty `$wpdb->options`, `get_option()` issued the malformed query SELECT option_value FROM WHERE option_name = 'blog_charset' LIMIT 1 producing a noisy MariaDB syntax error on every triggering request. Replace the `wp_send_json()` call with a direct emit of `Content-Type: application/json; charset=UTF-8` (the WordPress default for `blog_charset`) plus `wp_json_encode()` and `exit`, none of which touch `get_option()` or `$wpdb->options`. Extract the emit step into a protected `send_async_dns_response()` method gated by a new `wu_async_dns_response_short_circuit` filter so plugins and tests can intercept the response without invoking `exit`. Add a regression test that simulates the empty-`$wpdb->options` state and asserts `verify_dns_mapping()` produces no `wpdb->last_error` and delivers the expected payload. Resolves #1164
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🔨 Build Complete - Ready for Testing!📦 Download Build Artifact (Recommended)Download the zip build, upload to WordPress and test:
🌐 Test in WordPress Playground (Very Experimental)Click the link below to instantly test this PR in your browser - no installation needed! Login credentials: |
|
Performance Test Results Performance test results for d9fa654 are in 🛎️! Note: the numbers in parentheses show the difference to the previous (baseline) test run. Differences below 2% or 0.5 in absolute values are not shown. URL:
|
Summary
SELECT option_value FROM WHERE option_name = 'blog_charset' LIMIT 1produced on every multisite request that hitsDomain_Mapping::verify_dns_mapping()(pre_get_site_by_path/ms_site_not_found).wp_send_json()call (which callsget_option('blog_charset')before$wpdb->set_prefix()has run inwp-includes/ms-settings.php) with a directheader(); wp_json_encode(); exit;emit using a hard-codedcharset=UTF-8.send_async_dns_response()method gated by a newwu_async_dns_response_short_circuitfilter so plugins and tests can intercept without invokingexit.$wpdb->optionsstate and asserts nowpdb->last_erroris produced.Why
verify_dns_mapping()runs from insidems_load_current_site_and_network()atwp-includes/ms-settings.php:77, before$wpdb->set_prefix( $table_prefix, false )runs at line 102 of the same file. At that point$wpdb->optionsis the empty string.wp_send_json()(wp-includes/functions.php:4541) callsget_option('blog_charset')before sending theContent-Typeheader, which executesSELECT option_value FROM {$wpdb->options} …with an empty table name and produces the noisy SQL error reported in #1164.The sibling DB call in the same path,
Domain::get_by_domain()(inc/models/class-domain.php:704), already handles the early-bootstrap case by deriving the mapped-domains table name from$wpdb->base_prefix(set fromwp-config.phpbefore sunrise).wp_send_json()does not, so we bypass it for this single early-bootstrap response.Verification
The two pre-existing PHPCS warnings on
tests/WP_Ultimo/Domain_Mapping_Test.php(lines 530 and 878) are not introduced by this PR — they predate the change onorigin/mainand are out of scope.Files
inc/class-domain-mapping.php—verify_dns_mapping()no longer callswp_send_json; newsend_async_dns_response()+wu_async_dns_response_short_circuitfilter.tests/WP_Ultimo/Domain_Mapping_Test.php—test_verify_dns_mapping_does_not_query_options_table_in_early_bootstrapregression test.Resolves #1164
aidevops.sh v3.15.12 plugin for OpenCode v1.14.41 with claude-opus-4-7 spent 9m and 32,649 tokens on this with the user in an interactive session.