| 1706 | /** |
| 1707 | * Create a new term |
| 1708 | * |
| 1709 | * @uses wp_insert_term() |
| 1710 | * @param array $args Method parameters. Contains: |
| 1711 | * - int $blog_id |
| 1712 | * - string $username |
| 1713 | * - string $password |
| 1714 | * - array $content_struct. |
| 1715 | * The $content_struct must contain: |
| 1716 | * - 'name' |
| 1717 | * - 'taxonomy' |
| 1718 | * Also, it can optionally contain: |
| 1719 | * - 'parent' |
| 1720 | * - 'description' |
| 1721 | * - 'slug' |
| 1722 | * @return int term_id |
| 1723 | */ |
| 1724 | function wp_newTerm( $args ) { |
| 1725 | $this->escape( $args ); |
| 1726 | |
| 1727 | $blog_id = (int) $args[0]; |
| 1728 | $username = $args[1]; |
| 1729 | $password = $args[2]; |
| 1730 | $content_struct = $args[3]; |
| 1731 | |
| 1732 | if ( ! $user = $this->login( $username, $password ) ) |
| 1733 | return $this->error; |
| 1734 | |
| 1735 | do_action( 'xmlrpc_call', 'wp.newTerm' ); |
| 1736 | |
| 1737 | if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) |
| 1738 | return new IXR_Error( 403, __( 'Invalid taxonomy.' ) ); |
| 1739 | |
| 1740 | $taxonomy = get_taxonomy( $content_struct['taxonomy'] ); |
| 1741 | |
| 1742 | if( ! current_user_can( $taxonomy->cap->manage_terms ) ) |
| 1743 | return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) ); |
| 1744 | |
| 1745 | $taxonomy = (array) $taxonomy; |
| 1746 | |
| 1747 | // hold the data of the term |
| 1748 | $term_data = array(); |
| 1749 | |
| 1750 | $term_data['name'] = trim( $content_struct['name'] ); |
| 1751 | if ( empty ( $term_data['name'] ) ) |
| 1752 | return new IXR_Error( 403, __( 'The term name cannot be empty.' ) ); |
| 1753 | |
| 1754 | if( isset ( $content_struct['parent'] ) ) { |
| 1755 | if( ! $taxonomy['hierarchical'] ) |
| 1756 | return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) ); |
| 1757 | |
| 1758 | $parent_term_id = (int) $content_struct['parent']; |
| 1759 | $parent_term = get_term( $parent_term_id , $taxonomy['name'] ); |
| 1760 | |
| 1761 | if ( is_wp_error( $parent_term ) ) |
| 1762 | return new IXR_Error( 500, $parent_term->get_error_message() ); |
| 1763 | |
| 1764 | if ( ! $parent_term ) |
| 1765 | return new IXR_Error( 500, __('Parent term does not exist.') ); |
| 1766 | |
| 1767 | $term_data['parent'] = $content_struct['parent']; |
| 1768 | } |
| 1769 | |
| 1770 | if( isset ( $content_struct['description'] ) ) |
| 1771 | $term_data['description'] = $content_struct['description']; |
| 1772 | |
| 1773 | if( isset ( $content_struct['slug'] ) ) |
| 1774 | $term_data['slug'] = $content_struct['slug']; |
| 1775 | |
| 1776 | $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data ); |
| 1777 | |
| 1778 | if ( is_wp_error( $term ) ) |
| 1779 | return new IXR_Error( 500, $term->get_error_message() ); |
| 1780 | |
| 1781 | if ( ! $term ) |
| 1782 | return new IXR_Error( 500, __('Sorry, your term could not be created. Something wrong happened.') ); |
| 1783 | |
| 1784 | return $term['term_id']; |
| 1785 | } |
| 1786 | |