定制 elipettingale/wordpress-transformation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elipettingale/wordpress-transformation

最新稳定版本:v1.1.5

Composer 安装命令:

composer require elipettingale/wordpress-transformation

包简介

A package to easily transform data by creating transformer classes (wordpress version)

README 文档

README

A package to easily transform data using transformer classes. The idea behind this package is to be able to take data in one format and return it in a different or stripped down format. For example, you might use this when you are making an api endpoint and don't want to return everything or want to return something in multiple formats.

This is the wordpress version of this package. There is also a generic version here: https://github.com/elipettingale/transformation

How to Install

Just install the package using composer:

composer require elipettingle/wordpress-transformation

How to Use

The wordpress version of this package includes all the functionality of the generic version, but also works with custom post types and acf fields. Please refer to the readme for the generic package for detailed instructions on how to use this package: https://github.com/elipettingale/transformation.

The following are specific to the wordpress version of the package.

Post Transformer

The Post Transformer can be used in place of the regular transformer class, you can then use this to transform posts or custom post types. It includes compatibility with ACF. A Post Transformer will return a subset of the base wordpress attributes (all attributes are available using $includes) and any acf fields it has.

For example the following transformer:

class TeamMemberTransformer extends PostTransformer
{

}

Might return the following:

[
    'ID' => 108,
    'post_title' => 'Brandyn Sporer',
    'post_date' => '2020-02-05 09:27:57'
    'post_status' => 'publish'
    'post_content' => '<!-- wp:paragraph --> <p>Ut beatae et hic eum deserunt sit nihil. Asperiores consequatur quo et quia minus impedit modi voluptas. Consectetur ex voluptatem maiores. Ex ut sint sequi et minus. Molestias dolorem officia consectetur quia. Placeat voluptatem earum error aut dolores doloremque. Sed officiis quod eum saepe at a. Maxime omnis aut deserunt eveniet sit voluptas voluptas.</p> <!-- /wp:paragraph -->'
    'job_title' => 'Airframe Mechanic'
    'department' => 'Logistics'
    'address' => [
        'address' => '353 Little Bourke St, Melbourne VIC 3000, Australia'
        'lat' => -37.8134498
        'lng' => 144.9622691
        'zoom' => 14
        'place_id' => 'ChIJZdO0OLVC1moReE9bPKvo0UQ'
        'street_number' => '353'
        'street_name' => 'Little Bourke Street'
        'street_name_short' => 'Little Bourke St'
        'city' => 'Melbourne;
        'state' => 'Victoria'
        'state_short' => 'VIC'
        'post_code' => '3000'
        'country' => 'Australia'
        'country_short' => 'AU'
    ],
    'permalink' => 'http://wordpress.test/2020/02/05/brandyn-sporer/'
]

Jam Packed Example

Here is an example using everything at your disposal:

class AddressTransformer extends Transformer
{
    protected $includes = [
        'address',
        'coordinates'
    ];

    protected $rename = [
        'address' => 'full_address'
    ];

    public function getCoordinatesAttribute()
    {
        return [
            'lat' => $this->item['lat'],
            'lng' => $this->item['lng']
        ];
    }
}

class TeamMemberTransformer extends PostTransformer
{
    protected $includes = [
        'post_title',
        'first_name',
        'last_name',
        'initials',
        'address'
    ];

    protected $rename = [
        'post_title' => 'full_name'
    ];

    public function getFirstNameAttribute()
    {
        return explode(' ', $this->item->post_title)[0];
    }

    public function getLastNameAttribute()
    {
        return explode(' ', $this->item->post_title)[1];
    }

    public function getInitialsAttribute()
    {
        return $this->getFirstNameAttribute()[0] . $this->getLastNameAttribute()[0];
    }

    public function getAddressAttribute()
    {
        return Transform::one($this->item->address, AddressTransformer::class);
    }
}

$query = new WP_Query([
    'post_type' => 'team_member'
]);

$team_members = $query->get_posts();
$team_members = Transform::all($team_members, TeamMemberTransformer::class);

Using the same data as above this would return:

[
    'full_name' => 'Brandyn Sporer',
    'first_name' => 'Brandyn',
    'last_name' => 'Sporer',
    'initials' => 'BS',
    'address' => [
        'full_address' => '353 Little Bourke St, Melbourne VIC 3000, Australia',
        'coordinates' => [
            'lat' => -37.8134498,
            'lng' => 144.9622691
        ]
    ]
]

统计信息

  • 总下载量: 389
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固