fix[faustwp]: (#1872) add filter for search_pattern in handle_generate_endpoint#2315
Conversation
…_generate_endpoint Add apply_filters( 'faustwp_generate_endpoint_search_pattern' ) so non-standard WordPress installations like Bedrock can customize the regex pattern used to match the /generate endpoint. Bedrock returns /wp/generate from site_url() but REQUEST_URI only contains /generate, causing the preg_match to fail and previews to 404. The filter lets users adjust the pattern without modifying the plugin source. Closes wpengine#1872
|
Currently, we do not support the creation of preview deployments based on changes coming from forked repositories. |
🦋 Changeset detectedLatest commit: 8c27dce The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Thanks for the contribution! I don't think we need a new filter here. The fix is to use $search_pattern = ':^' . home_url( '/generate', 'relative' ) . ':';
See also #1897 which addresses the same class of Bedrock subdirectory issue on the JS side. |
|
Currently, we do not support the creation of preview deployments based on changes coming from forked repositories. |
* fix[faustwp]: (#1872) use home_url() in handle_generate_endpoint On Bedrock-style installs where WordPress core lives under /wp/, site_url('/generate') returns https://example.com/wp/generate but $_SERVER['REQUEST_URI'] is still /generate, so the preg_match in handle_generate_endpoint() never fires and the auth-code redirect silently breaks. home_url() returns the public-facing URL (what REQUEST_URI actually contains), which matches on both standard and Bedrock installs. Per @josephfusco's guidance on #2315, which was closed in favor of this simpler fix — a new filter is not needed, just the correct URL helper. * test[faustwp]: regression test for handle_generate_endpoint() Bedrock case (#1872) Adds AuthCallbacksTests.php covering handle_generate_endpoint() with four scenarios: - Standard install reaches wp_safe_redirect (login redirect path). - Bedrock layout (site_url=/wp/x, home_url=/x) still matches REQUEST_URI=/x. This is the regression guard against future reverts to site_url(): verified to fail when callbacks.php is reverted to site_url() and pass with home_url(). - Unrelated REQUEST_URI early-returns without redirecting. - /generate without redirect_uri early-returns. Uses Patchwork to redefine wp_safe_redirect so the bare 'exit;' is bypassed and the captured redirect target can be asserted. Patchwork is already loaded via phpunit.xml.dist's LOAD_PATCHWORK=1. Also adds tests/mu-plugins/mu-bedrock-simulator.php -- a tiny mu-plugin that makes site_url() return /wp/<path> while home_url() stays at /<path>, for manual browser reproduction inside the docker-compose stack without requiring a full roots/bedrock install. * test[faustwp]: tighten AuthCallbacks regression test fidelity Peer-review polish on the earlier test commit: - Use update_option('siteurl', ...) instead of an add_filter('site_url') to drive Bedrock-style URL divergence. This matches what real Bedrock configures via WP_SITEURL in wp-config.php, so every consumer of get_option('siteurl') and site_url() sees the divergent value, not just callers that go through the site_url filter. Removes the remove_all_filters() tearDown that nuked unrelated core filters. - Replace the RuntimeException + magic-string catch trick with a dedicated RedirectAttempted exception class. Eliminates the risk of accidentally swallowing unrelated runtime errors during the redirect-capture flow. - Tighten assertions on the Bedrock-divergence test: now also asserts the captured redirect contains 'wp-login.php' (matching the standard-install test) and adds two sanity-check assertions that confirm the simulated divergence is actually in place before the handler is invoked. Total assertions in the suite increase from 5 to 8. - Document the simulator mu-plugin's narrower scope vs the test's full fidelity: the mu-plugin filters site_url() output (sufficient for browser reproduction), while the PHPUnit suite updates the option directly. Verified via temporary revert of callbacks.php to site_url(): the Bedrock divergence test now fails with the polished message; restoring home_url() greens the suite on both single-site and multisite. * test[faustwp]: widen AuthCallbacks coverage and document home_url() filter caveat Three small additions on top of the regression-test commits: 1. Assert that the redirect_uri query arg is preserved through to the wp-login redirect_to param, in both the standard and Bedrock cases. A future change that successfully matched the request but mangled or dropped redirect_uri would previously slip through. 2. Add test_wp_in_subdirectory_install_matches_with_home_url for the Codex-documented 'Giving WordPress its own directory' pattern -- WP core in /wordpress, public site at root. Different prefix from Bedrock, same shape of divergence. Proves the fix is not a /wp-special case. Renames set_bedrock_siteurl() to set_split_install_siteurl($suffix) so both tests share the helper. 3. Document the home_url() filter dependency in handle_generate_endpoint()'s docblock. Multilingual plugins (WPML, Polylang, TranslatePress) that filter home_url() to prepend locale paths can still cause this match to miss for non-locale-prefixed frontend callers. Notes the likely follow-up direction (REST route migration) so the next person to touch this knows what they're inheriting. Suite is now 5 tests / 14 assertions. Verified red-on-revert: both the Bedrock and WP-in-subdirectory tests fail cleanly when callbacks.php is reverted to site_url(); restoring home_url() greens both single-site and multisite suites. --------- Co-authored-by: latenighthackathon <latenighthackathon@users.noreply.github.com> Co-authored-by: Joe Fusco <hello@josephfus.co>
Description
Non-standard WordPress installations like Bedrock use a different directory structure where
site_url()returns a path with a/wp/prefix (e.g./wp/generate) but$_SERVER['REQUEST_URI']contains only/generate. This causes thepreg_matchinhandle_generate_endpoint()to fail, resulting in 404 errors when previewing content.This PR adds an
apply_filters()call on$search_patternso sites with non-standard directory structures can adjust the matching pattern without modifying the plugin source.Usage example for Bedrock:
Related Issues
Closes #1872
Testing
Confirm the issue (before the fix)
1. Verify no filter exists on
$search_pattern:2. Verify
$search_patternusessite_url()which includes subdirectories on Bedrock:Confirm the fix
3. Verify the filter is applied after
$search_patternis constructed:4. Verify the docblock follows WordPress standards (@SInCE, @param):
Run the test suites
phpcs — confirms the filter follows WordPress coding standards:
JS tests — confirms no regressions:
WordPress PHPUnit — confirms plugin functionality: