Skip to content
Open
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
13 changes: 7 additions & 6 deletions src/helpers/route-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
class Route_Helper {

/**
* Gets the route from a name, rewrite and rest_base.
* Gets the route from a name and rest_base.
*
* The post type or taxonomy name is used as the route by default, because it is unique and
* stable. The rewrite slug is deliberately not used: it is not guaranteed to be unique (a
* custom type can reuse a built-in type's rewrite slug) and it can change, which would make
* the settings section unreachable or its URL unstable. See https://github.com/Yoast/wordpress-seo/issues/20864.
*
* @param string $name The name.
* @param array $rewrite The rewrite data.
* @param string $rest_base The rest base.
*
* @return string The route.
*/
public function get_route( $name, $rewrite, $rest_base ) {
public function get_route( $name, $rest_base ) {
$route = $name;
if ( isset( $rewrite['slug'] ) ) {
$route = $rewrite['slug'];
}
if ( ! empty( $rest_base ) ) {
$route = $rest_base;
}
Expand Down
4 changes: 2 additions & 2 deletions src/integrations/settings-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ protected function transform_post_types( $post_types ) {
foreach ( $post_types as $post_type ) {
$transformed[ $post_type->name ] = [
'name' => $post_type->name,
'route' => $this->route_helper->get_route( $post_type->name, $post_type->rewrite, $post_type->rest_base ),
'route' => $this->route_helper->get_route( $post_type->name, $post_type->rest_base ),
'label' => $post_type->label,
'singularLabel' => $post_type->labels->singular_name,
'hasArchive' => $this->post_type_helper->has_archive( $post_type ),
Expand Down Expand Up @@ -1047,7 +1047,7 @@ protected function transform_taxonomies( $taxonomies, $post_type_names ) {
foreach ( $taxonomies as $taxonomy ) {
$transformed[ $taxonomy->name ] = [
'name' => $taxonomy->name,
'route' => $this->route_helper->get_route( $taxonomy->name, $taxonomy->rewrite, $taxonomy->rest_base ),
'route' => $this->route_helper->get_route( $taxonomy->name, $taxonomy->rest_base ),
'label' => $taxonomy->label,
'showUi' => $taxonomy->show_ui,
'singularLabel' => $taxonomy->labels->singular_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function get_link(): ?string {
$post_type = \get_post_type_object( $this->get_post_type() );
$link = \sprintf(
'admin.php?page=wpseo_page_settings#/post-type/%s',
$this->route_helper->get_route( $post_type->name, $post_type->rewrite, $post_type->rest_base ),
$this->route_helper->get_route( $post_type->name, $post_type->rest_base ),
);

return \self_admin_url( $link );
Expand Down
82 changes: 82 additions & 0 deletions tests/Unit/Helpers/Route_Helper_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Yoast\WP\SEO\Tests\Unit\Helpers;

use Yoast\WP\SEO\Helpers\Route_Helper;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Unit Test Class.
*
* @coversDefaultClass \Yoast\WP\SEO\Helpers\Route_Helper
*
* @group helpers
*/
final class Route_Helper_Test extends TestCase {

/**
* The instance to test.
*
* @var Route_Helper
*/
private $instance;

/**
* Sets up the test fixtures.
*
* @return void
*/
protected function set_up() {
parent::set_up();

$this->instance = new Route_Helper();
}

/**
* Tests that the route is built from the name, falling back to the rest base, with leading
* slashes stripped.
*
* @covers ::get_route
*
* @dataProvider provider_get_route
*
* @param string $name The name.
* @param string $rest_base The rest base.
* @param string $expected The expected route.
*
* @return void
*/
public function test_get_route( $name, $rest_base, $expected ) {
$this->assertSame( $expected, $this->instance->get_route( $name, $rest_base ) );
}

/**
* Data provider for test_get_route.
*
* @return array<string, array<string, string>>
*/
public static function provider_get_route() {
return [
'Uses the name when no rest base is set' => [
'name' => 'my-post-type',
'rest_base' => '',
'expected' => 'my-post-type',
],
'Uses the rest base when it is set' => [
'name' => 'my-post-type',
'rest_base' => 'my-rest-base',
'expected' => 'my-rest-base',
],
'Strips a leading slash from the name' => [
'name' => '/my-post-type',
'rest_base' => '',
'expected' => 'my-post-type',
],
'Strips multiple leading slashes from the rest base' => [
'name' => 'my-post-type',
'rest_base' => '///my-rest-base',
'expected' => 'my-rest-base',
],
];
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Integrations/Settings_Integration_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public function test_transform_post_types( $post_types, $new_post_types, $expect
$post_type = $post_types['book'];
$this->route_helper
->expects( 'get_route' )
->with( $post_type->name, $post_type->rewrite, $post_type->rest_base )
->with( $post_type->name, $post_type->rest_base )
->andReturn( $post_type->rewrite['slug'] );
$result = $this->instance_double->transform_post_types( $post_types );

Expand Down Expand Up @@ -486,7 +486,7 @@ public function test_transform_taxonomies( $taxonomies, $post_type_names, $new_t
$taxonomy = $taxonomies['book_category'];
$this->route_helper
->expects( 'get_route' )
->with( $taxonomy->name, $taxonomy->rewrite, $taxonomy->rest_base )
->with( $taxonomy->name, $taxonomy->rest_base )
->andReturn( $taxonomy->rewrite['slug'] );

$result = $this->instance_double->transform_taxonomies( $taxonomies, $post_type_names );
Expand Down
Loading