-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/alley/wp/alleyvate/features/class-disable-attachment-routing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* Class file for Disable_Attachment_Routing | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Alley\WP\Alleyvate\Feature; | ||
|
||
/** | ||
* Disable attachment routing. | ||
*/ | ||
final class Disable_Attachment_Routing implements Feature { | ||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
add_filter( 'rewrite_rules_array', [ self::class, 'filter__rewrite_rules_array' ] ); | ||
add_filter( 'attachment_link', [ self::class, 'filter__attachment_link' ] ); | ||
add_action( 'pre_get_posts', [ self::class, 'action__pre_get_posts' ] ); | ||
add_action( 'admin_bar_menu', [ self::class, 'action__admin_bar_menu' ], 100 ); | ||
} | ||
|
||
/** | ||
* Remove support for the attachment rewrite rule. | ||
* | ||
* @param array $rules Rewrite rules. | ||
* @return array | ||
*/ | ||
public static function filter__rewrite_rules_array( $rules ): array { | ||
foreach ( $rules as $regex => $query ) { | ||
if ( strpos( $regex, 'attachment' ) || strpos( $query, 'attachment' ) ) { | ||
unset( $rules[ $regex ] ); | ||
} | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
/** | ||
* Remove the attachment link. | ||
* | ||
* @param string $link Attachment link. | ||
* @return string | ||
*/ | ||
public static function filter__attachment_link( $link ): string { | ||
return ''; | ||
} | ||
|
||
/** | ||
* Ensure attachment pages return 404s. | ||
* | ||
* @param WP_Query $query WP_Query object. | ||
*/ | ||
public static function action__pre_get_posts( $query ) { | ||
if ( is_admin() || ! $query->is_main_query() ) { | ||
return; | ||
} | ||
|
||
if ( | ||
$query->queried_object instanceof \WP_Post | ||
&& 'attachment' === get_post_type( $query->get_queried_object_id() ) | ||
) { | ||
$query->set_404(); | ||
status_header( 404 ); | ||
} | ||
} | ||
|
||
/** | ||
* Remove attachment link from admin bar. | ||
* | ||
* @param \WP_Admin_Bar $wp_admin_bar Admin bar class. | ||
*/ | ||
public static function action__admin_bar_menu( \WP_Admin_Bar $wp_admin_bar ) { | ||
if ( 'attachment' == get_post_type() ) { | ||
$wp_admin_bar->remove_node( 'view' ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/alley/wp/alleyvate/features/test-disable-attachment-routing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Class file for Test_Disable_Attachment_Routing | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Alley\WP\Alleyvate\Feature; | ||
use Mantle\Testkit\Test_Case; | ||
|
||
/** | ||
* Tests for fully disabling attachment routing. | ||
*/ | ||
final class Test_Disable_Attachment_Routing extends Test_Case { | ||
use \Mantle\Testing\Concerns\Admin_Screen; | ||
use \Mantle\Testing\Concerns\Refresh_Database; | ||
|
||
/** | ||
* Feature instance. | ||
* | ||
* @var Feature | ||
*/ | ||
private Feature $feature; | ||
|
||
/** | ||
* Set up. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->feature = new Disable_Attachment_Routing(); | ||
} | ||
|
||
/** | ||
* Test that the attachment permalink is empty. | ||
*/ | ||
public function test_attachment_permalink(): void { | ||
$attachment_id = $this->factory()->attachment->create(); | ||
|
||
$this->assertNotEmpty( get_permalink( $attachment_id ) ); | ||
|
||
$this->feature->boot(); | ||
|
||
$this->assertEmpty( get_permalink( $attachment_id ) ); | ||
} | ||
|
||
/** | ||
* Test that the attachment page returns a 404. | ||
*/ | ||
public function test_attachment_page(): void { | ||
$attachment_id = $this->factory()->attachment->create(); | ||
$permalink = get_permalink( $attachment_id ); | ||
|
||
$this | ||
->get( $permalink ) | ||
->assertOk() | ||
->assertQueriedObjectId( $attachment_id ); | ||
|
||
$this->feature->boot(); | ||
|
||
$this->get( $permalink )->assertNotFound(); | ||
} | ||
} |