Skip to content

Commit

Permalink
Closes #5279: WP Rockets picks up wrong URL as referrer while using W…
Browse files Browse the repository at this point in the history
…eglot (#6197)

Co-authored-by: Gael Robin <“[email protected]”>
Co-authored-by: Cyrille C <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2023
1 parent 5ca0902 commit cd0e9e2
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 7 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
},
"scripts": {
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,TranslatePress,WPGeotargeting,Pressidium",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,TranslatePress,WPGeotargeting,Weglot,Pressidium",
"test-integration-adminonly": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly",
"test-integration-bb": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group BeaverBuilder",
"test-integration-cloudflare": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Cloudflare",
Expand Down Expand Up @@ -158,9 +158,9 @@
"test-integration-rapidload": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group RapidLoad",
"test-integration-proisp": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group ProIsp",
"test-integration-wp-geotargeting": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group WPGeotargeting",
"test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress",
"test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium",

"test-integration-translatepress": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group TranslatePress",
"test-integration-weglot": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Weglot",
"test-integration-pressidium": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Pressidium",
"run-tests": [
"@test-unit",
"@test-integration",
Expand Down Expand Up @@ -202,7 +202,8 @@
"@test-integration-rapidload",
"@test-integration-proisp",
"@test-integration-wp-geotargeting",
"@test-integration-translatepress",
"@test-integration-translatepress",
"@test-integration-weglot",
"@test-integration-pressidium"
],
"run-stan": "vendor/bin/phpstan analyze --memory-limit=2G --no-progress",
Expand Down
9 changes: 8 additions & 1 deletion inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,14 @@ public function add_clear_usedcss_bar_item( WP_Admin_Bar $wp_admin_bar ) {

if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
$referer_url = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer_url ) );

/**
* Filters to act on the referer url for the admin bar.
*
* @param string $uri Current uri
*/
$referer = (string) apply_filters( 'rocket_admin_bar_referer', esc_url( $referer_url ) );
$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer ) );
}

/**
Expand Down
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ private function init_common_subscribers() {
'wpgeotargeting',
'lazyload_css_subscriber',
'shoptimizer',
'weglot',
];

$host_type = HostResolver::get_host_service();
Expand Down
40 changes: 40 additions & 0 deletions inc/ThirdParty/Plugins/I18n/Weglot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace WP_Rocket\ThirdParty\Plugins\I18n;

use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Subscriber for compatibility with Weglot.
*/
class Weglot implements Subscriber_Interface {

/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array|string[]
*/
public static function get_subscribed_events() {
if ( ! class_exists( 'Context_Weglot' ) ) {
return [];
}

return [
'rocket_admin_bar_referer' => 'add_langs_to_referer',
];
}

/**
* Modify the referer URL by appending the current language from Weglot as a prefix to the URL path.
*
* @param string $referer The original referer URL.
* @return string The modified referer URL with the language as a prefix.
*/
public function add_langs_to_referer( $referer ) {
if ( ! function_exists( 'weglot_get_request_url_service' ) || ! function_exists( 'weglot_get_current_full_url' ) ) {
return $referer;
}

return weglot_get_request_url_service()->url_to_relative( weglot_get_current_full_url() );
}
}
4 changes: 4 additions & 0 deletions inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use WP_Rocket\ThirdParty\Plugins\Optimization\RocketLazyLoad;
use WP_Rocket\ThirdParty\Plugins\Optimization\Perfmatters;
use WP_Rocket\ThirdParty\Plugins\Optimization\RapidLoad;
use WP_Rocket\ThirdParty\Plugins\I18n\Weglot;

/**
* Service provider for WP Rocket third party compatibility
Expand Down Expand Up @@ -101,6 +102,7 @@ class ServiceProvider extends AbstractServiceProvider {
'rapidload',
'translatepress',
'wpgeotargeting',
'weglot',
];

/**
Expand Down Expand Up @@ -255,6 +257,8 @@ public function register() {
->addTag( 'common_subscriber' );
$this->getContainer()
->share( 'rapidload', RapidLoad::class );
$this->getContainer()
->share( 'weglot', Weglot::class );
$this->getContainer()->share( 'translatepress', TranslatePress::class );
$this->getContainer()->share( 'wpgeotargeting', WPGeotargeting::class );
}
Expand Down
8 changes: 7 additions & 1 deletion inc/common/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ function rocket_admin_bar( $wp_admin_bar ) {
global $pagenow, $post;

if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
$referer = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
$uri = filter_var( wp_unslash( $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL ) );
/**
* Filters to act on the referer url for the admin bar.
*
* @param string $uri Current uri
*/
$referer = (string) apply_filters( 'rocket_admin_bar_referer', esc_url( $uri ) );
$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer ) );
} else {
$referer = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

return [
'shouldReturnUpdatedLangsReferer' => [
'noChangeHomepage' => [
'referer' => '/',
'lang' => '',
'expected' => '/'
],
'noChangePage' => [
'referer' => '/path/to/page/',
'lang' => '',
'expected' => '/path/to/page/'
],
'ChangeLanguagePage' => [
'referer' => '/path/to/page/',
'lang' => 'es',
'expected' => '/es/path/to/page/'
],
'ChangeLanguageWithQueryParams' => [
'referer' => '/path/to/page?param1=value1&param2=value2',
'lang' => 'fr',
'expected' => '/fr/path/to/page?param1=value1&param2=value2'
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Plugins\I18n\Weglot;

use WP_Rocket\ThirdParty\Plugins\I18n\Weglot;
use WP_Rocket\Tests\Integration\TestCase;
use Brain\Monkey\Functions;


/**
* @covers \WP_Rocket\ThirdParty\Plugins\I18n\Weglot::add_langs_to_referer
* @group Weglot
*/
class Test_addLangsToReferer extends TestCase {

/**
* @var Weglot
*/
protected $weglot;

public function setUp(): void {
parent::setUp();

$this->weglot = new Weglot();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
Functions\expect('weglot_get_current_language')->andReturn($config['lang']);

$this->assertSame(
$config['expected'],
apply_filters( 'rocket_admin_bar_referer', $config['referer'] )
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\ThirdParty\Plugins\I18n\Weglot;

use WP_Rocket\ThirdParty\Plugins\I18n\Weglot;
use WP_Rocket\Tests\Unit\TestCase;
use Brain\Monkey\Functions;

/**
* @covers \WP_Rocket\ThirdParty\Plugins\I18n\Weglot::add_langs_to_referer
*/
class Test_AddLangsToReferer extends TestCase {

/**
* @var Weglot
*/
protected $weglot;

public function setUp(): void {
parent::setUp();

$this->weglot = new Weglot();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected($config) {
Functions\expect('weglot_get_current_language')->andReturn($config['lang']);
Functions\expect('wp_parse_url')->andReturn(parse_url($config['referer']));

$this->assertSame(
$config['expected'],
$this->weglot->add_langs_to_referer($config['referer'])
);
}
}

0 comments on commit cd0e9e2

Please sign in to comment.