sources = array(); } public static function getInstance() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } protected $sources; /** * Register a Data source * @param \YellowTree\GeoipDetect\DataSources\AbstractDataSource $source */ public function register($source) { $id = $source->getId(); $this->sources[$id] = $source; } const DEFAULT_SOURCE = 'hostinfo'; public function getCurrentSourceId() { return get_option('geoip-detect-source', self::DEFAULT_SOURCE); } /** * Returns the currently chosen source. * @deprecated Use getSource() instead * @return \YellowTree\GeoipDetect\DataSources\AbstractDataSource */ public function getCurrentSource() { return $this->getSource($this->getCurrentSourceId()); } /** * Returns the source known by this id. * @param string $id Source id * @return \YellowTree\GeoipDetect\DataSources\AbstractDataSource */ public function getSource($id) { if (isset($this->sources[$id])) return $this->sources[$id]; if (WP_DEBUG) trigger_error('The source with id "' . $id . '" was requested, but no such source was found. Using default source instead.', E_USER_NOTICE); if (isset($this->sources[self::DEFAULT_SOURCE])) return $this->sources[self::DEFAULT_SOURCE]; return null; } /** * Check if a source named $id exists. * @param string $id * @return boolean */ public function sourceExists($id) { return isset($this->sources[$id]); } /** * Choose a new source as "current source". * @param string $id */ public function setCurrentSource($id) { $oldSource = $this->getCurrentSource(); $newSource = $this->getSource($id); if ($oldSource->getId() != $newSource->getId()) { $oldSource->deactivate(); update_option('geoip-detect-source', $newSource->getId()); $newSource->activate(); } update_option('geoip-detect-ui-has-chosen-source', true); } /** * Returns all registered sources. * * @return array(\YellowTree\GeoipDetect\DataSources\AbstractDataSource) */ public function getAllSources() { return $this->sources; } public function isSourceCachable($source) { // Don't cache for file access based sources (not worth the effort/time) $sources_not_cachable = apply_filters('geoip2_detect_sources_not_cachable', array('auto', 'manual', 'header')); return !in_array($source, $sources_not_cachable); } public function isCachingUsed() { return $this->isSourceCachable($this->getCurrentSourceId()); } public function uninstall() { foreach($this->sources as $source) { $source->deactivate(); $source->uninstall(); } // Delete all options from this plugin foreach ( wp_load_alloptions() as $option => $value ) { if ( strpos( $option, 'geoip-detect-' ) === 0 ) { delete_option( $option ); } } } }