-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
169 lines (124 loc) · 4.44 KB
/
Client.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Instagram_Helper;
/**
*
*/
class Client {
private $return_url;
private static $notices;
private $config = array(
'user_name' => '',
'client_id' => '',
'client_secret' => '',
);
public $carbon_config_fields = array(
'user_name' => 'crb_instagram_username',
'hashtags' => 'crb_instagram_hashtags',
'client_id' => 'crb_instagram_client_id',
'client_secret' => 'crb_instagram_client_secret',
'redirect_uri' => 'crb_instagram_redirect_uri',
);
function __construct( $config_fields ){
if ( isset( $config_fields[ 'enable_carbon_support' ] ) && $config_fields[ 'enable_carbon_support' ] === true ) {
$this->config = $this->carbon_config_fields;
$this->return_url = admin_url( '/admin.php?page=crbn-instagram-settings.php' );
foreach ( $this->config as &$option ) {
$option = get_option( $option );
}
} else {
$this->config = $config_fields;
$this->config[ 'redirect_uri' ] = $this->get_redirect_uri();
$this->return_url = admin_url();
}
if ( empty( $this->config ) ) {
return;
}
$this->get_user_id();
self::$notices = new Notice_Manager();
add_action( 'wp_ajax_insta_code_detection', array( $this, 'get_access_token' ) );
add_action( 'wp_ajax_insta_delete_token', array( $this, 'delete_access_token' ) );
}
public function get_redirect_uri(){
$url = admin_url('/admin-ajax.php?action=insta_code_detection');
return $url;
}
public function generate_authentication_url(){
$base = 'https://api.instagram.com/oauth/authorize/';
$params = array(
'client_id' => $this->config[ 'client_id' ],
'redirect_uri' => $this->config[ 'redirect_uri' ],
'response_type' => 'code',
);
$url = add_query_arg( $params, $base );
return $url;
}
public function generate_delete_token_url(){
$url = admin_url( '/admin-ajax.php?action=insta_delete_token' );
return $url;
}
public function get_access_token() {
if ( !isset( $_GET[ 'code' ] ) ) {
return;
}
$code = $_GET[ 'code' ];
$url = ' https://api.instagram.com/oauth/access_token';
$params = array(
'client_id' => $this->config[ 'client_id' ],
'client_secret' => $this->config[ 'client_secret' ],
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->config[ 'redirect_uri' ],
);
$token_request = wp_remote_post( $url, array( 'body' => $params ) );
if ( is_wp_error( $token_request ) || ( $token_request[ 'response' ][ 'code' ] !== 200 ) ) {
if ( is_wp_error( $token_request ) ) {
self::$notices->add_notice( 'error', $token_request->get_error_message() );
} else {
self::$notices->add_notice( 'error', __( 'Something went wrong. Please check your configuration and try again.', 'crb' ) );
}
wp_redirect( $this->return_url );
exit;
}
$request_body = json_decode( $token_request[ 'body' ] );
if ( isset ( $request_body->access_token ) ) {
update_option( 'crb_instagram_access_token', $request_body->access_token );
self::$notices->add_notice( 'success', __( 'Instagram authentication completed successfully!', 'crb' ));
wp_redirect( $this->return_url );
}
exit;
}
public function delete_access_token(){
delete_option( 'crb_instagram_access_token' );
self::$notices->add_notice( 'success', __( 'Instagram access token deleted successfully!', 'crb' ));
wp_redirect( $this->return_url );
exit;
}
private function get_user_id(){
$transient = 'crb_cached_user_id_for_' . $this->config[ 'user_name' ];
$cached_user_id = get_transient( $transient );
if ( $cached_user_id ) {
update_option( 'crb_instagram_user_id', $cached_user_id );
set_transient( $transient, $cached_user_id, 24 * HOUR_IN_SECONDS );
return;
}
$base = 'https://api.instagram.com/v1/users/search';
$params = array(
'q' => $this->config[ 'user_name' ],
'client_id' => $this->config[ 'client_id' ],
);
$url = add_query_arg( $params, $base );
$user_id_request = wp_remote_get( $url );
if ( is_wp_error( $user_id_request ) || ( $user_id_request[ 'response' ][ 'code' ] !== 200 ) ) {
return;
}
$response_data = json_decode( $user_id_request[ 'body' ] );
foreach ( $response_data->data as $entry ) {
if ( $entry->username !== $this->config[ 'user_name' ] ) {
continue;
}
set_transient( $transient, $entry->id, 24 * HOUR_IN_SECONDS );
update_option( 'crb_instagram_user_id', $entry->id );
break;
}
}
}