humanmade/rest-api-search-by-id
Composer 安装命令:
composer require humanmade/rest-api-search-by-id
包简介
WordPress plugin to support the include & exclude parameters on the REST API /search endpoint.
README 文档
README
This plugin modifies the WordPress REST API /wp/v2/search endpoint to support the include and exclude query parameters used in other collections.
In the default REST API you must know the post type for a resource in order to be able to query it, because API routes are broken out by resource type. However, in WordPress we have many functions which accept only an ID and return a matching post regardless of type. With this plugin active it is possible to query for a specific post object by ID, even if you do not know that record's post type:
/wp/v2/search?type=post&include=12345
useResourceById Hook
In addition to the API query parameter changes described above, when this plugin is active there is also a script with the handle use-resource-by-id enqueued while you are using the Block Editor. This script declares a React hook which wraps the WordPress Block Editor's own getEntityRecord selector to let you request a post resource without knowing its post type.
Usage:
// The hook is exposed as a browser global. const { useResourceById } = window; const MyComponent = () => { // Assuming the post object with ID 2501 is a 'customPostType', these two // hook calls will result in the exact same data: const result1 = useSelect( ( select ) => { return select( 'core' ).getEntityRecord( 'postType', 'post', 2501 ); } ); const result2 = useResourceById( 2501 ); };
This hook uses the search endpoint with the include parameter to determine the subtype of the requested record. That subtype is cached in memory once known, and the hook will thereafter return the results of a direct getEntityRecord call so that the resource is properly routed through the Block Editor data store.
Installation
Add this to your site with Composer by running,
composer require humanmade/rest-api-search-by-id
The package specifies that this is a WordPress plugin, so depending on how you have your Composer installers configured, this should automatically end up in your plugins directory. You can of course also manually download a zipfile of a release and load it into your site by other means.
Caveats
At present include and exclude as implemented in this plugin assume you are querying only for post objects. Support may be added later to permit searching across object types.
Release Process
To release a new version,
- bump the version numbers in
package.jsonandplugin.php(both the plugin header and the version parameter in thewp_enqueue_scriptcall) - generate the minified file with
npm run build - commit the built code
- tag with the desired version number
For example,
# (Manually edit package.json and plugin.php to set version 1.2.3)
npm run build
git add js/use-resource-by-id.min.js
git commit -m 'Release v1.2.3'
git tag v1.2.3
git push origin main
git push origin --tags
License
GPL v2 or later
humanmade/rest-api-search-by-id 适用场景与选型建议
humanmade/rest-api-search-by-id 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 humanmade/rest-api-search-by-id 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 humanmade/rest-api-search-by-id 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2021-10-15