Advertisement
verygoodplugins

Untitled

Jul 2nd, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1.  
  2.     /**
  3.      * Gets all tags currently applied to the user, also update the list of available tags
  4.      *
  5.      * @access public
  6.      * @return array Tags
  7.      */
  8.  
  9.     public function get_tags( $contact_id ) {
  10.  
  11.         if ( ! $this->params ) {
  12.             $this->get_params();
  13.         }
  14.  
  15.         $request      = 'https://api.hubapi.com/contacts/v1/contact/vid/' . $contact_id . '/profile';
  16.         $response     = wp_remote_get( $request, $this->params );
  17.  
  18.         if( is_wp_error( $response ) ) {
  19.             return $response;
  20.         }
  21.  
  22.         $body_json = json_decode( wp_remote_retrieve_body( $response ) );
  23.  
  24.         $tags = array();
  25.  
  26.         if( empty( $body_json ) || empty( $body_json->{'list-memberships'} ) ) {
  27.             return $tags;
  28.         }
  29.  
  30.         foreach( $body_json->{'list-memberships'} as $list ) {
  31.  
  32.             $tags[] = $list->{'static-list-id'};
  33.  
  34.         }
  35.  
  36.         return $tags;
  37.     }
  38.  
  39.     /**
  40.      * Applies tags to a contact
  41.      *
  42.      * @access public
  43.      * @return bool
  44.      */
  45.  
  46.     public function apply_tags( $tags, $contact_id ) {
  47.  
  48.         if ( ! $this->params ) {
  49.             $this->get_params();
  50.         }
  51.  
  52.         foreach( $tags as $tag ) {
  53.  
  54.             $params = $this->params;
  55.             $params['body'] = json_encode( array( 'vids' => array( $contact_id ) ) );
  56.  
  57.             $request      = 'https://api.hubapi.com/contacts/v1/lists/' . $tag . '/add';
  58.             $response     = wp_remote_post( $request, $params );
  59.  
  60.             if( is_wp_error( $response ) ) {
  61.                 return $response;
  62.             }
  63.  
  64.         }
  65.  
  66.         return true;
  67.  
  68.     }
  69.  
  70.     /**
  71.      * Removes tags from a contact
  72.      *
  73.      * @access public
  74.      * @return bool
  75.      */
  76.  
  77.     public function remove_tags( $tags, $contact_id ) {
  78.  
  79.         if ( ! $this->params ) {
  80.             $this->get_params();
  81.         }
  82.  
  83.         foreach( $tags as $tag ) {
  84.  
  85.             $params = $this->params;
  86.             $params['body'] = json_encode( array( 'vids' => array( $contact_id ) ) );
  87.  
  88.             $request      = 'https://api.hubapi.com/contacts/v1/lists/' . $tag . '/remove';
  89.             $response     = wp_remote_post( $request, $params );
  90.  
  91.             if( is_wp_error( $response ) ) {
  92.                 return $response;
  93.             }
  94.  
  95.         }
  96.  
  97.         return true;
  98.  
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement