forked from Automattic/media-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mexp-keyring-user-creds.php
65 lines (48 loc) · 1.89 KB
/
mexp-keyring-user-creds.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
Plugin Name: MEXP Keyring Credentials
Description: MEXP Keyring Credentials
Version: 1.0
Author: Michael Blouin, Automattic
Author URI: http://automattic.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
add_filter( 'mexp_instagram_user_credentials', 'mexp_instagram_user_credentials_callback' );
function mexp_instagram_user_credentials_callback( $credentials ) {
if ( ! class_exists( 'Keyring') ) {
return $credentials;
}
// Check that the instagram service is setup
$keyring = Keyring::init()->get_service_by_name( 'instagram' );
if ( is_null( $keyring ) ) {
return $credentials;
}
$keyring_store = Keyring::init()->get_token_store();
// Hacky time, Keyring is designed to handle requests, but we're just stealing its access_token.
if ( method_exists( $keyring_store, 'get_tokens_by_user' ) ) {
// The wpcom version uses the get_tokens_by_user method
$users_tokens = $keyring_store->get_tokens_by_user( get_current_user_id() );
if ( in_array( 'instagram', $users_tokens ) ) {
$credentials['access_token'] = $users_tokens['instagram'][0]->token;
}
} elseif ( method_exists( $keyring_store, 'get_tokens' ) ) {
// The released version uses the get_tokens method
$users_tokens = $keyring_store->get_tokens(
array(
'service' => 'instagram',
'user_id' => get_current_user_id(),
)
);
if ( count( $users_tokens ) > 0 ) {
$credentials['access_token'] = $users_tokens[0]->token;
}
}
return $credentials;
}