, max_mb?: int}|null $pipeline * @return array */ public function upload( array $file, ?int $folder_id = null, ?array $pipeline = null ): array { if ( ! current_user_can( 'upload_files' ) ) { throw new \RuntimeException( esc_html__( 'You are not allowed to upload files.', 'foldrocket' ) ); } $error = isset( $file['error'] ) ? (int) $file['error'] : UPLOAD_ERR_NO_FILE; if ( UPLOAD_ERR_OK !== $error ) { throw new \RuntimeException( esc_html( $this->upload_error_message( $error ) ) ); } $tmp = isset( $file['tmp_name'] ) ? (string) $file['tmp_name'] : ''; if ( $tmp === '' || ! file_exists( $tmp ) || ! is_readable( $tmp ) ) { throw new \RuntimeException( esc_html__( 'Invalid upload.', 'foldrocket' ) ); } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $name = isset( $file['name'] ) ? (string) $file['name'] : 'upload.bin'; $name = sanitize_file_name( $name ); if ( $name === '' ) { $name = 'upload.bin'; } /* * Always materialize a local copy and sideload it. * REST / Local / proxies sometimes break is_uploaded_file(), which makes * media_handle_upload() fail even when the temp file is present. */ $sideload_tmp = wp_tempnam( $name ); if ( ! is_string( $sideload_tmp ) || $sideload_tmp === '' ) { throw new \RuntimeException( esc_html__( 'Could not create a temporary file for upload.', 'foldrocket' ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_copy $copied = @copy( $tmp, $sideload_tmp ); if ( ! $copied ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink @unlink( $sideload_tmp ); throw new \RuntimeException( esc_html__( 'Could not read the uploaded file.', 'foldrocket' ) ); } $file_array = array( 'name' => $name, 'type' => isset( $file['type'] ) ? (string) $file['type'] : '', 'tmp_name' => $sideload_tmp, 'error' => 0, 'size' => isset( $file['size'] ) ? (int) $file['size'] : (int) filesize( $sideload_tmp ), ); $override = null; if ( is_array( $pipeline ) ) { $override = array( 'analyze' => ! empty( $pipeline['analyze'] ), 'aiRename' => ! empty( $pipeline['aiRename'] ), ); } UploadPipelineService::set_override( $override ); try { $attachment_id = media_handle_sideload( $file_array, 0 ); if ( is_wp_error( $attachment_id ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink @unlink( $sideload_tmp ); throw new \RuntimeException( esc_html( $attachment_id->get_error_message() ) ); } return $this->finalize( (int) $attachment_id, $folder_id ); } finally { UploadPipelineService::set_override( null ); } } /** * @return array */ private function finalize( int $attachment_id, ?int $folder_id ): array { ( new AssetIndexer() )->index_now( $attachment_id ); // add_attachment already ran the pipeline; queue analysis again only if still eligible // (idempotent via open-job check). Covers the rare case Hooks registered late. ( new UploadPipelineService() )->maybe_queue_analysis( $attachment_id ); if ( $folder_id && $folder_id > 0 ) { ( new FolderRepository() )->assign_attachments( $folder_id, array( $attachment_id ) ); } $asset = ( new AssetRepository() )->find_by_attachment( $attachment_id ); if ( ! $asset ) { throw new \RuntimeException( esc_html__( 'Upload succeeded but the asset could not be loaded.', 'foldrocket' ) ); } return $asset; } private function upload_error_message( int $error ): string { return match ( $error ) { UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE => __( 'The file is too large.', 'foldrocket' ), UPLOAD_ERR_PARTIAL => __( 'The file was only partially uploaded.', 'foldrocket' ), UPLOAD_ERR_NO_FILE => __( 'No file was uploaded.', 'foldrocket' ), UPLOAD_ERR_NO_TMP_DIR => __( 'Missing a temporary folder on the server.', 'foldrocket' ), UPLOAD_ERR_CANT_WRITE => __( 'Failed to write file to disk.', 'foldrocket' ), UPLOAD_ERR_EXTENSION => __( 'A PHP extension blocked the upload.', 'foldrocket' ), default => __( 'Upload failed.', 'foldrocket' ), }; } }