1 | <?php |
||
16 | class Pins extends EntityProvider |
||
17 | { |
||
18 | use Searchable, |
||
19 | CanBeDeleted, |
||
20 | SendsMessages, |
||
21 | TryIt, |
||
22 | CanBeShared; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $loginRequiredFor = [ |
||
28 | 'like', |
||
29 | 'feed', |
||
30 | 'copy', |
||
31 | 'move', |
||
32 | 'repin', |
||
33 | 'unLike', |
||
34 | 'create', |
||
35 | 'activity', |
||
36 | 'analytics', |
||
37 | 'visualSimilar', |
||
38 | ]; |
||
39 | |||
40 | protected $searchScope = 'pins'; |
||
41 | protected $entityIdName = 'id'; |
||
42 | |||
43 | protected $messageEntityName = 'pin'; |
||
44 | |||
45 | protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN; |
||
46 | |||
47 | /** |
||
48 | * Likes pin with current ID. |
||
49 | * @param string $pinId |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function like($pinId) |
||
53 | { |
||
54 | return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Removes your like from pin with current ID. |
||
59 | * @param string $pinId |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function unLike($pinId) |
||
66 | |||
67 | /** |
||
68 | * Create a pin. Returns created pin info. |
||
69 | * |
||
70 | * @param string $imageUrl |
||
71 | * @param int $boardId |
||
72 | * @param string $description |
||
73 | * @param string $link |
||
74 | * @return array |
||
75 | */ |
||
76 | public function create($imageUrl, $boardId, $description = '', $link = '') |
||
77 | { |
||
78 | // Upload image if first argument is not an url |
||
79 | if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) { |
||
80 | $imageUrl = $this->upload($imageUrl); |
||
81 | } |
||
82 | |||
83 | $requestOptions = [ |
||
84 | 'method' => 'scraped', |
||
85 | 'description' => $description, |
||
86 | 'link' => empty($link) ? $imageUrl : $link, |
||
87 | 'image_url' => $imageUrl, |
||
88 | 'board_id' => $boardId, |
||
89 | ]; |
||
90 | |||
91 | $this->post(UrlBuilder::RESOURCE_CREATE_PIN, $requestOptions); |
||
92 | |||
93 | return $this->response->getResponseData(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Edit pin by ID. You can move pin to a new board by setting this board id. |
||
98 | * |
||
99 | * @param int $pindId |
||
100 | * @param string $description |
||
101 | * @param string $link |
||
102 | * @param int|null $boardId |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function edit($pindId, $description = '', $link = '', $boardId = null) |
||
106 | { |
||
107 | $requestOptions = ['id' => $pindId]; |
||
108 | |||
109 | if(!empty($description)) { |
||
110 | $requestOptions['description'] = $description; |
||
111 | } |
||
112 | |||
113 | if ($boardId !== null) { |
||
114 | $requestOptions['board_id'] = $boardId; |
||
115 | } |
||
116 | |||
117 | if (!empty($link)) { |
||
118 | $requestOptions['link'] = stripslashes($link); |
||
119 | } |
||
120 | |||
121 | return $this->post(UrlBuilder::RESOURCE_UPDATE_PIN, $requestOptions); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Moves pin to a new board |
||
126 | * |
||
127 | * @param int $pinId |
||
128 | * @param int $boardId |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function moveToBoard($pinId, $boardId) |
||
132 | { |
||
133 | return $this->edit($pinId, '', '', $boardId); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Make a repin. |
||
138 | * |
||
139 | * @param int $repinId |
||
140 | * @param int $boardId |
||
141 | * @param string $description |
||
142 | * @return array |
||
143 | */ |
||
144 | public function repin($repinId, $boardId, $description = '') |
||
145 | { |
||
146 | $requestOptions = [ |
||
147 | 'board_id' => $boardId, |
||
148 | 'description' => stripslashes($description), |
||
149 | 'link' => '', |
||
150 | 'is_video' => null, |
||
151 | 'pin_id' => $repinId, |
||
152 | ]; |
||
153 | |||
154 | $this->post(UrlBuilder::RESOURCE_REPIN, $requestOptions); |
||
155 | |||
156 | return $this->response->getResponseData(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get information of a pin by PinID. |
||
161 | * |
||
162 | * @param string $pinId |
||
163 | * @return array|bool |
||
164 | */ |
||
165 | public function info($pinId) |
||
166 | { |
||
167 | $requestOptions = [ |
||
168 | 'id' => $pinId, |
||
169 | 'field_set_key' => 'detailed', |
||
170 | ]; |
||
171 | |||
172 | return $this->get(UrlBuilder::RESOURCE_PIN_INFO, $requestOptions); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will |
||
177 | * return recent Pins from flickr.com |
||
178 | * |
||
179 | * @param string $source |
||
180 | * @param int $limit |
||
181 | * @return Pagination |
||
182 | */ |
||
183 | public function fromSource($source, $limit = Pagination::DEFAULT_LIMIT) |
||
184 | { |
||
185 | $data = ['domain' => $source]; |
||
186 | |||
187 | return $this->paginate(UrlBuilder::RESOURCE_DOMAIN_FEED, $data, $limit); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get the latest pin activity with pagination. |
||
192 | * |
||
193 | * @param string $pinId |
||
194 | * @param int $limit |
||
195 | * @return Pagination |
||
196 | */ |
||
197 | public function activity($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
198 | { |
||
199 | return $this->getAggregatedActivity($pinId, [], $limit); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $pinId |
||
204 | * @param array $additionalData |
||
205 | * @param int $limit |
||
206 | * @return Pagination |
||
207 | */ |
||
208 | protected function getAggregatedActivity($pinId, $additionalData = [], $limit) |
||
209 | { |
||
210 | $aggregatedPinId = $this->getAggregatedPinId($pinId); |
||
211 | |||
212 | if ($aggregatedPinId === null) { |
||
213 | return new Pagination(); |
||
214 | } |
||
215 | |||
216 | $additionalData['aggregated_pin_data_id'] = $aggregatedPinId; |
||
217 | |||
218 | return $this->paginate(UrlBuilder::RESOURCE_ACTIVITY, $additionalData, $limit); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get pins from user's feed |
||
223 | * |
||
224 | * @param int $limit |
||
225 | * @return Pagination |
||
226 | */ |
||
227 | public function feed($limit = Pagination::DEFAULT_LIMIT) |
||
228 | { |
||
229 | return $this->paginate(UrlBuilder::RESOURCE_USER_FEED, [], $limit); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $pinId |
||
234 | * @param int $limit |
||
235 | * @return Pagination |
||
236 | */ |
||
237 | public function related($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
238 | { |
||
239 | $requestData = [ |
||
240 | 'pin' => $pinId, |
||
241 | 'add_vase' => true, |
||
242 | ]; |
||
243 | |||
244 | return $this->paginate(UrlBuilder::RESOURCE_RELATED_PINS, $requestData, $limit); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Copy pins to board |
||
249 | * |
||
250 | * @param array|string $pinIds |
||
251 | * @param int $boardId |
||
252 | * @return bool|Response |
||
253 | */ |
||
254 | public function copy($pinIds, $boardId) |
||
255 | { |
||
256 | return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Delete pins from board. |
||
261 | * |
||
262 | * @param string|array $pinIds |
||
263 | * @param int $boardId |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function deleteFromBoard($pinIds, $boardId) |
||
267 | { |
||
268 | return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Move pins to board |
||
273 | * |
||
274 | * @param string|array $pinIds |
||
275 | * @param int $boardId |
||
276 | * @return bool|Response |
||
277 | */ |
||
278 | public function move($pinIds, $boardId) |
||
282 | |||
283 | /** |
||
284 | * @param string $pinId |
||
285 | * @param int $limit |
||
286 | * @return Pagination |
||
287 | */ |
||
288 | public function visualSimilar($pinId, $limit = Pagination::DEFAULT_LIMIT) |
||
289 | { |
||
290 | $data = [ |
||
291 | 'pin_id' => $pinId, |
||
292 | // Some magic numbers, I have no idea about them |
||
293 | 'crop' => [ |
||
294 | 'x' => 0.16, |
||
295 | 'y' => 0.16, |
||
296 | 'w' => 0.66, |
||
297 | 'h' => 0.66, |
||
298 | 'num_crop_actions' => 1, |
||
299 | ], |
||
300 | 'force_refresh' => true, |
||
301 | 'keep_duplicates' => false, |
||
302 | ]; |
||
303 | |||
304 | return $this->paginate(UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS, $data, $limit); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Saves the pin original image to the specified path. On success |
||
309 | * returns full path to saved image. Otherwise returns false. |
||
310 | * |
||
311 | * @param string $pinId |
||
312 | * @param string $path |
||
313 | * @return false|string |
||
314 | */ |
||
315 | public function saveOriginalImage($pinId, $path) |
||
327 | |||
328 | /** |
||
329 | * @param string $query |
||
330 | * @param int $limit |
||
331 | * @return Pagination |
||
332 | */ |
||
333 | public function searchInMyPins($query, $limit = Pagination::DEFAULT_LIMIT) |
||
334 | { |
||
335 | return $this->paginateCustom( |
||
336 | function () use ($query) { |
||
337 | return $this->execSearchRequest($query, 'my_pins'); |
||
338 | } |
||
339 | )->take($limit); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Returns trending pins from http://pinterest.com/discover page. Uses topic id, that can be received |
||
344 | * from $bot->topics->explore() method. |
||
345 | * |
||
346 | * @param string $topicId |
||
347 | * @param int $limit |
||
348 | * @return Pagination |
||
349 | */ |
||
350 | public function explore($topicId, $limit = Pagination::DEFAULT_LIMIT) |
||
361 | |||
362 | /** |
||
363 | * Get pin analytics, like numbers of clicks, views and repins |
||
364 | * @param $pinId |
||
365 | * @return array|bool|Response |
||
366 | */ |
||
367 | public function analytics($pinId) |
||
374 | |||
375 | /** |
||
376 | * Calls Pinterest API to like or unlike Pin by ID. |
||
377 | * |
||
378 | * @param string $pinId |
||
379 | * @param string $resourceUrl |
||
380 | * @return bool |
||
381 | */ |
||
382 | protected function likePinMethodCall($pinId, $resourceUrl) |
||
386 | |||
387 | /** |
||
388 | * @param string $pinId |
||
389 | * @return int|null |
||
390 | */ |
||
391 | protected function getAggregatedPinId($pinId) |
||
399 | |||
400 | /** |
||
401 | * @param string|array $pinIds |
||
402 | * @param int $boardId |
||
403 | * @param string $editUrl |
||
404 | * @return bool |
||
405 | */ |
||
406 | protected function bulkEdit($pinIds, $boardId, $editUrl) |
||
415 | } |
||
416 |