array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), ); register_rest_route( self::NS, '/assets/(?P\d+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'get_asset' ), 'permission_callback' => array( __CLASS__, 'perm_edit' ), 'args' => $id, ) ); register_rest_route( self::NS, '/assets/(?P\d+)/source', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'get_source' ), 'permission_callback' => array( __CLASS__, 'perm_edit' ), 'args' => $id, ) ); register_rest_route( self::NS, '/assets/(?P\d+)/replace', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'get_replace_context' ), 'permission_callback' => array( __CLASS__, 'perm_edit' ), 'args' => $id, ) ); register_rest_route( self::NS, '/assets/(?P\d+)/save', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'save' ), 'permission_callback' => array( __CLASS__, 'perm_save' ), 'args' => $id + array( 'mode' => array( 'type' => 'string', 'enum' => array( 'CREATE_NEW', 'REPLACE_EXISTING' ), 'required' => true, ), ), ) ); register_rest_route( self::NS, '/assets/(?P\d+)/backups/delete', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'delete_backups' ), 'permission_callback' => array( __CLASS__, 'perm_replace' ), 'args' => $id, ) ); register_rest_route( self::NS, '/assets/(?P\d+)/restore', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'restore' ), 'permission_callback' => array( __CLASS__, 'perm_replace' ), 'args' => $id, ) ); } /** * Permission: open in editor. * * @param WP_REST_Request $r Request. * @return bool|WP_Error */ public static function perm_edit( $r ) { $id = absint( $r['id'] ); if ( ! Capabilities::is_editable_image( $id ) ) { return new WP_Error( 'pictureeditor_not_image', __( 'That attachment is not an image the editor can open.', 'pictureeditor' ), array( 'status' => 404 ) ); } return Capabilities::can_edit( $id ) ? true : new WP_Error( 'pictureeditor_forbidden', __( 'You do not have permission to edit this image.', 'pictureeditor' ), array( 'status' => rest_authorization_required_code() ) ); } /** * Permission: save in the requested mode. * * @param WP_REST_Request $r Request. * @return bool|WP_Error */ public static function perm_save( $r ) { $id = absint( $r['id'] ); $mode = (string) $r->get_param( 'mode' ); if ( 'REPLACE_EXISTING' === $mode ) { return self::perm_replace( $r ); } if ( ! Capabilities::is_editable_image( $id ) ) { return new WP_Error( 'pictureeditor_not_image', __( 'That attachment is not an image the editor can open.', 'pictureeditor' ), array( 'status' => 404 ) ); } return Capabilities::can_save_new( $id ) ? true : new WP_Error( 'pictureeditor_forbidden', __( 'You do not have permission to add images.', 'pictureeditor' ), array( 'status' => rest_authorization_required_code() ) ); } /** * Permission: replace / restore. * * @param WP_REST_Request $r Request. * @return bool|WP_Error */ public static function perm_replace( $r ) { $id = absint( $r['id'] ); return Capabilities::can_replace( $id ) ? true : new WP_Error( 'pictureeditor_forbidden', __( 'You do not have permission to replace this image.', 'pictureeditor' ), array( 'status' => rest_authorization_required_code() ) ); } /** * Asset descriptor (AssetDescriptor contract, minus the bytes). * * @param WP_REST_Request $r Request. * @return WP_REST_Response */ public static function get_asset( $r ) { $id = absint( $r['id'] ); $file = self::source_path( $id ); $meta = wp_get_attachment_metadata( $id ); $name = $file ? wp_basename( $file ) : wp_basename( (string) get_post_meta( $id, '_wp_attached_file', true ) ); $name = preg_replace( '/-scaled(\.[a-z0-9]+)$/i', '$1', $name ); return new WP_REST_Response( array( 'id' => (string) $id, 'name' => $name, 'mimeType' => get_post_mime_type( $id ), 'width' => isset( $meta['width'] ) ? (int) $meta['width'] : null, 'height' => isset( $meta['height'] ) ? (int) $meta['height'] : null, 'sizeBytes' => $file && file_exists( $file ) ? filesize( $file ) : null, 'alt' => (string) get_post_meta( $id, '_wp_attachment_image_alt', true ), 'title' => get_the_title( $id ), 'permissions' => Capabilities::for_editor( $id ), 'editUrl' => get_edit_post_link( $id, 'raw' ), 'local' => (bool) ( $file && file_exists( $file ) ), ) ); } /** * Highest-resolution file for an attachment. * * @param int $id Attachment ID. * @return string|false */ private static function source_path( $id ) { $orig = function_exists( 'wp_get_original_image_path' ) ? wp_get_original_image_path( $id ) : false; if ( $orig && file_exists( $orig ) ) { return $orig; } $file = get_attached_file( $id, true ); return $file ? $file : false; } /** * Stream the original bytes. Same-origin, so no CDN CORS problems; only for users who * may edit it. Offloaded files fall back to the attachment URL (no server-side fetching * of arbitrary URLs — §10.2, SSRF). * * @param WP_REST_Request $r Request. * @return WP_REST_Response|WP_Error|void */ public static function get_source( $r ) { $id = absint( $r['id'] ); $file = self::source_path( $id ); if ( ! $file || ! file_exists( $file ) ) { $url = wp_get_original_image_url( $id ); if ( ! $url ) { $url = wp_get_attachment_url( $id ); } return new WP_REST_Response( array( 'redirect' => $url ), 200 ); } $mime = wp_get_image_mime( $file ); if ( ! $mime ) { $mime = get_post_mime_type( $id ); } if ( ! headers_sent() ) { nocache_headers(); header( 'Content-Type: ' . $mime ); header( 'Content-Length: ' . filesize( $file ) ); header( 'X-Content-Type-Options: nosniff' ); header( 'Content-Disposition: inline; filename="' . rawurlencode( wp_basename( $file ) ) . '"' ); } readfile( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions exit; } /** * Replace context: where the image is used, what blocks replacing it, backups. * * @param WP_REST_Request $r Request. * @return WP_REST_Response */ public static function get_replace_context( $r ) { $id = absint( $r['id'] ); $can = Capabilities::can_replace( $id ); $backups = Saver::backups( $id ); return new WP_REST_Response( array( 'usages' => Usage::find( $id ), 'blockers' => $can ? Compat::replace_blockers( $id ) : array( __( 'You do not have permission to replace this image.', 'pictureeditor' ) ), 'cautions' => Compat::replace_cautions(), 'backup' => true, 'backups' => array_map( function ( $b ) { return array( 'time' => (int) $b['time'] ); }, $backups ), ) ); } /** * Save. Idempotent per user + key for 24 h, so a retried request never creates two copies. * * @param WP_REST_Request $r Request. * @return WP_REST_Response|WP_Error */ public static function save( $r ) { $id = absint( $r['id'] ); $mode = (string) $r->get_param( 'mode' ); $key = substr( preg_replace( '/[^A-Za-z0-9_-]/', '', (string) $r->get_param( 'idempotency_key' ) ), 0, 64 ); $context = absint( $r->get_param( 'context_post' ) ); $idem = $key ? 'pictureeditor_idem_' . md5( get_current_user_id() . '|' . $id . '|' . $key ) : ''; if ( $idem ) { $prev = get_transient( $idem ); if ( is_array( $prev ) && ! empty( $prev['assetId'] ) && get_post( (int) $prev['assetId'] ) ) { $prev['replayed'] = true; return new WP_REST_Response( $prev, 200 ); } } $files = $r->get_file_params(); $upload = Saver::validate_upload( isset( $files['file'] ) ? $files['file'] : null ); if ( is_wp_error( $upload ) ) { Log::add( array( 'source' => $id, 'mode' => $mode, 'outcome' => 'failed', 'code' => $upload->get_error_code(), ) ); return $upload; } $alt = $r->get_param( 'alt' ); $alt = null === $alt ? null : sanitize_text_field( (string) $alt ); $manifest = (string) $r->get_param( 'manifest' ); if ( 'REPLACE_EXISTING' === $mode ) { $result = Saver::replace( $id, $upload, $alt, $manifest ); } else { $parent = 0; if ( $context && current_user_can( 'edit_post', $context ) ) { $parent = $context; } $result = Saver::create_new( $id, $upload, (string) $r->get_param( 'name' ), $alt, $manifest, $parent ); } if ( is_wp_error( $result ) ) { Log::add( array( 'source' => $id, 'mode' => $mode, 'outcome' => 'failed', 'code' => $result->get_error_code(), 'product' => $context, ) ); return $result; } $body = array( 'assetId' => (string) $result, 'status' => 'READY', 'url' => wp_get_attachment_url( $result ), 'editUrl' => get_edit_post_link( $result, 'raw' ), 'replacedId' => 'REPLACE_EXISTING' === $mode ? (string) $id : null, 'width' => $upload['width'], 'height' => $upload['height'], ); if ( $idem ) { set_transient( $idem, $body, DAY_IN_SECONDS ); } Log::add( array( 'source' => $id, 'result' => $result, 'mode' => $mode, 'outcome' => 'saved', 'product' => $context, ) ); return new WP_REST_Response( $body, 'REPLACE_EXISTING' === $mode ? 200 : 201 ); } /** * Delete all backups of an attachment. * * @param WP_REST_Request $r Request. * @return WP_REST_Response */ public static function delete_backups( $r ) { $id = absint( $r['id'] ); $n = Saver::delete_backups( $id ); Log::add( array( 'source' => $id, 'mode' => 'delete_backups', 'outcome' => 'deleted', ) ); return new WP_REST_Response( array( 'deleted' => $n ) ); } /** * Restore latest backup. * * @param WP_REST_Request $r Request. * @return WP_REST_Response|WP_Error */ public static function restore( $r ) { $id = absint( $r['id'] ); $res = Saver::restore_latest( $id ); if ( is_wp_error( $res ) ) { return $res; } Log::add( array( 'source' => $id, 'result' => $id, 'mode' => 'restore', 'outcome' => 'restored', ) ); return new WP_REST_Response( array( 'restored' => true, 'url' => wp_get_attachment_url( $id ), ) ); } }