register( 'taxonomies_list', array( 'description' => 'List registered taxonomies.', 'domain' => 'content', 'capability' => 'manage_categories', 'inputSchema' => array( 'type' => 'object', 'properties' => array() ), ), function () { $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); $items = array(); foreach ( $taxonomies as $tax ) { $items[] = array( 'name' => $tax->name, 'label' => $tax->label, ); } return array( 'taxonomies' => $items ); } ); $registry->register( 'terms_list', array( 'description' => 'List terms in a taxonomy.', 'domain' => 'content', 'capability' => 'manage_categories', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'taxonomy' => array( 'type' => 'string', 'default' => 'category' ), 'search' => array( 'type' => 'string' ), 'hide_empty'=> array( 'type' => 'boolean', 'default' => false ), ), ), ), function ( $args ) { $taxonomy = isset( $args['taxonomy'] ) ? sanitize_key( $args['taxonomy'] ) : 'category'; $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => ! empty( $args['hide_empty'] ), 'search' => isset( $args['search'] ) ? sanitize_text_field( $args['search'] ) : '', ) ); if ( is_wp_error( $terms ) ) { return $terms; } $items = array(); foreach ( $terms as $term ) { $items[] = array( 'id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'count' => $term->count, 'parent' => $term->parent, ); } return array( 'terms' => $items ); } ); $registry->register( 'terms_create', array( 'description' => 'Create a taxonomy term.', 'domain' => 'content', 'capability' => 'manage_categories', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'taxonomy' => array( 'type' => 'string' ), 'name' => array( 'type' => 'string' ), 'slug' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'parent' => array( 'type' => 'integer' ), ), 'required' => array( 'taxonomy', 'name' ), ), ), function ( $args ) { $taxonomy = sanitize_key( $args['taxonomy'] ); $tax_obj = get_taxonomy( $taxonomy ); if ( ! $tax_obj ) { return new WP_Error( 'agentdesk_taxonomy_unknown', 'Unknown taxonomy.' ); } if ( ! current_user_can( $tax_obj->cap->manage_terms ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to manage terms in this taxonomy.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } $result = wp_insert_term( sanitize_text_field( $args['name'] ), $taxonomy, array( 'slug' => isset( $args['slug'] ) ? sanitize_title( $args['slug'] ) : '', 'description' => isset( $args['description'] ) ? sanitize_textarea_field( $args['description'] ) : '', 'parent' => isset( $args['parent'] ) ? absint( $args['parent'] ) : 0, ) ); return $result; } ); $registry->register( 'terms_assign', array( 'description' => 'Assign terms to a post.', 'domain' => 'content', 'capability' => 'edit_posts', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer' ), 'taxonomy' => array( 'type' => 'string' ), 'term_ids' => array( 'type' => 'array', 'items' => array( 'type' => 'integer' ) ), ), 'required' => array( 'post_id', 'taxonomy', 'term_ids' ), ), ), function ( $args ) { $post_id = absint( $args['post_id'] ); $taxonomy = sanitize_key( $args['taxonomy'] ); $term_ids = array_map( 'absint', (array) $args['term_ids'] ); $allowed = Agentdesk_Security::can_edit_post( $post_id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $tax_obj = get_taxonomy( $taxonomy ); if ( ! $tax_obj ) { return new WP_Error( 'agentdesk_taxonomy_unknown', 'Unknown taxonomy.' ); } if ( ! current_user_can( $tax_obj->cap->assign_terms ) ) { return new WP_Error( 'agentdesk_forbidden', __( 'You are not allowed to assign terms in this taxonomy.', 'agentdesk-site-operations' ), array( 'status' => 403 ) ); } $result = wp_set_object_terms( $post_id, $term_ids, $taxonomy ); if ( is_wp_error( $result ) ) { return $result; } return array( 'post_id' => $post_id, 'taxonomy' => $taxonomy, 'term_ids' => $term_ids ); } ); } );