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

Disable Attachment Routing #64

Merged
merged 15 commits into from
Jan 10, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

### Added

* `disable_attachment_routing`: Added a feature to disable attachment routing.
* `disable_custom_fields_meta_box`: Added a feature to disable the custom fields meta box.
* `disable_password_change_notification`: Added a feature that disables sending password change notification emails to site admins.

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,87 @@
<?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( 'pre_option_wp_attachment_pages_enabled', '__return_zero', 100 );
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 ): void {
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_custom_fields_meta_box' => new Features\Disable_Custom_Fields_Meta_Box(),
'disable_dashboard_widgets' => new Features\Disable_Dashboard_Widgets(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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 = self::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 = self::factory()->attachment->create();
$permalink = get_permalink( $attachment_id );

$this
->get( $permalink )
->assertOk()
->assertQueriedObjectId( $attachment_id );

$this->feature->boot();

$this->get( $permalink )->assertNotFound();
}

/**
* Test that the attachment pages are disabled using the new
* 'wp_attachment_pages_enabled' option from WordPress 6.4.
*/
public function test_attachment_pages_disabled_using_option() {
update_option( 'wp_attachment_pages_enabled', '1' );

$this->assertEquals( '1', get_option( 'wp_attachment_pages_enabled' ) );

$this->feature->boot();

$this->assertEquals( '0', get_option( 'wp_attachment_pages_enabled' ) );
}
}
Loading