Completed
Push — branch-8.9 ( 73b63d...9a3328 )
by Jeremy
35:24 queued 26:43
created

Terms_Of_Service   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A agree() 0 9 1
A reject() 0 9 1
A has_agreed() 0 14 2
A is_offline_mode() 0 3 1
A get_raw_has_agreed() 0 3 1
A set_agree() 0 3 1
A set_reject() 0 3 1
1
<?php
2
/**
3
 * A Terms of Service class for Jetpack.
4
 *
5
 * @package automattic/jetpack-terms-of-service
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Terms_Of_Service
14
 *
15
 * Helper class that is responsible for the state of agreement of the terms of service.
16
 */
17
class Terms_Of_Service {
18
	/**
19
	 * Jetpack option name where the terms of service state is stored.
20
	 *
21
	 * @var string
22
	 */
23
	const OPTION_NAME = 'tos_agreed';
24
25
	/**
26
	 * Allow the site to agree to the terms of service.
27
	 */
28
	public function agree() {
29
		$this->set_agree();
30
		/**
31
		 * Acton fired when the master user has agreed to the terms of service.
32
		 *
33
		 * @since 7.9.0
34
		 */
35
		do_action( 'jetpack_agreed_to_terms_of_service' );
36
	}
37
38
	/**
39
	 * Allow the site to reject to the terms of service.
40
	 */
41
	public function reject() {
42
		$this->set_reject();
43
		/**
44
		 * Acton fired when the master user has revoked their agreement to the terms of service.
45
		 *
46
		 * @since 7.9.1
47
		 */
48
		do_action( 'jetpack_reject_terms_of_service' );
49
	}
50
51
	/**
52
	 * Returns whether the master user has agreed to the terms of service.
53
	 *
54
	 * The following conditions have to be met in order to agree to the terms of service.
55
	 * 1. The master user has gone though the connect flow.
56
	 * 2. The site is not in dev mode.
57
	 * 3. The master user of the site is still connected (deprecated @since 8.9.0).
58
	 *
59
	 * @return bool
60
	 */
61
	public function has_agreed() {
62
		if ( $this->is_offline_mode() ) {
63
			return false;
64
		}
65
		/**
66
		 * Before 8.9.0 we used to also check if the master user of the site is connected
67
		 * by calling the Connection related `is_active` method.
68
		 * As of 8.9.0 we have removed this check in order to resolve the
69
		 * circular dependencies it was introducing to composer packages.
70
		 *
71
		 * @since 8.9.0
72
		 */
73
		return $this->get_raw_has_agreed();
74
	}
75
76
	/**
77
	 * Abstracted for testing purposes.
78
	 * Tells us if the site is in dev mode.
79
	 *
80
	 * @return bool
81
	 */
82
	protected function is_offline_mode() {
83
		return ( new Status() )->is_offline_mode();
84
	}
85
86
	/**
87
	 * Gets just the Jetpack Option that contains the terms of service state.
88
	 * Abstracted for testing purposes.
89
	 *
90
	 * @return bool
91
	 */
92
	protected function get_raw_has_agreed() {
93
		return \Jetpack_Options::get_option( self::OPTION_NAME, false );
94
	}
95
96
	/**
97
	 * Sets the correct Jetpack Option to mark the that the site has agreed to the terms of service.
98
	 * Abstracted for testing purposes.
99
	 */
100
	protected function set_agree() {
101
		\Jetpack_Options::update_option( self::OPTION_NAME, true );
102
	}
103
104
	/**
105
	 * Sets the correct Jetpack Option to mark that the site has rejected the terms of service.
106
	 * Abstracted for testing purposes.
107
	 */
108
	protected function set_reject() {
109
		\Jetpack_Options::update_option( self::OPTION_NAME, false );
110
	}
111
112
}
113