GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9cd7e5...74e7fc )
by Chris
16:50
created

PPP_Linkedin   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 178
rs 10
c 0
b 0
f 0
ccs 0
cts 95
cp 0
wmc 25
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B ppp_load_linkedin() 0 25 6
C ppp_initialize_linkedin() 0 61 12
A ppp_get_linkedin_auth_url() 0 17 2
A ppp_linkedin_share() 0 37 3
A ppp_linkedin_profile() 0 9 1
1
<?php
2
3
// Exit if accessed directly
4
if ( !defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Linkedin Class
10
 *
11
 * Handles all linkedin functions
12
 *
13
 */
14
if( !class_exists( 'PPP_Linkedin' ) ) {
15
16
	class PPP_Linkedin {
17
18
		var $linkedin;
19
20
		public function __construct(){
21
			ppp_maybe_start_session();
22
		}
23
24
		/**
25
		 * Include Linkedin Class
26
		 *
27
		 * Handles to load linkedin class
28
		 */
29
		public function ppp_load_linkedin() {
30
31
				if( !class_exists( 'LinkedIn' ) ) {
32
					require_once ( PPP_PATH . '/includes/libs/linkedin/linkedin_oAuth.php' );
33
				}
34
35
36
				ppp_set_social_tokens();
37
38
				if ( ! defined( 'LINKEDIN_KEY' ) || ! defined( 'LINKEDIN_SECRET' ) ) {
39
					return false;
40
				}
41
42
				global $ppp_social_settings;
43
				$config = array( 'appKey' => LINKEDIN_KEY, 'appSecret' => LINKEDIN_SECRET );
44
				if ( isset( $ppp_social_settings['linkedin']->access_token ) ) {
45
					$config['accessToken'] = $ppp_social_settings['linkedin']->access_token;
46
				}
47
48
				if ( !$this->linkedin ) {
49
					$this->linkedin = new LinkedIn( $config );
50
				}
51
52
				return true;
53
		}
54
55
		/**
56
		 * Initializes Linkedin API
57
		 *
58
		 */
59
		public function ppp_initialize_linkedin() {
60
			$linkedin = $this->ppp_load_linkedin();
61
62
			//when user is going to logged in and verified successfully session will create
63
			if ( isset( $_REQUEST['li_access_token'] ) && isset( $_REQUEST['expires_in'] ) ) {
64
65
				$access_token = $_REQUEST['li_access_token'];
66
				$expires_in   = $_REQUEST['expires_in'];
67
68
			} elseif ( isset( $_GET['state'] ) && strpos( $_GET['state'], 'ppp-local-keys-li' ) !== false ) {
69
				$access_code = isset( $_GET['code'] ) ? $_GET['code'] : false;
70
71
				if ( empty( $access_code ) ) {
72
					return;
73
				}
74
75
				$params  = 'grant_type=authorization_code&client_id=' . LINKEDIN_KEY;
76
				$params .= '&client_secret=' . LINKEDIN_SECRET;
77
				$params .= '&code=' . $access_code;
78
				$params .= '&redirect_uri=' . admin_url( 'admin.php?page=ppp-social-settings' );
79
				$url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . $params;
80
				$response = json_decode( wp_remote_retrieve_body( wp_remote_post( $url ) ) );
81
82
				$access_token = isset( $response->access_token ) ? $response->access_token : false;
83
				$expires_in   = isset( $response->expires_in ) ? $response->expires_in : false;
84
85
			}
86
87
			if ( ! empty( $access_token ) && ! empty( $expires_in ) ) {
88
				global $ppp_social_settings;
89
				$ppp_social_settings = get_option( 'ppp_social_settings' );
90
91
				//check linkedin class is loaded or not
92
				if( !$linkedin ) {
93
					return false;
94
				}
95
96
				$data = new stdClass();
97
				$data->access_token = $access_token;
98
99
				$expires_in = (int) $expires_in;
100
				$data->expires_on = current_time( 'timestamp' ) + $expires_in;
101
102
				update_option( '_ppp_linkedin_refresh', current_time( 'timestamp' ) + round( $expires_in/1.25 ) );
103
104
				$ppp_social_settings['linkedin'] = $data;
105
				update_option( 'ppp_social_settings', $ppp_social_settings );
106
				// Now that we have a valid auth, get some user info
107
				$user_info = json_decode( $this->ppp_linkedin_profile( $data->access_token ) );
108
109
				$ppp_social_settings['linkedin']->id        = $user_info->id;
110
				$ppp_social_settings['linkedin']->firstName = $user_info->localizedFirstName;
111
				$ppp_social_settings['linkedin']->lastName  = $user_info->localizedLastName;
112
113
				update_option( 'ppp_social_settings', $ppp_social_settings );
114
115
				$url = remove_query_arg( array( 'li_access_token' , 'expires_in' ) );
116
				wp_redirect( $url );
117
				die();
118
			}
119
		}
120
121
		/**
122
		 * Get auth url for linkedin
123
		 *
124
		 */
125
		public function ppp_get_linkedin_auth_url ( $return_url ) {
126
127
			if ( ! PPP_LOCAL_TOKENS ) {
128
				$base_url = 'https://postpromoterpro.com/?ppp-social-auth';
129
				$url  = $base_url . '&ppp-service=li&ppp-license-key=' . trim( get_option( '_ppp_license_key' ) );
130
				$url .= '&nocache';
131
				$url .= '&return_url=' . esc_url( $return_url );
132
			} else {
133
				$url  = 'https://www.linkedin.com/uas/oauth2/authorization?response_type=code';
134
				$url .= '&client_id=' . LINKEDIN_KEY;
135
				$url .= '&scope=w_share%20r_basicprofile';
136
				$url .= '&state=ppp-local-keys-li';
137
				$url .= '&redirect_uri=' . $return_url;
138
			}
139
140
			return $url;
141
		}
142
143
		/**
144
		 * Share somethign on linkedin
145
		 */
146
		public function ppp_linkedin_share( $args ) {
147
			if ( empty( $args ) ) {
148
				return false;
149
			}
150
151
			$this->ppp_load_linkedin();
152
			global $ppp_social_settings;
153
			$li_user_info = $ppp_social_settings['linkedin'];
154
			$url          = 'https://api.linkedin.com/v2/shares/';
155
156
			$share = array(
157
				'content' => array(
158
					'contentEntities' => array(
159
						array(
160
							'entityLocation' => $args['submitted-url'],
161
						)
162
					),
163
					'description' => $args['description'],
164
					'title' => $args['title'],
165
				),
166
				'owner' => 'urn:li:person:' . $li_user_info->id,
167
			);
168
169
170
			if ( $args['submitted-image-url'] !== false ) {
171
				$share['content']['contentEntities'][0]['thumbnails'] = array(
172
					array(
173
						'resolvedUrl' => $args['submitted-image-url'],
174
					)
175
				);
176
			}
177
178
			$headers = array( 'X-Restli-Protocol-Version' => '2.0.0', 'Authorization' => 'Bearer ' . $ppp_social_settings['linkedin']->access_token, 'Content-Type' => 'application/json' );
179
			$request = wp_remote_post( $url, array( 'httpversion' => '1.1', 'timeout' => '30', 'headers' => $headers, 'body' => json_encode( $share ) ) );
180
181
			return json_decode( wp_remote_retrieve_body( $request ) );
182
		}
183
184
		public function ppp_linkedin_profile( $access_token ) {
185
			$url = 'https://api.linkedin.com/v2/me';
186
187
			$headers = array( 'Authorization' => 'Bearer ' . $access_token );
188
189
			$request = wp_remote_get( $url, array( 'headers' => $headers ) );
190
			$data = wp_remote_retrieve_body( $request );
191
			return $data;
192
		}
193
	}
194
195
}
196