Completed
Push — add/deterministic-init ( d333b9...ff5714 )
by
unknown
07:06
created

Config::enable_tos()   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
 * The base Jetpack configuration class file.
4
 *
5
 * @package automattic/jetpack-config
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Sync\Main as Sync_Main;
11
use Automattic\Jetpack\Plugin\Tracking as Plugin_Tracking;
12
use Automattic\Jetpack\Terms_Of_Service;
13
14
/**
15
 * The configuration class.
16
 */
17
class Config {
18
19
	/**
20
	 * The initial setting values.
21
	 *
22
	 * @var Array
23
	 */
24
	protected $config = array(
25
		'sync'     => false,
26
		'tracking' => false,
27
		'tos'      => false,
28
	);
29
30
	/**
31
	 * Creates the configuration class instance.
32
	 */
33
	public function __construct() {
34
35
		/**
36
		 * Adding the config handler to run on priority 2 because the class itself is
37
		 * being constructed on priority 1.
38
		 */
39
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
40
	}
41
42
	/**
43
	 * Require a feature to be initialized. It's up to the package consumer to actually add
44
	 * the package to their composer project. Declaring a requirement using this method
45
	 * instructs the class to initalize it.
46
	 *
47
	 * @param String $feature the feature slug.
48
	 */
49
	public function ensure( $feature ) {
50
		$this->config[ $feature ] = true;
51
	}
52
53
	/**
54
	 * Runs on default plugins_loaded hook priority.
55
	 *
56
	 * @action plugins_loaded
57
	 */
58
	public function on_plugins_loaded() {
59
		if ( $this->config['tracking'] ) {
60
61
			$this->ensure_class( 'Automattic\Jetpack\Terms_Of_Service' )
62
				&& $this->ensure_class( 'Automattic\Jetpack\Tracking' )
63
				&& $this->ensure_feature( 'tracking' );
64
		}
65
66
		if ( $this->config['sync'] ) {
67
			$this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
68
				&& $this->ensure_feature( 'sync' );
69
		}
70
	}
71
72
	/**
73
	 * Returns true if the required class is available and alerts the user if it's not available
74
	 * in case the site is in debug mode.
75
	 *
76
	 * @param String $classname a fully qualified class name.
77
	 * @return Boolean whether the class is available.
78
	 */
79
	protected function ensure_class( $classname ) {
80
		$available = class_exists( $classname );
81
82
		if ( ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
83
			trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
84
				sprintf(
85
					/* translators: %1$s is a PHP class name, %2$s is a Jetpack feature code, like 'sync'. */
86
					esc_html__(
87
						'Unable to load class %1$s, not enabling %2$s feature. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
88
						'jetpack'
89
					),
90
					esc_html( $classname )
91
				),
92
				E_USER_NOTICE
93
			);
94
		}
95
96
		return $available;
97
	}
98
99
	/**
100
	 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
101
	 *
102
	 * @param String $feature slug of the feature.
103
	 * @return Boolean whether the feature was enabled. If it was already enabled, returns false.
104
	 */
105
	protected function ensure_feature( $feature ) {
106
		if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
107
			return false;
108
		}
109
110
		$method = 'enable_' . $feature;
111
		if ( ! method_exists( $this, $method ) ) {
112
			return false;
113
		}
114
115
		$this->{ $method }();
116
117
		/**
118
		 * Fires when a specific Jetpack package feature is initalized using the Config package.
119
		 *
120
		 * @since 8.1.0
121
		 */
122
		do_action( 'jetpack_feature_' . $feature . '_enabled' );
123
	}
124
125
	/**
126
	 * Dummy method to enable Terms of Service.
127
	 */
128
	protected function enable_tos() {
129
		return true;
130
	}
131
132
	/**
133
	 * Enables the tracking feature. Depends on the Terms of Service package, so enables it too.
134
	 */
135
	protected function enable_tracking() {
136
137
		// Enabling dependencies.
138
		$this->ensure_feature( 'tos' );
139
140
		$terms_of_service = new Terms_Of_Service();
141
		$tracking         = new Plugin_Tracking();
142
		if ( $terms_of_service->has_agreed() ) {
143
			add_action( 'init', array( $tracking, 'init' ) );
144
		} else {
145
			/**
146
			 * Initialize tracking right after the user agrees to the terms of service.
147
			 */
148
			add_action( 'jetpack_agreed_to_terms_of_service', array( $tracking, 'init' ) );
149
		}
150
151
		return true;
152
	}
153
154
	/**
155
	 * Enables the Sync feature.
156
	 */
157
	protected function enable_sync() {
158
		Sync_Main::configure();
159
160
		return true;
161
	}
162
163
}
164