Skip to content

Commit a65b9cf

Browse files
committed
Centralize Trait_View_Stream_Permission loading
Each of the 5 read abilities (get-records, get-record, get-alerts, get-connectors, get-exclusion-rules) previously did its own require_once for trait-view-stream-permission.php at the top of the class file. A new read ability that forgot the require would silently fall back to Ability::permission_callback() and gate on WP_STREAM_SETTINGS_CAPABILITY instead of view_stream. Move the require_once into the two chokepoints that actually load ability files: Abilities::load_abilities() for production and Abilities_TestCase::setUp() for PHPUnit. New abilities now get the trait available automatically -- no per-file require to forget. Verified via PHP introspection (trait_exists, class_uses on all 5 ability classes) and a live REST smoke test that admin GETs return 200 and subscriber GETs return 403 across all five read abilities.
1 parent d1fc2e0 commit a65b9cf

7 files changed

Lines changed: 15 additions & 10 deletions

abilities/class-ability-get-alerts.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace WP_Stream;
99

10-
require_once __DIR__ . '/trait-view-stream-permission.php';
11-
1210
/**
1311
* Class - Ability_Get_Alerts
1412
*/

abilities/class-ability-get-connectors.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace WP_Stream;
99

10-
require_once __DIR__ . '/trait-view-stream-permission.php';
11-
1210
/**
1311
* Class - Ability_Get_Connectors
1412
*/

abilities/class-ability-get-exclusion-rules.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace WP_Stream;
99

10-
require_once __DIR__ . '/trait-view-stream-permission.php';
11-
1210
/**
1311
* Class - Ability_Get_Exclusion_Rules
1412
*/

abilities/class-ability-get-record.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace WP_Stream;
99

10-
require_once __DIR__ . '/trait-view-stream-permission.php';
11-
1210
/**
1311
* Class - Ability_Get_Record
1412
*/

abilities/class-ability-get-records.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace WP_Stream;
99

10-
require_once __DIR__ . '/trait-view-stream-permission.php';
11-
1210
/**
1311
* Class - Ability_Get_Records
1412
*/

classes/class-abilities.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ public function get_ability_slugs() {
221221
public function load_abilities() {
222222
$dir = trailingslashit( $this->plugin->locations['dir'] ) . 'abilities/';
223223

224+
// Load shared trait once before any ability file is included. The
225+
// read abilities `use Trait_View_Stream_Permission` and PHP needs the
226+
// trait declared before the class declaration is parsed. Doing it
227+
// here keeps the require centralized -- new read abilities don't
228+
// have to remember to require the trait themselves.
229+
require_once $dir . 'trait-view-stream-permission.php';
230+
224231
foreach ( $this->get_ability_slugs() as $slug ) {
225232
$file = $dir . 'class-ability-' . $slug . '.php';
226233
if ( ! is_readable( $file ) ) {

tests/phpunit/abilities/abilities-testcase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public function setUp(): void {
4848
$this->markTestSkipped( 'Requires WordPress 6.9+ (Abilities API).' );
4949
}
5050

51+
// Mirror Abilities::load_abilities(): the trait must be loaded before
52+
// any ability class file is `require_once`'d by a test setUp, since the
53+
// `use Trait_View_Stream_Permission` in the class body needs the trait
54+
// declared at parse time. In production the require lives in
55+
// load_abilities(), but tests bypass that path and pull each ability
56+
// file in directly.
57+
require_once $this->plugin->locations['dir'] . 'abilities/trait-view-stream-permission.php';
58+
5159
$this->admin_user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
5260
$this->subscriber_user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
5361

0 commit comments

Comments
 (0)