Use PSR-4 autoloading of existing classes; add remaining test files

This commit is contained in:
Daniel Bachhuber 2017-04-04 09:02:09 -07:00
parent 5e897b3cab
commit 1d2de8ca31
28 changed files with 674 additions and 200 deletions

13
.distignore Normal file
View file

@ -0,0 +1,13 @@
.DS_Store
.git
.gitignore
.gitlab-ci.yml
.editorconfig
.travis.yml
behat.yml
circle.yml
bin/
features/
utils/
*.zip
*.tar.gz

25
.editorconfig Normal file
View file

@ -0,0 +1,25 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[{.jshintrc,*.json,*.yml,*.feature}]
indent_style = space
indent_size = 2

[{*.txt,wp-config-sample.php}]
end_of_line = crlf

[composer.json]
indent_style = space
indent_size = 4

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (C) 2011-2017 WP-CLI Development Group (https://github.com/wp-cli/entity-command/contributors)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

518
README.md Normal file
View file

@ -0,0 +1,518 @@
wp-cli/entity-command
=====================

Manage WordPress core entities.

[![Build Status](https://travis-ci.org/wp-cli/entity-command.svg?branch=master)](https://travis-ci.org/wp-cli/entity-command)

Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing)

## Using

This package implements the following commands:

### wp comment

Manage comments.

~~~
wp comment
~~~

**EXAMPLES**

# Create a new comment.
$ wp comment create --comment_post_ID=15 --comment_content="hello blog" --comment_author="wp-cli"
Success: Created comment 932.

# Update an existing comment.
$ wp comment update 123 --comment_author='That Guy'
Success: Updated comment 123.

# Delete an existing comment.
$ wp comment delete 1337 --force
Success: Deleted comment 1337.

# Delete all spam comments.
$ wp comment delete $(wp comment list --status=spam --format=ids)
Success: Deleted comment 264.
Success: Deleted comment 262.



### wp comment meta

Manage comment custom fields.

~~~
wp comment meta
~~~

**EXAMPLES**

# Set comment meta
$ wp comment meta set 123 description "Mary is a WordPress developer."
Success: Updated custom field 'description'.

# Get comment meta
$ wp comment meta get 123 description
Mary is a WordPress developer.

# Update comment meta
$ wp comment meta update 123 description "Mary is an awesome WordPress developer."
Success: Updated custom field 'description'.

# Delete comment meta
$ wp comment meta delete 123 description
Success: Deleted custom field.





### wp menu

List, create, assign, and delete menus.

~~~
wp menu
~~~

**EXAMPLES**

# Create a new menu
$ wp menu create "My Menu"
Success: Created menu 200.

# List existing menus
$ wp menu list
+---------+----------+----------+-----------+-------+
| term_id | name | slug | locations | count |
+---------+----------+----------+-----------+-------+
| 200 | My Menu | my-menu | | 0 |
| 177 | Top Menu | top-menu | primary | 7 |
+---------+----------+----------+-----------+-------+

# Create a new menu link item
$ wp menu item add-custom my-menu Apple http://apple.com --porcelain
1922

# Assign the 'my-menu' menu to the 'primary' location
$ wp menu location assign my-menu primary
Success: Assigned location to menu.



### wp menu item

List, add, and delete items associated with a menu.

~~~
wp menu item
~~~

**EXAMPLES**

# Add an existing post to an existing menu
$ wp menu item add-post sidebar-menu 33 --title="Custom Test Post"
Success: Menu item added.

# Create a new menu link item
$ wp menu item add-custom sidebar-menu Apple http://apple.com
Success: Menu item added.

# Delete menu item
$ wp menu item delete 45
Success: 1 menu item deleted.





### wp menu location

Manage a menu's assignment to locations.

~~~
wp menu location
~~~

**EXAMPLES**

# List available menu locations
$ wp menu location list
+----------+-------------------+
| location | description |
+----------+-------------------+
| primary | Primary Menu |
| social | Social Links Menu |
+----------+-------------------+

# Assign the 'primary-menu' menu to the 'primary' location
$ wp menu location assign primary-menu primary
Success: Assigned location to menu.

# Remove the 'primary-menu' menu from the 'primary' location
$ wp menu location remove primary-menu primary
Success: Removed location from menu.





### wp network meta

Manage network custom fields.

~~~
wp network meta
~~~

**EXAMPLES**

# Get a list of super-admins
$ wp network meta get 1 site_admins
array (
0 => 'supervisor',
)





### wp post

Manage posts.

~~~
wp post
~~~

**EXAMPLES**

# Create a new post.
$ wp post create --post_type=post --post_title='A sample post'
Success: Created post 123.

# Update an existing post.
$ wp post update 123 --post_status=draft
Success: Updated post 123.

# Delete an existing post.
$ wp post delete 123
Success: Trashed post 123.



### wp post meta

Manage post custom fields.

~~~
wp post meta
~~~

**EXAMPLES**

# Set post meta
$ wp post meta set 123 _wp_page_template about.php
Success: Updated custom field '_wp_page_template'.

# Get post meta
$ wp post meta get 123 _wp_page_template
about.php

# Update post meta
$ wp post meta update 123 _wp_page_template contact.php
Success: Updated custom field '_wp_page_template'.

# Delete post meta
$ wp post meta delete 123 _wp_page_template
Success: Deleted custom field.





### wp post term

Manage post terms.

~~~
wp post term
~~~

**EXAMPLES**

# Set post terms
$ wp post term set 123 test category
Success: Set terms.





### wp post-type

Manage post types.

~~~
wp post-type
~~~

**EXAMPLES**

# Get details about a post type
$ wp post-type get page --fields=name,label,hierarchical --format=json
{"name":"page","label":"Pages","hierarchical":true}

# List post types with 'post' capability type
$ wp post-type list --capability_type=post --fields=name,public
+---------------+--------+
| name | public |
+---------------+--------+
| post | 1 |
| attachment | 1 |
| revision | |
| nav_menu_item | |
+---------------+--------+



### wp site

Perform site-wide operations.

~~~
wp site
~~~

**EXAMPLES**

# Create site
$ wp site create --slug=example
Success: Site 3 created: www.example.com/example/

# Output a simple list of site URLs
$ wp site list --field=url
http://www.example.com/
http://www.example.com/subdir/

# Delete site
$ wp site delete 123
Are you sure you want to delete the 'http://www.example.com/example' site? [y/n] y
Success: The site at 'http://www.example.com/example' was deleted.



### wp taxonomy

Manage taxonomies.

~~~
wp taxonomy
~~~

**EXAMPLES**

# List all taxonomies with 'post' object type.
$ wp taxonomy list --object_type=post --fields=name,public
+-------------+--------+
| name | public |
+-------------+--------+
| category | 1 |
| post_tag | 1 |
| post_format | 1 |
+-------------+--------+

# Get capabilities of 'post_tag' taxonomy.
$ wp taxonomy get post_tag --field=cap
{"manage_terms":"manage_categories","edit_terms":"manage_categories","delete_terms":"manage_categories","assign_terms":"edit_posts"}



### wp term

Manage terms.

~~~
wp term
~~~

**EXAMPLES**

# Create a new term.
$ wp term create category Apple --description="A type of fruit"
Success: Created category 199.

# Get details about a term.
$ wp term get category 199 --format=json --fields=term_id,name,slug,count
{"term_id":199,"name":"Apple","slug":"apple","count":1}

# Update an existing term.
$ wp term update category 15 --name=Apple
Success: Term updated.

# Get the term's URL.
$ wp term list post_tag --include=123 --field=url
http://example.com/tag/tips-and-tricks

# Delete post category
$ wp term delete category 15
Success: Deleted category 15.

# Recount posts assigned to each categories and tags
$ wp term recount category post_tag
Success: Updated category term count
Success: Updated post_tag term count



### wp term meta

Manage term custom fields.

~~~
wp term meta
~~~

**EXAMPLES**

# Set term meta
$ wp term meta set 123 bio "Mary is a WordPress developer."
Success: Updated custom field 'bio'.

# Get term meta
$ wp term meta get 123 bio
Mary is a WordPress developer.

# Update term meta
$ wp term meta update 123 bio "Mary is an awesome WordPress developer."
Success: Updated custom field 'bio'.

# Delete term meta
$ wp term meta delete 123 bio
Success: Deleted custom field.





### wp user

Manage users.

~~~
wp user
~~~

**EXAMPLES**

# List user IDs
$ wp user list --field=ID
1

# Create a new user.
$ wp user create bob bob@example.com --role=author
Success: Created user 3.
Password: k9**&I4vNH(&

# Update an existing user.
$ wp user update 123 --display_name=Mary --user_pass=marypass
Success: Updated user 123.

# Delete user 123 and reassign posts to user 567
$ wp user delete 123 --reassign=567
Success: Removed user 123 from http://example.com



### wp user meta

Manage user custom fields.

~~~
wp user meta
~~~

**EXAMPLES**

# Add user meta
$ wp user meta add 123 bio "Mary is an WordPress developer."
Success: Added custom field.

# List user meta
$ wp user meta list 123 --keys=nickname,description,wp_capabilities
+---------+-----------------+--------------------------------+
| user_id | meta_key | meta_value |
+---------+-----------------+--------------------------------+
| 123 | nickname | supervisor |
| 123 | description | Mary is a WordPress developer. |
| 123 | wp_capabilities | {"administrator":true} |
+---------+-----------------+--------------------------------+

# Update user meta
$ wp user meta update 123 bio "Mary is an awesome WordPress developer."
Success: Updated custom field 'bio'.

# Delete user meta
$ wp user meta delete 123 bio
Success: Deleted custom field.





### wp user term

Manage user terms.

~~~
wp user term
~~~

**EXAMPLES**

# Set user terms
$ wp user term set 123 test category
Success: Set terms.

## Installing

Installing this package requires WP-CLI v0.23.0 or greater. Update to the latest stable release with `wp cli update`.

Once you've done so, you can install this package with `wp package install wp-cli/entity-command`.

## Contributing

We appreciate you taking the initiative to contribute to this project.

Contributing isnt limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

### Reporting a bug

Think youve found a bug? Wed love for you to help us get it fixed.

Before you create a new issue, you should [search existing issues](https://github.com/wp-cli/entity-command/issues?q=label%3Abug%20) to see if theres an existing resolution to it, or if its already been fixed in a newer version.

Once youve done a bit of searching and discovered there isnt an open or fixed issue for your bug, please [create a new issue](https://github.com/wp-cli/entity-command/issues/new) with the following:

1. What you were doing (e.g. "When I run `wp post list`").
2. What you saw (e.g. "I see a fatal about a class being undefined.").
3. What you expected to see (e.g. "I expected to see the list of posts.")

Include as much detail as you can, and clear steps to reproduce if possible.

### Creating a pull request

Want to contribute a new feature? Please first [open a new issue](https://github.com/wp-cli/entity-command/issues/new) to discuss whether the feature is a good fit for the project.

Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience:

1. Create a feature branch for each contribution.
2. Submit your pull request early for feedback.
3. Include functional tests with your changes. [Read the WP-CLI documentation](https://wp-cli.org/docs/pull-requests/#functional-tests) for an introduction.
4. Follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/).


*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*

55
composer.json Normal file
View file

@ -0,0 +1,55 @@
{
"name": "wp-cli/entity-command",
"description": "Manage WordPress core entities.",
"type": "wp-cli-package",
"homepage": "https://github.com/wp-cli/entity-command",
"support": {
"issues": "https://github.com/wp-cli/entity-command/issues"
},
"license": "MIT",
"authors": [
{
"name": "Daniel Bachhuber",
"email": "daniel@runcommand.io",
"homepage": "https://runcommand.io"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"": "src/",
"WP_CLI\\": "src/WP_CLI"
},
"files": [ "entity-command.php" ]
},
"require": {},
"require-dev": {
"behat/behat": "~2.5",
"wp-cli/wp-cli": "*"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"commands": [
"comment",
"comment meta",
"menu",
"menu item",
"menu location",
"network meta",
"post",
"post meta",
"post term",
"post-type",
"site",
"taxonomy",
"term",
"term meta",
"user",
"user meta",
"user term"
]
}
}

40
entity-command.php Normal file
View file

@ -0,0 +1,40 @@
<?php

if ( ! class_exists( 'WP_CLI' ) ) {
return;
}

$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
}

WP_CLI::add_command( 'comment', 'Comment_Command' );
WP_CLI::add_command( 'comment meta', 'Comment_Meta_Command' );
WP_CLI::add_command( 'menu', 'Menu_Command' );
WP_CLI::add_command( 'menu item', 'Menu_Item_Command' );
WP_CLI::add_command( 'menu location', 'Menu_Location_Command' );
WP_CLI::add_command( 'network meta', 'Network_Meta_Command', array(
'before_invoke' => function () {
if ( !is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
}
) );
WP_CLI::add_command( 'post', 'Post_Command' );
WP_CLI::add_command( 'post meta', 'Post_Meta_Command' );
WP_CLI::add_command( 'post term', 'Post_Term_Command' );
WP_CLI::add_command( 'post-type', 'Post_Type_Command' );
WP_CLI::add_command( 'site', 'Site_Command' );
WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );
WP_CLI::add_command( 'term', 'Term_Command' );
WP_CLI::add_command( 'term meta', 'Term_Meta_Command', array(
'before_invoke' => function() {
if ( \WP_CLI\Utils\wp_version_compare( '4.4', '<' ) ) {
WP_CLI::error( "Requires WordPress 4.4 or greater." );
}
})
);
WP_CLI::add_command( 'user', 'User_Command' );
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
WP_CLI::add_command( 'user term', 'User_Term_Command' );

View file

@ -1,153 +0,0 @@
Feature: Manage WordPress site options

Scenario: Site Option CRUD
Given a WP multisite install

# String values
When I run `wp site option add str_opt 'bar'`
Then STDOUT should not be empty

When I run `wp site option get str_opt`
Then STDOUT should be:
"""
bar
"""

When I run `wp site option list`
Then STDOUT should not be empty

When I run `wp site option list`
Then STDOUT should contain:
"""
str_opt bar
"""

When I run `wp site option list --search='str_o*'`
Then STDOUT should be a table containing rows:
| meta_key | meta_value |
| str_opt | bar |

When I run `wp site option list --search='str_o*' --format=total_bytes`
Then STDOUT should be:
"""
3
"""

When I run `wp site option list`
Then STDOUT should contain:
"""
admin_user_id 1
"""

When I run `wp site option delete str_opt`
Then STDOUT should not be empty

When I run `wp site option list`
Then STDOUT should not contain:
"""
str_opt bar
"""

When I try `wp site option get str_opt`
Then the return code should be 1

# Integer values
When I run `wp site option update admin_user_id 2`
Then STDOUT should not be empty

When I run `wp site option get admin_user_id`
Then STDOUT should be:
"""
2
"""

When I run `wp site option update admin_user_id 1`
Then STDOUT should contain:
"""
Success: Updated 'admin_user_id' site option.
"""

When I run the previous command again
Then STDOUT should contain:
"""
Success: Value passed for 'admin_user_id' site option is unchanged.
"""

When I run `wp site option get admin_user_id`
Then STDOUT should be:
"""
1
"""

# JSON values
When I run `wp site option set json_opt '[ 1, 2 ]' --format=json`
Then STDOUT should not be empty

When I run the previous command again
Then STDOUT should not be empty

When I run `wp site option get json_opt --format=json`
Then STDOUT should be:
"""
[1,2]
"""

# Reading from files
Given a value.json file:
"""
{
"foo": "bar",
"list": [1, 2, 3]
}
"""
When I run `wp site option set foo --format=json < value.json`
And I run `wp site option get foo --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": "bar",
"list": [1, 2, 3]
}
"""

Scenario: Error on single install
Given a WP install

When I try `wp site option get str_opt`
Then STDERR should be:
"""
Error: This is not a multisite install.
"""

When I try `wp site option add str_opt 'bar'`
Then STDERR should be:
"""
Error: This is not a multisite install.
"""

Scenario: Filter options by `--site_id`
Given a WP multisite install

When I run `wp db query "INSERT INTO wp_sitemeta (site_id,meta_key,meta_value) VALUES (2,'wp_cli_test_option','foobar');"`
Then the return code should be 0

When I run `wp site option list`
Then STDOUT should contain:
"""
wp_cli_test_option
"""
And STDERR should be empty

When I run `wp site option list --site_id=1`
Then STDOUT should not contain:
"""
wp_cli_test_option
"""
And STDERR should be empty

When I run `wp site option list --site_id=2`
Then STDOUT should contain:
"""
wp_cli_test_option
"""
And STDERR should be empty

View file

@ -659,5 +659,3 @@ class Comment_Command extends \WP_CLI\CommandWithDBObject {
}
}
}

WP_CLI::add_command( 'comment', 'Comment_Command' );

View file

@ -36,4 +36,3 @@ class Comment_Meta_Command extends \WP_CLI\CommandWithMeta {
}
}

WP_CLI::add_command( 'comment meta', 'Comment_Meta_Command' );

View file

@ -198,5 +198,3 @@ class Menu_Command extends WP_CLI_Command {
}

}

WP_CLI::add_command( 'menu', 'Menu_Command' );

View file

@ -499,5 +499,3 @@ class Menu_Item_Command extends WP_CLI_Command {
}

}

WP_CLI::add_command( 'menu item', 'Menu_Item_Command' );

View file

@ -166,5 +166,3 @@ class Menu_Location_Command extends WP_CLI_Command {
}

}

WP_CLI::add_command( 'menu location', 'Menu_Location_Command' );

View file

@ -14,12 +14,3 @@
class Network_Meta_Command extends \WP_CLI\CommandWithMeta {
protected $meta_type = 'site';
}

WP_CLI::add_command( 'network meta', 'Network_Meta_Command', array(
'before_invoke' => function () {
if ( !is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
}
) );

View file

@ -591,5 +591,3 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
return file_get_contents( $readfile );
}
}

WP_CLI::add_command( 'post', 'Post_Command' );

View file

@ -35,5 +35,3 @@ class Post_Meta_Command extends \WP_CLI\CommandWithMeta {
return $post->ID;
}
}

WP_CLI::add_command( 'post meta', 'Post_Meta_Command' );

View file

@ -22,5 +22,3 @@ class Post_Term_Command extends \WP_CLI\CommandWithTerms {
return $post->post_type;
}
}

WP_CLI::add_command( 'post term', 'Post_Term_Command' );

View file

@ -169,5 +169,3 @@ class Post_Type_Command extends WP_CLI_Command {
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'post-type' );
}
}

WP_CLI::add_command( 'post-type', 'Post_Type_Command' );

View file

@ -686,5 +686,3 @@ class Site_Command extends \WP_CLI\CommandWithDBObject {
}
}
}

WP_CLI::add_command( 'site', 'Site_Command' );

View file

@ -197,5 +197,3 @@ class Taxonomy_Command extends WP_CLI_Command {
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'taxonomy' );
}
}

WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );

View file

@ -576,5 +576,3 @@ class Term_Command extends WP_CLI_Command {
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'term' );
}
}

WP_CLI::add_command( 'term', 'Term_Command' );

View file

@ -38,12 +38,3 @@ class Term_Meta_Command extends \WP_CLI\CommandWithMeta {
}

}

WP_CLI::add_command( 'term meta', 'Term_Meta_Command', array(
'before_invoke' => function() {
if ( \WP_CLI\Utils\wp_version_compare( '4.4', '<' ) ) {
WP_CLI::error( "Requires WordPress 4.4 or greater." );
}
})
);

View file

@ -951,5 +951,3 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
}

}

WP_CLI::add_command( 'user', 'User_Command' );

View file

@ -209,5 +209,3 @@ class User_Meta_Command extends \WP_CLI\CommandWithMeta {
}

}

WP_CLI::add_command( 'user meta', 'User_Meta_Command' );

View file

@ -12,5 +12,3 @@
class User_Term_Command extends \WP_CLI\CommandWithTerms {
protected $obj_type = 'user';
}

WP_CLI::add_command( 'user term', 'User_Term_Command' );

2
wp-cli.yml Normal file
View file

@ -0,0 +1,2 @@
require:
- entity-command.php