summaryrefslogtreecommitdiff
blob: a92b6f51e1ee3cfa26081d87a2c7debbbd94eae4 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
 * Keyring helper.
 *
 * @package automattic/jetpack-publicize
 */

namespace Automattic\Jetpack\Publicize;

use Automattic\Jetpack\Connection\Secrets;
use Automattic\Jetpack\Paths;
use Jetpack_IXR_Client;
use Jetpack_Options;

/**
 * A series of utilities to interact with a Keyring instance.
 */
class Keyring_Helper {
	/**
	 * Class instance
	 *
	 * @var \Automattic\Jetpack\Publicize\Keyring_Helper
	 */
	private static $instance = null;

	/**
	 * Whether the `sharing` page is registered.
	 *
	 * @var bool
	 */
	private static $is_sharing_page_registered = false;

	/**
	 * Initialize instance.
	 */
	public static function init() {
		if ( self::$instance === null ) {
			self::$instance = new Keyring_Helper();
		}

		return self::$instance;
	}

	const SERVICES = array(
		'facebook'                 => array(
			'for' => 'publicize',
		),
		'twitter'                  => array(
			'for' => 'publicize',
		),
		'linkedin'                 => array(
			'for' => 'publicize',
		),
		'tumblr'                   => array(
			'for' => 'publicize',
		),
		'path'                     => array(
			'for' => 'publicize',
		),
		'google_plus'              => array(
			'for' => 'publicize',
		),
		'google_site_verification' => array(
			'for' => 'other',
		),
	);

	/**
	 * Constructor
	 */
	private function __construct() {
		add_action( 'admin_menu', array( __CLASS__, 'register_sharing_page' ) );

		add_action( 'load-settings_page_sharing', array( __CLASS__, 'admin_page_load' ), 9 );
	}

	/**
	 * We need a `sharing` page to be able to connect and disconnect services.
	 */
	public static function register_sharing_page() {
		if ( self::$is_sharing_page_registered ) {
			return;
		}

		self::$is_sharing_page_registered = true;

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		global $_registered_pages;

		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		$hookname = get_plugin_page_hookname( 'sharing', 'options-general.php' );
		add_action( $hookname, array( __CLASS__, 'admin_page_load' ) );
		$_registered_pages[ $hookname ] = true; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	}

	/**
	 * Gets a URL to the public-api actions. Works like WP's admin_url.
	 * On WordPress.com this is/calls Keyring::admin_url.
	 *
	 * @param string $service Shortname of a specific service.
	 * @param array  $params  Parameters to append to an API connection URL.
	 *
	 * @return URL to specific public-api process
	 */
	private static function api_url( $service = false, $params = array() ) {
		/**
		 * Filters the API URL used to interact with WordPress.com.
		 *
		 * @since 2.0.0
		 *
		 * @param string https://public-api.wordpress.com/connect/?jetpack=publicize Default Publicize API URL.
		 */
		$url = apply_filters( 'publicize_api_url', 'https://public-api.wordpress.com/connect/?jetpack=publicize' );

		if ( $service ) {
			$url = add_query_arg( array( 'service' => $service ), $url );
		}

		if ( count( $params ) ) {
			$url = add_query_arg( $params, $url );
		}

		return $url;
	}

	/**
	 * Build a connection URL (sharing settings page with unique query args to create a connection).
	 *
	 * @param string $service_name Service name.
	 * @param string $for          Feature name.
	 */
	public static function connect_url( $service_name, $for ) {
		return add_query_arg(
			array(
				'action'   => 'request',
				'service'  => $service_name,
				'kr_nonce' => wp_create_nonce( 'keyring-request' ),
				'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
				'for'      => $for,
			),
			admin_url( 'options-general.php?page=sharing' )
		);
	}

	/**
	 * Build a URL to refresh a connection (sharing settings page with unique query args to refresh a connection).
	 * Similar to connect_url, but with a refresh parameter.
	 *
	 * @param string $service_name Service name.
	 * @param string $for          Feature name.
	 */
	public static function refresh_url( $service_name, $for ) {
		return add_query_arg(
			array(
				'action'   => 'request',
				'service'  => $service_name,
				'kr_nonce' => wp_create_nonce( 'keyring-request' ),
				'refresh'  => 1,
				'for'      => $for,
				'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
			),
			admin_url( 'options-general.php?page=sharing' )
		);
	}

	/**
	 * Build a URL to delete a connection (sharing settings page with unique query args to delete a connection).
	 *
	 * @param string $service_name Service name.
	 * @param string $id           Connection ID.
	 */
	public static function disconnect_url( $service_name, $id ) {
		return add_query_arg(
			array(
				'action'   => 'delete',
				'service'  => $service_name,
				'id'       => $id,
				'kr_nonce' => wp_create_nonce( 'keyring-request' ),
				'nonce'    => wp_create_nonce( "keyring-request-$service_name" ),
			),
			admin_url( 'options-general.php?page=sharing' )
		);
	}

	/**
	 * Build contents handling Keyring connection management into Sharing settings screen.
	 */
	public static function admin_page_load() {
		if ( isset( $_GET['action'] ) ) {
			if ( isset( $_GET['service'] ) ) {
				$service_name = filter_var( wp_unslash( $_GET['service'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We verify below.
			}

			switch ( $_GET['action'] ) {

				case 'request':
					check_admin_referer( 'keyring-request', 'kr_nonce' );
					check_admin_referer( "keyring-request-$service_name", 'nonce' );

					$verification = ( new Secrets() )->generate( 'publicize' );
					if ( ! $verification ) {
						$url = ( new Paths() )->admin_url( 'page=jetpack#/settings' );
						wp_die(
							sprintf(
								wp_kses(
									/* Translators: placeholder is a URL to a Settings page. */
									__( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack-publicize-pkg' ),
									array(
										'a' => array(
											'href' => array(),
										),
									)
								),
								esc_url( $url )
							)
						);

					}
					$stats_options = get_option( 'stats_options' );
					$wpcom_blog_id = Jetpack_Options::get_option( 'id' );
					$wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id'];

					$user     = wp_get_current_user();
					$redirect = self::api_url(
						$service_name,
						urlencode_deep(
							array(
								'action'       => 'request',
								'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ),
								'for'          => 'publicize',
								// required flag that says this connection is intended for publicize.
								'siteurl'      => site_url(),
								'state'        => $user->ID,
								'blog_id'      => $wpcom_blog_id,
								'secret_1'     => $verification['secret_1'],
								'secret_2'     => $verification['secret_2'],
								'eol'          => $verification['exp'],
							)
						)
					);
					wp_redirect( $redirect ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- The API URL is an external URL and is filterable.
					exit;

				case 'completed':
					/*
					 * We do not use a nonce here,
					 * since we're populating a local cache of
					 * the Publicize connections that were created and stored on WordPress.com.
					 */
					$xml = new Jetpack_IXR_Client();
					$xml->query( 'jetpack.fetchPublicizeConnections' );

					if ( ! $xml->isError() ) {
						$response = $xml->getResponse();
						Jetpack_Options::update_option( 'publicize_connections', $response );
					}

					break;

				case 'delete':
					$id = isset( $_GET['id'] ) ? filter_var( wp_unslash( $_GET['id'] ) ) : null;

					check_admin_referer( 'keyring-request', 'kr_nonce' );
					check_admin_referer( "keyring-request-$service_name", 'nonce' );

					self::disconnect( $service_name, $id );

					do_action( 'connection_disconnected', $service_name );
					break;
			}
		}
	}

	/**
	 * Remove a Publicize connection
	 *
	 * @param string   $service_name  Service name.
	 * @param string   $connection_id Connection ID.
	 * @param int|bool $_blog_id      Blog ID.
	 * @param int|bool $_user_id      User ID.
	 * @param bool     $force_delete  Force delete the connection.
	 */
	public static function disconnect( $service_name, $connection_id, $_blog_id = false, $_user_id = false, $force_delete = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		$xml = new Jetpack_IXR_Client();
		$xml->query( 'jetpack.deletePublicizeConnection', $connection_id );

		if ( ! $xml->isError() ) {
			Jetpack_Options::update_option( 'publicize_connections', $xml->getResponse() );
		} else {
			return false;
		}
	}
}