/
class-linkoscope-post-meta.php
111 lines (94 loc) · 3.13 KB
/
class-linkoscope-post-meta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Class LinkoScope_Post_Meta
* Handles additional fields in the REST API for LinkoScope type posts.
*
* Created by Nabeel Sulieman.
* User: Nabeel
* Date: 2015-10-23
* Time: 12:11 PM
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'LinkoScope_Post_Meta' ) ) :
class LinkoScope_Post_Meta {
public function run() {
add_action( 'rest_api_init', [ $this, 'register_fields' ] );
}
public function register_fields() {
$meta_callbacks = array(
'get_callback' => [ $this, 'get_meta' ],
'update_callback' => [ $this, 'set_meta' ],
'schema' => null,
);
register_api_field( 'linkoscope_link', 'linkoscope_score', $meta_callbacks );
register_api_field( 'linkoscope_link', 'linkoscope_likes', $meta_callbacks );
$author_name_callbacks = array(
'get_callback' => [ $this, 'get_author_name' ],
'update_callback' => [ $this, 'no_op' ],
'schema' => null,
);
register_api_field( 'linkoscope_link', 'author_name', $author_name_callbacks );
$comment_count_callbacks = array(
'get_callback' => [ $this, 'get_comment_count' ],
'update_callback' => [ $this, 'no_op' ],
'schema' => null,
);
register_api_field( 'linkoscope_link', 'comment_count', $comment_count_callbacks );
$tags_callbacks = array(
'get_callback' => [ $this, 'get_tags' ],
'update_callback' => [ $this, 'set_tags' ],
'schema' => null,
);
register_api_field( 'linkoscope_link', 'tags', $tags_callbacks );
$raw = array(
'schema' => array(
'type' => 'string',
'raw' => array(
'type' => 'string',
'description' => 'Title for the object, as it exists in the database.',
'context' => array( 'view', 'edit' ) ),
)
);
register_api_field( 'linkoscope_link', 'title', $raw );
register_api_field( 'linkoscope_link', 'content', $raw );
}
public function set_meta( $value, $object, $field_name ) {
return update_post_meta( $object->ID, $field_name, strip_tags( $value ) );
}
public function get_meta( $object, $field_name, $request ) {
$meta = get_post_meta( $object['id'], $field_name );
$result = null;
if ( is_array( $meta ) && count( $meta ) > 0 ) {
$result = $meta[0];
}
return $result;
}
public function get_author_name( $object, $field_name, $request ) {
$user = get_userdata( $object['author'] );
return $user->display_name;
}
public function get_comment_count( $object, $field_name, $request ) {
$comments = get_comment_count( $object['id'] );
return $comments['approved'];
}
public function no_op( $value, $object, $field_name ) {
}
public function get_tags( $object, $field_name, $request ) {
$ret = [ ];
foreach ( wp_get_post_tags( $object['id'] ) as $tag ) {
$ret[$tag->term_id] = $tag->name;
}
return $ret;
}
public function set_tags( $value, $object, $field_name ) {
$tags = [ ];
foreach ( $value as $k => $v ) {
error_log( "Setting tag: $k" );
$tags[] = $k;
}
return wp_set_post_tags( $object->ID, $tags );
}
}
endif;