hellonico / wp-cli-fixtures
轻松为WordPress生成假数据
Requires
- php: ^7.3 || ^8.0
- fakerphp/faker: ^1.13
- nelmio/alice: ^3.8
- wp-cli/wp-cli: ^2.4
Requires (Dev)
- wp-cli/db-command: ^2.0
- wp-cli/entity-command: ^2.0
- wp-cli/wp-cli-tests: ^3.1
This package is auto-updated.
Last update: 2024-09-14 01:09:38 UTC
README
本包受Faker启发,提供了一个简单的方式来为您的WordPress安装创建大量和自定义的假数据。本包基于nelmio/alice和fzaninotto/Faker。请参考这些包的文档以获取高级用法。
警告:本包主要适用于开发目的。使用时请自行承担风险,不要在生产数据库上运行,或者在使用前先进行备份。
安装
wp package install git@github.com:nlemoine/wp-cli-fixtures.git
需要PHP ^7.3
。
使用
创建固定数据
在项目根目录下创建一个 fixtures.yml
文件(您可以从这里下载)
# # USERS # Hellonico\Fixtures\Entity\User: user{1..10}: user_login (unique): <username()> # '(unique)' is required user_pass: 123456 user_email: <safeEmail()> user_url: <url()> user_registered: <dateTimeThisDecade()> first_name: <firstName()> last_name: <lastName()> description: <sentence()> role: <randomElement(['subscriber', 'editor'])> meta: phone_number: <phoneNumber()> address: <streetAddress()> zip: <postcode()> city: <city()> acf: facebook_url: <url()> twitter_url: <url()> # # ATTACHMENTS # Hellonico\Fixtures\Entity\Attachment: default (template): # templates can be extended to keep things DRY post_title: <words(2, true)> post_date: <dateTimeThisDecade()> post_content: <paragraphs(5, true)> images{1..15} (extends default): file: <image(<uploadDir()>, 1200, 1200, 'cats')> # <uploadDir()> is required, image() is the default faker provider and gets images from lorempixel. pics{1..15} (extends default): file: <picsum(<uploadDir()>, 1200, 1200)> # Alternatively we provide a picsum() provider which uses picsum for images. It's quicker but doesn't support image categories. documents{1..2} (extends default): file: <fileIn('relative/path/to/pdfs')> custom_images{1..10} (extends default): file: <fileIn('relative/path/to/images')> # # TERMS # Hellonico\Fixtures\Entity\Term: category{1..10}: name (unique): <words(2, true)> # '(unique)' is required description: <sentence()> parent: '50%? <termId(childless=1)>' # 50% of created categories will have a top level parent category taxonomy: 'category' # could be skipped, default to 'category' tag{1..40}: name (unique): <words(2, true)> # '(unique)' is required description: <sentence()> taxonomy: post_tag places{1..4}: # custom taxonomy name (unique): <words(2, true)> # '(unique)' is required description: <sentences(3, true)> taxonomy: place acf: address: <streetAddress> zip: <postcode()> city: <city()> image: '@custom_images*->ID' # # POSTS # Hellonico\Fixtures\Entity\Post: # TEMPLATE default (template): post_title: <words(2, true)> post_date: <dateTimeThisDecade()> post_content: <paragraphs(5, true)> post_excerpt: <paragraphs(1, true)> meta: _thumbnail_id: '@attachment*->ID' # POSTS post{1..30} (extends default): # 'meta' and 'meta_input' are basically the same, you can use one or both, # they will be merged, just don't provide the same keys in each definition meta: _thumbnail_id: '@attachment*->ID' meta_input: _extra_field: <paragraphs(1, true)> post_category: '3x @category*->term_id' # post_category only accepts IDs tax_input: post_tag: '5x @tag*->term_id' # post_tag: '5x <words(2, true)> # Or tags can be dynamically created # PAGES page{contact, privacy}: post_title: <current()> post_type: page # CUSTOM POST TYPE product{1..15}: post_type: product acf: # number field price: <numberBetween(10, 200)> # gallery field gallery: '3x @attachment*->ID' # oembed field video: https://www.youtube.com/watch?v=E90_aL870ao # link field link: url: https://www.youtube.com/watch?v=E90_aL870ao title: <words(2, true)> target: _blank # repeater field features: - label: <words(2, true)> value: <sentence()> - label: <words(2, true)> value: <sentence()> - label: <words(2, true)> value: <sentence()> # layout field blocks: - acf_fc_layout: text_image title: <words(4, true)> content: <sentences(8, true)> image: '@attachment*->ID' - acf_fc_layout: image_image image_left: '@attachment*->ID' image_right: '@attachment*->ID' # # COMMENTS # Hellonico\Fixtures\Entity\Comment: comment{1..50}: comment_post_ID: '@post*->ID' user_id: '@user*->ID' comment_date: <dateTimeThisDecade()> comment_author: <username()> comment_author_email: <safeEmail()> comment_author_url: <url()> comment_content: <paragraphs(2, true)> comment_agent: <userAgent()> comment_author_IP: <ipv4()> comment_approved: 1 comment_karma: <numberBetween(1, 100)> # 'meta' and 'comment_meta' are basically the same, you can use one or both, # they will be merged, just don't provide the same keys in each definition comment_meta: some_key: <sentence()> meta: another_key: <sentence()> # # NAV MENUS # Hellonico\Fixtures\Entity\NavMenu: header: name: header locations: - header - footer # # NAV MENUS ITEMS # Hellonico\Fixtures\Entity\NavMenuItem: custom_menu: menu_item_url: <url()> menu_item_title: <words(4, true)> menu_id: '@header->term_id' categories{1..3}: menu_item_object: '@category*' menu_id: '@header->term_id' posts{1..3}: menu_item_object: '@post*' menu_id: '@header->term_id' page: menu_item_object: '@page*' menu_id: '@header->term_id'
上面的示例将生成以下数据
- 10个用户
- 15个附件
- 10个分类
- 40个标签
- 30篇带缩略图、3个分类和5个标签的文章
- 10个页面
- 15个名为'product'的定制文章类型
- 与文章和用户相关的50条评论
- 1个导航菜单
- 6个导航菜单项
重要:确保引用的ID在使用之前被放置在 前面。
示例:如果您的固定数据中引用了Term
或Attachment
对象,那么这些对象必须在Post
之前放置。
加载固定数据
wp fixtures load
您也可以使用 --file
参数指定自定义文件
wp fixtures load --file=data.yml
删除固定数据
wp fixtures delete
您也可以删除单个固定数据类型
wp fixtures delete post
有效类型包括 post
、attachment
、comment
、term
、user
。
向现有内容添加假数据
wp-cli-fixtures
允许您通过将ID作为构造函数参数传递,将内容添加到现有实体。
向文章ID 1添加/更新数据
Hellonico\Fixtures\Entity\Post: my_post: __construct: [1] # Pass your post ID as the constructor argument post_title: '<sentence()>' post_content: '<paragraphs(5, true)>' post_excerpt: '<paragraphs(1, true)>'
向10个随机现有文章添加/更新数据
Hellonico\Fixtures\Entity\Post: post{1..10}: __construct: [<postId()>] # Use a custom formatters to return a random post ID as the constructor argument post_title: '<sentence()>' post_content: '<paragraphs(5, true)>' post_excerpt: '<paragraphs(1, true)>'
实体
文章
Hellonico\Fixtures\Entity\Post
可以接受在wp_insert_post
中可用的任何参数 + meta
和acf
键。
注意:post_date_gmt
和post_modified_gmt
已被禁用,它们由post_date
和post_modified
设置。
附件
Hellonico\Fixtures\Entity\Attachment
可以接受在wp_insert_attachment
中可用的任何参数 + meta
、file
和acf
自定义键。
注意:parent
必须使用post_parent
键传递。
术语
Hellonico\Fixtures\Entity\Term
可以接受在wp_insert_term
中可用的任何参数 + meta
和acf
自定义键。
注意:term
和taxonomy
必须分别使用name
和taxonomy
键传递。
用户
Hellonico\Fixtures\Entity\User
可以接受在wp_insert_user
中可用的任何参数 + meta
和acf
自定义键。
注释
Hellonico\Fixtures\Entity\Comment
可以接受 wp_insert_comment
函数中可用的任何参数以及自定义的 meta
键。
comment_date_gmt
已禁用,它从 comment_date
设置。
导航菜单
Hellonico\Fixtures\Entity\NavMenu
与 Hellonico\Fixtures\Entity\Term
一样是一个术语。它接受一个额外的 locations
参数来设置菜单位置。
Hellonico\Fixtures\Entity\NavMenu: header: name: header locations: - header
导航菜单项
Hellonico\Fixtures\Entity\NavMenuItem
使用与 wp_update_nav_menu_item
中的 $menu_item_data
相同的参数。
注意1:请将键中的短横线替换为下划线(例如,menu-item-object
变为 menu_item_object
)。
注意2:menu-item-object
也可以接受实体对象,如果是这样,则自动将 menu-item-type
和 menu-item-object-id
填充为适当的值。
ACF 支持
每个 ACF 支持的实体(文章、术语、用户)都可以有一个 acf
键,它就像 meta
一样工作。
Hellonico\Fixtures\Entity\Post: post{1..30}: post_title: <words(3, true)> post_date: <dateTimeThisDecade()> acf: # number field number: <numberBetween(10, 200)> # repeater field features: - label: <words(2, true)> value: <sentence() - label: <words(2, true)> value: <sentence()> - label: <words(2, true)> value: <sentence()>
注意重复字段键,如果您有多个具有相同键的字段,则建议使用 ACF 字段键(例如 field_948d1qj5mn4d3
)。
自定义格式化程序
除了 fzaninotto/Faker 提供的格式化程序之外,您还可以使用以下自定义格式化程序。
postId($args)
返回一个随机存在的文章 ID。 $args
是可选的,可以接受来自 get_posts
的任何参数。
示例
<postId(category=1,2,3)>
attachmentId($args)
返回一个随机存在的附件 ID。 $args
是可选的,可以接受来自 get_posts
的任何参数。
示例
<attachmentId(year=2016)>
termId($args)
返回一个随机存在的术语 ID。 $args
是可选的,可以接受来自 get_terms
的任何参数。
示例
<termId(taxonomy=post_tag)>
userId($args)
返回一个随机存在的用户 ID。 $args
是可选的,可以接受来自 get_users
的任何参数。
示例
<userId(role=subscriber)>
fileContent($file)
返回文件的内容。
示例
<fileContent('path/to/file.html')>
fileIn($src, $target, false)
这是对 file provider 的包装,因为一些 Faker 提供者 与 PHP 原生冲突。返回文件路径或目录中的文件名($src
相对于 fixtures.yml
)。
默认目标为 WordPress 的 uploads
目录。
示例
<fileIn('my/set/of/images')>
提示
在玩 fixtures 的同时,database command 包可以用于更快地重置数据库,比 wp fixtures delete
要快。
贡献
此包遵循 PSR2 编码标准,并使用 Behat 进行测试。执行 composer run test
确保您的 PR 通过。
在第一次运行之前,您需要运行
composer run prepare-tests
。