From ff3acd0a697b249cf16d9de4630c72560e005139 Mon Sep 17 00:00:00 2001 From: Bruno Meilick Date: Sat, 8 Apr 2017 16:56:41 +0200 Subject: [PATCH] removed need for account and userid when used without user Signed-off-by: Bruno Meilick --- kirby-instagram-api.php | 41 +++++++++++++++++++++++++---------------- package.json | 2 +- readme.md | 38 +++++++++++++++++++++++++------------- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/kirby-instagram-api.php b/kirby-instagram-api.php index 2592251..d381d1e 100644 --- a/kirby-instagram-api.php +++ b/kirby-instagram-api.php @@ -65,29 +65,38 @@ function instagramapi($user, $endpoint, $snippet = '', $params = []) { $endpoint = trim($endpoint); $endpoint = rtrim($endpoint,'/'); if(substr($endpoint, 0, 1) != '/') $endpoint = '/'.$endpoint; - + // PARAMS if(!$params || gettype($params) != 'array') { $params = array(); } - // USER + // TOKEN $userInstagram = null; - if(is_string($user)) { - if(str::contains($user, ' ')) { - $iad = explode(' ', $user); // see $account_ID_Token - if(count($iad) == 3) { - $userInstagram = [ - 'account' => $iad[0], - 'userid' => $iad[1], - 'token' => $iad[2], - ]; - } + $token = c::get('plugin.instagram-api.token', ''); + if($token && strlen(trim($token)) > 0) { + $userInstagram = [ + 'account' => '', + 'userid' => '', + 'token' => $token, + ]; + $user = null; + } + + // USER + if($user && is_string($user)) { + if($tryUser = site()->user($user)) { + $user = $tryUser; } else { - $user = site()->user($user); + $userInstagram = [ + 'account' => '', + 'userid' => '', + 'token' => $token, + ]; + $user = null; } } - if(is_a($user, 'User')) { + if($user && is_a($user, 'User')) { if($iad = $user->instagramapi()) { $iad = explode(' ', $iad); // see $account_ID_Token if(count($iad) == 3) { @@ -99,10 +108,10 @@ function instagramapi($user, $endpoint, $snippet = '', $params = []) { } } } - if(is_array($user)) { + if($user && is_array($user)) { $userInstagram = $user; } - + if(!$userInstagram || !is_array($userInstagram)) { return 'Invalid User.'; } diff --git a/package.json b/package.json index d018bf5..9a19354 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "kirby-instagram-api", "description": "Kirby CMS User-Field, Tag and Page Method to access Instagram API Endpoints. This is a boilerplate to create your own Instagram embeds.", - "version": "0.7.0", + "version": "0.7.1", "type": "kirby-plugin", "license": "Commercial", "author": "Bruno Meilick" diff --git a/readme.md b/readme.md index 2bac321..16d78b5 100644 --- a/readme.md +++ b/readme.md @@ -152,26 +152,34 @@ Of course you can use the Page or Site Methods directly. Take a look at the [exa ### Use without extending a Kirby User (v0.7+) -If you already have `account`, `userid` and `token` you can use this plugin without its Kirby User Field. Like it was suggested by @olach in [Issue #1](https://github.com/bnomei/kirby-instagram-api/issues/1). - -Use the Tag with `space` seperated values. +If you already have a `token` you can use this plugin without its Kirby User Field. Like it was suggested by @olach in [Issue #1](https://github.com/bnomei/kirby-instagram-api/issues/1). Just use your `token` instead of the username. ``` -(instagramapi: ACCOUNT USERID TOKEN endpoint: users/self/media/recent snippet: ia-example-media) +(instagramapi: TOKEN endpoint: users/self/media/recent snippet: ia-example-media) ``` ```php -// as array -$logindata = [ - 'account' => 'ACCOUNT', - 'userid' => 'USERID', - 'token' => 'TOKEN', -]; -// or as string -$logindata = 'ACCOUNT USERID TOKEN'; -$result = $page->instagramapi($logindata, 'users/self/media/recent'); +$result = $page->instagramapi(TOKEN, 'users/self/media/recent'); foreach($result['data'] as $data) { /*...*/ } +``` +If you set the token in you `site/config/config.php` it will override all other means. + +```php +c::set('plugin.instagram-api.token', 'TOKEN'); +``` + +``` +(instagramapi: DoesNotMatter endpoint: users/self/media/recent snippet: ia-example-media) +``` + +```php +// suggested +$result = $page->instagramapi(c::get('plugin.instagram-api.token'), 'users/self/media/recent'); +// or even +$result = $page->instagramapi('DoesNotMatter', 'users/self/media/recent'); +$result = $page->instagramapi(null, 'users/self/media/recent'); +foreach($result['data'] as $data) { /*...*/ } ``` ## Other Setting @@ -188,6 +196,10 @@ You can set these in your `site/config/config.php`. ### plugin.instagram-api.client-secret - get it from [Instagram Deverloper](https://www.instagram.com/developer/clients/manage/) +### plugin.instagram-api.token +- default: '' +- set this if you want to ommit specifying the token. + ### plugin.instagram-api.tag.endpoint - default: '' - set this if you want to ommit specifying the endpoint in the tag.