Completed
Push — add/3rdpc_test_package ( 004120 )
by
unknown
331:47 queued 321:55
created

Tpc_Tester::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The 3rd Party Cookie Tester class
4
 *
5
 * @package automattic/tpc-tester
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Constants;
11
12
class Tpc_Tester {
13
14
	const COOKIE_NAME       = 'jetpack_tpc_tester';
15
	const SUPPORTED_VALUE   = 'supported';
16
	const UNSUPPORTED_VALUE = 'unsupported';
17
18
	private static $cookie_value;
19
	private static $tested;
20
	private static $initialized = false;
21
22
	public static function init() {
23
		if ( self::$initialized ) {
24
			return;
25
		}
26
		session_start();
27
		self::$initialized  = true;
28
		self::$tested       = ! empty( $_COOKIE[ self::COOKIE_NAME ] );
29
		self::$cookie_value = isset( $_COOKIE[ self::COOKIE_NAME ] ) ? $_COOKIE[ self::COOKIE_NAME ] : '';
30
		error_log( json_encode( $_COOKIE ) );
31
		error_log( json_encode( $_COOKIE[ self::COOKIE_NAME ] ) );
32
33
		if ( ! self::$tested && is_user_logged_in() ) {
34
			add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
35
		}
36
	}
37
38
	public static function is_tested() {
39
		return self::$tested;
40
	}
41
42
	public static function supports() {
43
		return self::is_tested() && self::SUPPORTED_VALUE === self::$cookie_value;
44
	}
45
46
	public static function enqueue_scripts() {
47
		$api_base_url = Constants::get_constant( 'JETPACK__API_BASE' );
48
		wp_enqueue_script( 'jetpack_tpc_tester_client', plugin_dir_url( __DIR__ ) . '/src/jetpack_tpc_tester.js', array(), rand() );
49
		wp_enqueue_script( 'jetpack_tpc_tester_server', $api_base_url . 'test_3rdpc_set/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/', array( 'jetpack_tpc_tester_client' ), rand() );
50
51
		wp_localize_script(
52
			'jetpack_tpc_tester_client',
53
			'jetpack_tpc_tester_client',
54
			array(
55
				'test_url'          => $api_base_url . 'test_3rdpc_get/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/',
56
				'cookie_name'       => self::COOKIE_NAME,
57
				'supported_value'   => self::SUPPORTED_VALUE,
58
				'unsupported_value' => self::UNSUPPORTED_VALUE,
59
			)
60
		);
61
	}
62
}
63