| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* |
| 6 |
* @author Christoph Amthor |
| 7 |
* @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 8 |
* @license GPL-3.0+ |
| 9 |
* |
| 10 |
* @since 0.37.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 14 |
if (!class_exists('TagGroups_REST_API')) { |
| 15 |
/** |
| 16 |
* Adds endpoints to the WordPress REST API |
| 17 |
*/ |
| 18 |
class TagGroups_REST_API |
| 19 |
{ |
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register the REST API endpoints and schemata |
| 26 |
* |
| 27 |
* |
| 28 |
* @param void |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public static function register_hook() |
| 32 |
{ |
| 33 |
if (defined('CM_DEBUG')) { |
| 34 |
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 35 |
// development |
| 36 |
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 37 |
// error_log( 'register_routes(), nonce: ' . wp_create_nonce( 'wp_rest' ) ); |
| 38 |
add_filter('wp_is_application_passwords_available', '__return_true'); |
| 39 |
} |
| 40 |
add_action('rest_api_init', array( 'TagGroups_REST_API', 'register_routes' )); |
| 41 |
} |
| 42 |
|
| 43 |
public static function register_routes() |
| 44 |
{ |
| 45 |
global $tag_groups_current_user_id; |
| 46 |
/** |
| 47 |
* Make the current user ID that we retrieved from application password available in permission callbacks |
| 48 |
* (workaround for a suspected bug in 5.6-RC1 |
| 49 |
*/ |
| 50 |
$tag_groups_current_user_id = get_current_user_id(); |
| 51 |
|
| 52 |
register_rest_route( |
| 53 |
'tag-groups/v1', |
| 54 |
'/groups/(?P<id>\\d+)', |
| 55 |
array( |
| 56 |
array( |
| 57 |
'methods' => WP_REST_Server::READABLE, |
| 58 |
'callback' => array('TagGroups_REST_API', 'get_groups'), |
| 59 |
'args' => array( |
| 60 |
'id' => array( |
| 61 |
'validate_callback' => function ($param, $request, $key) { |
| 62 |
return is_numeric($param); |
| 63 |
}, |
| 64 |
), |
| 65 |
), |
| 66 |
'schema' => array('TagGroups_REST_API', 'get_group_schema'), |
| 67 |
'permission_callback' => function ($request) { |
| 68 |
return TagGroups_REST_API::current_user_can_access_endoint('groups'); |
| 69 |
}, |
| 70 |
), |
| 71 |
array( |
| 72 |
'methods' => WP_REST_Server::EDITABLE, |
| 73 |
'callback' => array('TagGroups_REST_API', 'edit_group'), |
| 74 |
'args' => array( |
| 75 |
'id' => array( |
| 76 |
'validate_callback' => function ($param, $request, $key) { |
| 77 |
return is_numeric($param); |
| 78 |
}, |
| 79 |
), |
| 80 |
), |
| 81 |
'permission_callback' => array('TagGroups_REST_API', 'current_user_can_edit_groups'), |
| 82 |
), |
| 83 |
array( |
| 84 |
'methods' => WP_REST_Server::DELETABLE, |
| 85 |
'callback' => array('TagGroups_REST_API', 'delete_group'), |
| 86 |
'args' => array( |
| 87 |
'id' => array( |
| 88 |
'validate_callback' => function ($param, $request, $key) { |
| 89 |
return is_numeric($param); |
| 90 |
}, |
| 91 |
), |
| 92 |
), |
| 93 |
'permission_callback' => array('TagGroups_REST_API', 'current_user_can_edit_groups'), |
| 94 |
) |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
register_rest_route('tag-groups/v1', '/groups/', array( |
| 99 |
'methods' => WP_REST_Server::READABLE, |
| 100 |
'callback' => array('TagGroups_REST_API', 'get_groups'), |
| 101 |
'schema' => array('TagGroups_REST_API', 'get_group_schema'), |
| 102 |
'permission_callback' => function ($request) { |
| 103 |
return TagGroups_REST_API::current_user_can_access_endoint('groups'); |
| 104 |
}, |
| 105 |
)); |
| 106 |
|
| 107 |
register_rest_route( |
| 108 |
'tag-groups/v1', |
| 109 |
'/terms/(?P<id>\\d+)', |
| 110 |
array( |
| 111 |
array( |
| 112 |
'methods' => WP_REST_Server::READABLE, |
| 113 |
'callback' => array('TagGroups_REST_API', 'get_terms'), |
| 114 |
'args' => array( |
| 115 |
'id' => array( |
| 116 |
'validate_callback' => function ($param, $request, $key) { |
| 117 |
return is_numeric($param); |
| 118 |
}, |
| 119 |
), |
| 120 |
), |
| 121 |
'schema' => array('TagGroups_REST_API', 'get_term_schema'), |
| 122 |
'permission_callback' => function ($request) { |
| 123 |
return TagGroups_REST_API::current_user_can_access_endoint('terms'); |
| 124 |
}, |
| 125 |
), |
| 126 |
array( |
| 127 |
'methods' => WP_REST_Server::EDITABLE, |
| 128 |
'callback' => array('TagGroups_REST_API', 'edit_term'), |
| 129 |
'args' => array( |
| 130 |
'id' => array( |
| 131 |
'validate_callback' => function ($param, $request, $key) { |
| 132 |
return is_numeric($param); |
| 133 |
}, |
| 134 |
), |
| 135 |
), |
| 136 |
'permission_callback' => array('TagGroups_REST_API', 'current_user_can_edit_tags'), |
| 137 |
) |
| 138 |
) |
| 139 |
); |
| 140 |
|
| 141 |
register_rest_route('tag-groups/v1', '/terms/', array( |
| 142 |
'methods' => WP_REST_Server::READABLE, |
| 143 |
'callback' => array('TagGroups_REST_API', 'get_terms'), |
| 144 |
'schema' => array('TagGroups_REST_API', 'get_term_schema'), |
| 145 |
'permission_callback' => function ($request) { |
| 146 |
return TagGroups_REST_API::current_user_can_access_endoint('terms'); |
| 147 |
}, |
| 148 |
)); |
| 149 |
|
| 150 |
register_rest_route( |
| 151 |
'tag-groups/v1', |
| 152 |
'/taxonomies/', |
| 153 |
array( |
| 154 |
array( |
| 155 |
'methods' => WP_REST_Server::READABLE, |
| 156 |
'callback' => array('TagGroups_REST_API', 'get_taxonomies'), |
| 157 |
'schema' => array('TagGroups_REST_API', 'get_taxonomy_schema'), |
| 158 |
'permission_callback' => '__return_true', |
| 159 |
), |
| 160 |
array( |
| 161 |
'methods' => WP_REST_Server::EDITABLE, |
| 162 |
'callback' => array('TagGroups_REST_API', 'edit_taxonomies'), |
| 163 |
'permission_callback' => array('TagGroups_REST_API', 'current_user_can_manage_options'), |
| 164 |
) |
| 165 |
) |
| 166 |
); |
| 167 |
|
| 168 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 169 |
register_rest_route( |
| 170 |
'tag-groups/v1', |
| 171 |
'/post-tags/(?P<id>\\d+)', |
| 172 |
array( |
| 173 |
array( |
| 174 |
'methods' => WP_REST_Server::READABLE, |
| 175 |
'callback' => array('TagGroups_REST_API', 'get_posts'), |
| 176 |
'args' => array( |
| 177 |
'id' => array( |
| 178 |
'validate_callback' => function ($param, $request, $key) { |
| 179 |
return is_numeric($param); |
| 180 |
}, |
| 181 |
), |
| 182 |
), |
| 183 |
'schema' => array('TagGroups_REST_API', 'get_post_schema'), |
| 184 |
'permission_callback' => '__return_true', |
| 185 |
), |
| 186 |
array( |
| 187 |
'methods' => WP_REST_Server::EDITABLE, |
| 188 |
'callback' => array('TagGroups_REST_API', 'edit_post'), |
| 189 |
'args' => array( |
| 190 |
'id' => array( |
| 191 |
'validate_callback' => function ($param, $request, $key) { |
| 192 |
return is_numeric($param); |
| 193 |
}, |
| 194 |
), |
| 195 |
), |
| 196 |
'permission_callback' => array('TagGroups_REST_API', 'current_user_can_manage_post_tags'), |
| 197 |
), |
| 198 |
) |
| 199 |
); |
| 200 |
|
| 201 |
register_rest_route( |
| 202 |
'tag-groups/v1', |
| 203 |
'/post-tags/', |
| 204 |
array( |
| 205 |
'methods' => WP_REST_Server::READABLE, |
| 206 |
'callback' => array('TagGroups_REST_API', 'get_posts'), |
| 207 |
'args' => array( |
| 208 |
'page' => array( |
| 209 |
'default' => 1, |
| 210 |
'sanitize_callback' => 'absint', |
| 211 |
'validate_callback' => function ($param) { |
| 212 |
return is_numeric($param) && (int) $param > 0; |
| 213 |
}, |
| 214 |
), |
| 215 |
'per_page' => array( |
| 216 |
'default' => 100, |
| 217 |
'sanitize_callback' => 'absint', |
| 218 |
'validate_callback' => function ($param) { |
| 219 |
return is_numeric($param) && (int) $param > 0 && (int) $param <= 100; |
| 220 |
}, |
| 221 |
), |
| 222 |
), |
| 223 |
'schema' => array('TagGroups_REST_API', 'get_post_schema'), |
| 224 |
'permission_callback' => '__return_true', |
| 225 |
) |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
/** |
| 232 |
* Get one or more groups |
| 233 |
* |
| 234 |
* Returns array consisting of items: tag group ID => tag group label or string of tag group label, if id was provided |
| 235 |
* |
| 236 |
* Arguments: |
| 237 |
* taxonomy default: post_tag |
| 238 |
* hide_empty default: true |
| 239 |
* fields for example: ids, all, names; default: all |
| 240 |
* |
| 241 |
* |
| 242 |
* @param object $request |
| 243 |
* @return array|object |
| 244 |
*/ |
| 245 |
public static function get_groups(WP_REST_Request $request) |
| 246 |
{ |
| 247 |
global $tag_group_groups; |
| 248 |
$id = $request->get_param('id'); |
| 249 |
// don't sanitize here so that we can detect NULL |
| 250 |
$taxonomy = sanitize_title($request->get_param('taxonomy')); |
| 251 |
$hide_empty = ( $request->get_param('hide_empty') ? true : false ); |
| 252 |
$fields = sanitize_title($request->get_param('fields')); |
| 253 |
$orderby = sanitize_title($request->get_param('orderby')); |
| 254 |
$order = sanitize_title($request->get_param('order')); |
| 255 |
|
| 256 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 257 |
$expand = sanitize_title($request->get_param('expand')); |
| 258 |
|
| 259 |
if (!empty($expand)) { |
| 260 |
$expand_a = explode(',', $expand); |
| 261 |
$expand_a = array_map('intval', $expand_a); |
| 262 |
$expand_a = $tag_group_groups->expand_parents($expand_a); |
| 263 |
|
| 264 |
$groups = $tag_group_groups->get_info_of_all( |
| 265 |
$taxonomy, |
| 266 |
$hide_empty, |
| 267 |
$fields, |
| 268 |
$orderby, |
| 269 |
$order |
| 270 |
); |
| 271 |
|
| 272 |
return array_intersect_key($groups, array_flip($expand_a)); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (isset($id)) { |
| 277 |
$id = (int) $id; |
| 278 |
// particular group |
| 279 |
$tg_group = new TagGroups_Group($id); |
| 280 |
if (!$tg_group->exists()) { |
| 281 |
return new WP_Error('no_group', 'Invalid group', array( |
| 282 |
'status' => 404, |
| 283 |
)); |
| 284 |
} |
| 285 |
return $tg_group->get_info( |
| 286 |
$taxonomy, |
| 287 |
$hide_empty, |
| 288 |
$fields, |
| 289 |
$orderby, |
| 290 |
$order |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
$groups = $tag_group_groups->get_info_of_all( |
| 295 |
$taxonomy, |
| 296 |
$hide_empty, |
| 297 |
$fields, |
| 298 |
$orderby, |
| 299 |
$order |
| 300 |
); |
| 301 |
|
| 302 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 303 |
$type = $request->get_param('type'); |
| 304 |
|
| 305 |
if ('metabox' == $type) { |
| 306 |
$tag_group_meta_box_include = TagGroups_Options::get_option('tag_group_meta_box_include', array()); |
| 307 |
|
| 308 |
if (!empty($tag_group_meta_box_include)) { |
| 309 |
foreach ($groups as $key => $group) { |
| 310 |
if (!in_array($group['term_group'], $tag_group_meta_box_include)) { |
| 311 |
unset($groups[$key]); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
$groups = array_values($groups); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return $groups; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Delete a group |
| 325 |
* |
| 326 |
* @param object $request |
| 327 |
* @return void |
| 328 |
*/ |
| 329 |
public static function delete_group(WP_REST_Request $request) |
| 330 |
{ |
| 331 |
$id = (int) $request->get_param('id'); |
| 332 |
|
| 333 |
if (0 == $id) { |
| 334 |
return new WP_Error('wrong_id', 'You cannot delete this groups', array( |
| 335 |
'status' => 400, |
| 336 |
)); |
| 337 |
} else { |
| 338 |
$tg_group = new TagGroups_Group($id); |
| 339 |
if (!$tg_group->exists()) { |
| 340 |
return new WP_Error('wrong_id', "A group with this ID doesn't exists", array( |
| 341 |
'status' => 400, |
| 342 |
)); |
| 343 |
} |
| 344 |
$tg_group->delete(); |
| 345 |
if (!empty($tg_group->error)) { |
| 346 |
return new WP_Error('error', $tg_group->error, array( |
| 347 |
'status' => 400, |
| 348 |
)); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Edit a group or create a new group |
| 355 |
* |
| 356 |
* Sets the provided arguments, or creates a new group if ID is zero; in the letter case the label is mandatory |
| 357 |
* |
| 358 |
* Arguments: |
| 359 |
* label group name |
| 360 |
* position default for new groups: end of list |
| 361 |
* |
| 362 |
* |
| 363 |
* @param object $request |
| 364 |
* @return void |
| 365 |
*/ |
| 366 |
public static function edit_group(WP_REST_Request $request) |
| 367 |
{ |
| 368 |
$id = (int) $request->get_param('id'); |
| 369 |
$label = sanitize_text_field($request->get_param('label')); |
| 370 |
$position = (int) $request->get_param('position'); |
| 371 |
|
| 372 |
if (0 == $id) { |
| 373 |
// create |
| 374 |
if ('' == $label) { |
| 375 |
return new WP_Error('empty_label', 'A new group needs a label', array( |
| 376 |
'status' => 400, |
| 377 |
)); |
| 378 |
} |
| 379 |
$tg_group_check = new TagGroups_Group(); |
| 380 |
if ($tg_group_check->find_by_label($label)) { |
| 381 |
return new WP_Error('duplicate_label', 'A group with this label already exists', array( |
| 382 |
'status' => 400, |
| 383 |
)); |
| 384 |
} |
| 385 |
$tg_group = new TagGroups_Group(); |
| 386 |
$tg_group->create($label, $position); |
| 387 |
} else { |
| 388 |
// update |
| 389 |
$tg_group = new TagGroups_Group($id); |
| 390 |
if (!$tg_group->exists()) { |
| 391 |
return new WP_Error('no_group', 'Group with this ID not found', array( |
| 392 |
'status' => 400, |
| 393 |
)); |
| 394 |
} |
| 395 |
if (!empty($label)) { |
| 396 |
$tg_group->set_label($label); |
| 397 |
} |
| 398 |
if (!empty($position)) { |
| 399 |
$tg_group->set_position($position); |
| 400 |
} |
| 401 |
$tg_group->save(); |
| 402 |
} |
| 403 |
|
| 404 |
if (!empty($tg_group->error)) { |
| 405 |
return new WP_Error('error', $tg_group->error, array( |
| 406 |
'status' => 400, |
| 407 |
)); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Get one or more terms |
| 413 |
* |
| 414 |
* Arguments: |
| 415 |
* taxonomy default: post_tag |
| 416 |
* hide_empty default: true |
| 417 |
* |
| 418 |
* |
| 419 |
* @param object $request |
| 420 |
* @return array|object |
| 421 |
*/ |
| 422 |
public static function get_terms(WP_REST_Request $request) |
| 423 |
{ |
| 424 |
global $tag_group_premium_terms ; |
| 425 |
$term_o = new TagGroups_Term(); |
| 426 |
$id = (int) $request->get_param('id'); |
| 427 |
|
| 428 |
if (!empty($id)) { |
| 429 |
$term_o = new TagGroups_Term($id); |
| 430 |
return array( |
| 431 |
'id' => $id, |
| 432 |
'name' => $term_o->get_name(), |
| 433 |
'slug' => $term_o->get_slug(), |
| 434 |
'taxonomy' => $term_o->get_taxonomy(), |
| 435 |
'groups' => $term_o->get_groups(), |
| 436 |
); |
| 437 |
} else { |
| 438 |
$orderby = sanitize_title($request->get_param('orderby')); |
| 439 |
if (empty($orderby)) { |
| 440 |
$orderby = 'name'; |
| 441 |
} |
| 442 |
$order = sanitize_title($request->get_param('order')); |
| 443 |
if (empty($order)) { |
| 444 |
$order = 'ASC'; |
| 445 |
} |
| 446 |
$taxonomy = sanitize_title($request->get_param('taxonomy')); |
| 447 |
|
| 448 |
if (empty($taxonomy)) { |
| 449 |
$taxonomy = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 450 |
} elseif ('public' == strtolower($taxonomy)) { |
| 451 |
$taxonomy = array_values(get_taxonomies(array( |
| 452 |
'public' => true, |
| 453 |
), 'names')); |
| 454 |
} |
| 455 |
|
| 456 |
$hide_empty = ( $request->get_param('hide_empty') ? true : false ); |
| 457 |
$short = ( $request->get_param('short') ? true : false ); |
| 458 |
$lang = sanitize_title($request->get_param('lang')); |
| 459 |
if (empty($lang)) { |
| 460 |
$lang = null; |
| 461 |
} |
| 462 |
$args = array( |
| 463 |
'taxonomy' => $taxonomy, |
| 464 |
'hide_empty' => $hide_empty, |
| 465 |
'orderby' => $orderby, |
| 466 |
'order' => $order, |
| 467 |
); |
| 468 |
$group = $request->get_param('group'); |
| 469 |
|
| 470 |
if (isset($group)) { |
| 471 |
$group = (int) $group; |
| 472 |
$tg_group = new TagGroups_Group($group); |
| 473 |
if (!$tg_group->exists()) { |
| 474 |
return new WP_Error('no_group', 'Invalid group', array( |
| 475 |
'status' => 404, |
| 476 |
)); |
| 477 |
} |
| 478 |
$group_terms = $tg_group->get_group_terms($taxonomy, $hide_empty, 'ids'); |
| 479 |
if (false === $group_terms) { |
| 480 |
return new WP_Error('no_terms', 'Invalid terms', array( |
| 481 |
'status' => 404, |
| 482 |
)); |
| 483 |
} |
| 484 |
$args['include'] = $group_terms; |
| 485 |
} |
| 486 |
|
| 487 |
$terms = get_terms($args); |
| 488 |
if (class_exists('TagGroups_Premium_Term')) { |
| 489 |
$post_counts = $tag_group_premium_terms->get_post_counts($lang); |
| 490 |
} |
| 491 |
$result = array(); |
| 492 |
foreach ($terms as $term) { |
| 493 |
if (is_object($term)) { |
| 494 |
$term_o = new TagGroups_Term($term); |
| 495 |
$info = array( |
| 496 |
'id' => $term->term_id, |
| 497 |
'name' => html_entity_decode($term->name), |
| 498 |
'slug' => $term->slug, |
| 499 |
'taxonomy' => $term->taxonomy, |
| 500 |
'description' => $term->description, |
| 501 |
); |
| 502 |
|
| 503 |
if (!$short) { |
| 504 |
$info['groups'] = $term_o->get_groups(); |
| 505 |
|
| 506 |
if (isset($post_counts) && isset($post_counts[$term->term_id])) { |
| 507 |
$info['post_count'] = $post_counts[$term->term_id]; |
| 508 |
} else { |
| 509 |
$info['post_count'] = $term->count; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
$result[] = $info; |
| 514 |
} |
| 515 |
} |
| 516 |
return $result; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Set the groups of a term |
| 522 |
* |
| 523 |
* Arguments: |
| 524 |
* groups: comma-separated list of group IDs |
| 525 |
* |
| 526 |
* @param object $request |
| 527 |
* @return void |
| 528 |
*/ |
| 529 |
public static function edit_term(WP_REST_Request $request) |
| 530 |
{ |
| 531 |
$id = (int) $request->get_param('id'); |
| 532 |
$groups = array_map('intval', explode(',', $request->get_param('groups'))); |
| 533 |
|
| 534 |
if (0 == count($groups)) { |
| 535 |
return new WP_Error('no_groups', 'You have to supply a comma-separated list of groups', array( |
| 536 |
'status' => 400, |
| 537 |
)); |
| 538 |
} else { |
| 539 |
$tg_term = new TagGroups_Term($id); |
| 540 |
if (!$tg_term->exists()) { |
| 541 |
return new WP_Error('wrong_id', "A term with this ID doesn't exists", array( |
| 542 |
'status' => 400, |
| 543 |
)); |
| 544 |
} |
| 545 |
$tg_term->set_group($groups)->save(); |
| 546 |
if (!empty($tg_term->error)) { |
| 547 |
return new WP_Error('error', $tg_term->error, array( |
| 548 |
'status' => 400, |
| 549 |
)); |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Get taxonomies, enabled for tag groups or for the metabox |
| 556 |
* |
| 557 |
* Arguments: |
| 558 |
* type metabox, enabled |
| 559 |
* |
| 560 |
* @param object $request |
| 561 |
* @return array|object |
| 562 |
*/ |
| 563 |
public static function get_taxonomies(WP_REST_Request $request) |
| 564 |
{ |
| 565 |
$type = $request->get_param('type'); |
| 566 |
$result = array(); |
| 567 |
switch ($type) { |
| 568 |
case 'metabox': |
| 569 |
$taxonomy_slugs = TagGroups_Taxonomy::get_taxonomies_for_metabox(); |
| 570 |
break; |
| 571 |
case 'enabled': |
| 572 |
default: |
| 573 |
$taxonomy_slugs = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 574 |
break; |
| 575 |
} |
| 576 |
foreach ($taxonomy_slugs as $taxonomy_slug) { |
| 577 |
$result[] = array( |
| 578 |
'slug' => $taxonomy_slug, |
| 579 |
'name' => TagGroups_Taxonomy::get_name_from_slug($taxonomy_slug), |
| 580 |
); |
| 581 |
} |
| 582 |
return $result; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Set taxonomies |
| 587 |
* |
| 588 |
* Arguments: |
| 589 |
* enabled comma-separated list of taxonomy slugs |
| 590 |
* metabox comma-separated list of taxonomy slugs |
| 591 |
* |
| 592 |
* @param object $request |
| 593 |
* @return array|object |
| 594 |
*/ |
| 595 |
public static function edit_taxonomies(WP_REST_Request $request) |
| 596 |
{ |
| 597 |
$enabled = array_map('sanitize_title', explode(',', $request->get_param('enabled'))); |
| 598 |
$metabox = array_map('sanitize_title', explode(',', $request->get_param('metabox'))); |
| 599 |
|
| 600 |
if (count($enabled) && $enabled[0]) { |
| 601 |
foreach ($enabled as $taxonomy) { |
| 602 |
if (!taxonomy_exists($taxonomy)) { |
| 603 |
return new WP_Error('wrong_taxonomy', sprintf("A taxonomy with the slug %s doesn't exist.", $taxonomy), array( |
| 604 |
'status' => 400, |
| 605 |
)); |
| 606 |
} |
| 607 |
} |
| 608 |
TagGroups_Taxonomy::update_enabled($enabled); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
if (count($metabox) && $metabox[0]) { |
| 613 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 614 |
foreach ($metabox as $taxonomy) { |
| 615 |
if (!taxonomy_exists($taxonomy)) { |
| 616 |
return new WP_Error('wrong_taxonomy', sprintf("A taxonomy with the slug %s doesn't exist.", $taxonomy), array( |
| 617 |
'status' => 400, |
| 618 |
)); |
| 619 |
} |
| 620 |
if (!in_array($taxonomy, $enabled_taxonomies)) { |
| 621 |
return new WP_Error('wrong_taxonomy', sprintf("The taxonomy with the slug %s isn't enabled for tag groups.", $taxonomy), array( |
| 622 |
'status' => 400, |
| 623 |
)); |
| 624 |
} |
| 625 |
} |
| 626 |
TagGroups_Options::update_option('tag_group_meta_box_taxonomy', $metabox); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Get one or more posts |
| 632 |
* |
| 633 |
* Arguments: |
| 634 |
* post_type |
| 635 |
* page default: 1 |
| 636 |
* per_page default: 100; maximum: 100 |
| 637 |
* |
| 638 |
* @param WP_REST_Request $request |
| 639 |
* @return array|object |
| 640 |
*/ |
| 641 |
public static function get_posts(WP_REST_Request $request) |
| 642 |
{ |
| 643 |
$id = $request->get_param('id'); |
| 644 |
// don't sanitize here so that we can detect NULL |
| 645 |
$post_type = sanitize_title($request->get_param('post_type')); |
| 646 |
|
| 647 |
if (!empty($id)) { |
| 648 |
$id = (int) $id; |
| 649 |
$post = get_post($id); |
| 650 |
if (!is_object($post)) { |
| 651 |
return new WP_Error('wrong_post', sprintf("A post with the ID %d doesn't exist.", $id), array( |
| 652 |
'status' => 400, |
| 653 |
)); |
| 654 |
} |
| 655 |
/** |
| 656 |
* check persmission |
| 657 |
*/ |
| 658 |
|
| 659 |
if (get_current_user_id()) { |
| 660 |
if (!current_user_can('read_post', $id)) { |
| 661 |
return new WP_Error('permission', "You are not allowed to view this post.", array( |
| 662 |
'status' => 403, |
| 663 |
)); |
| 664 |
} |
| 665 |
} else { |
| 666 |
if ('' !== $post->post_password || 'publish' !== $post->post_status) { |
| 667 |
return new WP_Error('permission', "You are not allowed to view this post.", array( |
| 668 |
'status' => 400, |
| 669 |
)); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
$tg_post = new TagGroups_Premium_Post($id); |
| 674 |
return array( |
| 675 |
'id' => $id, |
| 676 |
'terms' => $tg_post->get_terms_by_group(), |
| 677 |
); |
| 678 |
} else { |
| 679 |
$return_data = array(); |
| 680 |
$page = max(1, absint($request->get_param('page'))); |
| 681 |
$per_page = absint($request->get_param('per_page')); |
| 682 |
$per_page = $per_page ? min($per_page, 100) : 100; |
| 683 |
$args = array( |
| 684 |
'posts_per_page' => $per_page, |
| 685 |
'paged' => $page, |
| 686 |
'fields' => 'ids', |
| 687 |
'post_type' => $post_type, |
| 688 |
'post_status' => array( |
| 689 |
'publish', |
| 690 |
'pending', |
| 691 |
'draft', |
| 692 |
'future', |
| 693 |
'private' |
| 694 |
), |
| 695 |
); |
| 696 |
$post_ids = get_posts($args); |
| 697 |
foreach ($post_ids as $post_id) { |
| 698 |
/** |
| 699 |
* retrieving objects one by one so that we don't risk running into memory issues |
| 700 |
*/ |
| 701 |
$post = get_post($post_id); |
| 702 |
/** |
| 703 |
* check persmission |
| 704 |
*/ |
| 705 |
|
| 706 |
if (get_current_user_id()) { |
| 707 |
if (!current_user_can('read_post', $post_id)) { |
| 708 |
continue; |
| 709 |
} |
| 710 |
} else { |
| 711 |
if ('' !== $post->post_password || 'publish' !== $post->post_status) { |
| 712 |
continue; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
$tg_post = new TagGroups_Premium_Post($post_id); |
| 717 |
$return_data[] = array( |
| 718 |
'id' => $post_id, |
| 719 |
'terms' => $tg_post->get_terms_by_group(), |
| 720 |
); |
| 721 |
} |
| 722 |
return $return_data; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
/** |
| 727 |
* get taxonomies |
| 728 |
* |
| 729 |
* Arguments: |
| 730 |
* terms JSON-encoded array of group IDs as keys and an array of term IDs as each value |
| 731 |
* taxonomy The taxonomy slug |
| 732 |
* |
| 733 |
* @param object $request |
| 734 |
* @return array|object |
| 735 |
*/ |
| 736 |
public static function edit_post(WP_REST_Request $request) |
| 737 |
{ |
| 738 |
$id = (int) $request->get_param('id'); |
| 739 |
$terms = json_decode($request->get_param('terms'), true); |
| 740 |
if (is_null($terms) || !is_array($terms)) { |
| 741 |
return new WP_Error('wrong_term_data', "The term data has a wrong format.", array( |
| 742 |
'status' => 400, |
| 743 |
)); |
| 744 |
} |
| 745 |
$taxonomy = sanitize_title($request->get_param('taxonomy')); |
| 746 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 747 |
if (empty($taxonomy) || !in_array($taxonomy, $enabled_taxonomies)) { |
| 748 |
return new WP_Error('wrong_taxonomy', "Missing or wrong taxonomy.", array( |
| 749 |
'status' => 400, |
| 750 |
)); |
| 751 |
} |
| 752 |
$post = get_post($id); |
| 753 |
if (!is_object($post)) { |
| 754 |
return new WP_Error('wrong_post', sprintf("A post with the ID %d doesn't exist.", $id), array( |
| 755 |
'status' => 400, |
| 756 |
)); |
| 757 |
} |
| 758 |
/** |
| 759 |
* check persmission |
| 760 |
*/ |
| 761 |
$post_type_object = get_post_type_object($post->post_type); |
| 762 |
if (!is_object($post_type_object) || !is_object($post_type_object->cap) || !current_user_can($post_type_object->cap->edit_post, $id)) { |
| 763 |
return new WP_Error('permission', "You are not allowed to edit this post.", array( |
| 764 |
'status' => 400, |
| 765 |
)); |
| 766 |
} |
| 767 |
$post_terms = array(); |
| 768 |
$unassigned_post_terms = array(); |
| 769 |
foreach ($terms as $group_id => $term_array) { |
| 770 |
if (!is_int($group_id)) { |
| 771 |
return new WP_Error('wrong_group_id', sprintf("Wrong group ID %s.", $group_id), array( |
| 772 |
'status' => 400, |
| 773 |
)); |
| 774 |
} |
| 775 |
$tg_group = new TagGroups_Group($group_id); |
| 776 |
if (!$tg_group->exists()) { |
| 777 |
return new WP_Error('wrong_group_id', sprintf("Wrong group ID %s.", $group_id), array( |
| 778 |
'status' => 400, |
| 779 |
)); |
| 780 |
} |
| 781 |
foreach ($term_array as $term_id) { |
| 782 |
if (!is_int($term_id) || !get_term_by('id', $term_id, $taxonomy)) { |
| 783 |
return new WP_Error('wrong_term_id', sprintf("Wrong term ID %s.", $term_id), array( |
| 784 |
'status' => 400, |
| 785 |
)); |
| 786 |
} |
| 787 |
$tg_term = new TagGroups_Term($term_id); |
| 788 |
|
| 789 |
if (0 == $group_id) { |
| 790 |
$unassigned_post_terms[] = $term_id; |
| 791 |
if (!$tg_term->has_exactly_groups(0)) { |
| 792 |
$tg_term->set_group(0)->save(); |
| 793 |
} |
| 794 |
} else { |
| 795 |
$post_terms[] = array( |
| 796 |
'term_id' => (int) $term_id, |
| 797 |
'taxonomy' => $taxonomy, |
| 798 |
'term_group' => (int) $group_id, |
| 799 |
); |
| 800 |
if (!$tg_term->has_all_groups($group_id)) { |
| 801 |
$tg_term->add_group($group_id)->save(); |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
$tg_post = new TagGroups_Premium_Post($id); |
| 807 |
$tg_post->set_terms($post_terms, $unassigned_post_terms, 'term_id')->save(); |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* |
| 812 |
*/ |
| 813 |
public static function get_group_schema() |
| 814 |
{ |
| 815 |
$schema = array( |
| 816 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 817 |
'title' => 'group', |
| 818 |
'type' => 'object', |
| 819 |
'properties' => array( |
| 820 |
'term_group' => array( |
| 821 |
'type' => 'integer', |
| 822 |
'label' => 'Object ID.', |
| 823 |
'readonly' => true, |
| 824 |
), |
| 825 |
'label' => array( |
| 826 |
'description' => 'The object name.', |
| 827 |
'type' => 'string', |
| 828 |
), |
| 829 |
'position' => array( |
| 830 |
'description' => 'The position of the object in lists, menus and tag clouds.', |
| 831 |
'type' => 'integer', |
| 832 |
), |
| 833 |
'terms' => array( |
| 834 |
'description' => 'The terms that are assigned to this group.', |
| 835 |
'type' => 'array', |
| 836 |
), |
| 837 |
), |
| 838 |
); |
| 839 |
return $schema; |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
* |
| 844 |
*/ |
| 845 |
public static function get_term_schema() |
| 846 |
{ |
| 847 |
$schema = array( |
| 848 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 849 |
'title' => 'term', |
| 850 |
'type' => 'object', |
| 851 |
'properties' => array( |
| 852 |
'id' => array( |
| 853 |
'type' => 'integer', |
| 854 |
'label' => 'Object ID.', |
| 855 |
'readonly' => true, |
| 856 |
), |
| 857 |
'name' => array( |
| 858 |
'description' => 'The object name.', |
| 859 |
'type' => 'string', |
| 860 |
), |
| 861 |
'slug' => array( |
| 862 |
'description' => 'The term slug.', |
| 863 |
'type' => 'string', |
| 864 |
), |
| 865 |
'taxonomy' => array( |
| 866 |
'description' => 'The term taxonomy.', |
| 867 |
'type' => 'string', |
| 868 |
), |
| 869 |
'description' => array( |
| 870 |
'description' => 'The term description.', |
| 871 |
'type' => 'string', |
| 872 |
), |
| 873 |
'groups' => array( |
| 874 |
'description' => 'The groups that this term is assigned to.', |
| 875 |
'type' => 'array', |
| 876 |
), |
| 877 |
'post_count' => array( |
| 878 |
'description' => 'The post count per group (published posts).', |
| 879 |
'type' => 'array', |
| 880 |
), |
| 881 |
), |
| 882 |
); |
| 883 |
return $schema; |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
* |
| 888 |
*/ |
| 889 |
public static function get_taxonomy_schema() |
| 890 |
{ |
| 891 |
$schema = array( |
| 892 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 893 |
'title' => 'taxonomy', |
| 894 |
'type' => 'object', |
| 895 |
'properties' => array( |
| 896 |
'name' => array( |
| 897 |
'description' => 'The taxonomy name.', |
| 898 |
'type' => 'string', |
| 899 |
), |
| 900 |
), |
| 901 |
); |
| 902 |
return $schema; |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* |
| 907 |
*/ |
| 908 |
public static function get_post_schema() |
| 909 |
{ |
| 910 |
$schema = array( |
| 911 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 912 |
'title' => 'post', |
| 913 |
'type' => 'object', |
| 914 |
'properties' => array( |
| 915 |
'id' => array( |
| 916 |
'type' => 'integer', |
| 917 |
'label' => 'Object ID.', |
| 918 |
'readonly' => true, |
| 919 |
), |
| 920 |
'terms' => array( |
| 921 |
'description' => 'The post tags in their groups.', |
| 922 |
'type' => 'array', |
| 923 |
), |
| 924 |
), |
| 925 |
); |
| 926 |
return $schema; |
| 927 |
} |
| 928 |
|
| 929 |
/** |
| 930 |
* Determines whether the current user is allowed to edit tag groups |
| 931 |
* |
| 932 |
* @return boolean |
| 933 |
*/ |
| 934 |
public static function current_user_can_edit_groups() |
| 935 |
{ |
| 936 |
/** |
| 937 |
* editable REST API is opt-in |
| 938 |
*/ |
| 939 |
|
| 940 |
if (!defined('TAG_GROUPS_REST_API_EDITABLE') || !TAG_GROUPS_REST_API_EDITABLE) { |
| 941 |
TagGroups_Error::verbose_log('[Tag Groups] REST API is not editable. Add to wp-config.php: define( "TAG_GROUPS_REST_API_EDITABLE", true );'); |
| 942 |
return false; |
| 943 |
} |
| 944 |
|
| 945 |
global $tag_groups_current_user_id ; |
| 946 |
$tag_group_role_edit_groups = 'edit_pages'; |
| 947 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 948 |
$tag_group_role_edit_groups = TagGroups_Options::get_option('tag_group_role_edit_groups', 'edit_pages'); |
| 949 |
} |
| 950 |
|
| 951 |
if (!get_current_user_id() && $tag_groups_current_user_id) { |
| 952 |
wp_set_current_user($tag_groups_current_user_id); |
| 953 |
} |
| 954 |
return current_user_can($tag_group_role_edit_groups); |
| 955 |
} |
| 956 |
|
| 957 |
/** |
| 958 |
* Determines whether the current user is allowed to edit the groups of tags |
| 959 |
* |
| 960 |
* @return boolean |
| 961 |
*/ |
| 962 |
public static function current_user_can_edit_tags() |
| 963 |
{ |
| 964 |
/** |
| 965 |
* editable REST API is opt-in |
| 966 |
*/ |
| 967 |
|
| 968 |
if (!defined('TAG_GROUPS_REST_API_EDITABLE') || !TAG_GROUPS_REST_API_EDITABLE) { |
| 969 |
TagGroups_Error::verbose_log('[Tag Groups] REST API is not editable. Add to wp-config.php: define( "TAG_GROUPS_REST_API_EDITABLE", true );'); |
| 970 |
return false; |
| 971 |
} |
| 972 |
|
| 973 |
global $tag_groups_current_user_id ; |
| 974 |
$tag_group_role_edit_tags = 'edit_pages'; |
| 975 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 976 |
$tag_group_role_edit_tags = TagGroups_Options::get_option('tag_group_role_edit_tags', 'edit_pages'); |
| 977 |
} |
| 978 |
|
| 979 |
if (!get_current_user_id() && $tag_groups_current_user_id) { |
| 980 |
wp_set_current_user($tag_groups_current_user_id); |
| 981 |
} |
| 982 |
return current_user_can($tag_group_role_edit_tags); |
| 983 |
} |
| 984 |
|
| 985 |
/** |
| 986 |
* Determines whether the current user is allowed to manage options |
| 987 |
* |
| 988 |
* @return boolean |
| 989 |
*/ |
| 990 |
public static function current_user_can_manage_options() |
| 991 |
{ |
| 992 |
/** |
| 993 |
* editable REST API is opt-in |
| 994 |
*/ |
| 995 |
|
| 996 |
if (!defined('TAG_GROUPS_REST_API_EDITABLE') || !TAG_GROUPS_REST_API_EDITABLE) { |
| 997 |
TagGroups_Error::verbose_log('[Tag Groups] REST API is not editable. Add to wp-config.php: define( "TAG_GROUPS_REST_API_EDITABLE", true );'); |
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
global $tag_groups_current_user_id ; |
| 1002 |
if (!get_current_user_id() && $tag_groups_current_user_id) { |
| 1003 |
wp_set_current_user($tag_groups_current_user_id); |
| 1004 |
} |
| 1005 |
return current_user_can('manage_options'); |
| 1006 |
} |
| 1007 |
|
| 1008 |
/** |
| 1009 |
* Determines whether the current user/guest is allowed to access endpoint |
| 1010 |
* |
| 1011 |
* @return boolean |
| 1012 |
*/ |
| 1013 |
public static function current_user_can_access_endoint($option = 'groups') |
| 1014 |
{ |
| 1015 |
global $tag_groups_current_user_id ; |
| 1016 |
$can_access = false; |
| 1017 |
|
| 1018 |
if (!get_current_user_id() && $tag_groups_current_user_id) { |
| 1019 |
wp_set_current_user($tag_groups_current_user_id); |
| 1020 |
} |
| 1021 |
|
| 1022 |
//enable admin by default |
| 1023 |
if (current_user_can('manage_options')) { |
| 1024 |
$can_access = true; |
| 1025 |
} elseif ($option == 'groups' && !empty(TagGroups_Options::get_option('tag_group_enable_group_public_api_access', 0))) { |
| 1026 |
$can_access = true; |
| 1027 |
} elseif ($option == 'terms' && !empty(TagGroups_Options::get_option('tag_group_enable_terms_public_api_access', 0))) { |
| 1028 |
$can_access = true; |
| 1029 |
} |
| 1030 |
|
| 1031 |
return $can_access; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Determines whether the current user is allowed to manage post tags |
| 1036 |
* |
| 1037 |
* @return boolean |
| 1038 |
*/ |
| 1039 |
public static function current_user_can_manage_post_tags() |
| 1040 |
{ |
| 1041 |
/** |
| 1042 |
* editable REST API is opt-in |
| 1043 |
*/ |
| 1044 |
|
| 1045 |
if (!defined('TAG_GROUPS_REST_API_EDITABLE') || !TAG_GROUPS_REST_API_EDITABLE) { |
| 1046 |
TagGroups_Error::verbose_log('[Tag Groups] REST API is not editable. Add to wp-config.php: define( "TAG_GROUPS_REST_API_EDITABLE", true );'); |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
|
| 1050 |
global $tag_groups_current_user_id ; |
| 1051 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 1052 |
if (!get_current_user_id() && $tag_groups_current_user_id) { |
| 1053 |
wp_set_current_user($tag_groups_current_user_id); |
| 1054 |
} |
| 1055 |
|
| 1056 |
return current_user_can('edit_posts'); |
| 1057 |
} |
| 1058 |
|
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|