Completed
Push — update/recurring-payments-prim... ( 1aff01 )
by
unknown
16:32 queued 09:53
created

Utils::update_user_token()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Jetpack Connection package Utils class file.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Constants;
11
12
/**
13
 * Provides utility methods for the Connection package.
14
 */
15
class Utils {
16
17
	/**
18
	 * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requests.
19
	 * This method sets the URL scheme to HTTP when HTTPS requests can't be made.
20
	 *
21
	 * @param string $url The url.
22
	 * @return string The url with the required URL scheme.
23
	 */
24
	public static function fix_url_for_bad_hosts( $url ) {
25
		// If we receive an http url, return it.
26
		if ( 'http' === wp_parse_url( $url, PHP_URL_SCHEME ) ) {
27
			return $url;
28
		}
29
30
		// If the url should never be https, ensure it isn't https.
31
		if ( 'NEVER' === Constants::get_constant( 'JETPACK_CLIENT__HTTPS' ) ) {
32
			return set_url_scheme( $url, 'http' );
33
		}
34
35
		// Otherwise, return the https url.
36
		return $url;
37
	}
38
39
	/**
40
	 * Enters a user token into the user_tokens option
41
	 *
42
	 * @param int    $user_id The user id.
43
	 * @param string $token The user token.
44
	 * @param bool   $is_master_user Whether the user is the master user.
45
	 * @return bool
46
	 */
47
	public static function update_user_token( $user_id, $token, $is_master_user ) {
48
		// Not designed for concurrent updates.
49
		$user_tokens = \Jetpack_Options::get_option( 'user_tokens' );
50
		if ( ! is_array( $user_tokens ) ) {
51
			$user_tokens = array();
52
		}
53
		$user_tokens[ $user_id ] = $token;
54
		if ( $is_master_user ) {
55
			$master_user = $user_id;
56
			$options     = compact( 'user_tokens', 'master_user' );
57
		} else {
58
			$options = compact( 'user_tokens' );
59
		}
60
		return \Jetpack_Options::update_options( $options );
61
	}
62
}
63