Recipe_Post_Rest_Api::set_recipe_terms( $request )
Set recipe terms & tags
Contents
Parameters Parameters
- $request
-
(Required)
Return Return
(WP_REST_Response)
Source Source
File: src/core/automator-post-types/uo-recipe/class-recipe-post-rest-api.php
public function set_recipe_terms( WP_REST_Request $request ) { // Make sure we have a post ID and a post status $params = $request->get_body_params(); if ( isset( $params['recipe_id'] ) && isset( $params['term_id'] ) ) { $update_count = false; $recipe_id = absint( $params['recipe_id'] ); $taxonomy = (string) sanitize_text_field( $params['term_id'] ); if ( 'recipe_category' === $taxonomy && isset( $params['category_id'] ) && ! empty( $params['category_id'] ) ) { $term_id = absint( $params['category_id'] ); $set_cat = 'true' === sanitize_text_field( $params['set_category'] ) ? true : false; if ( true === $set_cat ) { wp_add_object_terms( $recipe_id, $term_id, $taxonomy ); } elseif ( ! $set_cat ) { wp_remove_object_terms( $recipe_id, $term_id, $taxonomy ); } } elseif ( 'recipe_tag' === $taxonomy && isset( $params['tags']['commaSeparated'] ) && ! empty( $params['tags']['commaSeparated'] ) ) { $tags_sanitized = sanitize_text_field( $params['tags']['commaSeparated'] ); $tags = explode( ',', $tags_sanitized ); wp_set_object_terms( $recipe_id, $tags, $taxonomy ); } if ( $update_count ) { $all_terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, ) ); if ( $all_terms ) { $term_ids = array_column( $all_terms, 'term_id' ); wp_update_term_count_now( $term_ids, $taxonomy ); } } $return['message'] = 'Updated!'; $return['success'] = true; $return['action'] = 'set_recipe_terms'; return new WP_REST_Response( $return, 200 ); } $return['message'] = 'Failed to update'; $return['success'] = false; $return['action'] = 'show_error'; return new WP_REST_Response( $return, 200 ); }
Expand full source code Collapse full source code View on Github