Skip to content

Commit

Permalink
Added methods to update and create terms
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiosanches committed Mar 7, 2016
1 parent a6df996 commit b5e65c6
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 0 deletions.
186 changes: 186 additions & 0 deletions includes/abstracts/abstract-wc-rest-terms-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ public function get_items_permissions_check( $request ) {
return true;
}

/**
* Check if a given request has access to create a term.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function create_item_permissions_check( $request ) {
$taxonomy = get_taxonomy( $this->taxonomy );

if ( ! current_user_can( $taxonomy->cap->manage_terms ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you cannot create new resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

/**
* Check if a given request has access to read a term.
*
Expand All @@ -108,6 +124,26 @@ public function get_item_permissions_check( $request ) {
return $this->get_items_permissions_check( $request );
}

/**
* Check if a given request has access to update a term.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
$term = get_term( (int) $request['id'], $this->taxonomy );
if ( ! $term ) {
return new WP_Error( "woocommerce_rest_{$this->taxonomy}_term_invalid", __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) );
}

$taxonomy = get_taxonomy( $this->taxonomy );
if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
return new WP_Error( 'woocommerce_rest_cannot_update', __( 'Sorry, you cannot update resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

/**
* Check if a given request has access to delete a term.
*
Expand Down Expand Up @@ -236,6 +272,78 @@ public function get_items( $request ) {
return $response;
}

/**
* Create a single term for a taxonomy.
*
* @param WP_REST_Request $request Full details about the request
* @return WP_REST_Request|WP_Error
*/
public function create_item( $request ) {
$name = $request['name'];
$args = array();

if ( isset( $request['description'] ) ) {
$args['description'] = $request['description'];
}
if ( isset( $request['slug'] ) ) {
$args['slug'] = $request['slug'];
}

if ( isset( $request['parent'] ) ) {
if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce' ), array( 'status' => 400 ) );
}

$parent = get_term( (int) $request['parent'], $this->taxonomy );

if ( ! $parent ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( "Parent resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) );
}

$args['parent'] = $parent->term_id;
}

$term = wp_insert_term( $name, $this->taxonomy, $args );
if ( is_wp_error( $term ) ) {

// If we're going to inform the client that the term exists, give them the identifier
// they can actually use.
if ( ( $term_id = $term->get_error_data( 'term_exists' ) ) ) {
$existing_term = get_term( $term_id, $this->taxonomy );
$term->add_data( $existing_term->term_id, 'term_exists' );
}

return $term;
}

$term = get_term( $term['term_id'], $this->taxonomy );

$this->update_additional_fields_for_object( $term, $request );

// Add term data.
$meta_fields = $this->update_term_meta_fields( $term, $request );
if ( is_wp_error( $meta_fields ) ) {
return $meta_fields;
}

/**
* Fires after a single term is created or updated via the REST API.
*
* @param WP_Term $term Inserted Term object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating term, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->taxonomy}", $term, $request, true );

$request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( '/' . WC_API::REST_API_NAMESPACE . '/' . $this->rest_base . '/' . $term->term_id ) );

return $response;
}

/**
* Get a single term from a taxonomy.
*
Expand All @@ -258,6 +366,73 @@ public function get_item( $request ) {
return rest_ensure_response( $response );
}

/**
* Update a single term from a taxonomy
*
* @param WP_REST_Request $request Full details about the request
* @return WP_REST_Request|WP_Error
*/
public function update_item( $request ) {

$prepared_args = array();
if ( isset( $request['name'] ) ) {
$prepared_args['name'] = $request['name'];
}
if ( isset( $request['description'] ) ) {
$prepared_args['description'] = $request['description'];
}
if ( isset( $request['slug'] ) ) {
$prepared_args['slug'] = $request['slug'];
}

if ( isset( $request['parent'] ) ) {
if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_not_hierarchical', __( 'Can not set resource parent, taxonomy is not hierarchical.', 'woocommerce' ), array( 'status' => 400 ) );
}

$parent = get_term( (int) $request['parent'], $this->taxonomy );

if ( ! $parent ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( "Parent resource doesn't exist.", 'woocommerce' ), array( 'status' => 400 ) );
}

$prepared_args['parent'] = $parent->term_id;
}

$term = get_term( (int) $request['id'], $this->taxonomy );

// Only update the term if we haz something to update.
if ( ! empty( $prepared_args ) ) {
$update = wp_update_term( $term->term_id, $term->taxonomy, $prepared_args );
if ( is_wp_error( $update ) ) {
return $update;
}
}

$term = get_term( (int) $request['id'], $this->taxonomy );

$this->update_additional_fields_for_object( $term, $request );

// Update term data.
$meta_fields = $this->update_term_meta_fields( $term, $request );
if ( is_wp_error( $meta_fields ) ) {
return $meta_fields;
}

/**
* Fires after a single term is created or updated via the REST API.
*
* @param WP_Term $term Inserted Term object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating term, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->taxonomy}", $term, $request, false );

$request->set_param( 'context', 'view' );
$response = $this->prepare_item_for_response( $term, $request );
return rest_ensure_response( $response );
}

/**
* Delete a single term from a taxonomy.
*
Expand Down Expand Up @@ -322,6 +497,17 @@ protected function prepare_links( $term ) {
return $links;
}

/**
* Update term meta fields.
*
* @param WP_Term $term
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
protected function update_term_meta_fields( $term, $request ) {
return true;
}

/**
* Get the terms attached to a product.
*
Expand Down
31 changes: 31 additions & 0 deletions includes/api/wc-rest-product-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ public function prepare_item_for_response( $item, $request ) {
return apply_filters( "woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request );
}

/**
* Update term meta fields.
*
* @param WP_Term $term
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
protected function update_term_meta_fields( $term, $request ) {
$id = (int) $term->term_id;

update_woocommerce_term_meta( $id, 'display_type', $request['display'] );

if ( ! empty( $request['image'] ) ) {
$upload = wc_rest_api_upload_image_from_url( esc_url_raw( $request['image'] ) );

if ( is_wp_error( $upload ) ) {
return $upload;
}

$image_id = wc_rest_api_set_uploaded_image_as_attachment( $upload );

// Check if image_id is a valid image attachment before updating the term meta.
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
}
}

return true;
}

/**
* Get the Term's schema, conforming to JSON Schema.
*
Expand Down Expand Up @@ -133,6 +163,7 @@ public function get_item_schema() {
'display' => array(
'description' => __( 'Category archive display type.', 'woocommerce' ),
'type' => 'string',
'default' => 'default',
'enum' => array( 'default', 'products', 'subcategories', 'both' ),
'context' => array( 'view', 'edit' ),
),
Expand Down

0 comments on commit b5e65c6

Please sign in to comment.