Skip to content

Commit ab95505

Browse files
committed
added pagination for business_discovery
1 parent ceb3ef6 commit ab95505

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

src/Instagram/User/BusinessDiscovery.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,35 +128,69 @@ public function getSelf( $params = array() ) {
128128
* @return void
129129
*/
130130
public function calcNextLink( &$response ) {
131-
if ( isset( $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::AFTER] ) ) { // we have another page
132-
$config = array( // setup config
133-
'user_id' => $response[Fields::BUSINESS_DISCOVERY][Fields::ID],
134-
'access_token' => $this->accessToken
135-
);
131+
if ( isset( $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::BEFORE] ) ) { // we have previous page
132+
// get fields string
133+
$fieldsString = $this->getParams();
134+
135+
// calculate after string with cursor
136+
$snippet = Fields::MEDIA . '.' . Params::BEFORE . '(' . $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::BEFORE] . '){';
137+
138+
// update old fields with cursor
139+
$newFieldsParams = str_replace( Fields::MEDIA . '{', $snippet, $fieldsString );
136140

137-
// create new media
138-
$media = new Media( $config );
141+
// create our media endpoint
142+
$endpoint = '/' . $this->userId . '/';
143+
144+
// create our request
145+
$request = new Request( Request::METHOD_GET, $endpoint, $newFieldsParams, $this->graphVersion, $this->accessToken );
139146

140-
// use params from media class
141-
$params = $media->getParams();
147+
// set paging next to the url for the next request
148+
$response[Fields::PAGING][Params::PREVIOUS] = $request->getUrl();
149+
}
142150

143-
// need the access token
144-
$params['access_token'] = $this->accessToken;
151+
if ( isset( $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::AFTER] ) ) { // we have another page
152+
// get fields string
153+
$fieldsString = $this->getParams();
145154

146-
// set the after cursor
147-
$params[Params::AFTER] = $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::AFTER];
155+
// calculate after string with cursor
156+
$snippet = Fields::MEDIA . '.' . Params::AFTER . '(' . $response[Fields::BUSINESS_DISCOVERY][Fields::MEDIA][Fields::PAGING][Fields::CURSORS][Params::AFTER] . '){';
157+
158+
// update old fields with cursor
159+
$newFieldsParams = str_replace( Fields::MEDIA . '{', $snippet, $fieldsString );
148160

149161
// create our media endpoint
150-
$endpoint = '/' . $media->userId . '/' . Media::ENDPOINT;
162+
$endpoint = '/' . $this->userId . '/';
151163

152164
// create our request
153-
$request = new Request( Request::METHOD_GET, $endpoint, $params, $this->graphVersion, $this->accessToken );
165+
$request = new Request( Request::METHOD_GET, $endpoint, $newFieldsParams, $this->graphVersion, $this->accessToken );
154166

155167
// set paging next to the url for the next request
156168
$response[Fields::PAGING][Params::NEXT] = $request->getUrl();
157169
}
158170
}
159171

172+
/**
173+
* Request previous or next page data.
174+
*
175+
* @param string $page specific page to request.
176+
* @return array of previous or next page data..
177+
*/
178+
public function getMediaPage( $page ) {
179+
// get the page to use
180+
$pageUrl = Params::NEXT == $page ? $this->pagingNextLink : $this->pagingPreviousLink;
181+
182+
// return the response from the request
183+
$mediaPageRequest = $this->sendCustomRequest( $pageUrl );
184+
185+
// calculate the next link for paging
186+
$this->calcNextLink( $mediaPageRequest );
187+
188+
// set prev and next links
189+
$this->setPrevNextLinks( $mediaPageRequest );
190+
191+
return $mediaPageRequest;
192+
}
193+
160194
/**
161195
* Get params for the request.
162196
*

0 commit comments

Comments
 (0)