├── composer.json └── src ├── ConstantStrategy.php ├── DatabaseStrategy.php ├── Factory.php ├── FilteredOptionStore.php ├── OptionStore.php ├── OptionStoreInterface.php └── StrategyInterface.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typisttech/wp-option-store", 3 | "description": "Extending WordPress Options API, read options from places other than database, the OOP way", 4 | "keywords": [ 5 | "wordpress", 6 | "wp" 7 | ], 8 | "homepage": "https://typist.tech/projects/wp-option-store", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Typist Tech", 13 | "email": "wp-option-store@typist.tech", 14 | "homepage": "https://typist.tech/" 15 | }, 16 | { 17 | "name": "Tang Rufus", 18 | "email": "tangrufus@gmail.com", 19 | "homepage": "https://typist.tech/", 20 | "role": "Developer" 21 | } 22 | ], 23 | "support": { 24 | "email": "wp-option-store@typist.tech", 25 | "issues": "https://github.com/TypistTech/wp-option-store/issues", 26 | "source": "https://github.com/TypistTech/wp-option-store" 27 | }, 28 | "require": { 29 | "php": "^7.2" 30 | }, 31 | "require-dev": { 32 | "itinerisltd/itineris-wp-coding-standards": "^0.3.5", 33 | "roave/security-advisories": "dev-master" 34 | }, 35 | "suggest": { 36 | "typisttech/imposter-plugin": "Wrap all composer vendor packages inside your own namespace, to prevent collisions when multiple plugins use this library", 37 | "typisttech/wp-better-settings": "A simplified OOP implementation of the WP Settings API", 38 | "typisttech/wp-contained-hook": "Lazily instantiate objects from dependency injection container to WordPress hooks (actions and filters)" 39 | }, 40 | "autoload": { 41 | "psr-4": { 42 | "TypistTech\\WPOptionStore\\": "src/" 43 | } 44 | }, 45 | "scripts": { 46 | "style:check": "phpcs", 47 | "style:fix": "phpcbf", 48 | "pretag": [ 49 | "composer update", 50 | "@test", 51 | "@style:check", 52 | "composer normalize", 53 | "npx doctoc README.md", 54 | "github_changelog_generator --no-verbose" 55 | ], 56 | "test": "codecept run", 57 | "test:coverage": "codecept run --coverage --coverage-xml --coverage-html", 58 | "test:wpunit": "codecept run wpunit" 59 | }, 60 | "config": { 61 | "sort-packages": true 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/ConstantStrategy.php: -------------------------------------------------------------------------------- 1 | constantNameFor($optionName); 24 | 25 | if (! defined($constantName)) { 26 | return null; 27 | } 28 | 29 | return constant($constantName); 30 | } 31 | 32 | /** 33 | * Normalize option name and key to SCREAMING_SNAKE_CASE constant name. 34 | * 35 | * @param string $optionName Name of option to retrieve. 36 | * 37 | * @return string 38 | */ 39 | private function constantNameFor(string $optionName): string 40 | { 41 | return strtoupper($optionName); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/DatabaseStrategy.php: -------------------------------------------------------------------------------- 1 | filterTagFor($optionName), 20 | parent::get($optionName) 21 | ); 22 | } 23 | 24 | /** 25 | * Normalize option name and key to snake_case filter tag. 26 | * 27 | * @param string $optionName Name of option to retrieve. 28 | * Expected to not be SQL-escaped. 29 | * 30 | * @return string 31 | */ 32 | private function filterTagFor(string $optionName): string 33 | { 34 | return strtolower($optionName); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/OptionStore.php: -------------------------------------------------------------------------------- 1 | strategies = $strategies; 24 | } 25 | 26 | /** 27 | * Get an option value. 28 | * 29 | * @param string $optionName Name of option to retrieve. 30 | * 31 | * @return mixed|null 32 | */ 33 | public function get(string $optionName) 34 | { 35 | foreach ($this->strategies as $strategy) { 36 | $value = $strategy->get($optionName); 37 | 38 | if (null !== $value) { 39 | return $value; 40 | } 41 | } 42 | 43 | return null; 44 | } 45 | 46 | /** 47 | * Cast an option value into boolean. 48 | * 49 | * @param string $optionName Name of option to retrieve. 50 | * 51 | * @return bool 52 | */ 53 | public function getBoolean(string $optionName): bool 54 | { 55 | return in_array( 56 | $this->get($optionName), 57 | self::TRUE_VALUES, 58 | true 59 | ); 60 | } 61 | 62 | /** 63 | * Cast an option value into integer. 64 | * 65 | * @param string $optionName Name of option to retrieve. 66 | * 67 | * @return int 68 | */ 69 | public function getInt(string $optionName): int 70 | { 71 | return (int) $this->get($optionName); 72 | } 73 | 74 | /** 75 | * Cast an option value into string. 76 | * 77 | * @param string $optionName Name of option to retrieve. 78 | * 79 | * @return string 80 | */ 81 | public function getString(string $optionName): string 82 | { 83 | return (string) $this->get($optionName); 84 | } 85 | 86 | /** 87 | * Cast an option value into array. 88 | * 89 | * @param string $optionName Name of option to retrieve. 90 | * 91 | * @return array 92 | */ 93 | public function getArray(string $optionName): array 94 | { 95 | return (array) $this->get($optionName); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/OptionStoreInterface.php: -------------------------------------------------------------------------------- 1 |