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_User::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 6
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_User' ) ) {
15
16
	class PPP_Twitter_User {
17
18
		public function __construct( $_user_id = 0 ) {
19
			ppp_maybe_start_session();
20
			$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...
21
22
			if ( ! empty( $this->user_id ) ) {
23
				$this->verify_credentials();
24
			}
25
		}
26
27
		/**
28
		 * Include Twitter Class
29
		 *
30
		 * Handles to load twitter class
31
		 */
32
		public function load() {
33
				if( !class_exists( 'TwitterOAuth' ) ) {
34
					require_once ( PPP_PATH . '/includes/libs/twitter/twitteroauth.php' );
35
				}
36
37
				ppp_set_social_tokens();
38
39
				if ( ! defined( 'PPP_TW_CONSUMER_KEY' ) || ! defined( 'PPP_TW_CONSUMER_SECRET' ) ) {
40
					return false;
41
				}
42
43
				$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET );
0 ignored issues
show
Bug introduced by
The property twitter 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...
44
45
				return true;
46
		}
47
48
		public function revoke_access() {
49
			delete_user_meta( $this->user_id, '_ppp_twitter_data' );
50
		}
51
52
		/**
53
		 * Initializes Twitter API
54
		 *
55
		 */
56
		public function init() {
57
58
			//when user is going to logged in in twitter and verified successfully session will create
59
			if ( isset( $_REQUEST['oauth_verifier'] ) ) {
60
61
				//load twitter class
62
				$twitter       = $this->load();
0 ignored issues
show
Unused Code introduced by
$twitter 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...
63
				$this->twitter = new TwitterOAuth( PPP_TW_CONSUMER_KEY, PPP_TW_CONSUMER_SECRET, $_SESSION['ppp_user_twt_oauth_token'], $_SESSION['ppp_user_twt_oauth_token_secret'] );
64
65
				// Request access tokens from twitter
66
				$ppp_tw_access_token = $this->twitter->getAccessToken( $_REQUEST['oauth_verifier'] );
67
68
				//session for verifier
69
				$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...
70
				$_SESSION[ 'ppp_twt_user_cache' ] = $verifier;
71
72
				//getting user data from twitter
73
				$response = $this->twitter->get( 'account/verify_credentials' );
74
75
				//if user data get successfully
76
				if ( $response->id_str ) {
77
					$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...
78
					$data['user']->accessToken = $ppp_tw_access_token;
79
80
					update_user_meta( $this->user_id, '_ppp_twitter_data', $data );
81
				}
82
			}
83
		}
84
85
		public function verify_credentials() {
86
			$this->load();
87
88
			$user_settings = get_user_meta( $this->user_id, '_ppp_twitter_data', true );
89
			if ( ! empty( $user_settings ) ) {
90
91
				$this->twitter = new TwitterOAuth(
92
					PPP_TW_CONSUMER_KEY,
93
					PPP_TW_CONSUMER_SECRET,
94
					$user_settings['user']->accessToken['oauth_token'],
95
					$user_settings['user']->accessToken['oauth_token_secret']
96
				);
97
98
				$response = $this->twitter->get('account/verify_credentials');
99
				if ( is_object( $response ) && property_exists( $response, 'errors' ) && count( $response->errors ) > 0 ) {
100
					foreach ( $response->errors as $error ) {
101
						if ( $error->code == 89 ) { // Expired or revoked tokens
102
103
							$this->revoke_access();
104
105
							return array( 'error' => __( 'Post Promoter Pro has been removed from your Twitter account. Please reauthorize to continue promoting your content.', 'ppp-txt' ) );
106
						}
107
					}
108
				}
109
			}
110
111
			return true;
112
		}
113
114
		/**
115
		 * Get auth url for twitter
116
		 *
117
		 */
118
		public function get_auth_url ( $return_url = '' ) {
119
120
			if ( empty( $return_url ) ) {
121
				$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...
122
			}
123
124
			//load twitter class
125
			$twitter       = $this->load();
0 ignored issues
show
Unused Code introduced by
$twitter 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...
126
			$request_token = $this->twitter->getRequestToken( 'oob' );
127
128
			// If last connection failed don't display authorization link.
129
			switch( $this->twitter->http_code ) {
130
				case 200:
131
					$_SESSION['ppp_user_twt_oauth_token']        = $request_token['oauth_token'];
132
					$_SESSION['ppp_user_twt_oauth_token_secret'] = $request_token['oauth_token_secret'];
133
134
					$token = $request_token['oauth_token'];
135
					$url = $this->twitter->getAuthorizeURL( $token, NULL );
136
				break;
137
				default:
138
					$url = '';
139
				break;
140
			}
141
142
			return $url;
143
		}
144
145
		public function send_tweet( $message = '', $media = null ) {
146
			if ( empty( $message ) ) {
147
				return false;
148
			}
149
150
			$verify = $this->verify_credentials();
151
			if ( $verify === true ) {
152
				$args = array();
153
				if ( ! empty( $media ) ) {
154
					$endpoint = 'statuses/update_with_media';
155
					$args['media[]'] = wp_remote_retrieve_body( wp_remote_get( $media ) );
156
				} else {
157
					$endpoint = 'statuses/update';
158
				}
159
				$args['status'] = $message;
160
161
				return $this->twitter->post( $endpoint, $args, true );
162
			} else {
163
				return false;
164
			}
165
		}
166
167
		public function retweet( $tweet_id ) {
168
			if ( empty( $tweet_id ) ) {
169
				return false;
170
			}
171
172
			$verify = $this->verify_credentials();
173
			if ( $verify === true ) {
174
				$endpoint = 'statuses/retweet/' . $tweet_id;
175
176
				return $this->twitter->post( $endpoint, array(), true );
177
			} else {
178
				return false;
179
			}
180
		}
181
182
	}
183
184
}
185