@@ -332,13 +332,21 @@ public function create_item_permissions_check( $request ) {
332332 * @return WP_REST_Request|WP_Error
333333 */
334334 public function create_item ( $ request ) {
335- $ prepared_term = $ this ->prepare_item_for_database ( $ request );
335+ if ( isset ( $ request ['parent ' ] ) ) {
336+ if ( ! is_taxonomy_hierarchical ( $ this ->taxonomy ) ) {
337+ return new WP_Error ( 'rest_taxonomy_not_hierarchical ' , __ ( 'Can not set resource parent, taxonomy is not hierarchical. ' ), array ( 'status ' => 400 ) );
338+ }
336339
337- if ( is_wp_error ( $ prepared_term ) ) {
338- return rest_ensure_response ( $ prepared_term );
340+ $ parent = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
341+
342+ if ( ! $ parent ) {
343+ return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 400 ) );
344+ }
339345 }
340346
341- $ term = wp_insert_term ( $ prepared_term ->name , $ this ->taxonomy , $ prepared_term ->args );
347+ $ prepared_term = $ this ->prepare_item_for_database ( $ request );
348+
349+ $ term = wp_insert_term ( $ prepared_term ->name , $ this ->taxonomy , $ prepared_term );
342350 if ( is_wp_error ( $ term ) ) {
343351
344352 // If we're going to inform the client that the term exists, give them the identifier
@@ -404,17 +412,25 @@ public function update_item_permissions_check( $request ) {
404412 * @return WP_REST_Request|WP_Error
405413 */
406414 public function update_item ( $ request ) {
407- $ prepared_term = $ this ->prepare_item_for_database ( $ request );
415+ if ( isset ( $ request ['parent ' ] ) ) {
416+ if ( ! is_taxonomy_hierarchical ( $ this ->taxonomy ) ) {
417+ return new WP_Error ( 'rest_taxonomy_not_hierarchical ' , __ ( 'Can not set resource parent, taxonomy is not hierarchical. ' ), array ( 'status ' => 400 ) );
418+ }
408419
409- if ( is_wp_error ( $ prepared_term ) ) {
410- return rest_ensure_response ( $ prepared_term );
420+ $ parent = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
421+
422+ if ( ! $ parent ) {
423+ return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 400 ) );
424+ }
411425 }
412426
427+ $ prepared_term = $ this ->prepare_item_for_database ( $ request );
428+
413429 $ term = get_term ( (int ) $ request ['id ' ], $ this ->taxonomy );
414430
415431 // Only update the term if we haz something to update.
416- if ( ! empty ( $ prepared_term-> args ) ) {
417- $ update = wp_update_term ( $ term ->term_id , $ term ->taxonomy , $ prepared_term-> args );
432+ if ( ! empty ( $ prepared_term ) ) {
433+ $ update = wp_update_term ( $ term ->term_id , $ term ->taxonomy , ( array ) $ prepared_term );
418434 if ( is_wp_error ( $ update ) ) {
419435 return $ update ;
420436 }
@@ -496,33 +512,44 @@ public function delete_item( $request ) {
496512 */
497513 public function prepare_item_for_database ( $ request ) {
498514 $ prepared_term = new stdClass ;
499- $ prepared_term ->args = array ();
500515
501516 if ( isset ( $ request ['name ' ] ) ) {
502517 $ prepared_term ->name = $ request ['name ' ];
503- $ prepared_term ->args ['name ' ] = $ request ['name ' ];
504518 }
505519
506- if ( isset ( $ request ['description ' ] ) ) {
507- $ prepared_term ->args [ ' description ' ] = $ request ['description ' ];
520+ if ( isset ( $ request ['slug ' ] ) ) {
521+ $ prepared_term ->slug = $ request ['slug ' ];
508522 }
509523
510- if ( isset ( $ request ['slug ' ] ) ) {
511- $ prepared_term ->args [ ' slug ' ] = $ request ['slug ' ];
524+ if ( isset ( $ request ['term_group ' ] ) ) {
525+ $ prepared_term ->term_group = ( int ) $ request ['term_group ' ];
512526 }
513527
514- if ( isset ( $ request ['parent ' ] ) ) {
515- if ( ! is_taxonomy_hierarchical ( $ this ->taxonomy ) ) {
516- return new WP_Error ( 'rest_taxonomy_not_hierarchical ' , __ ( 'Can not set resource parent, taxonomy is not hierarchical. ' ), array ( 'status ' => 400 ) );
517- }
528+ if ( isset ( $ request ['term_taxonomy_id ' ] ) ) {
529+ $ prepared_term ->term_taxonomy_id = (int ) $ request ['term_taxonomy_id ' ];
530+ }
518531
519- $ parent = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
532+ if ( isset ( $ request ['taxonomy ' ] ) ) {
533+ $ prepared_term ->taxonomy = $ request ['taxonomy ' ];
534+ }
520535
521- if ( ! $ parent ) {
522- return new WP_Error ( 'rest_term_invalid ' , __ ( "Parent resource doesn't exist. " ), array ( 'status ' => 400 ) );
536+ if ( isset ( $ request ['description ' ] ) ) {
537+ $ prepared_term ->description = $ request ['description ' ];
538+ }
539+
540+ if ( isset ( $ request ['parent ' ] ) ) {
541+ $ parent_term_id = 0 ;
542+ $ parent_term = get_term ( (int ) $ request ['parent ' ], $ this ->taxonomy );
543+
544+ if ( $ parent_term ) {
545+ $ parent_term_id = $ parent_term ->term_id ;
523546 }
524547
525- $ prepared_term ->args ['parent ' ] = $ parent ->term_id ;
548+ $ prepared_term ->parent = $ parent_term_id ;
549+ }
550+
551+ if ( isset ( $ request ['count ' ] ) ) {
552+ $ prepared_term ->count = (int ) $ request ['count ' ];
526553 }
527554
528555 /**
0 commit comments