class-configuration-service.php
3.52 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
<?php
/**
* @package WPSEO\Admin\ConfigurationUI
*/
/**
* Class WPSEO_Configuration_Service
*/
class WPSEO_Configuration_Service {
/** @var WPSEO_Configuration_Structure */
protected $structure;
/** @var WPSEO_Configuration_Components */
protected $components;
/** @var WPSEO_Configuration_Storage */
protected $storage;
/** @var WPSEO_Configuration_Endpoint */
protected $endpoint;
/** @var WPSEO_Configuration_Options_Adapter */
protected $adapter;
/**
* Hook into the REST API and switch language.
*/
public function initialize() {
// Switch to the user locale with fallback to the site locale.
if ( function_exists( 'switch_to_locale' ) ) {
switch_to_locale( WPSEO_Utils::get_user_locale() );
}
// Make sure we have our translations available.
wpseo_load_textdomain();
$this->set_default_providers();
$this->populate_configuration();
$this->endpoint->register();
// @todo: check if this is really needed, since the switch happens only in the API.
if ( function_exists( 'restore_current_locale' ) ) {
restore_current_locale();
}
}
/**
* Set default handlers
*/
public function set_default_providers() {
$this->set_storage( new WPSEO_Configuration_Storage() );
$this->set_options_adapter( new WPSEO_Configuration_Options_Adapter() );
$this->set_components( new WPSEO_Configuration_Components() );
$this->set_endpoint( new WPSEO_Configuration_Endpoint() );
$this->set_structure( new WPSEO_Configuration_Structure() );
}
/**
* Set storage handler
*
* @param WPSEO_Configuration_Storage $storage Storage handler to use.
*/
public function set_storage( WPSEO_Configuration_Storage $storage ) {
$this->storage = $storage;
}
/**
* Set endpoint handler
*
* @param WPSEO_Configuration_Endpoint $endpoint Endpoint implementation to use.
*/
public function set_endpoint( WPSEO_Configuration_Endpoint $endpoint ) {
$this->endpoint = $endpoint;
$this->endpoint->set_service( $this );
}
/**
* Set the options adapter
*
* @param WPSEO_Configuration_Options_Adapter $adapter Adapter to use.
*/
public function set_options_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
$this->adapter = $adapter;
}
/**
* Set components provider
*
* @param WPSEO_Configuration_Components $components Component provider to use.
*/
public function set_components( WPSEO_Configuration_Components $components ) {
$this->components = $components;
}
/**
* Set structure provider
*
* @param WPSEO_Configuration_Structure $structure Structure provider to use.
*/
public function set_structure( WPSEO_Configuration_Structure $structure ) {
$this->structure = $structure;
}
/**
* Populate the configuration
*/
protected function populate_configuration() {
$this->storage->set_adapter( $this->adapter );
$this->storage->add_default_fields();
$this->components->initialize();
$this->components->set_storage( $this->storage );
}
/**
* Used by endpoint to retrieve configuration
*
* @return array List of settings.
*/
public function get_configuration() {
$fields = $this->storage->retrieve();
$steps = $this->structure->retrieve();
return array(
'fields' => $fields,
'steps' => $steps,
);
}
/**
* Used by endpoint to store changes
*
* @param WP_REST_Request $request Request from the REST API.
*
* @return array List of feedback per option if saving succeeded.
*/
public function set_configuration( WP_REST_Request $request ) {
$this->populate_configuration();
return $this->storage->store( $request->get_json_params() );
}
}