draft_service = $draft_service; } /** * Permission callback for wp_register_ability. * * @return bool Whether the current user has the required capability. */ public function check_permissions(): bool { return current_user_can( 'edit_pages' ); } /** * Register this tool as a WordPress ability. */ public function register(): void { wp_register_ability( self::TOOL_ID, array( 'label' => __( 'Discard Page Draft', 'airo-wp' ), 'description' => __( 'Discards a draft page by deleting it without merging changes into the original published page.', 'airo-wp' ), 'input_schema' => $this->get_input_schema(), 'output_schema' => $this->get_output_schema(), 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => array( $this, 'check_permissions' ), 'category' => 'content-management', ) ); } /** * Execute the tool. * * @param array $input Input parameters. * @return array Execution result. */ public function execute( array $input ): array { $draft_id = absint( $input['draft_id'] ?? 0 ); if ( $draft_id <= 0 ) { return array( 'success' => false, 'message' => __( 'A valid draft page ID is required.', 'airo-wp' ), ); } $draft = get_post( $draft_id ); if ( ! $draft ) { return array( 'success' => false, 'message' => __( 'The requested draft page does not exist.', 'airo-wp' ), ); } if ( 'page' !== $draft->post_type ) { return array( 'success' => false, 'message' => __( 'Only page drafts can be discarded.', 'airo-wp' ), ); } if ( 'draft' !== $draft->post_status ) { return array( 'success' => false, 'message' => __( 'Only posts in draft status can be discarded as a page draft.', 'airo-wp' ), ); } $original_meta = get_post_meta( $draft_id, DraftPageService::META_DRAFT_OF, true ); if ( empty( $original_meta ) ) { return array( 'success' => false, 'message' => __( 'The page is not a draft copy of a published page.', 'airo-wp' ), ); } $original_id = absint( $original_meta ); if ( $original_id <= 0 ) { return array( 'success' => false, 'message' => __( 'The draft is missing a valid original page reference.', 'airo-wp' ), ); } if ( ! current_user_can( 'edit_post', $original_id ) || ! current_user_can( 'delete_post', $draft_id ) ) { return array( 'success' => false, 'message' => __( 'You do not have permission to discard this draft.', 'airo-wp' ), ); } $force_delete = array_key_exists( 'force_delete', $input ) ? (bool) $input['force_delete'] : false; $result = $this->draft_service->discard_draft( $draft_id, $force_delete ); if ( is_wp_error( $result ) ) { return array( 'success' => false, 'message' => $result->get_error_message(), ); } return array( 'success' => true, 'original_id' => $original_id, 'message' => __( 'Draft discarded successfully.', 'airo-wp' ), ); } /** * Get input schema. * * @return array */ private function get_input_schema(): array { return array( 'type' => 'object', 'properties' => array( 'draft_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => __( 'The ID of the draft page to discard.', 'airo-wp' ), ), 'force_delete' => array( 'type' => 'boolean', 'description' => __( 'Whether to permanently delete the draft (true) or move it to trash so it can be restored (false). Defaults to false.', 'airo-wp' ), 'default' => false, ), ), 'required' => array( 'draft_id' ), ); } /** * Get output schema. * * @return array */ private function get_output_schema(): array { return $this->build_output_schema( __( 'Page draft discard result', 'airo-wp' ), array( 'original_id' => array( 'type' => 'integer', 'description' => __( 'The ID of the original page the draft was created from.', 'airo-wp' ), ), ) ); } }