-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxonomyTools.php
More file actions
191 lines (165 loc) · 5.74 KB
/
Copy pathTaxonomyTools.php
File metadata and controls
191 lines (165 loc) · 5.74 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Taxonomy MCP tools.
*
* @package WP_Forge
*/
namespace WP_Forge\Tools;
use WP_Forge\Response;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers taxonomy tools.
*/
trait TaxonomyTools {
/**
* Taxonomy abilities.
*
* @return void
*/
private function add_taxonomy_abilities() {
$taxonomy_schema = function () {
return $this->schema(
array(
'taxonomy' => $this->enum_string_prop( 'Registered taxonomy slug.', $this->get_registered_taxonomy_slugs() ),
),
array( 'taxonomy' )
);
};
$term_get_schema = function () {
return $this->schema(
array(
'taxonomy' => $this->enum_string_prop( 'Registered taxonomy slug.', $this->get_registered_taxonomy_slugs() ),
'id' => $this->int_prop( 'Term ID.' ),
),
array( 'taxonomy', 'id' )
);
};
$term_save_schema = function () {
return $this->schema(
array(
'taxonomy' => $this->enum_string_prop( 'Registered taxonomy slug.', $this->get_registered_taxonomy_slugs() ),
'id' => $this->int_prop( 'Term ID. Omit to create a new term.' ),
'name' => $this->string_prop( 'Term name.' ),
'slug' => $this->string_prop( 'Term slug.' ),
'description' => $this->string_prop( 'Description.' ),
),
array( 'taxonomy' )
);
};
$list_taxonomies_schema = function () {
return $this->schema(
array(
'post_type' => $this->enum_string_prop( 'Filter taxonomies by registered object type.', $this->get_registered_post_type_slugs() ),
)
);
};
$this->add_ability( self::INTERNAL_PREFIX . 'taxonomy-list', 'List Taxonomies', 'List registered WordPress taxonomies', $list_taxonomies_schema, function ( $params ) {
return $this->list_taxonomies_tool( $params );
} );
$this->add_ability( self::INTERNAL_PREFIX . 'taxonomy-term-list', 'List Taxonomy Terms', 'List terms for a registered taxonomy', $taxonomy_schema, function ( $params ) {
return $this->list_taxonomy_terms_tool( $params['taxonomy'] );
} );
$this->add_ability( self::INTERNAL_PREFIX . 'taxonomy-term-get', 'Get Taxonomy Term', 'Get a term from a registered taxonomy by ID', $term_get_schema, function ( $params ) {
return $this->get_taxonomy_term_tool( $params['taxonomy'], (int) $params['id'] );
} );
$this->add_ability( self::INTERNAL_PREFIX . 'taxonomy-term-save', 'Save Taxonomy Term', 'Create or update a term in a registered taxonomy', $term_save_schema, function ( $params ) {
return $this->save_taxonomy_term_tool( $params );
}, false, 'manage_categories' );
$this->add_ability( self::INTERNAL_PREFIX . 'taxonomy-term-delete', 'Delete Taxonomy Term', 'Delete a term from a registered taxonomy', $term_get_schema, function ( $params ) {
return $this->delete_term( $params['taxonomy'], (int) $params['id'] );
}, false, 'manage_categories', array( 'destructive' => true, 'idempotent' => true ) );
}
/**
* List taxonomies.
*
* @param array<string,mixed> $params Params.
* @return array<string,mixed>|array<int,array<string,mixed>>
*/
private function list_taxonomies_tool( $params ) {
if ( ! function_exists( 'get_taxonomies' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
$taxonomies = get_taxonomies( array(), 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy ) {
if ( ! empty( $params['post_type'] ) && ! in_array( $params['post_type'], $taxonomy->object_type, true ) ) {
continue;
}
$out[] = array(
'name' => $taxonomy->name,
'label' => $taxonomy->label,
'description' => $taxonomy->description,
'public' => (bool) $taxonomy->public,
'hierarchical' => (bool) $taxonomy->hierarchical,
'object_type' => array_values( $taxonomy->object_type ),
'rest_base' => $taxonomy->rest_base,
);
}
return $out;
}
/**
* List taxonomy terms.
*
* @param string $taxonomy Taxonomy.
* @return mixed
*/
private function list_taxonomy_terms_tool( $taxonomy ) {
$taxonomy_check = $this->validate_taxonomy( $taxonomy );
if ( isset( $taxonomy_check['status'] ) && 'error' === $taxonomy_check['status'] ) {
return $taxonomy_check;
}
return $this->list_terms( $taxonomy_check['taxonomy'] );
}
/**
* Get a taxonomy term.
*
* @param string $taxonomy Taxonomy.
* @param int $id Term ID.
* @return mixed
*/
private function get_taxonomy_term_tool( $taxonomy, $id ) {
$taxonomy_check = $this->validate_taxonomy( $taxonomy );
if ( isset( $taxonomy_check['status'] ) && 'error' === $taxonomy_check['status'] ) {
return $taxonomy_check;
}
return $this->get_term_item( $taxonomy_check['taxonomy'], $id );
}
/**
* Save a taxonomy term.
*
* @param array<string,mixed> $params Params.
* @return mixed
*/
private function save_taxonomy_term_tool( $params ) {
$taxonomy_check = $this->validate_taxonomy( $params['taxonomy'] );
if ( isset( $taxonomy_check['status'] ) && 'error' === $taxonomy_check['status'] ) {
return $taxonomy_check;
}
$taxonomy = $taxonomy_check['taxonomy'];
if ( ! empty( $params['id'] ) ) {
return $this->update_term( $taxonomy, (int) $params['id'], $params );
}
if ( empty( $params['name'] ) ) {
return Response::error( 'Term name is required when creating a term.', 400 );
}
return $this->insert_term( $taxonomy, $params );
}
/**
* Validate taxonomy.
*
* @param string $taxonomy Taxonomy.
* @return array<string,mixed>
*/
private function validate_taxonomy( $taxonomy ) {
if ( ! function_exists( 'taxonomy_exists' ) ) {
return Response::error( 'This ability requires a WordPress runtime.', 500 );
}
$taxonomy = $this->sanitize_key_value( $taxonomy );
if ( ! taxonomy_exists( $taxonomy ) ) {
return Response::error( 'Taxonomy not found: ' . $taxonomy, 404 );
}
return array( 'taxonomy' => $taxonomy );
}
}