| | 1706 | /** |
| | 1707 | * Set post terms |
| | 1708 | * |
| | 1709 | * @uses wp_set_object_terms() |
| | 1710 | * @param array $args Method parameters. Contains: |
| | 1711 | * - int $blog_id |
| | 1712 | * - string $username |
| | 1713 | * - string $password |
| | 1714 | * - int $post_id |
| | 1715 | * - array $content_struct contains term_ids with taxonomy as keys |
| | 1716 | * @return boolean true |
| | 1717 | */ |
| | 1718 | function wp_setPostTerms( $args ) { |
| | 1719 | |
| | 1720 | $this->escape( $args ); |
| | 1721 | |
| | 1722 | $blog_ID = (int) $args[0]; |
| | 1723 | $username = $args[1]; |
| | 1724 | $password = $args[2]; |
| | 1725 | $post_ID = (int) $args[3]; |
| | 1726 | $content_struct = $args[4]; |
| | 1727 | $append = $args[5] ? true : false; |
| | 1728 | |
| | 1729 | |
| | 1730 | if ( ! $user = $this->login( $username, $password ) ) |
| | 1731 | return $this->error; |
| | 1732 | |
| | 1733 | $post = wp_get_single_post( $post_ID, ARRAY_A ); |
| | 1734 | if ( empty( $post['ID'] ) ) |
| | 1735 | return new IXR_Error( 404, __( 'Invalid post ID.' ) ); |
| | 1736 | |
| | 1737 | $post_type = get_post_type_object( $post['post_type'] ); |
| | 1738 | |
| | 1739 | if( ! current_user_can( $post_type->cap->edit_post , $post_ID ) ) |
| | 1740 | return new IXR_Error( 401, __( 'Sorry, You are not allowed to edit this post.' )); |
| | 1741 | |
| | 1742 | $post_type_taxonomies = get_object_taxonomies( $post['post_type'] ); |
| | 1743 | |
| | 1744 | $taxonomies = array_keys( $content_struct ); |
| | 1745 | |
| | 1746 | // validating term ids |
| | 1747 | foreach( $taxonomies as $taxonomy ) { |
| | 1748 | |
| | 1749 | if( ! in_array( $taxonomy , $post_type_taxonomies ) ) |
| | 1750 | return new IXR_Error( 401, __( 'Sorry, one of the given taxonomy is not supported by the post type.' )); |
| | 1751 | |
| | 1752 | $term_ids = $content_struct[ $taxonomy ]; |
| | 1753 | foreach ( $term_ids as $term_id) { |
| | 1754 | |
| | 1755 | $term = get_term( $term_id, $taxonomy ); |
| | 1756 | |
| | 1757 | if ( is_wp_error( $term ) ) |
| | 1758 | return new IXR_Error( 500, $term->get_error_message() ); |
| | 1759 | |
| | 1760 | if ( ! $term ) |
| | 1761 | return new IXR_Error( 401, __( 'Invalid term ID' ) ); |
| | 1762 | |
| | 1763 | } |
| | 1764 | |
| | 1765 | } |
| | 1766 | |
| | 1767 | foreach( $taxonomies as $taxonomy ) { |
| | 1768 | |
| | 1769 | $term_ids = $content_struct[ $taxonomy ]; |
| | 1770 | $term_ids = array_map( 'intval', $term_ids ); |
| | 1771 | $term_ids = array_unique( $term_ids ); |
| | 1772 | wp_set_object_terms( $post_ID , $term_ids, $taxonomy , $append); |
| | 1773 | |
| | 1774 | } |
| | 1775 | |
| | 1776 | return true; |
| | 1777 | |
| | 1778 | } |
| | 1779 | |