Completed
Push — improve/rename-anti-spam-in-si... ( e5a567...f08131 )
by
unknown
75:27 queued 67:29
created

Terms_Of_Service::is_development_mode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Connection\Manager;
11
use Automattic\Jetpack\Status;
12
13
/**
14
 * Class Terms_Of_Service
15
 *
16
 * Helper class that is responsible for the state of agreement of the terms of service.
17
 */
18
class Terms_Of_Service {
19
	/**
20
	 * Jetpack option name where the terms of service state is stored.
21
	 *
22
	 * @var string
23
	 */
24
	const OPTION_NAME = 'tos_agreed';
25
26
	/**
27
	 * Allow the site to agree to the terms of service.
28
	 */
29
	public function agree() {
30
		$this->set_agree();
31
		/**
32
		 * Acton fired when the master user has agreed to the terms of service.
33
		 *
34
		 * @since 7.9.0
35
		 */
36
		do_action( 'jetpack_agreed_to_terms_of_service' );
37
	}
38
39
	/**
40
	 * Allow the site to reject to the terms of service.
41
	 */
42
	public function reject() {
43
		$this->set_reject();
44
		/**
45
		 * Acton fired when the master user has revoked their agreement to the terms of service.
46
		 *
47
		 * @since 7.9.1
48
		 */
49
		do_action( 'jetpack_reject_terms_of_service' );
50
	}
51
52
	/**
53
	 * Returns whether the master user has agreed to the terms of service.
54
	 *
55
	 * The following conditions have to be met in order to agree to the terms of service.
56
	 * 1. The master user has gone though the connect flow.
57
	 * 2. The site is not in dev mode.
58
	 * 3. The master user of the site is still connected.
59
	 *
60
	 * @return bool
61
	 */
62
	public function has_agreed() {
63
		if ( $this->is_offline_mode() ) {
64
			return false;
65
		}
66
67
		return $this->get_raw_has_agreed() || $this->is_active();
68
	}
69
70
	/**
71
	 * Abstracted for testing purposes.
72
	 * Tells us if the site is in dev mode.
73
	 *
74
	 * @return bool
75
	 */
76
	protected function is_offline_mode() {
77
		return ( new Status() )->is_offline_mode();
78
	}
79
80
	/**
81
	 * Tells us if the site is connected.
82
	 * Abstracted for testing purposes.
83
	 *
84
	 * @return bool
85
	 */
86
	protected function is_active() {
87
		return ( new Manager() )->is_active();
88
	}
89
90
	/**
91
	 * Gets just the Jetpack Option that contains the terms of service state.
92
	 * Abstracted for testing purposes.
93
	 *
94
	 * @return bool
95
	 */
96
	protected function get_raw_has_agreed() {
97
		return \Jetpack_Options::get_option( self::OPTION_NAME, false );
98
	}
99
100
	/**
101
	 * Sets the correct Jetpack Option to mark the that the site has agreed to the terms of service.
102
	 * Abstracted for testing purposes.
103
	 */
104
	protected function set_agree() {
105
		\Jetpack_Options::update_option( self::OPTION_NAME, true );
106
	}
107
108
	/**
109
	 * Sets the correct Jetpack Option to mark that the site has rejected the terms of service.
110
	 * Abstracted for testing purposes.
111
	 */
112
	protected function set_reject() {
113
		\Jetpack_Options::update_option( self::OPTION_NAME, false );
114
	}
115
116
}
117