This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
/
posts.class.php
92 lines (83 loc) · 2.42 KB
/
posts.class.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
<?php
class ct_posts {
private $categories;
public function __construct() {
$this->categories = get_categories();
register_rest_field('post',
'e_categories',
array(
'get_callback' => array($this,'ct_get_categories'),
'update_callback' => array($this,'ct_update_categories'),
'schema' => null,
)
);
register_rest_field('post',
'e_tags',
array(
'get_callback' => array($this,'ct_get_tags'),
'update_callback' => array($this,'ct_update_tags'),
'schema' => null,
)
);
}
public function ct_get_categories($object,$field_name,$request) {
$return = array();
$post_categories = wp_get_post_categories($object['id']);
foreach ($post_categories as $category) {
$return[] = get_category($category);
}
return $return;
}
public function ct_update_categories($categories,$object,$field_name) {
//wp_mail("judah@thosetechguys.org","debug info",serialize(array("object"=>$object,"categories"=>$categories)));
if (empty($categories) || !$categories) {
return;
}
if (is_string($categories)) {
$categories = explode(",",$categories);
}elseif (is_object($categories)) {
$categories = (array) $categories;
}
$input_categories = array();
foreach ($categories as $category) {
$cat_check = $this->category_id($category);
if ($cat_check === false) {
return;
}else {
$input_categories[] = $cat_check;
}
}
return wp_set_post_categories($object->ID,$input_categories);
}
public function ct_get_tags($object,$field_name,$request) {
return wp_get_post_tags($object['id']);
}
public function ct_update_tags($tags,$object,$field_name) {
if (empty($tags) || !tags) {
return;
}
return wp_set_post_tags($object->ID,$tags);
}
/**
* For an input $category, returns either $category or the ID of
* the category if it exists. If not, creats category and returns ID
* @param unknown $category
*/
private function category_id($category=NULL) {
if (is_null($category)) {
return false;
}
foreach ($this->categories as $cat_data) { // if category is name or slug
if ($category == $cat_data->name || $category == $cat_data->slug) {
return $cat_data->term_id;
}
}
foreach ($this->categories as $cat_data) { // if category is ID of category
if ($category == $cat_data->term_id) {
return $cat_data->term_id;
}
}
$new_category = wp_insert_term($category,"category");
return $new_category['term_id'];
}
}