Completed
Push — renovate/dealerdirect-phpcodes... ( 14797c...4cde9a )
by
unknown
27:42 queued 19:25
created

Utils::get_jetpack_api_version()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 5
rs 10
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
	const DEFAULT_JETPACK_API_VERSION = 1;
18
19
	/**
20
	 * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requests.
21
	 * This method sets the URL scheme to HTTP when HTTPS requests can't be made.
22
	 *
23
	 * @param string $url The url.
24
	 * @return string The url with the required URL scheme.
25
	 */
26
	public static function fix_url_for_bad_hosts( $url ) {
27
		// If we receive an http url, return it.
28
		if ( 'http' === wp_parse_url( $url, PHP_URL_SCHEME ) ) {
29
			return $url;
30
		}
31
32
		// If the url should never be https, ensure it isn't https.
33
		if ( 'NEVER' === Constants::get_constant( 'JETPACK_CLIENT__HTTPS' ) ) {
34
			return set_url_scheme( $url, 'http' );
35
		}
36
37
		// Otherwise, return the https url.
38
		return $url;
39
	}
40
41
	/**
42
	 * Enters a user token into the user_tokens option
43
	 *
44
	 * @param int    $user_id The user id.
45
	 * @param string $token The user token.
46
	 * @param bool   $is_master_user Whether the user is the master user.
47
	 * @return bool
48
	 */
49
	public static function update_user_token( $user_id, $token, $is_master_user ) {
50
		// Not designed for concurrent updates.
51
		$user_tokens = \Jetpack_Options::get_option( 'user_tokens' );
52
		if ( ! is_array( $user_tokens ) ) {
53
			$user_tokens = array();
54
		}
55
		$user_tokens[ $user_id ] = $token;
56
		if ( $is_master_user ) {
57
			$master_user = $user_id;
58
			$options     = compact( 'user_tokens', 'master_user' );
59
		} else {
60
			$options = compact( 'user_tokens' );
61
		}
62
		return \Jetpack_Options::update_options( $options );
63
	}
64
65
	/**
66
	 * Returns the Jetpack__API_VERSION constant if it exists, else returns a
67
	 * default value of 1.
68
	 *
69
	 * @return integer
70
	 */
71
	public static function get_jetpack_api_version() {
72
		$api_version = Constants::get_constant( 'JETPACK__API_VERSION' );
73
		$api_version = $api_version ? $api_version : self::DEFAULT_JETPACK_API_VERSION;
74
		return $api_version;
75
	}
76
}
77