0, "Body" => 0, ...] * If the slot is used by an item, the value is the item_id, else it is 0 * * The inventory ID of an item is generated each time the item is added to the character's inventory. * It is not the same as the item definition ID. */ class Item_Definition { public $id; public $code; public $name; public $description; public $stats; public $image_url; public function __construct( $id, $code, $name, $description, $stats, $image_url ) { $this->id = $id; $this->code = $code; $this->name = $name; $this->description = $description; $this->stats = json_decode( $stats, true ); $this->image_url = $image_url; } public static function get_item_definition_by_id( $id ): ?Item_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } public static function get_item_definition_by_code( $code ): ?Item_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions WHERE code = %s", $code ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } public static function get_all_item_definitions(): array { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions", ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $item_definitions = array(); foreach ( $results as $result ) { $item_definitions[] = new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } // sort by name usort( $item_definitions, function ( $a, $b ) { return strcmp( $a->name, $b->name ); } ); return $item_definitions; } public static function create_item_definition( $code, $name, $description, $stats, $image_url ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'name' => $name, 'description' => $description, 'stats' => json_encode( $stats ), 'image_url' => $image_url ) ); } public static function update_item_definition( $id, $code, $name, $description, $stats, $image_url ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->update( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'name' => $name, 'description' => $description, 'stats' => json_encode( $stats ), 'image_url' => $image_url ), array( 'id' => $id ) ); } public static function delete_item_definition( $id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } // get stats for this item public function get_stats(): array { return $this->stats; } public function get_item_slot(): ?string { if ( isset( $this->stats['Slot'] ) && ! empty( $this->stats['Slot'] ) ) { return $this->stats['Slot']; } return null; } /** * Generates the HTML representation of the item, including its image, name, and description. * * @param Combat_System $adventurebook_combat_system The combat system to determine which stats to show. * * @return string The HTML content for the item. */ public function get_item_html( Combat_System $adventurebook_combat_system, $count = 0 ): string { $item = $this; $item_id = 'adventurebook_item_' . $item->id; // eindeutige ID für JS-Selektoren $content = '