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_Twitter::ppp_verify_twitter_credentials()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
ccs 0
cts 21
cp 0
crap 56
1
<?php
2
3
// Exit if accessed directly
4
if ( !defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Twitter Class
10
 *
11
 * Handles all twitter functions
12
 *
13
 */
14
if( !class_exists( 'PPP_Twitter' ) ) {
15
16
	class PPP_Twitter {
17
18
		var $twitter;
19
20
		public function __construct( $_user_id = 0 ) {
21
			ppp_maybe_start_session();
22
23
			$this->user_id = $_user_id;
0 ignored issues
show
Bug introduced by
The property user_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
		}
25
26
		/**
27
		 * Include Twitter Class
28
		 *
29
		 * Handles to load twitter class
30
		 */
31
		public function ppp_load_twitter() {
32
				if( !class_exists( 'TwitterOAuth' ) ) {
33
					require_once ( PPP_PATH . '/includes/libs/twitter/twitteroauth.php' );
34
				}
35
36
				ppp_set_social_tokens();
37
38
				if ( ! defined( 'PPP_TW_CONSUMER_KEY' ) || ! defined( 'PPP_TW_CONSUMER_SECRET' ) ) {
39
					return false;
40
				}
41
42
				$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET );
43
44
				return true;
45
		}
46
47
		public function revoke_access() {
48
			global $ppp_social_settings;
49
50
			unset( $ppp_social_settings['twitter'] );
51
52
			update_option( 'ppp_social_settings', $ppp_social_settings );
53
		}
54
55
		/**
56
		 * Initializes Twitter API
57
		 *
58
		 */
59
		public function ppp_initialize_twitter() {
60
61
			//when user is going to logged in in twitter and verified successfully session will create
62
			if ( ! empty( $_REQUEST['oauth_verifier'] ) ) {
63
				$ppp_social_settings = get_option( 'ppp_social_settings' );
64
65
				//load twitter class
66
				$twitter = $this->ppp_load_twitter();
67
68
				//check twitter class is loaded or not
69
				if( ! $twitter ) {
70
					return false;
71
				}
72
73
				$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET, $_SESSION['ppp_twt_oauth_token'], $_SESSION['ppp_twt_oauth_token_secret'] );
74
75
				// Request access tokens from twitter
76
				$ppp_tw_access_token = $this->twitter->getAccessToken( $_REQUEST['oauth_verifier'] );
77
78
				//session for verifier
79
				$verifier['oauth_verifier'] = $_REQUEST['oauth_verifier'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$verifier was never initialized. Although not strictly required by PHP, it is generally a good practice to add $verifier = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
80
81
				$_SESSION[ 'ppp_twt_user_cache' ] = $verifier;
82
83
				//getting user data from twitter
84
				$response = $this->twitter->get('account/verify_credentials');
85
86
				//if user data get successfully
87
				if ( $response->id_str ) {
88
89
					$data['user'] = $response;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
					$data['user']->accessToken = $ppp_tw_access_token;
91
92
					$ppp_social_settings['twitter'] = $data;
93
					update_option( 'ppp_social_settings', $ppp_social_settings );
94
95
				}
96
			}
97
		}
98
99
		public function ppp_verify_twitter_credentials() {
100
			$this->ppp_load_twitter();
101
102
			global $ppp_social_settings;
103
			if ( isset( $ppp_social_settings['twitter'] ) ) {
104
105
				$this->twitter = new TwitterOAuth(
106
					PPP_TW_CONSUMER_KEY,
107
					PPP_TW_CONSUMER_SECRET,
108
					$ppp_social_settings['twitter']['user']->accessToken['oauth_token'],
109
					$ppp_social_settings['twitter']['user']->accessToken['oauth_token_secret']
110
				);
111
112
				$response = $this->twitter->get('account/verify_credentials');
113
				if ( is_object( $response ) && property_exists( $response, 'errors' ) && count( $response->errors ) > 0 ) {
114
					foreach ( $response->errors as $error ) {
115
						if ( $error->code == 89 ) { // Expired or revoked tokens
116
							unset( $ppp_social_settings['twitter'] );
117
							update_option( 'ppp_social_settings', $ppp_social_settings );
118
119
							return array( 'error' => __( 'Post Promoter Pro has been removed from your Twitter account. Please reauthorize to continue promoting your content.', 'ppp-txt' ) );
120
						}
121
					}
122
				}
123
			}
124
125
			return true;
126
		}
127
128
		/**
129
		 * Get auth url for twitter
130
		 *
131
		 */
132
		public function ppp_get_twitter_auth_url ( $return_url = '' ) {
133
134
			if ( empty( $return_url ) ) {
135
				$return_url = admin_url( 'admin.php?page=ppp-social-settings' );
0 ignored issues
show
Unused Code introduced by
$return_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
			}
137
			//load twitter class
138
			$twitter = $this->ppp_load_twitter();
139
140
			//check twitter class is loaded or not
141
			if( !$twitter ) {
142
				return false;
143
			}
144
145
			$request_token = $this->twitter->getRequestToken( 'oob' );
146
147
			// If last connection failed don't display authorization link.
148
			switch( $this->twitter->http_code ) {
149
150
			case 200:
151
				$_SESSION['ppp_twt_oauth_token']        = $request_token['oauth_token'];
152
				$_SESSION['ppp_twt_oauth_token_secret'] = $request_token['oauth_token_secret'];
153
154
				$token = $request_token['oauth_token'];
155
				$url = $this->twitter->getAuthorizeURL( $token, NULL );
156
				break;
157
			default:
158
				$url = '';
159
				break;
160
			}
161
			return $url;
162
		}
163
164
		public function ppp_tweet( $message = '', $media = null ) {
165
			if ( empty( $message ) ) {
166
				return false;
167
			}
168
169
			$verify = $this->ppp_verify_twitter_credentials();
170
			if ( $verify === true ) {
171
				$args = array();
172
				if ( ! empty( $media ) ) {
173
					$endpoint = 'statuses/update_with_media';
174
					$args['media[]'] = wp_remote_retrieve_body( wp_remote_get( $media ) );
175
				} else {
176
					$endpoint = 'statuses/update';
177
				}
178
				$args['status'] = $message;
179
180
				return $this->twitter->post( $endpoint, $args, true );
181
			} else {
182
				return false;
183
			}
184
		}
185
186
	}
187
188
}
189