wpdb; if($lock)$raw=$wpdb->get_var($wpdb->prepare('SELECT option_value FROM %i WHERE option_name=%s FOR UPDATE',$wpdb->options,self::OPTION)); else $raw=$wpdb->get_var($wpdb->prepare('SELECT option_value FROM %i WHERE option_name=%s',$wpdb->options,self::OPTION)); if($wpdb->last_error)throw new \RuntimeException('Unable to read resource preferences.'); if($raw===null)return null; $value=maybe_unserialize($raw); if(!is_array($value)||($value['version']??null)!==1||!is_array($value['enabled_ids']??null))throw new \RuntimeException('Invalid resource preferences.'); $ids=$value['enabled_ids'];foreach($ids as $id)if(!is_int($id)||$id<1)throw new \RuntimeException('Invalid resource preference identifier.'); return array_values(array_unique($ids)); } public function write(array $ids):void { $ids=array_values(array_unique(array_map('intval',$ids)));sort($ids,SORT_NUMERIC); update_option(self::OPTION,['version'=>1,'enabled_ids'=>$ids],false); if($this->read()!==$ids)throw new \RuntimeException('Unable to save resource preferences.'); } public function initialize():void { if($this->read()!==null)return; $resources=new ResourcesRepository($this->wpdb); $resources->serializeCapacityWrites(function()use($resources){$resources->transaction(null,function(){ if($this->read(true)!==null)return; $wpdb=$this->wpdb; $ids=$wpdb->get_col($wpdb->prepare('SELECT id FROM %i WHERE is_active=1 AND is_visible=1 AND is_bookable=1 ORDER BY id',\ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'resources' ))); if($wpdb->last_error)throw new \RuntimeException('Unable to initialize resource preferences.'); $this->write(array_map('intval',$ids)); });}); } public function clearCache():void {wp_cache_delete(self::OPTION,'options');wp_cache_delete('alloptions','options');wp_cache_delete('notoptions','options');} }