typisttech/wp-option-store
Composer 安装命令:
composer require typisttech/wp-option-store
包简介
Extending WordPress Options API, read options from places other than database, the OOP way
关键字:
README 文档
README
Extending WordPress Options API, read options from places other than database, the OOP way.
- The Goals, or What This Package Does?
- Install
- Usage
- Frequently Asked Questions
- Can I implement my own strategy classes?
- Can I change the order of the strategies?
- Is this a plugin?
- What to do when wp.org plugin team tell me to clean up the
vendorfolder? - Can two different plugins use this package at the same time?
- Do you have real life examples that use this package?
- Will you add support for older PHP versions?
- It looks awesome. Where can I find some more goodies like this?
- Where can I give ⭐⭐⭐⭐⭐ reviews?
- Sponsoring ❤️
- Running the Tests
- Feedback
- Change log
- Security
- Credits
- License
The Goals, or What This Package Does?
WordPress Option API only allows you get_option from database. This package is for those who not accepting the status quo.
Install
Installation should be done via composer, details of how to install composer can be found at https://getcomposer.org/.
$ composer require typisttech/wp-option-store
You should put all WP Option Store classes under your own namespace to avoid class name conflicts.
Usage
Example
use TypistTech\WPOptionStore\Factory; // By default, the `Factory` adds 2 strategies (order matters): // 1. ConstantStrategy // 2. DatabaseStrategy $filteredOptionStore = Factory::build(); // To get an option from strategies: // 1. Read `MY_OPTION` constant // 2. If (1) is not `null`, jump to (6) // 3. Read from `get_option('my_option')`, the normal WordPress Options API // 4. If (3) is not `null`, jump to (6) // 5. We have tried all strategies, pass `null` to (6) // 6. Pass whatever we have read (could be null) through `apply_filters('my_option', $whateverWeHaveRead);` $filteredOptionStore->get('my_option'); // To get an option and perform type cast. $filteredOptionStore->getBoolean('my_boolean_option'); $filteredOptionStore->getInt('my_integer_option'); $filteredOptionStore->getString('my_string_option'); $filteredOptionStore->getArray('my_array_option');
ConstantStrategy
This strategy gets options from PHP constants.
define('MY_OPTION', 'abc123'); $strategy = new ConstantStrategy(); $value1 = $strategy->get('my_option'); // $value1 === 'abc123'; $value2 = $strategy->get('my_non_exist_option'); // $value2 === null;
DatabaseStrategy
This strategy gets options from WordPress Options API.
update_option('my_option', 'abc123'); $strategy = new DatabaseStrategy(); $value1 = $strategy->get('my_option'); // $value1 === 'abc123'; $value2 = $strategy->get('my_non_exist_option'); // $value2 === null;
Important: An unset value should be null instead of WordPress' default false.
OptionStore
This class gets option values from strategies.
__construct(StrategyInterface ...$strategies)
OptionStore constructor.
- @param StrategyInterface[] ...$strategies Strategies that get option values.
$databaseStrategy = new DatabaseStrategy(); $constantStrategy = new ConstantStrategy(); $optionStore = new OptionStore($constantStrategy, $databaseStrategy);
Note: Strategies order matters!
get(string $optionName)
Get an option value.
- @param string $optionName Name of option to retrieve.
// It returns the first non-null value from strategies. define('MY_OPTION', 'abc'); update_option('my_option', 'xyz'); $value1 = $optionStore->get('my_option'); // $value1 === 'abc'; // It returns `null` when option not found. $value2 = $optionStore->get('my_non_exist_option'); // $value2 === null;
Type casting
OptionStore provides several helper methods for type casting.
$optionStore->getBoolean('my_boolean_option'); $optionStore->getInt('my_integer_option'); $optionStore->getString('my_string_option'); $optionStore->getArray('my_array_option');
FilteredOptionStore
This is a subclass of OptionStore.
get(string $optionName)
Get an option value.
- @param string $optionName Name of option to retrieve.
// It returns the first non-null value from strategies, // and applies filters. define('MY_OPTION', 'abc'); update_option('my_option', 'xyz'); add_filter('my_option', function($value) { return 'filtered ' . $value; }); $value = $filteredOptionStore->get('my_option'); // $value === 'filtered abc';
Note: Filters are applied before type casting.
Factory
Factory is a helper class to reduce boilerplate code for those who use default strategies. If you use a custom strategy or reorder the strategies, don't use this class.
build(): FilteredOptionStore
$filteredOptionStore = Factory::build();
Frequently Asked Questions
Can I implement my own strategy classes?
Of course! Just implements the StrategyInterface.
Take a look at classes ConstantStrategy and DatabaseStrategy as well as their tests for example implementations of StrategyInterface.
If you'd like to create a open-source package to do this to help others, open a new issue to let us know, we'd love to help you with it.
Can I change the order of the strategies?
Why not? Don't use Factory.
// Order matters! $optionStore = new FilteredOptionStore( new MyStrategy1(), new MyStrategy2(), new MyStrategy3(), );
Is this a plugin?
No, this is a package that should be part of your plugin.
What to do when wp.org plugin team tell me to clean up the vendor folder?
Re-install packages via the following command. This package exports only necessary files to dist.
$ composer install --no-dev --prefer-dist --optimize-autoloader
Can two different plugins use this package at the same time?
Yes, if put all WP Option Store classes under your own namespace to avoid class name conflicts.
Do you have real life examples that use this package?
Here you go:
Add your own plugin here
Will you add support for older PHP versions?
Never! This plugin will only work on actively supported PHP versions.
Don't use it on end of life or security fixes only PHP versions.
It looks awesome. Where can I find some more goodies like this?
- Articles on Typist Tech's blog
- More projects on Typist Tech's GitHub profile
- More plugins on TangRufus' wp.org profiles
- Stay tuned on Typist Tech's newsletter
- Follow @TangRufus on Twitter
- Hire Tang Rufus to build your next awesome site
Where can I give ⭐⭐⭐⭐⭐ reviews?
Thanks! Glad you like it. It's important to let my know somebody is using this project. Since this is not hosted on wordpress.org, please consider:
- tweet something good with mentioning @TangRufus
- ⭐ star this Github repo
- 👀 watch this Github repo
- write blog posts
- submit pull requests
- sponsor Tang Rufus to maintain his open source projects
- hire Tang Rufus to build your next awesome site
Sponsoring ❤️
Love WP Option Store? Help me maintain it, a sponsorship here can help with it.
GitHub Sponsors Matching Fund
Do you know GitHub is going to match your sponsorship?
Sponsor now via GitHub to double your greatness.
Why don't you hire me?
Ready to take freelance WordPress jobs. Contact me via the contact form here or, via email info@typist.tech
Want to help in other way? Want to be a sponsor?
Contact: Tang Rufus
Running the Tests
Run the tests:
$ composer test
$ composer style:check
Feedback
Please provide feedback! We want to make this project useful in as many projects as possible. Please submit an issue and point out what you do and don't like, or fork the project and make suggestions. No issue is too small.
Change log
Please see CHANGELOG for more information on what has changed recently.
Security
If you discover any security related issues, please email wp-option-store@typist.tech instead of using the issue tracker.
Credits
WP Option Store is a Typist Tech project and maintained by Tang Rufus, freelance developer for hire.
Full list of contributors can be found here.
License
The MIT License (MIT). Please see License File for more information.
typisttech/wp-option-store 适用场景与选型建议
typisttech/wp-option-store 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.43k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2017 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「wp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 typisttech/wp-option-store 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 typisttech/wp-option-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 typisttech/wp-option-store 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Inertia.js server-side adapter for PHP. Handles full visits, Inertia XHR visits, asset-version 409 handshakes and partial reloads. Framework-agnostic, with transparent WordPress integration.
Adds Cache-Tag HTTP response headers for singular WordPress content and purges Cloudflare by tag when content changes.
AI Chatbot & Helpdesk Agent for WooCommerce. Connects your store to Egentify for AI-powered chat, order tracking, email, and ticketing.
MIDDAG's reference architecture for a WordPress plugin — composes the OSS framework/wordpress/ui libraries end to end.
Assets helper package for the Hybrid Core framework.
统计信息
- 总下载量: 17.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 27
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2017-10-15