id = intval( $id ); $this->code = $code; $this->name = $name; $this->is_hostile = boolval( $is_hostile ); $this->can_be_attacked = boolval( $can_be_attacked ); $this->xp_value = intval( $xp_value ); $this->description = $description; $this->stats = json_decode( $stats, true ); $this->inventory = json_decode( $inventory, true ); $this->image_url = $image_url; } public static function get_enemy_definition_by_id( $id ): ?Enemy_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_enemy_definitions WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Enemy_Definition( $result['id'], $result['code'], $result['name'], $result['is_hostile'], $result['can_be_attacked'], $result['xp_value'], $result['description'], $result['stats'], $result['inventory'], $result['image_url'] ); } public static function get_enemy_definition_by_code( $code ): ?Enemy_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_enemy_definitions WHERE code = %s", $code ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Enemy_Definition( $result['id'], $result['code'], $result['name'], $result['is_hostile'], $result['can_be_attacked'], $result['xp_value'], $result['description'], $result['stats'], $result['inventory'], $result['image_url'] ); } public static function get_all_enemy_definitions(): array { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "adventurebook_enemy_definitions", ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $enemy_definitions = array(); foreach ( $results as $result ) { $enemy_definitions[] = new Enemy_Definition( $result['id'], $result['code'], $result['name'], $result['is_hostile'], $result['can_be_attacked'], $result['xp_value'], $result['description'], $result['stats'], $result['inventory'], $result['image_url'] ); } // sort by name usort( $enemy_definitions, function ( $a, $b ) { return strcmp( $a->name, $b->name ); } ); return $enemy_definitions; } public static function create_enemy_definition( $code, $name, $is_hostile, $can_be_attacked, $xp_value, $description, $stats, $inventory, $image_url ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemy_definitions'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'name' => $name, 'is_hostile' => $is_hostile, 'can_be_attacked' => $can_be_attacked, 'xp_value' => $xp_value, 'description' => $description, 'stats' => json_encode( $stats ), 'inventory' => json_encode( $inventory ), 'image_url' => $image_url, ) ); } public static function delete_enemy_definition( $id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemy_definitions'; $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public function get_html(): string { $html = '
No items
'; } $html = ''; foreach ( $this->inventory as $inventory_item ) { // each inventory item is in the form x:m:n:o // we need to extract the item code x $parts = explode( ':', $inventory_item ); $item_code = $parts[0]; // get the item definition by code if($item_code == 'money') { $html .= '