Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes#4830 Rankmath class not found #6695

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions inc/ThirdParty/Plugins/SEO/RankMathSEO.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,44 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\ThirdParty\Plugins\SEO;

use RankMath\Helper;
use RankMath\Sitemap\Router;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;

class RankMathSEO implements Subscriber_Interface {


/**
* Options instance.
* Returns an array of events this subscriber wants to listen to.
*
* @var Options_Data
* @return array
*/
protected $option;

/**
* Instantiate class.
*
* @param Options_Data $option Options instance.
*/
public function __construct( Options_Data $option ) {
$this->option = $option;
}
public static function get_subscribed_events(): array {
if ( ! class_exists( 'RankMath\Helper' ) ) {
return [];
}

/**
* Subscribed events.
*/
public static function get_subscribed_events() {
// @phpstan-ignore-next-line
if ( ! defined( 'RANK_MATH_FILE' ) || ! Helper::is_module_active( 'sitemap' ) ) {
return [];
}

return [
'rocket_sitemap_preload_list' => [ 'rocket_sitemap', 15 ],
'rocket_sitemap_preload_list' => [ 'add_sitemap', 15 ],
];
}

/**
* Add SEO sitemap URL to the sitemaps to preload
*
* @param array $sitemaps Sitemaps to preload.
* @return array Updated Sitemaps to preload
*
* @return array
*/
public function rocket_sitemap( $sitemaps ) {
public function add_sitemap( $sitemaps ) {
if ( ! class_exists( 'RankMath\Sitemap\Router' ) ) {
return $sitemaps;
}

// @phpstan-ignore-next-line
$sitemaps[] = Router::get_base_url( 'sitemap_index.xml' );

return $sitemaps;
Expand Down
1 change: 0 additions & 1 deletion inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ public function register(): void {
->addTag( 'common_subscriber' );
$this->getContainer()
->addShared( 'rank_math_seo', RankMathSEO::class )
->addArgument( $options )
->addTag( 'common_subscriber' );
$this->getContainer()
->addShared( 'all_in_one_seo_pack', AllInOneSEOPack::class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
return [
'shouldAddSitemap' => [
'config' => [
'sitemap' => 'sitemap',
'sitemaps' => []
'sitemap' => 'sitemap',
'sitemaps' => [],
],
'expected' => [
'sitemap',
]
]
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Plugins\SEO\RankMathSEO;

use RankMath\Sitemap\Router;
use WP_Rocket\Tests\Integration\TestCase;

/**
* Test class covering \WP_Rocket\ThirdParty\Plugins\SEO\RankMathSEO::add_sitemap
*
* @group RankMathSEO
* @group ThirdParty
*/
class TestRocketSitemap extends TestCase {
public function set_up() {
parent::set_up();

$this->unregisterAllCallbacksExcept( 'rocket_sitemap_preload_list', 'add_sitemap', 15 );
}

public function tear_down() {
$this->restoreWpHook( 'rocket_sitemap_preload_list' );

parent::tear_down();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected ) {
Router::$sitemap = $config['sitemap'];

$this->assertSame(
$expected,
apply_filters( 'rocket_sitemap_preload_list', $config['sitemaps'] )
);
}
}

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\ThirdParty\Plugins\SEO\RankMathSEO;

use RankMath\Sitemap\Router;
use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\ThirdParty\Plugins\SEO\RankMathSEO;

/**
* Test class covering \WP_Rocket\ThirdParty\Plugins\SEO\RankMathSEO::add_sitemap
*
* @group RankMathSEO
* @group ThirdParty
*/
class Test_RocketSitemap extends TestCase {
protected $option;
protected $subscriber;

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

$this->subscriber = new RankMathSEO();
}

/**
* @dataProvider configTestData
*/
public function testShouldReturnAsExpected( $config, $expected ) {
Router::$sitemap = $config['sitemap'];

$this->assertSame(
$expected,
$this->subscriber->add_sitemap( $config['sitemaps'] )
);
}
}

This file was deleted.

Loading