|
26 | 26 |
|
27 | 27 | use FacebookAds\Api; |
28 | 28 | use FacebookAds\Object\Fields\CustomAudienceFields; |
| 29 | +use FacebookAds\Object\Values\CustomAudienceTypes; |
29 | 30 |
|
30 | 31 | class CustomAudience extends AbstractCrudObject { |
31 | 32 |
|
@@ -84,28 +85,77 @@ protected function getEndpoint() { |
84 | 85 | * Add users to the AdCustomAudiences. There is no max on the total number of |
85 | 86 | * users that can be added to an audience, but up to 10000 users can be added |
86 | 87 | * at a given time. |
| 88 | + * Hash type should only be used on email and phone |
87 | 89 | * |
88 | | - * @param array $users Array of user info |
| 90 | + * @param array $users |
| 91 | + * @param string $type |
| 92 | + * @param string $hash_type the algorithm to use when hasing |
89 | 93 | * @return boolean Returns true on success |
90 | 94 | */ |
91 | | - public function addUsers($users) { |
| 95 | + public function addUsers(array $users, $type, $hash_type = null) { |
| 96 | + $params = $this->formatParams($users, $type, $hash_type); |
92 | 97 | return $this->getApi()->call( |
93 | 98 | '/'.$this->assureId().'/users', |
94 | 99 | Api::HTTP_METHOD_POST, |
95 | | - array('users' => $users))->getResponse(); |
| 100 | + $params)->getResponse(); |
96 | 101 | } |
97 | 102 |
|
98 | 103 | /** |
99 | 104 | * Delete users from AdCustomAudiences |
| 105 | + * Hash type should only be used on email and phone |
100 | 106 | * |
101 | | - * @param array $users Array of user info |
| 107 | + * @param array $users |
| 108 | + * @param string $type |
| 109 | + * @param string $hash_type the algorithm to use when hasing |
102 | 110 | * @return boolean Returns true on success |
103 | 111 | */ |
104 | | - public function removeUsers($users) { |
| 112 | + public function removeUsers(array $users, $type, $hash_type = null) { |
| 113 | + $params = $this->formatParams($users, $type, $hash_type); |
105 | 114 | return $this->getApi()->call( |
106 | 115 | '/'.$this->assureId().'/users', |
107 | 116 | Api::HTTP_METHOD_DELETE, |
108 | | - array('users' => $users))->getResponse(); |
| 117 | + $params)->getResponse(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Remove list of users decided to opt-out from all custom audiences |
| 122 | + * Hash type should only be used on email and phone |
| 123 | + * |
| 124 | + * @param array $users |
| 125 | + * @param string $type |
| 126 | + * @param string $hash_type the algorithm to use when hasing |
| 127 | + * @return boolean Returns true on success |
| 128 | + */ |
| 129 | + public function optOutUsers(array $users, $type, $hash_type = null) { |
| 130 | + $params = $this->formatParams($users, $type, $hash_type); |
| 131 | + return $this->getApi()->call( |
| 132 | + '/'.$this->assureParentId().'/usersofanyaudience', |
| 133 | + Api::HTTP_METHOD_DELETE, |
| 134 | + $params)->getResponse(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Take users and format them correctly for the request |
| 139 | + * |
| 140 | + * @param array $users |
| 141 | + * @param string $type |
| 142 | + * @param string $hash_type the algorithm to use when hasing |
| 143 | + */ |
| 144 | + protected function formatParams(array $users, $type, $hash_type = null) { |
| 145 | + $request = array(); |
| 146 | + if ($type == CustomAudienceTypes::EMAIL |
| 147 | + || $type == CustomAudienceTypes::PHONE || $hash_type !== null) { |
| 148 | + $hash_type = is_null($hash_type) ?: self::HASH_TYPE_SHA256; |
| 149 | + $request['hash_type'] = $hash_type; |
| 150 | + } |
| 151 | + |
| 152 | + foreach ($users as $u) { |
| 153 | + if (!is_null($hash_type)) { |
| 154 | + $u = hash(self::HASH_TYPE_SHA256, $u); |
| 155 | + } |
| 156 | + $request['users'][] = array($type => $u); |
| 157 | + } |
| 158 | + return $request; |
109 | 159 | } |
110 | 160 |
|
111 | 161 | /** |
@@ -134,16 +184,4 @@ public function removeSharedAccounts($act_ids) { |
134 | 184 | array('adaccounts' => $act_ids))->getResponse(); |
135 | 185 | } |
136 | 186 |
|
137 | | - /** |
138 | | - * Remove list of users decided to opt-out from all custom audiences |
139 | | - * |
140 | | - * @param array $users Array of user info |
141 | | - * @return boolean Returns true on success |
142 | | - */ |
143 | | - public function optOutUsers($users) { |
144 | | - return $this->getApi()->call( |
145 | | - '/'.$this->assureParentId().'/usersofanyaudience', |
146 | | - Api::HTTP_METHOD_DELETE, |
147 | | - array('users' => $users))->getResponse(); |
148 | | - } |
149 | 187 | } |
0 commit comments