Skip to content

Commit

Permalink
Disable attachment routing
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jan 9, 2024
1 parent 32420a0 commit 5a9329b
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Added

* Added a feature to disable attachment routing.

## 2.2.1

### Added
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Each feature's handle is listed below, along with a description of what it does.

This feature removes selected nodes from the admin bar.

### `disable_attachment_routing`

This feature disables WordPress attachment pages entirely from the front end of the site.

### `disable_comments`

This feature disables WordPress comments entirely, including the ability to post, view, edit, list, count, modify settings for, or access URLs that are related to comments completely.
Expand Down
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' );
}
}
}
1 change: 1 addition & 0 deletions src/alley/wp/alleyvate/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
function available_features(): array {
return [
'clean_admin_bar' => new Features\Clean_Admin_Bar(),
'disable_attachment_routing' => new Features\Disable_Attachment_Routing(),
'disable_comments' => new Features\Disable_Comments(),
'disable_dashboard_widgets' => new Features\Disable_Dashboard_Widgets(),
'disable_sticky_posts' => new Features\Disable_Sticky_Posts(),
Expand Down
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();
}
}

0 comments on commit 5a9329b

Please sign in to comment.