This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Implement new-style meta with register_meta #15
Open
rmccue
wants to merge
36
commits into
master
Choose a base branch
from
new-meta-new-life
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1832b0b
Breathe life into the new meta implementation
rmccue 8719aa9
Merge the default schema into the supplied one
rmccue bbf8bad
Add prepare_callback for meta values
rmccue 4313e34
Update tests
rmccue af85a61
Don't allow non-serializable objects to be output
rmccue b6adc93
Remove old tests
rmccue 8aeb573
Add new testing
rmccue bc185ff
Remove duplicate blank line
rmccue e1f3a77
Measure code coverage
rmccue 8e1f97e
Add codecoverage unit test settings
rmccue 51d408c
Add test for multiple values
rmccue 9d79ba3
Remove "latest" as a test version
rmccue 756e223
Change array format to long-style
rmccue 54dd858
Factor out granting of write permission
rmccue 4963518
Add deletion test
rmccue d378ed3
Test database errors
rmccue 963dd5b
Test updating multi value without authentication
rmccue 6646ff1
Test auth callbacks are called correctly
rmccue 6e3da95
Test registered meta without API access
rmccue 58a30ad
Fix coding standards error
rmccue bc81e19
Test auth callbacks are called when deleting
rmccue ad637d3
Test database error on deletion
rmccue 7913a36
Register meta before API init
rmccue 702af8b
Test for DB errors in updating
rmccue e9a0a7f
Set type to array for multi-meta schema
rmccue 8f369fe
Add tests for schema
rmccue c787eb5
Add testing for type coersion
rmccue 5f83bf2
Remove references to options/settings
rmccue 4d523cf
Correct doc block
rmccue 8eef1b8
Post meta -> Meta
rmccue f17752d
Check item type for multi-value
rmccue d2c4126
Yoda-ify a conditional
rmccue e6404a8
Use better diffing for multi-value
rmccue 2bfb563
Use correct variable naming
rmccue dc98102
Add failing test case for delete_metadata
rmccue 965944d
Only remove metadata once for multi-value
rmccue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,62 @@ | ||
# WP REST API Meta Endpoints # | ||
**Contributors:** rmccue, rachelbaker, danielbachhuber, joehoyle | ||
**Tags:** json, rest, api, rest-api | ||
**Requires at least:** 4.4 | ||
**Tested up to:** 4.5-alpha | ||
**Stable tag:** 0.1.0 | ||
**License:** GPLv2 or later | ||
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html | ||
# WP REST API - Meta Support | ||
|
||
WP REST API companion plugin for post meta endpoints. | ||
[![Build Status](https://travis-ci.org/WP-API/wp-api-meta-endpoints.svg?branch=master)](https://travis-ci.org/WP-API/wp-api-meta-endpoints) | ||
|
||
## Description ## | ||
Just like other parts of WordPress, the REST API fully supports custom meta on posts, comments, terms and users. Similar to custom post types and taxonomies, only meta that opts-in to API support is available through the REST API. | ||
|
||
[![Build Status](https://travis-ci.org/WP-API/wp-api-meta-endpoints.svg?branch=master)](https://travis-ci.org/WP-API/wp-api-meta-endpoints) | ||
This plugin is a feature plugin for the main API plugin, and is expected to be integrated in the near future. | ||
|
||
|
||
## Meta registration | ||
|
||
For your meta fields to be exposed in the REST API, you need to register them. WordPress includes a `register_meta` function which is not usually required to get/set fields, but is required for API support. | ||
|
||
To register your field, simply call `register_meta` and set the `show_in_rest` flag to true. | ||
|
||
```php | ||
register_meta( 'post', 'field_name', array( | ||
'show_in_rest' => true, | ||
)); | ||
``` | ||
|
||
Note: `register_meta` must be called separately for each meta key. | ||
|
||
|
||
### Meta Options | ||
|
||
`register_meta` is part of WordPress core, but isn't heavily used in most parts of core, so you may not have seen it before. Here are the options relevant to the REST API: | ||
|
||
* `show_in_rest` - Should the field be exposed in the API? `true` for default behaviour, or an array of options to override defaults (see below). | ||
* `description` - Human-readable description of what the field is for. The API exposes this in the schema. | ||
* `single` - Is this field singular? WordPress allows multiple values per key on a single object, but the API needs to know whether you use this functionality. | ||
* `sanitize_callback` - Callback to sanitize the value before it's stored in the database. [See the `"sanitize_{$object_type}_meta_{$meta_key}"` filter.](https://developer.wordpress.org/reference/hooks/sanitize_object_type_meta_meta_key/) | ||
* `auth_callback` - Callback to determine whether the user can **write** to the field. [See the `"auth_post_{$post_type}_meta_{$meta_key}"` filter.](https://developer.wordpress.org/reference/hooks/auth_post_post_type_meta_meta_key/) | ||
|
||
|
||
### API=Specific Options | ||
|
||
The `show_in_rest` parameter defaults to `false`, but can be set to `true` to enable API support. Out of the box, the API makes some assumptions about your meta field, but you can override these by setting `show_in_rest` to an options array instead: | ||
|
||
```php | ||
register_meta( 'post', 'field_name', array( | ||
'show_in_rest' => array( | ||
'name' => 'fieldname' | ||
) | ||
)); | ||
``` | ||
|
||
The options you can set are: | ||
|
||
* `name` - The key exposed via the REST API. For example, if your meta key starts with an underscore, you may want to remove this for the API. | ||
* `schema` - The JSON Schema options for the field. Exposed via OPTIONS requests to the post. This is generated automatically based on other parameters, but can be specified manually if you'd like to override it. | ||
|
||
|
||
## Using Meta over the API | ||
|
||
Meta is added to existing resources as a new `meta` field. For example, to get the meta for a single post, fetch the post at `/wp/v2/posts/{id}`, then look in the `meta` field. This is a key-value map for the registered fields. | ||
|
||
WP REST API companion plugin for post meta endpoints. | ||
To update a field, simply send back a new value for it. | ||
|
||
## Changelog ## | ||
Setting a field to `null` will cause the value to be removed from the database. If a field has a default value set, this essentially "resets" the value to the default (although no default will be stored in the database). | ||
|
||
### 0.1.0 (February 9, 2016) ### | ||
* Initial release. | ||
For meta fields which can hold multiple values, this field will be a JSON list. Adding or removing items from the list will create or remove the corresponding values in the database. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<phpunit | ||
bootstrap="tests/bootstrap.php" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<php> | ||
<const name="WPAPI_CODE_COVERAGE" value="1" /> | ||
</php> | ||
<testsuites> | ||
<!-- Default test suite to run all tests --> | ||
<testsuite> | ||
<directory prefix="test-" suffix=".php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<blacklist> | ||
<directory suffix=".php">.</directory> | ||
</blacklist> | ||
<whitelist> | ||
<directory suffix=".php">./lib</directory> | ||
<file>./plugin.php</file> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
class WP_REST_Comment_Meta_Fields extends WP_REST_Meta_Fields { | ||
/** | ||
* Get the object type for meta. | ||
* | ||
* @return string | ||
*/ | ||
protected function get_meta_type() { | ||
return 'comment'; | ||
} | ||
|
||
/** | ||
* Get the type for `register_rest_field`. | ||
* | ||
* @return string | ||
*/ | ||
public function get_rest_field_type() { | ||
return 'comment'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll still need the tests to work with
latest
when this is merged to core forwp-api
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should drop support for 4.6, IMO