summaryrefslogtreecommitdiff
blob: 056004f503739d134dcd7c5a684309f734a72703 (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
<?php
/**
 * A Terms of Service class for Jetpack.
 *
 * @package automattic/jetpack-licensing
 */

namespace Automattic\Jetpack;

use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Jetpack_IXR_ClientMulticall;
use Jetpack_Options;
use WP_Error;

/**
 * Class Licensing.
 * Helper class that is responsible for attaching licenses to the current site.
 *
 * @since 1.1.1
 */
class Licensing {
	/**
	 * Name of the WordPress option that holds all known Jetpack licenses.
	 *
	 * @const string
	 */
	const LICENSES_OPTION_NAME = 'jetpack_licenses';

	/**
	 * Name of the WordPress transient that holds the last license attaching error, if any.
	 *
	 * @const string
	 */
	const ERROR_TRANSIENT_NAME = 'jetpack_licenses_error';

	/**
	 * Holds the singleton instance of this class.
	 *
	 * @var self
	 */
	protected static $instance = false;

	/**
	 * Singleton.
	 *
	 * @static
	 */
	public static function instance() {
		if ( ! self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Initialize.
	 *
	 * @return void
	 */
	public function initialize() {
		add_action( 'add_option_' . self::LICENSES_OPTION_NAME, array( $this, 'attach_stored_licenses' ) );
		add_action( 'update_option_' . self::LICENSES_OPTION_NAME, array( $this, 'attach_stored_licenses' ) );
		add_action( 'jetpack_authorize_ending_authorized', array( $this, 'attach_stored_licenses_on_connection' ) );
	}

	/**
	 * Get Jetpack connection manager instance.
	 *
	 * @return Connection_Manager
	 */
	protected function connection() {
		static $connection;

		if ( null === $connection ) {
			$connection = new Connection_Manager();
		}

		return $connection;
	}

	/**
	 * Get the last license attach request error that has occurred, if any.
	 *
	 * @return string Human-readable error message or an empty string.
	 */
	public function last_error() {
		return Jetpack_Options::get_option( 'licensing_error', '' );
	}

	/**
	 * Log an error to be surfaced to the user at a later time.
	 *
	 * @param string $error Human-readable error message.
	 * @return void
	 */
	public function log_error( $error ) {
		$substr = function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr';
		Jetpack_Options::update_option( 'licensing_error', $substr( $error, 0, 1024 ) );
	}

	/**
	 * Get all stored licenses.
	 *
	 * @return string[] License keys.
	 */
	public function stored_licenses() {
		$licenses = (array) get_option( self::LICENSES_OPTION_NAME, array() );
		$licenses = array_filter( $licenses, 'is_scalar' );
		$licenses = array_map( 'strval', $licenses );
		$licenses = array_filter( $licenses );

		return $licenses;
	}

	/**
	 * Append a license
	 *
	 * @param string $license A jetpack license key.
	 * @return bool True if the option was updated with the new license, false otherwise.
	 */
	public function append_license( $license ) {
		$licenses = $this->stored_licenses();

		array_push( $licenses, $license );

		return update_option( self::LICENSES_OPTION_NAME, $licenses );
	}

	/**
	 * Make an authenticated WP.com XMLRPC multicall request to attach the provided license keys.
	 *
	 * @param string[] $licenses License keys to attach.
	 * @return Jetpack_IXR_ClientMulticall
	 */
	protected function attach_licenses_request( array $licenses ) {
		$xml = new Jetpack_IXR_ClientMulticall( array( 'timeout' => 30 ) );

		foreach ( $licenses as $license ) {
			$xml->addCall( 'jetpack.attachLicense', $license );
		}

		$xml->query();

		return $xml;
	}

	/**
	 * Attach the given licenses.
	 *
	 * @param string[] $licenses Licenses to attach.
	 * @return array|WP_Error Results for each license (which may include WP_Error instances) or a WP_Error instance.
	 */
	public function attach_licenses( array $licenses ) {
		if ( ! $this->connection()->has_connected_owner() ) {
			return new WP_Error( 'not_connected', __( 'Jetpack doesn\'t have a connected owner.', 'jetpack-licensing' ) );
		}

		if ( empty( $licenses ) ) {
			return array();
		}

		$xml = $this->attach_licenses_request( $licenses );

		if ( $xml->isError() ) {
			$error = new WP_Error( 'request_failed', __( 'License attach request failed.', 'jetpack-licensing' ) );
			$error->add( $xml->getErrorCode(), $xml->getErrorMessage() );
			return $error;
		}

		$results = array_map(
			function ( $response ) {
				if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) {
					return new WP_Error( $response['faultCode'], $response['faultString'] );
				}

				return $response;
			},
			(array) $xml->getResponse()
		);

		return $results;
	}

	/**
	 * Attach all stored licenses.
	 *
	 * @return array|WP_Error Results for each license (which may include WP_Error instances) or a WP_Error instance.
	 */
	public function attach_stored_licenses() {
		$licenses = $this->stored_licenses();
		$results  = $this->attach_licenses( $licenses );

		if ( is_wp_error( $results ) ) {
			if ( 'request_failed' === $results->get_error_code() ) {
				$this->log_error(
					__( 'Failed to attach your Jetpack license(s). Please try reconnecting Jetpack.', 'jetpack-licensing' )
				);
			}

			return $results;
		}

		$failed = array();

		foreach ( $results as $index => $result ) {
			if ( isset( $licenses[ $index ] ) && is_wp_error( $result ) ) {
				$failed[] = $licenses[ $index ];
			}
		}

		if ( ! empty( $failed ) ) {
			$this->log_error(
				sprintf(
					/* translators: %s is a comma-separated list of license keys. */
					__( 'The following Jetpack licenses are invalid, already in use, or revoked: %s', 'jetpack-licensing' ),
					implode( ', ', $failed )
				)
			);
		}

		return $results;
	}

	/**
	 * Attach all stored licenses during connection flow for the connection owner.
	 *
	 * @return void
	 */
	public function attach_stored_licenses_on_connection() {
		if ( $this->connection()->is_connection_owner() ) {
			$this->attach_stored_licenses();
		}
	}

	/**
	 * Is the current user allowed to use the Licensing Input UI?
	 *
	 * @since 1.4.0
	 * @return bool
	 */
	public static function is_licensing_input_enabled() {
		/**
		 * Filter that checks if the user is allowed to see the Licensing UI. `true` enables it.
		 *
		 * @since 1.4.0
		 *
		 * @param bool False by default.
		 */
		return apply_filters( 'jetpack_licensing_ui_enabled', false ) && current_user_can( 'jetpack_connect_user' );
	}

	/**
	 * Gets the user-licensing activation notice dismissal info.
	 *
	 * @since 10.4.0
	 * @return array
	 */
	public function get_license_activation_notice_dismiss() {

		$default = array(
			'last_detached_count' => null,
			'last_dismissed_time' => null,
		);

		if ( $this->connection()->is_user_connected() && $this->connection()->is_connection_owner() ) {
			return Jetpack_Options::get_option( 'licensing_activation_notice_dismiss', $default );
		}

		return $default;
	}
}