summaryrefslogtreecommitdiff
blob: 1c280262d5bb9895315837baa1a1343ba68834ec (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
<?php
/**
 * The Jetpack Connection package Utils class file.
 *
 * @package automattic/jetpack-connection
 */

namespace Automattic\Jetpack\Connection;

use Automattic\Jetpack\Constants;

/**
 * Provides utility methods for the Connection package.
 */
class Utils {

	/**
	 * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requests.
	 * This method sets the URL scheme to HTTP when HTTPS requests can't be made.
	 *
	 * @param string $url The url.
	 * @return string The url with the required URL scheme.
	 */
	public static function fix_url_for_bad_hosts( $url ) {
		// If we receive an http url, return it.
		if ( 'http' === wp_parse_url( $url, PHP_URL_SCHEME ) ) {
			return $url;
		}

		// If the url should never be https, ensure it isn't https.
		if ( 'NEVER' === Constants::get_constant( 'JETPACK_CLIENT__HTTPS' ) ) {
			return set_url_scheme( $url, 'http' );
		}

		// Otherwise, return the https url.
		return $url;
	}

	/**
	 * Enters a user token into the user_tokens option
	 *
	 * @param int    $user_id The user id.
	 * @param string $token The user token.
	 * @param bool   $is_master_user Whether the user is the master user.
	 * @return bool
	 */
	public static function update_user_token( $user_id, $token, $is_master_user ) {
		// Not designed for concurrent updates.
		$user_tokens = \Jetpack_Options::get_option( 'user_tokens' );
		if ( ! is_array( $user_tokens ) ) {
			$user_tokens = array();
		}
		$user_tokens[ $user_id ] = $token;
		if ( $is_master_user ) {
			$master_user = $user_id;
			$options     = compact( 'user_tokens', 'master_user' );
		} else {
			$options = compact( 'user_tokens' );
		}
		return \Jetpack_Options::update_options( $options );
	}
}