Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions includes/Abilities/Gated/Gated_Abilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Registry of gated abilities.
*
* @package WordPress\AI\Abilities\Gated
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Gated;

use Throwable;
use WordPress\AI\Abstracts\Abstract_Gated_Ability;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Provides the abilities gated behind the Custom Abilities experiment.
*
* New gated abilities are added to the class list below, or registered by third
* parties via the `wpai_gated_abilities` filter — no other code needs to change.
*
* @internal
*
* @since x.x.x
*/
final class Gated_Abilities {
/**
* The default gated ability classes.
*
* @since x.x.x
*
* @var array<class-string<\WordPress\AI\Abstracts\Abstract_Gated_Ability>>
*/
private const GATED_ABILITY_CLASSES = array( // phpcs:ignore SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition -- This is used as an array const.
Post_Utilities::class,
Read_Settings::class,
Read_Users::class,
Read_Content::class,
);

/**
* Gets all registered gated ability instances.
*
* @since x.x.x
*
* @return array<int, \WordPress\AI\Abstracts\Abstract_Gated_Ability> The gated ability instances.
*/
public static function get_all(): array {
/**
* Filters the list of gated ability classes.
*
* Allows developers to add, remove, or replace the abilities gated behind
* the Custom Abilities experiment.
*
* @since x.x.x
*
* @param array<class-string<\WordPress\AI\Abstracts\Abstract_Gated_Ability>> $classes Gated ability class names.
*/
$items = apply_filters( 'wpai_gated_abilities', self::GATED_ABILITY_CLASSES );

$abilities = array();
foreach ( array_unique( (array) $items ) as $item ) {
if ( ! is_string( $item ) ) {
_doing_it_wrong(
__METHOD__,
esc_html__( 'Attempted to register an invalid gated ability. Gated abilities must be class-strings.', 'ai' ),
'x.x.x'
);
continue;
}

if ( ! is_a( $item, Abstract_Gated_Ability::class, true ) ) {
_doing_it_wrong(
__METHOD__,
esc_html__( 'Attempted to register an invalid gated ability. All gated abilities must extend Abstract_Gated_Ability.', 'ai' ),
'x.x.x'
);
continue;
}

try {
$abilities[] = new $item();
} catch ( Throwable $e ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: Gated ability class name, 2: Error message. */
esc_html__( 'Failed to instantiate gated ability "%1$s": %2$s', 'ai' ),
esc_html( $item ),
esc_html( $e->getMessage() )
),
'x.x.x'
);
continue;
}
}

return $abilities;
}
}
31 changes: 31 additions & 0 deletions includes/Abilities/Gated/Post_Utilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Gated ability: post utility abilities.
*
* @package WordPress\AI\Abilities\Gated
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Gated;

use WordPress\AI\Abilities\Utilities\Posts;
use WordPress\AI\Abstracts\Abstract_Gated_Ability;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Gates the ai/get-post-details and ai/get-post-terms abilities.
*
* @since x.x.x
*/
final class Post_Utilities extends Abstract_Gated_Ability {
/**
* {@inheritDoc}
*/
public function register(): void {
( new Posts() )->register();
}
}
38 changes: 38 additions & 0 deletions includes/Abilities/Gated/Read_Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Gated ability: read content.
*
* @package WordPress\AI\Abilities\Gated
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Gated;

use WordPress\AI\Abilities\Content\Content as Content_Ability;
use WordPress\AI\Abstracts\Abstract_Gated_Ability;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Gates the core/read-content ability.
*
* @since x.x.x
*/
final class Read_Content extends Abstract_Gated_Ability {
/**
* {@inheritDoc}
*/
public function requires_core_object_exposure(): bool {
return true;
}

/**
* {@inheritDoc}
*/
public function register(): void {
( new Content_Ability() )->init();
}
}
38 changes: 38 additions & 0 deletions includes/Abilities/Gated/Read_Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Gated ability: read settings.
*
* @package WordPress\AI\Abilities\Gated
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Gated;

use WordPress\AI\Abilities\Settings\Settings as Settings_Ability;
use WordPress\AI\Abstracts\Abstract_Gated_Ability;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Gates the core/read-settings ability.
*
* @since x.x.x
*/
final class Read_Settings extends Abstract_Gated_Ability {
/**
* {@inheritDoc}
*/
public function requires_core_object_exposure(): bool {
return true;
}

/**
* {@inheritDoc}
*/
public function register(): void {
( new Settings_Ability() )->init();
}
}
31 changes: 31 additions & 0 deletions includes/Abilities/Gated/Read_Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Gated ability: read users.
*
* @package WordPress\AI\Abilities\Gated
*/

declare( strict_types=1 );

namespace WordPress\AI\Abilities\Gated;

use WordPress\AI\Abilities\Users\Users as Users_Ability;
use WordPress\AI\Abstracts\Abstract_Gated_Ability;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Gates the core/read-users ability.
*
* @since x.x.x
*/
final class Read_Users extends Abstract_Gated_Ability {
/**
* {@inheritDoc}
*/
public function register(): void {
( new Users_Ability() )->init();
}
}
Loading
Loading