Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 2bfe126

Browse files
committed
Add page_type additional field
1 parent ec9ac33 commit 2bfe126

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Add-on for the WordPress REST API, requires Papi 3.0.0.
88

99
## Addtional fields
1010

11+
### Fields
12+
1113
Post types that has page types will get addtional field attached to the object with the `fields` that contains all fields for the the current post page type. This can be removed.
1214

1315
Example response with additional `fields` field:
@@ -23,6 +25,17 @@ Example response with additional `fields` field:
2325
]
2426
```
2527

28+
### Page type field
29+
30+
```json
31+
[
32+
{
33+
"id": 1,
34+
"page_type": "example-page-type"
35+
}
36+
]
37+
```
38+
2639
## Endpoints
2740

2841
### Fields

src/class-papi-rest-api-additional-fields.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public function setup_fields() {
3333

3434
foreach ( $post_types as $post_type ) {
3535
register_api_field( $post_type, 'fields', [
36-
'get_callback' => [$this, 'get_fields']
36+
'get_callback' => [$this, 'get_fields']
37+
] );
38+
39+
register_api_field( $post_type, 'page_type', [
40+
'get_callback' => [$this, 'get_page_type']
3741
] );
3842
}
3943
}
@@ -62,6 +66,19 @@ public function get_fields( array $data, $field_name, WP_REST_Request $request )
6266

6367
return $fields;
6468
}
69+
70+
/**
71+
* Get page type.
72+
*
73+
* @param array $data
74+
* @param string $field_name
75+
* @param WP_REST_Request $request
76+
*
77+
* @return array
78+
*/
79+
public function get_page_type( array $data, $field_name, WP_REST_Request $request ) {
80+
return papi_get_page_type_id( $data['ID'] ) ?: '';
81+
}
6582
}
6683

6784
new Papi_REST_API_Additional_Fields;

tests/cases/class-papi-rest-api-additional-fields-test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ public function test_get_fields() {
3030
$fields = $this->class->get_fields( ['ID' => $post_id], 'fields', new WP_REST_Request );
3131
$this->assertEquals( ['name' => 'Fredrik', 'text' => 'Hello, world!'], $fields );
3232
}
33+
34+
public function test_get_page_type() {
35+
$post_id = $this->factory->post->create();
36+
37+
$page_type = $this->class->get_page_type( ['ID' => $post_id], 'page_type', new WP_REST_Request );
38+
$this->assertSame( $page_type, '' );
39+
40+
update_post_meta( $post_id, papi_get_page_type_key(), 'simple-content-page-type' );
41+
42+
$page_type = $this->class->get_page_type( ['ID' => $post_id], 'page_type', new WP_REST_Request );
43+
$this->assertSame( $page_type, get_post_meta( $post_id, papi_get_page_type_key(), true ) );
44+
}
3345
}

0 commit comments

Comments
 (0)