8000 Merge pull request #2765 from WP-API/add-meta-fields · WP-API/WP-API@7a23eaf · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 7a23eaf

Browse files
authored
Merge pull request #2765 from WP-API/add-meta-fields
Add meta fields
2 parents ea20284 + 4ce21fc commit 7a23eaf

18 files changed

+1214
-8
lines changed

lib/endpoints/class-wp-rest-comments-controller.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
88
public function __construct() {
99
$this->namespace = 'wp/v2';
1010
$this->rest_base = 'comments';
11+
12+
$this->meta = new WP_REST_Comment_Meta_Fields();
1113
}
1214

1315
/**
@@ -374,6 +376,14 @@ public function create_item( $request ) {
374376
$this->handle_status_param( $request['status'], $comment );
375377
}
376378

379+
$schema = $this->get_item_schema();
380+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
381+
$meta_update = $this->meta->update_value( $request['meta'], $comment_id );
382+
if ( is_wp_error( $meta_update ) ) {
383+
return $meta_update;
384+
}
385+
}
386+
377387
$comment = get_comment( $comment_id );
378388
$fields_update = $this->update_additional_fields_for_object( $comment, $request );
379389
if ( is_wp_error( $fields_update ) ) {
@@ -457,6 +467,14 @@ public function update_item( $request ) {
457467
}
458468
}
459469

470+
$schema = $this->get_item_schema();
471+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
472+
$meta_update = $this->meta->update_value( $request['meta'], $id );
473+
if ( is_wp_error( $meta_update ) ) {
474+
return $meta_update;
475+
}
476+
}
477+
460478
$comment = get_comment( $id );
461479
$fields_update = $this->update_additional_fields_for_object( $comment, $request );
462480
if ( is_wp_error( $fields_update ) ) {
@@ -585,6 +603,10 @@ public function prepare_item_for_response( $comment, $request ) {
585603
$data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email );
586604
}
587605

606+
if ( ! empty( $schema['properties']['meta'] ) ) {
607+
$data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
608+
}
609+
588610
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
589611
$data = $this->add_additional_fields_to_object( $data, $request );
590612
$data = $this->filter_response_by_context( $data, $context );
@@ -951,6 +973,8 @@ public function get_item_schema() {
951973
);
952974
}
953975

976+
$schema['properties']['meta'] = $this->meta->get_field_schema();
977+
954978
return $this->add_additional_fields_schema( $schema );
955979
}
956980

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public function __construct( $post_type ) {
99
$this->namespace = 'wp/v2';
1010
$obj = get_post_type_object( $post_type );
1111
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
12+
13+
$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
1214
}
1315

1416
/**
@@ -426,6 +428,13 @@ public function create_item( $request ) {
426428
}
427429

428430
$post = $this->get_post( $post_id );
431+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
432+
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
433+
if ( is_wp_error( $meta_update ) ) {
434+
return $meta_update;
435+
}
436+
}
437+
429438
$fields_update = $this->update_additional_fields_for_object( $post, $request );
430439
if ( is_wp_error( $fields_update ) ) {
431440
return $fields_update;
@@ -532,6 +541,14 @@ public function update_item( $request ) {
532541
}
533542

534543
$post = $this->get_post( $post_id );
544+
545+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
546+
$meta_update = $this->meta->update_value( $request['meta'], $post->ID );
547+
if ( is_wp_error( $meta_update ) ) {
548+
return $meta_update;
549+
}
550+
}
551+
535552
$fields_update = $this->update_additional_fields_for_object( $post, $request );
536553
if ( is_wp_error( $fields_update ) ) {
537554
return $fields_update;
@@ -1264,6 +1281,10 @@ public function prepare_item_for_response( $post, $request ) {
12641281
}
12651282
}
12661283

1284+
if ( ! empty( $schema['properties']['meta'] ) ) {
1285+
$data['meta'] = $this->meta->get_value( $post->ID, $request );
1286+
}
1287+
12671288
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
12681289
foreach ( $taxonomies as $taxonomy ) {
12691290
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
@@ -1522,6 +1543,7 @@ public function get_item_schema() {
15221543
'revisions',
15231544
'page-attributes',
15241545
'post-formats',
1546+
'custom-fields',
15251547
);
15261548
$fixed_schemas = array(
15271549
'post' => array(
@@ -1533,6 +1555,7 @@ public function get_item_schema() {
15331555
'comments',
15341556
'revisions',
15351557
'post-formats',
1558+
'custom-fields',
15361559
),
15371560
'page' => array(
15381561
'title',
@@ -1543,12 +1566,14 @@ public function get_item_schema() {
15431566
'comments',
15441567
'revisions',
15451568
'page-attributes',
1569+
'custom-fields',
15461570
),
15471571
'attachment' => array(
15481572
'title',
15491573
'author',
15501574
'comments',
15511575
'revisions',
1576+
'custom-fields',
15521577
),
15531578
);
15541579
foreach ( $post_type_attributes as $attribute ) {
@@ -1683,6 +1708,10 @@ public function get_item_schema() {
16831708
);
16841709
break;
16851710

1711+
case 'custom-fields':
1712+
$schema['properties']['meta'] = $this->meta->get_field_schema();
1713+
break;
1714+
16861715
}
16871716
}
16881717

lib/endpoints/class-wp-rest-terms-controller.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function __construct( $taxonomy ) {
1515
$this->namespace = 'wp/v2';
1616
$tax_obj = get_taxonomy( $taxonomy );
1717
$this->rest_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name;
18+
19+
$this->meta = new WP_REST_Term_Meta_Fields( $taxonomy );
1820
}
1921

2022
/**
@@ -371,6 +373,13 @@ public function create_item( $request ) {
371373
*/
372374
do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
373375

376+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
377+
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
378+
if ( is_wp_error( $meta_update ) ) {
379+
return $meta_update;
380+
}
381+
}
382+
374383
$fields_update = $this->update_additional_fields_for_object( $term, $request );
375384
if ( is_wp_error( $fields_update ) ) {
376385
return $fields_update;
@@ -445,6 +454,14 @@ public function update_item( $request ) {
445454
/* This action is documented in lib/endpoints/class-wp-rest-terms-controller.php */
446455
do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );
447456

457+
$schema = $this->get_item_schema();
458+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
459+
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
460+
if ( is_wp_error( $meta_update ) ) {
461+
return $meta_update;
462+
}
463+
}
464+
448465
$fields_update = $this->update_additional_fields_for_object( $term, $request );
449466
if ( is_wp_error( $fields_update ) ) {
450467
return $fields_update;
@@ -593,6 +610,9 @@ public function prepare_item_for_response( $item, $request ) {
593610
if ( ! empty( $schema['properties']['parent'] ) ) {
594611
$data['parent'] = (int) $item->parent;
595612
}
613+
if ( ! empty( $schema['properties']['meta'] ) ) {
614+
$data['meta'] = $this->meta->get_value( $item->term_id, $request );
615+
}
596616

597617
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
598618
$data = $this->add_additional_fields_to_object( $data, $request );
@@ -739,6 +759,8 @@ public function get_item_schema() {
739759
'context' => array( 'view', 'edit' ),
740760
);
741761
}
762+
763+
$schema['properties']['meta'] = $this->meta->get_field_schema();
742764
return $this->add_additional_fields_schema( $schema );
743765
}
744766

lib/endpoints/class-wp-rest-users-controller.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
88
public function __construct() {
99
$this->namespace = 'wp/v2';
1010
$this->rest_base = 'users';
11+
12+
$this->meta = new WP_REST_User_Meta_Fields();
1113
}
1214

1315
/**
@@ -329,6 +331,13 @@ public function create_item( $request ) {
329331
array_map( array( $user, 'add_role' ), $request['roles'] );
330332
}
331333

334+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
335+
$meta_update = $this->meta->update_value( $request['meta'], $user_id );
336+
if ( is_wp_error( $meta_update ) ) {
337+
return $meta_update;
338+
}
339+
}
340+
332341
$fields_update = $this->update_additional_fields_for_object( $user, $request );
333342
if ( is_wp_error( $fields_update ) ) {
334343
return $fields_update;
@@ -421,6 +430,14 @@ public function update_item( $request ) {
421430
array_map( array( $user, 'add_role' ), $request['roles'] );
422431
}
423432

433+
$schema = $this->get_item_schema();
434+
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
435+
$meta_update = $this->meta->update_value( $request['meta'], $id );
436+
if ( is_wp_error( $meta_update ) ) {
437+
return $meta_update;
438+
}
439+
}
440+
424441
$fields_update = $this->update_additional_fields_for_object( $user, $request );
425442
if ( is_wp_error( $fields_update ) ) {
426443
return $fields_update;
@@ -579,6 +596,10 @@ public function prepare_item_for_response( $user, $request ) {
579596
$data['avatar_urls'] = rest_get_avatar_urls( $user->user_email );
580597
}
581598

599+
if ( ! empty( $schema['properties']['meta'] ) ) {
600+
$data['meta'] = $this->meta->get_value( $user->ID, $request );
601+
}
602+
582603
$context = ! empty( $request['context'] ) ? $request['context'] : 'embed';
583604

584605
$data = $this->add_additional_fields_to_object( $data, $request );
@@ -866,9 +887,10 @@ public function get_item_schema() {
866887
'readonly' => true,
867888
'properties' => $avatar_properties,
868889
);
869-
870890
}
871891

892+
$schema['properties']['meta'] = $this->meta->get_field_schema();
893+
872894
return $this->add_additional_fields_schema( $schema );
873895
}
874896

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
class WP_REST_Comment_Meta_Fields extends WP_REST_Meta_Fields {
4+
/**
5+
* Get the object type for meta.
6+
*
7+
* @return string
8+
*/
9+
protected function get_meta_type() {
10+
return 'comment';
11+
}
12+
13+
/**
14+
* Get the type for `register_rest_field`.
15+
*
16+
* @return string
17+
*/
18+
public function get_rest_field_type() {
19+
return 'comment';
20+
}
21+
}

0 commit comments

Comments
 (0)
0