-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathUser_Input.php
More file actions
292 lines (256 loc) · 7.41 KB
/
User_Input.php
File metadata and controls
292 lines (256 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Class Google\Site_Kit\Core\User_Input\User_Input
*
* @package Google\Site_Kit\Core\User_Input
* @copyright 2022 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\User_Input;
use ArrayAccess;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Key_Metrics\Key_Metrics_Setup_Completed_By;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Tracking\Feature_Metrics_Trait;
use Google\Site_Kit\Core\Tracking\Provides_Feature_Metrics;
use Google\Site_Kit\Core\User_Surveys\Survey_Queue;
use WP_Error;
use WP_User;
/**
* Class for handling User Input settings.
*
* @since 1.90.0
* @access private
* @ignore
*/
class User_Input implements Provides_Feature_Metrics {
use Feature_Metrics_Trait;
/**
* Site_Specific_Answers instance.
*
* @since 1.90.0
* @var Site_Specific_Answers
*/
protected $site_specific_answers;
/**
* User_Options instance.
*
* @since 1.90.0
* @var User_Options
*/
protected $user_options;
/**
* User_Specific_Answers instance.
*
* @since 1.90.0
* @var User_Specific_Answers
*/
protected $user_specific_answers;
/**
* REST_User_Input_Controller instance.
*
* @since 1.90.0
* @var REST_User_Input_Controller
*/
protected $rest_controller;
/**
* User Input questions.
*
* @since 1.90.0
* @var array|ArrayAccess
*/
private static $questions = array(
'purpose' => array(
'scope' => 'site',
),
'postFrequency' => array(
'scope' => 'user',
),
'goals' => array(
'scope' => 'user',
),
'includeConversionEvents' => array(
'scope' => 'site',
),
);
/**
* Constructor.
*
* @since 1.90.0
*
* @param Context $context Plugin context.
* @param Options $options Optional. Options instance. Default a new instance.
* @param User_Options $user_options Optional. User_Options instance. Default a new instance.
* @param Survey_Queue $survey_queue Optional. Survey_Queue instance. Default a new instance.
*/
public function __construct(
Context $context,
?Options $options = null,
?User_Options $user_options = null,
?Survey_Queue $survey_queue = null
) {
$this->site_specific_answers = new Site_Specific_Answers( $options ?: new Options( $context ) );
$this->user_options = $user_options ?: new User_Options( $context );
$this->user_specific_answers = new User_Specific_Answers( $this->user_options );
$this->rest_controller = new REST_User_Input_Controller(
$this,
$survey_queue ?: new Survey_Queue( $this->user_options ),
new Key_Metrics_Setup_Completed_By( $options ?: new Options( $context ) )
);
}
/**
* Registers functionality.
*
* @since 1.90.0
*/
public function register() {
$this->site_specific_answers->register();
$this->user_specific_answers->register();
$this->rest_controller->register();
$this->register_feature_metrics();
}
/**
* Gets the set of user input questions.
*
* @since 1.90.0
*
* @return array The user input questions.
*/
public static function get_questions() {
return self::$questions;
}
/**
* Gets user input answers.
*
* @since 1.90.0
*
* @return array|WP_Error User input answers.
*/
public function get_answers() {
$questions = self::$questions;
$site_answers = $this->site_specific_answers->get();
$user_answers = $this->user_specific_answers->get();
$settings = array_merge(
is_array( $site_answers ) ? $site_answers : array(),
is_array( $user_answers ) ? $user_answers : array()
);
// If there are no settings, return default empty values.
if ( empty( $settings ) ) {
array_walk(
$questions,
function ( &$question ) {
$question['values'] = array();
}
);
return $questions;
}
foreach ( $settings as &$setting ) {
if ( ! isset( $setting['answeredBy'] ) ) {
continue;
}
$answered_by = intval( $setting['answeredBy'] );
if ( ! $answered_by || $answered_by === $this->user_options->get_user_id() ) {
continue;
}
$setting['author'] = array(
'photo' => get_avatar_url( $answered_by ),
'login' => ( new WP_User( $answered_by ) )->user_login,
);
}
// If there are un-answered questions, return default empty values for them.
foreach ( $questions as $question_key => $question_value ) {
if ( ! isset( $settings[ $question_key ] ) ) {
$settings[ $question_key ] = $question_value;
$settings[ $question_key ]['values'] = array();
}
}
return $settings;
}
/**
* Determines whether the current user input settings have empty values or not.
*
* @since 1.90.0
*
* @param array $settings The settings to check.
* @return boolean|null TRUE if at least one of the settings has empty values, otherwise FALSE.
*/
public function are_settings_empty( $settings = array() ) {
if ( empty( $settings ) ) {
$settings = $this->get_answers();
if ( is_wp_error( $settings ) ) {
return null;
}
}
// Conversion events may be empty during setup if no events have been detected.
// Since this setting does not affect whether user input is considered "set up",
// we are excluding it from this check. It relates to user input initially being
// set up with detected events or events added later.
unset( $settings['includeConversionEvents'] );
foreach ( $settings as $setting ) {
if ( empty( $setting['values'] ) ) {
return true;
}
}
return false;
}
/**
* Sets user input answers.
*
* @since 1.90.0
*
* @param array $settings User settings.
* @return array|WP_Error User input answers.
*/
public function set_answers( $settings ) {
$site_settings = array();
$user_settings = array();
foreach ( $settings as $setting_key => $answers ) {
$setting_data = array();
$setting_data['values'] = $answers;
$setting_data['scope'] = self::$questions[ $setting_key ]['scope'];
if ( 'site' === $setting_data['scope'] ) {
$existing_answers = $this->get_answers();
$answered_by = $this->user_options->get_user_id();
if (
// If the answer to the "purpose" question changed,
// attribute the answer to the current user changing the
// answer.
(
! empty( $existing_answers['purpose']['values'] ) &&
! empty( array_diff( $existing_answers['purpose']['values'], $answers ) )
) ||
// If the answer to the "purpose" question was empty,
// attribute the answer to the current user.
empty( $existing_answers['purpose']['answeredBy'] )
) {
$answered_by = $this->user_options->get_user_id();
} else {
// Otherwise, attribute the answer to the user who answered
// the question previously.
$answered_by = $existing_answers['purpose']['answeredBy'];
}
$setting_data['answeredBy'] = $answered_by;
$site_settings[ $setting_key ] = $setting_data;
} elseif ( 'user' === $setting_data['scope'] ) {
$user_settings[ $setting_key ] = $setting_data;
}
}
$this->site_specific_answers->set( $site_settings );
$this->user_specific_answers->set( $user_settings );
return $this->get_answers();
}
/**
* Gets an array of internal feature metrics.
*
* @since 1.163.0
*
* @return array
*/
public function get_feature_metrics() {
return array(
'site_purpose' => $this->site_specific_answers->get()['purpose']['values'] ?? array(),
);
}
}