| 1 |
<?php |
| 2 |
/** |
| 3 |
* Slug generation experiment implementation. |
| 4 |
* |
| 5 |
* @package WordPress\AI |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WordPress\AI\Experiments\Slug_Generation; |
| 11 |
|
| 12 |
use WordPress\AI\Abilities\Slug_Generation\Slug_Generation as Slug_Generation_Ability; |
| 13 |
use WordPress\AI\Abstracts\Abstract_Feature; |
| 14 |
use WordPress\AI\Asset_Loader; |
| 15 |
use WordPress\AI\Experiments\Experiment_Category; |
| 16 |
|
| 17 |
use function WordPress\AI\get_min_content_length; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Slug generation experiment. |
| 25 |
* |
| 26 |
* @since 1.3.0 |
| 27 |
*/ |
| 28 |
class Slug_Generation extends Abstract_Feature { |
| 29 |
|
| 30 |
/** |
| 31 |
* {@inheritDoc} |
| 32 |
* |
| 33 |
* @since 1.3.0 |
| 34 |
*/ |
| 35 |
public static function get_id(): string { |
| 36 |
return 'slug-generation'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* {@inheritDoc} |
| 41 |
* |
| 42 |
* @since 1.3.0 |
| 43 |
*/ |
| 44 |
protected function load_metadata(): array { |
| 45 |
return array( |
| 46 |
'label' => __( 'Slug Generation', 'ai' ), |
| 47 |
'description' => __( 'Suggests SEO-friendly permalink slugs from post title or content. Requires an AI connector that includes support for text generation models.', 'ai' ), |
| 48 |
'category' => Experiment_Category::EDITOR, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritDoc} |
| 54 |
* |
| 55 |
* @since 1.3.0 |
| 56 |
*/ |
| 57 |
public function register(): void { |
| 58 |
add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); |
| 59 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers any needed abilities. |
| 64 |
* |
| 65 |
* @since 1.3.0 |
| 66 |
*/ |
| 67 |
public function register_abilities(): void { |
| 68 |
// Register the AI ability to generate slugs using the Abilities API. |
| 69 |
wp_register_ability( |
| 70 |
'ai/' . $this->get_id(), |
| 71 |
array( |
| 72 |
'label' => $this->get_label(), |
| 73 |
'description' => $this->get_description(), |
| 74 |
'ability_class' => Slug_Generation_Ability::class, |
| 75 |
), |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Enqueues and localizes the admin script. |
| 81 |
* |
| 82 |
* @since 1.3.0 |
| 83 |
* |
| 84 |
* @param string $hook_suffix The current admin page hook suffix. |
| 85 |
*/ |
| 86 |
public function enqueue_assets( string $hook_suffix ): void { |
| 87 |
// Only enqueue on post creation and edit screens. |
| 88 |
if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$screen = get_current_screen(); |
| 93 |
|
| 94 |
// Ensure the post type supports titles and isn't an attachment screen. |
| 95 |
if ( |
| 96 |
! $screen || |
| 97 |
! post_type_supports( $screen->post_type, 'title' ) || |
| 98 |
in_array( $screen->post_type, array( 'attachment' ), true ) |
| 99 |
) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Filters the default number of slug suggestions to generate for the editor UI. |
| 105 |
* |
| 106 |
* @since 1.3.0 |
| 107 |
* |
| 108 |
* @param int $number_of_suggestions Number of suggestions. Default 3. |
| 109 |
*/ |
| 110 |
$number_of_suggestions = (int) apply_filters( 'wpai_slug_generation_number_of_suggestions', 3 ); |
| 111 |
$number_of_suggestions = min( max( $number_of_suggestions, 1 ), 10 ); |
| 112 |
|
| 113 |
// Enqueue backend scripts, styles, and pass localized configuration settings to window. |
| 114 |
Asset_Loader::enqueue_script( 'slug_generation', 'experiments/slug-generation', array( 'include_core_abilities' => true ) ); |
| 115 |
Asset_Loader::enqueue_style( 'slug_generation', 'experiments/slug-generation' ); |
| 116 |
Asset_Loader::localize_script( |
| 117 |
'slug_generation', |
| 118 |
'SlugGenerationData', |
| 119 |
array( |
| 120 |
'enabled' => $this->is_enabled(), |
| 121 |
'minContentLength' => get_min_content_length( 'slug-generation', 250 ), |
| 122 |
'numberOfSuggestions' => $number_of_suggestions, |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
|