xwoo/data-type
最新稳定版本:v1.4.1
Composer 安装命令:
composer require xwoo/data-type
包简介
A library for working and standardizing working with WooCommerce data types.
README 文档
README
WC Data Type
Model-driven custom data objects for WooCommerce
This library provides a standardized way to define and work with custom WooCommerce-style data objects. Define a model once, then reuse consistent property handling, factories, queries, and data-store behavior around it.
Key Features
- Model-driven setup: Define object shape with the
#[Model]attribute. - WooCommerce-style objects: Extend
XWC_Dataand keep a familiar CRUD workflow. - Typed properties: Support for core props, meta props, and taxonomy props.
- Shared object helpers: Load single objects or collections through package utilities.
- Extensible architecture: Swap in custom data stores, factories, and meta stores when needed.
- WordPress-native integration: Designed for plugin code already built around WordPress and WooCommerce lifecycles.
Installation
You can install this package via Composer:
composer require x-wp/wc-data-type
Tip
We recommend using automattic/jetpack-autoloader with this package to reduce autoloading conflicts in WordPress environments.
Usage
Below is a simple example that defines a custom data object and loads it through the package helpers.
Defining a model
<?php use XWC\Data\Decorators\Model; #[Model( name: 'book', table: '{{PREFIX}}books', core_props: array( 'title' => array( 'default' => '', 'type' => 'string', ), 'slug' => array( 'default' => '', 'type' => 'slug', ), 'price' => array( 'default' => 0, 'type' => 'float', ), ), )] final class Book extends XWC_Data { protected $object_type = 'book'; }
Loading objects
<?php $book = xwc_get_object(15, 'book', null); $books = xwc_get_objects( 'book', array( 'limit' => 10, 'orderby' => 'title', 'order' => 'ASC', ), );
Generated getters and setters follow the declared props, so classes like Book can expose methods such as get_title(), set_title(), get_slug(), and set_price().
Testing
The package ships with a PHPUnit suite that boots a WordPress test environment and exercises model definitions, object factories, queries, props, and runtime object behavior.
Run the suite with:
composer test
To prepare the local WordPress test environment first:
composer test:install
composer test
To clean the local test environment:
composer test:clean
Documentation
For package-specific usage, start with the public entrypoints used throughout the library:
XWC\Data\Decorators\ModelXWC_Dataxwc_get_object()xwc_get_objects()
Additional project information is available in the repository.
For maintainers preparing prereleases or stable tags, see docs/release-process.md.
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2024-03-06