Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Gets all tags currently applied to the user, also update the list of available tags
- *
- * @access public
- * @return array Tags
- */
- public function get_tags( $contact_id ) {
- if ( ! $this->params ) {
- $this->get_params();
- }
- $request = 'https://api.hubapi.com/contacts/v1/contact/vid/' . $contact_id . '/profile';
- $response = wp_remote_get( $request, $this->params );
- if( is_wp_error( $response ) ) {
- return $response;
- }
- $body_json = json_decode( wp_remote_retrieve_body( $response ) );
- $tags = array();
- if( empty( $body_json ) || empty( $body_json->{'list-memberships'} ) ) {
- return $tags;
- }
- foreach( $body_json->{'list-memberships'} as $list ) {
- $tags[] = $list->{'static-list-id'};
- }
- return $tags;
- }
- /**
- * Applies tags to a contact
- *
- * @access public
- * @return bool
- */
- public function apply_tags( $tags, $contact_id ) {
- if ( ! $this->params ) {
- $this->get_params();
- }
- foreach( $tags as $tag ) {
- $params = $this->params;
- $params['body'] = json_encode( array( 'vids' => array( $contact_id ) ) );
- $request = 'https://api.hubapi.com/contacts/v1/lists/' . $tag . '/add';
- $response = wp_remote_post( $request, $params );
- if( is_wp_error( $response ) ) {
- return $response;
- }
- }
- return true;
- }
- /**
- * Removes tags from a contact
- *
- * @access public
- * @return bool
- */
- public function remove_tags( $tags, $contact_id ) {
- if ( ! $this->params ) {
- $this->get_params();
- }
- foreach( $tags as $tag ) {
- $params = $this->params;
- $params['body'] = json_encode( array( 'vids' => array( $contact_id ) ) );
- $request = 'https://api.hubapi.com/contacts/v1/lists/' . $tag . '/remove';
- $response = wp_remote_post( $request, $params );
- if( is_wp_error( $response ) ) {
- return $response;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement