__( 'Slug Generation', 'ai' ), 'description' => __( 'Suggests SEO-friendly permalink slugs from post title or content. Requires an AI connector that includes support for text generation models.', 'ai' ), 'category' => Experiment_Category::EDITOR, 'usage' => array( 'trigger' => 'on_demand', 'runs' => __( 'When you click Generate Slug or Generate Suggestions', 'ai' ), 'sends' => __( 'Post content, title, current slug, author name, excerpt, content type, and assigned categories and tags', 'ai' ), 'requests' => __( 'One request per click', 'ai' ), ), ); } /** * {@inheritDoc} * * @since 1.3.0 */ public function register(): void { add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Registers any needed abilities. * * @since 1.3.0 */ public function register_abilities(): void { // Register the AI ability to generate slugs using the Abilities API. wp_register_ability( 'ai/' . $this->get_id(), array( 'label' => $this->get_label(), 'description' => $this->get_description(), 'ability_class' => Slug_Generation_Ability::class, ), ); } /** * Enqueues and localizes the admin script. * * @since 1.3.0 * * @param string $hook_suffix The current admin page hook suffix. */ public function enqueue_assets( string $hook_suffix ): void { // Only enqueue on post creation and edit screens. if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) { return; } $screen = get_current_screen(); // Ensure the post type supports titles and isn't an attachment screen. if ( ! $screen || ! post_type_supports( $screen->post_type, 'title' ) || in_array( $screen->post_type, array( 'attachment' ), true ) ) { return; } /** * Filters the default number of slug suggestions to generate for the editor UI. * * @since 1.3.0 * * @param int $number_of_suggestions Number of suggestions. Default 3. */ $number_of_suggestions = (int) apply_filters( 'wpai_slug_generation_number_of_suggestions', 3 ); $number_of_suggestions = min( max( $number_of_suggestions, 1 ), 10 ); // Enqueue backend scripts, styles, and pass localized configuration settings to window. Asset_Loader::enqueue_script( 'slug_generation', 'experiments/slug-generation', array( 'include_core_abilities' => true ) ); Asset_Loader::enqueue_style( 'slug_generation', 'experiments/slug-generation' ); Asset_Loader::localize_script( 'slug_generation', 'SlugGenerationData', array( 'enabled' => $this->is_enabled(), 'minContentLength' => get_min_content_length( 'slug-generation', 250 ), 'numberOfSuggestions' => $number_of_suggestions, ) ); } }