__( 'Revision Change Summary', 'ai' ), 'description' => __( 'Summarizes what changed between two revisions of a post in plain language. 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 Summarize changes on a revisions screen', 'ai' ), 'sends' => __( 'The changed lines of the title and content between the two revisions', 'ai' ), 'requests' => __( 'One request per click', 'ai' ), ), ); } /** * {@inheritDoc} */ public function register(): void { add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_assets' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_classic_assets' ) ); } /** * Registers any needed abilities. * * @since 1.5.0 */ public function register_abilities(): void { wp_register_ability( 'ai/' . $this->get_id(), array( 'label' => $this->get_label(), 'description' => $this->get_description(), 'ability_class' => Revision_Change_Summary_Ability::class, ), ); } /** * Enqueues the script for the visual revisions screen in the block editor. * * @since 1.5.0 */ public function enqueue_assets(): void { $screen = get_current_screen(); if ( ! $screen || 'post' !== $screen->base || ! Revision_Change_Summary_Ability::post_type_is_supported( (string) $screen->post_type ) ) { return; } // Existing revisions can still be shown when revisions are turned off for the post. $post = get_post(); if ( $post && ! wp_revisions_enabled( $post ) ) { return; } Asset_Loader::enqueue_script( 'revision_change_summary', 'experiments/revision-change-summary', array( 'include_core_abilities' => true ) ); Asset_Loader::enqueue_style( 'revision_change_summary', 'experiments/revision-change-summary' ); Asset_Loader::localize_script( 'revision_change_summary', 'RevisionChangeSummaryData', array( 'enabled' => $this->is_enabled(), ) ); } /** * Enqueues the script for the classic revisions screen. * * @since 1.5.0 * * @param string $hook_suffix The current admin page. */ public function maybe_enqueue_classic_assets( string $hook_suffix ): void { if ( 'revision.php' !== $hook_suffix ) { return; } // The revisions screen loads its post into the global `$post`. $post = get_post(); if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) || ! Revision_Change_Summary_Ability::post_type_is_supported( $post->post_type ) || ! wp_revisions_enabled( $post ) ) { return; } Asset_Loader::enqueue_script( 'revision_change_summary_classic', 'experiments/revision-change-summary-classic', array( 'include_core_abilities' => true ) ); Asset_Loader::enqueue_style( 'revision_change_summary_classic', 'experiments/revision-change-summary-classic' ); Asset_Loader::localize_script( 'revision_change_summary_classic', 'RevisionChangeSummaryData', array( 'enabled' => $this->is_enabled(), 'postId' => $post->ID, ) ); } }