-
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.
Merge pull request #106 from alleyinteractive/feature/105-site-health
Adding feature to disable WordPress directory information
- Loading branch information
Showing
16 changed files
with
239 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: wp-alleyvate@feature | ||
|
||
inputs: | ||
- name: featureName | ||
description: "Feature Name" | ||
type: string | ||
- name: tests | ||
description: "Include Tests?" | ||
type: boolean | ||
default: true | ||
|
||
files: | ||
- source: feature.php.hbs | ||
destination: src/alley/wp/alleyvate/features/{{ wpClassFilename inputs.featureName }} | ||
- source: test.php.hbs | ||
if: "{{ inputs.tests }}" | ||
destination: tests/alley/wp/alleyvate/features/{{ wpClassFilename inputs.featureName prefix="test-" }} |
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,27 @@ | ||
<?php | ||
/** | ||
* Class file for {{ wpClassName inputs.featureName }} | ||
* | ||
* (c) Alley <info@alley.com> | ||
* | ||
* 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\Types\Feature; | ||
|
||
/** | ||
* {{ wpClassName inputs.featureName }} feature. | ||
*/ | ||
final class {{ wpClassName inputs.featureName }} implements Feature { | ||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
// ... | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
/** | ||
* Class file for {{ wpClassName inputs.featureName prefix="Test_" }} | ||
* | ||
* (c) Alley <info@alley.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Mantle\Testing\Concerns\Refresh_Database; | ||
use Mantle\Testkit\Test_Case; | ||
|
||
/** | ||
* Tests for {{ wpClassName inputs.featureName }} feature. | ||
*/ | ||
final class {{ wpClassName inputs.featureName prefix="Test_" }} extends Test_Case { | ||
use Refresh_Database; | ||
|
||
/** | ||
* Feature instance. | ||
* | ||
* @var {{ wpClassName inputs.featureName }} | ||
*/ | ||
private {{ wpClassName inputs.featureName }} $feature; | ||
|
||
/** | ||
* Set up. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->feature = new {{ wpClassName inputs.featureName }}(); | ||
} | ||
|
||
/** | ||
* Example Test. | ||
*/ | ||
public function test_example(): void { | ||
// Activate the feature. | ||
$this->feature->boot(); | ||
|
||
$this->assertTrue( true ); | ||
} | ||
} |
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
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
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
62 changes: 62 additions & 0 deletions
62
src/alley/wp/alleyvate/features/class-disable-site-health-directories.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,62 @@ | ||
<?php | ||
/** | ||
* Class file for Disable_Site_Health_Directories | ||
* | ||
* (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\Types\Feature; | ||
|
||
/** | ||
* Disable_Site_Health_Directories feature. | ||
*/ | ||
final class Disable_Site_Health_Directories implements Feature { | ||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
add_filter( 'rest_pre_dispatch', [ $this, 'filter_rest_pre_dispatch' ], 10, 3 ); | ||
add_filter( 'debug_information', [ $this, 'filter_debug_information' ] ); | ||
} | ||
|
||
/** | ||
* Filter REST API requests to remove Site Health directories. | ||
* | ||
* @param mixed $result Response to replace the requested version with. Can be anything a normal endpoint can return, or null to not hijack the request. | ||
* @param \WP_REST_Server $server Server instance. | ||
* @param \WP_REST_Request $request Request used to generate the response. | ||
* @return mixed Response to replace the requested version with. | ||
*/ | ||
public function filter_rest_pre_dispatch( $result, $server, $request ) { | ||
if ( $request->get_route() === '/wp-site-health/v1/directory-sizes' ) { | ||
return new \WP_Error( 'rest_disabled', 'REST API endpoint disabled.', [ 'status' => 403 ] ); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Filter debug information to remove Site Health directories. | ||
* | ||
* @param array<string, array{label: string, description: string, fields: array<int, mixed>}> $info Debug information. | ||
* @return array<string, array{label: string, description: string, fields: array<int, mixed>}> Debug information. | ||
*/ | ||
public function filter_debug_information( $info ): array { | ||
if ( ! \is_array( $info ) ) { | ||
$info = []; | ||
} | ||
|
||
if ( isset( $info['wp-paths-sizes'] ) ) { | ||
unset( $info['wp-paths-sizes'] ); | ||
} | ||
|
||
return $info; | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
tests/alley/wp/alleyvate/features/test-disable-site-health-directories.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,52 @@ | ||
<?php | ||
/** | ||
* Class file for Test_Disable_Site_Health_Directories | ||
* | ||
* (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 | ||
*/ | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Mantle\Testing\Concerns\Refresh_Database; | ||
use Mantle\Testkit\Test_Case; | ||
|
||
/** | ||
* Tests for Disable_Site_Health_Directories feature. | ||
*/ | ||
final class Test_Disable_Site_Health_Directories extends Test_Case { | ||
use Refresh_Database; | ||
|
||
/** | ||
* Feature instance. | ||
* | ||
* @var Disable_Site_Health_Directories | ||
*/ | ||
private Disable_Site_Health_Directories $feature; | ||
|
||
/** | ||
* Set up. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->feature = new Disable_Site_Health_Directories(); | ||
} | ||
|
||
/** | ||
* Test that the REST API endpoint is disabled. | ||
*/ | ||
public function test_rest_api_disabled(): void { | ||
$this->feature->boot(); | ||
|
||
$this->acting_as( 'administrator' ); | ||
|
||
$this->get( rest_url( 'wp-site-health/v1/directory-sizes' ) )->assertStatus( 403 ); | ||
} | ||
} |
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