1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Service_Helper { |
4
|
|
|
/** |
5
|
|
|
* @var Jetpack_Service_Helper |
6
|
|
|
**/ |
7
|
|
|
private static $instance = null; |
8
|
|
|
|
9
|
|
|
static function init() { |
10
|
|
|
if ( is_null( self::$instance ) ) { |
11
|
|
|
self::$instance = new Jetpack_Service_Helper; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
return self::$instance; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
private function __construct() { |
18
|
|
|
add_action( 'load-settings_page_sharing', array( $this, 'admin_page_load' ), 9 ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Gets a URL to the public-api actions. Works like WP's admin_url |
23
|
|
|
* |
24
|
|
|
* @param string $service Shortname of a specific service. |
25
|
|
|
* |
26
|
|
|
* @return URL to specific public-api process |
27
|
|
|
*/ |
28
|
|
|
// on WordPress.com this is/calls Keyring::admin_url |
29
|
|
|
static function api_url( $service = false, $params = array() ) { |
30
|
|
|
/** |
31
|
|
|
* Filters the API URL used to interact with WordPress.com. |
32
|
|
|
* |
33
|
|
|
* @module publicize |
34
|
|
|
* |
35
|
|
|
* @since 2.0.0 |
36
|
|
|
* |
37
|
|
|
* @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL. |
38
|
|
|
*/ |
39
|
|
|
$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' ); |
40
|
|
|
|
41
|
|
|
if ( $service ) { |
42
|
|
|
$url = add_query_arg( array( 'service' => $service ), $url ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ( count( $params ) ) { |
46
|
|
|
$url = add_query_arg( $params, $url ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $url; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
static function connect_url( $service_name ) { |
53
|
|
|
return add_query_arg( array( |
54
|
|
|
'action' => 'request', |
55
|
|
|
'service' => $service_name, |
56
|
|
|
'kr_nonce' => wp_create_nonce( 'keyring-request' ), |
57
|
|
|
'nonce' => wp_create_nonce( "keyring-request-$service_name" ), |
58
|
|
|
), menu_page_url( 'sharing', false ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
static function refresh_url( $service_name ) { |
62
|
|
|
return add_query_arg( array( |
63
|
|
|
'action' => 'request', |
64
|
|
|
'service' => $service_name, |
65
|
|
|
'kr_nonce' => wp_create_nonce( 'keyring-request' ), |
66
|
|
|
'refresh' => 1, |
67
|
|
|
'for' => 'publicize', |
68
|
|
|
'nonce' => wp_create_nonce( "keyring-request-$service_name" ), |
69
|
|
|
), admin_url( 'options-general.php?page=sharing' ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
static function disconnect_url( $service_name, $id ) { |
73
|
|
|
return add_query_arg( array( |
74
|
|
|
'action' => 'delete', |
75
|
|
|
'service' => $service_name, |
76
|
|
|
'id' => $id, |
77
|
|
|
'kr_nonce' => wp_create_nonce( 'keyring-request' ), |
78
|
|
|
'nonce' => wp_create_nonce( "keyring-request-$service_name" ), |
79
|
|
|
), menu_page_url( 'sharing', false ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function admin_page_load() { |
83
|
|
|
if ( isset( $_GET['action'] ) ) { |
84
|
|
|
if ( isset( $_GET['service'] ) ) { |
85
|
|
|
$service_name = $_GET['service']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
switch ( $_GET['action'] ) { |
89
|
|
|
case 'error': |
90
|
|
|
add_action( 'pre_admin_screen_sharing', array( $this, 'display_connection_error' ), 9 ); |
91
|
|
|
break; |
92
|
|
|
|
93
|
|
|
case 'request': |
94
|
|
|
check_admin_referer( 'keyring-request', 'kr_nonce' ); |
95
|
|
|
check_admin_referer( "keyring-request-$service_name", 'nonce' ); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$verification = Jetpack::generate_secrets( 'publicize' ); |
98
|
|
|
if ( ! $verification ) { |
|
|
|
|
99
|
|
|
$url = Jetpack::admin_url( 'jetpack#/settings' ); |
100
|
|
|
wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) ); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
$stats_options = get_option( 'stats_options' ); |
104
|
|
|
$wpcom_blog_id = Jetpack_Options::get_option( 'id' ); |
105
|
|
|
$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id']; |
106
|
|
|
|
107
|
|
|
$user = wp_get_current_user(); |
108
|
|
|
$redirect = $this->api_url( $service_name, urlencode_deep( array( |
109
|
|
|
'action' => 'request', |
110
|
|
|
'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ), |
111
|
|
|
'for' => 'publicize', |
112
|
|
|
// required flag that says this connection is intended for publicize |
113
|
|
|
'siteurl' => site_url(), |
114
|
|
|
'state' => $user->ID, |
115
|
|
|
'blog_id' => $wpcom_blog_id, |
116
|
|
|
'secret_1' => $verification['secret_1'], |
117
|
|
|
'secret_2' => $verification['secret_2'], |
118
|
|
|
'eol' => $verification['exp'], |
119
|
|
|
) ) ); |
120
|
|
|
wp_redirect( $redirect ); |
121
|
|
|
exit; |
|
|
|
|
122
|
|
|
break; |
|
|
|
|
123
|
|
|
|
124
|
|
|
case 'completed': |
125
|
|
|
Jetpack::load_xml_rpc_client(); |
126
|
|
|
$xml = new Jetpack_IXR_Client(); |
127
|
|
|
$xml->query( 'jetpack.fetchPublicizeConnections' ); |
128
|
|
|
|
129
|
|
|
if ( ! $xml->isError() ) { |
130
|
|
|
$response = $xml->getResponse(); |
131
|
|
|
Jetpack_Options::update_option( 'publicize_connections', $response ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
break; |
135
|
|
|
|
136
|
|
|
case 'delete': |
137
|
|
|
$id = $_GET['id']; |
138
|
|
|
|
139
|
|
|
check_admin_referer( 'keyring-request', 'kr_nonce' ); |
140
|
|
|
check_admin_referer( "keyring-request-$service_name", 'nonce' ); |
141
|
|
|
|
142
|
|
|
$this->disconnect( $service_name, $id ); |
|
|
|
|
143
|
|
|
|
144
|
|
|
add_action( 'admin_notices', array( $this, 'display_disconnected' ) ); |
145
|
|
|
break; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
function display_connection_error() { |
151
|
|
|
$code = false; |
152
|
|
|
if ( isset( $_GET['service'] ) ) { |
153
|
|
|
$service_name = $_GET['service']; |
154
|
|
|
$error = sprintf( __( 'There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'jetpack' ), Publicize::get_service_label( $service_name ) ); |
155
|
|
|
} else { |
156
|
|
|
if ( isset( $_GET['publicize_error'] ) ) { |
157
|
|
|
$code = strtolower( $_GET['publicize_error'] ); |
158
|
|
|
switch ( $code ) { |
159
|
|
|
case '400': |
160
|
|
|
$error = __( 'An invalid request was made. This normally means that something intercepted or corrupted the request from your server to the Jetpack Server. Try again and see if it works this time.', 'jetpack' ); |
161
|
|
|
break; |
162
|
|
|
case 'secret_mismatch': |
163
|
|
|
$error = __( 'We could not verify that your server is making an authorized request. Please try again, and make sure there is nothing interfering with requests from your server to the Jetpack Server.', 'jetpack' ); |
164
|
|
|
break; |
165
|
|
|
case 'empty_blog_id': |
166
|
|
|
$error = __( 'No blog_id was included in your request. Please try disconnecting Jetpack from WordPress.com and then reconnecting it. Once you have done that, try connecting Publicize again.', 'jetpack' ); |
167
|
|
|
break; |
168
|
|
|
case 'empty_state': |
169
|
|
|
$error = sprintf( __( 'No user information was included in your request. Please make sure that your user account has connected to Jetpack. Connect your user account by going to the <a href="%s">Jetpack page</a> within wp-admin.', 'jetpack' ), Jetpack::admin_url() ); |
170
|
|
|
break; |
171
|
|
|
default: |
172
|
|
|
$error = __( 'Something which should never happen, happened. Sorry about that. If you try again, maybe it will work.', 'jetpack' ); |
173
|
|
|
break; |
174
|
|
|
} |
175
|
|
|
} else { |
176
|
|
|
$error = __( 'There was a problem connecting with Publicize. Please try again in a moment.', 'jetpack' ); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
// Using the same formatting/style as Jetpack::admin_notices() error |
180
|
|
|
?> |
181
|
|
|
<div id="message" class="jetpack-message jetpack-err"> |
182
|
|
|
<div class="squeezer"> |
183
|
|
|
<h2><?php echo wp_kses( $error, array( 'a' => array( 'href' => true ), |
184
|
|
|
'code' => true, |
185
|
|
|
'strong' => true, |
186
|
|
|
'br' => true, |
187
|
|
|
'b' => true |
188
|
|
|
) ); ?></h2> |
189
|
|
|
<?php if ( $code ) : ?> |
|
|
|
|
190
|
|
|
<p><?php printf( __( 'Error code: %s', 'jetpack' ), esc_html( stripslashes( $code ) ) ); ?></p> |
191
|
|
|
<?php endif; ?> |
192
|
|
|
</div> |
193
|
|
|
</div> |
194
|
|
|
<?php |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: