@@ -332,17 +332,6 @@ public function create_item_permissions_check( $request ) {
332332 * @return WP_REST_Request|WP_Error
333333 */
334334 public function create_item ( $ request ) {
335- $ name = $ request ['name ' ];
336-
337- $ args = array ();
338-
339- if ( isset ( $ request ['description ' ] ) ) {
340- $ args ['description ' ] = $ request ['description ' ];
341- }
342- if ( isset ( $ request ['slug ' ] ) ) {
343- $ args ['slug ' ] = $ request ['slug ' ];
344- }
345-
346335 if ( isset ( $ request ['parent ' ] ) ) {
347336 if ( ! is_taxonomy_hierarchical ( $ this ->taxonomy ) ) {
348337 return new WP_Error ( 'rest_taxonomy_not_hierarchical ' , __ ( 'Can not set resource parent, taxonomy is not hierarchical. ' ), array ( 'status ' => 400 ) );
@@ -351,13 +340,13 @@ public function create_item( $request ) {
351340 $ parent = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
352341
353342 if ( ! $ parent ) {
354- return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 404 ) );
343+ return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 400 ) );
355344 }
356-
357- $ args ['parent ' ] = $ parent ->term_id ;
358345 }
359346
360- $ term = wp_insert_term ( $ name , $ this ->taxonomy , $ args );
347+ $ prepared_term = $ this ->prepare_item_for_database ( $ request );
348+
349+ $ term = wp_insert_term ( $ prepared_term ->name , $ this ->taxonomy , $ prepared_term );
361350 if ( is_wp_error ( $ term ) ) {
362351
363352 // If we're going to inform the client that the term exists, give them the identifier
@@ -423,18 +412,6 @@ public function update_item_permissions_check( $request ) {
423412 * @return WP_REST_Request|WP_Error
424413 */
425414 public function update_item ( $ request ) {
426-
427- $ prepared_args = array ();
428- if ( isset ( $ request ['name ' ] ) ) {
429- $ prepared_args ['name ' ] = $ request ['name ' ];
430- }
431- if ( isset ( $ request ['description ' ] ) ) {
432- $ prepared_args ['description ' ] = $ request ['description ' ];
433- }
434- if ( isset ( $ request ['slug ' ] ) ) {
435- $ prepared_args ['slug ' ] = $ request ['slug ' ];
436- }
437-
438415 if ( isset ( $ request ['parent ' ] ) ) {
439416 if ( ! is_taxonomy_hierarchical ( $ this ->taxonomy ) ) {
440417 return new WP_Error ( 'rest_taxonomy_not_hierarchical ' , __ ( 'Can not set resource parent, taxonomy is not hierarchical. ' ), array ( 'status ' => 400 ) );
@@ -445,15 +422,15 @@ public function update_item( $request ) {
445422 if ( ! $ parent ) {
446423 return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 400 ) );
447424 }
448-
449- $ prepared_args ['parent ' ] = $ parent ->term_id ;
450425 }
451426
427+ $ prepared_term = $ this ->prepare_item_for_database ( $ request );
428+
452429 $ term = get_term ( (int ) $ request ['id ' ], $ this ->taxonomy );
453430
454431 // Only update the term if we haz something to update.
455- if ( ! empty ( $ prepared_args ) ) {
456- $ update = wp_update_term ( $ term ->term_id , $ term ->taxonomy , $ prepared_args );
432+ if ( ! empty ( $ prepared_term ) ) {
433+ $ update = wp_update_term ( $ term ->term_id , $ term ->taxonomy , ( array ) $ prepared_term );
457434 if ( is_wp_error ( $ update ) ) {
458435 return $ update ;
459436 }
@@ -527,6 +504,51 @@ public function delete_item( $request ) {
527504 return $ response ;
528505 }
529506
507+ /**
508+ * Prepare a single term for create or update
509+ *
510+ * @param WP_REST_Request $request Request object.
511+ * @return object $prepared_term Term object.
512+ */
513+ public function prepare_item_for_database ( $ request ) {
514+ $ prepared_term = new stdClass ;
515+
516+ if ( isset ( $ request ['name ' ] ) ) {
517+ $ prepared_term ->name = $ request ['name ' ];
518+ }
519+
520+ if ( isset ( $ request ['slug ' ] ) ) {
521+ $ prepared_term ->slug = $ request ['slug ' ];
522+ }
523+
524+ if ( isset ( $ request ['taxonomy ' ] ) ) {
525+ $ prepared_term ->taxonomy = $ request ['taxonomy ' ];
526+ }
527+
528+ if ( isset ( $ request ['description ' ] ) ) {
529+ $ prepared_term ->description = $ request ['description ' ];
530+ }
531+
532+ if ( isset ( $ request ['parent ' ] ) ) {
533+ $ parent_term_id = 0 ;
534+ $ parent_term = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
535+
536+ if ( $ parent_term ) {
537+ $ parent_term_id = $ parent_term ->term_id ;
538+ }
539+
540+ $ prepared_term ->parent = $ parent_term_id ;
541+ }
542+
543+ /**
544+ * Filter term data before inserting term via the REST API.
545+ *
546+ * @param object $prepared_term Term object.
547+ * @param WP_REST_Request $request Request object.
548+ */
549+ return apply_filters ( "rest_pre_insert_ {$ this ->taxonomy }" , $ prepared_term , $ request );
550+ }
551+
530552 /**
531553 * Prepare a single term output for response
532554 *
0 commit comments