$row Raw row. * * @return array */ public function cast( array $row ): array { foreach ( array( 'id', 'size' ) as $column ) { if ( isset( $row[ $column ] ) ) { $row[ $column ] = (int) $row[ $column ]; } } foreach ( array( 'folder_id', 'assigned_user_id' ) as $column ) { if ( array_key_exists( $column, $row ) ) { $row[ $column ] = null === $row[ $column ] ? null : (int) $row[ $column ]; } } // The on-disk name is a filesystem secret. The client asks for bytes by id, through a route that // checks who is asking — it never learns where the file lives. unset( $row['disk_name'] ); return $row; } /** * Move every document out of one folder and into another (or to unfiled). * * A bulk reassign used when a folder is deleted — its documents must not be left pointing at a * `folder_id` that no longer exists. Deliberately a single UPDATE, not a loop of {@see update()}: a * folder deletion is not a per-document edit and does not want a modified_at bump or an event per row. * * @param int $from_folder The folder being emptied. * @param int|null $to_folder Where its documents go — null unfiles them. */ public function move_folder( int $from_folder, ?int $to_folder ): void { global $wpdb; $table = $this->table_name(); if ( null === $to_folder ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, no object cache to read from. $wpdb->query( $wpdb->prepare( "UPDATE %i SET folder_id = NULL WHERE folder_id = %d", $table, $from_folder ) ); return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom table, no object cache to read from. $wpdb->query( $wpdb->prepare( "UPDATE %i SET folder_id = %d WHERE folder_id = %d", $table, $to_folder, $from_folder ) ); } /** * Global-search hits. Runs through QueryBuilder, so record scoping applies. * * @param string $term What the user typed. * @param int $limit Maximum hits. * * @return array */ public function search( string $term, int $limit ): array { $rows = $this->query( 'search' ) ->search( $term, array( 't.name', 't.original_name' ) ) ->order_by( 't.name', 'ASC' ) ->paginate( $limit, 0 ) ->get(); return array_map( static fn( array $row ): array => array( 'id' => (int) $row['id'], 'title' => (string) $row['name'], 'subtitle' => (string) $row['original_name'], ), $rows ); } }