* @author Maximum Software Inc * @license GPL-3.0-only * @link https://agenistrator.com/ * * SPDX-FileCopyrightText: 2025-2026 Alex Ruban * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc * SPDX-License-Identifier: GPL-3.0-only */ namespace Agenistrator; // Direct access prevention if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Delete_Media_Tool class. */ class Delete_Media_Tool extends Tool { /** * Get the tool ID. * * @return string The tool ID. * @since 1.0.0 */ public function get_id() { return 'delete_media'; } /** * Get the tool name. * * @return string The tool name. * @since 1.0.0 */ public function get_name() { return __( 'Delete Media', 'agenistrator' ); } /** * Get the tool description. * * @return string The tool description. * @since 1.0.0 */ public function get_description() { return 'Delete a media item from the library.'; } /** * Get the parameters schema for this tool. * * @return array Parameters schema in JSON Schema format. * @since 1.0.0 */ public function get_parameters() { return array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'description' => 'The ID of the media item to delete', ), 'force_delete' => array( 'type' => 'boolean', 'description' => 'Whether to permanently delete (true) or move to trash (false)', 'default' => false, ), ), 'required' => array( 'id' ), ); } /** * Check if the current user can execute this tool. * * @return bool True if the current user has permission. * @since 1.0.0 */ public function current_user_can_execute() { return current_user_can( 'upload_files' ); } /** * Check if this tool is read-only. * * @return bool True if the tool only reads data. * @since 1.0.0 */ public function is_read_only() { return false; } /** * Execute the tool with the given arguments. * * @param array $args The tool arguments. * * @return array The tool execution result. * @since 1.0.0 */ protected function execute( $args ) { if ( ! isset( $args['id'] ) || empty( $args['id'] ) ) { return $this->error_response( 'Media ID is required' ); } $attachment_id = intval( $args['id'] ); $attachment = get_post( $attachment_id ); if ( ! $attachment || 'attachment' !== $attachment->post_type ) { return $this->error_response( 'Media item not found' ); } if ( ! current_user_can( 'delete_post', $attachment_id ) ) { return $this->error_response( 'You do not have permission to delete this media item' ); } $title = $attachment->post_title; $url = wp_get_attachment_url( $attachment_id ); $force_delete = isset( $args['force_delete'] ) && true === $args['force_delete']; $deleted = wp_delete_attachment( $attachment_id, $force_delete ); if ( ! $deleted ) { return $this->error_response( 'Failed to delete media item' ); } $message = $force_delete ? 'Media permanently deleted' : 'Media moved to trash'; return $this->success_response( $message, array( 'attachment_id' => $attachment_id, 'title' => $title, 'url' => $url, 'force_deleted' => $force_delete, ) ); } }