register( 'media_list', array( 'description' => 'List media library items.', 'domain' => 'media', 'capability' => 'upload_files', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'search' => array( 'type' => 'string' ), 'mime_type'=> array( 'type' => 'string' ), 'per_page' => array( 'type' => 'integer', 'default' => 20 ), 'page' => array( 'type' => 'integer', 'default' => 1 ), ), ), ), function ( $args ) { $query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 's' => isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '', 'post_mime_type' => isset( $args['mime_type'] ) ? sanitize_text_field( $args['mime_type'] ) : '', 'posts_per_page' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, 'paged' => isset( $args['page'] ) ? max( 1, absint( $args['page'] ) ) : 1, ) ); $items = array_map( 'agentdesk_format_attachment', $query->posts ); return array( 'items' => $items, 'total' => (int) $query->found_posts ); } ); $registry->register( 'media_get', array( 'description' => 'Get a media item by ID.', 'domain' => 'media', 'capability' => 'upload_files', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ) ), 'required' => array( 'id' ), ), ), function ( $args ) { $allowed = Agentdesk_Security::can_read_post( absint( $args['id'] ) ); if ( is_wp_error( $allowed ) ) { return $allowed; } $post = get_post( absint( $args['id'] ) ); if ( ! $post || 'attachment' !== $post->post_type ) { return new WP_Error( 'agentdesk_not_found', 'Media not found' ); } return agentdesk_format_attachment( $post ); } ); $registry->register( 'media_sideload', array( 'description' => 'Sideload media from a URL into the media library.', 'domain' => 'media', 'capability' => 'upload_files', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'url' => array( 'type' => 'string' ), 'title' => array( 'type' => 'string' ), 'alt_text' => array( 'type' => 'string' ), 'post_parent' => array( 'type' => 'integer' ), ), 'required' => array( 'url' ), ), ), function ( $args ) { if ( ! function_exists( 'media_sideload_image' ) ) { require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; } $url = esc_url_raw( $args['url'] ); $id = media_sideload_image( $url, isset( $args['post_parent'] ) ? absint( $args['post_parent'] ) : 0, isset( $args['title'] ) ? sanitize_text_field( $args['title'] ) : null, 'id' ); if ( is_wp_error( $id ) ) { return $id; } if ( ! empty( $args['alt_text'] ) ) { update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $args['alt_text'] ) ); } return agentdesk_format_attachment( get_post( $id ) ); } ); $registry->register( 'media_update', array( 'description' => 'Update media title, caption, alt text.', 'domain' => 'media', 'capability' => 'upload_files', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'title' => array( 'type' => 'string' ), 'caption' => array( 'type' => 'string' ), 'alt_text'=> array( 'type' => 'string' ), ), 'required' => array( 'id' ), ), ), function ( $args ) { $id = absint( $args['id'] ); $allowed = Agentdesk_Security::can_edit_post( $id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $data = array( 'ID' => $id ); if ( isset( $args['title'] ) ) { $data['post_title'] = sanitize_text_field( $args['title'] ); } if ( isset( $args['caption'] ) ) { $data['post_excerpt'] = sanitize_textarea_field( $args['caption'] ); } wp_update_post( $data ); if ( isset( $args['alt_text'] ) ) { update_post_meta( $id, '_wp_attachment_image_alt', sanitize_text_field( $args['alt_text'] ) ); } return agentdesk_format_attachment( get_post( $id ) ); } ); $registry->register( 'media_delete', array( 'description' => 'Delete a media item.', 'domain' => 'media', 'capability' => 'delete_posts', 'blocked_in_safe_mode' => true, 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'force' => array( 'type' => 'boolean', 'default' => true ), ), 'required' => array( 'id' ), ), ), function ( $args ) { $id = absint( $args['id'] ); $allowed = Agentdesk_Security::can_delete_post( $id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $result = wp_delete_attachment( $id, ! empty( $args['force'] ) ); if ( ! $result ) { return new WP_Error( 'agentdesk_delete_failed', 'Could not delete media' ); } return array( 'deleted' => true, 'id' => $id ); } ); } ); /** * Format attachment. * * @param WP_Post $post Attachment post. * @return array */ function agentdesk_format_attachment( $post ) { return array( 'id' => $post->ID, 'title' => $post->post_title, 'caption' => $post->post_excerpt, 'mime_type' => $post->post_mime_type, 'url' => wp_get_attachment_url( $post->ID ), 'alt_text' => get_post_meta( $post->ID, '_wp_attachment_image_alt', true ), 'date' => $post->post_date, ); }