Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Terms_Of_Service
90.91% covered (success)
90.91%
10 / 11
85.71% covered (warning)
85.71%
6 / 7
8.05
0.00% covered (danger)
0.00%
0 / 1
 agree
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 reject
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 has_agreed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 is_offline_mode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_raw_has_agreed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_agree
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_reject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * A Terms of Service class for Jetpack.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack;
9
10/**
11 * Class Terms_Of_Service
12 *
13 * Helper class that is responsible for the state of agreement of the terms of service.
14 */
15class Terms_Of_Service {
16    /**
17     * Jetpack option name where the terms of service state is stored.
18     *
19     * @var string
20     */
21    const OPTION_NAME = 'tos_agreed';
22
23    /**
24     * Allow the site to agree to the terms of service.
25     */
26    public function agree() {
27        $this->set_agree();
28        /**
29         * Acton fired when the master user has agreed to the terms of service.
30         *
31         * @since 1.0.4
32         * @since-jetpack 7.9.0
33         */
34        do_action( 'jetpack_agreed_to_terms_of_service' );
35    }
36
37    /**
38     * Allow the site to reject to the terms of service.
39     */
40    public function reject() {
41        $this->set_reject();
42        /**
43         * Acton fired when the master user has revoked their agreement to the terms of service.
44         *
45         * @since 1.0.4
46         * @since-jetpack 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 1.4.0).
58     *
59     * @return bool
60     */
61    public function has_agreed() {
62        if ( $this->is_offline_mode() ) {
63            return false;
64        }
65        /**
66         * Before 1.4.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 1.4.0 we have removed this check in order to resolve the
69         * circular dependencies it was introducing to composer packages.
70         *
71         * @since 1.4.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}