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
|
<?php
/**
* Simple wrapper that allows enumerating cached static instances
* of sync modules.
*
* @package automattic/jetpack-sync
*/
namespace Automattic\Jetpack\Sync;
/**
* A class to handle loading of sync modules.
*/
class Modules {
/**
* Lists classnames of sync modules we load by default.
*
* @access public
*
* @var array
*/
const DEFAULT_SYNC_MODULES = array(
'Jetpack_Sync_Modules_Constants',
'Jetpack_Sync_Modules_Callables',
'Jetpack_Sync_Modules_Network_Options',
'Jetpack_Sync_Modules_Options',
'Jetpack_Sync_Modules_Terms',
'Jetpack_Sync_Modules_Menus',
'Jetpack_Sync_Modules_Themes',
'Jetpack_Sync_Modules_Users',
'Jetpack_Sync_Modules_Import',
'Jetpack_Sync_Modules_Posts',
'Jetpack_Sync_Modules_Protect',
'Jetpack_Sync_Modules_Comments',
'Jetpack_Sync_Modules_Updates',
'Jetpack_Sync_Modules_Attachments',
'Jetpack_Sync_Modules_Meta',
'Jetpack_Sync_Modules_Plugins',
'Jetpack_Sync_Modules_Stats',
'Jetpack_Sync_Modules_Full_Sync',
'Automattic\\Jetpack\\Sync\\Modules\\Term_Relationships',
);
/**
* Maps classnames of sync modules before to v7.5 to classnames of sync modules after v7.5.
*
* @access public
*
* @var array
*/
const LEGACY_SYNC_MODULES_MAP = array(
'Jetpack_Sync_Modules_Constants' => 'Automattic\\Jetpack\\Sync\\Modules\\Constants',
'Jetpack_Sync_Modules_Callables' => 'Automattic\\Jetpack\\Sync\\Modules\\Callables',
'Jetpack_Sync_Modules_Network_Options' => 'Automattic\\Jetpack\\Sync\\Modules\\Network_Options',
'Jetpack_Sync_Modules_Options' => 'Automattic\\Jetpack\\Sync\\Modules\\Options',
'Jetpack_Sync_Modules_Terms' => 'Automattic\\Jetpack\\Sync\\Modules\\Terms',
'Jetpack_Sync_Modules_Menus' => 'Automattic\\Jetpack\\Sync\\Modules\\Menus',
'Jetpack_Sync_Modules_Themes' => 'Automattic\\Jetpack\\Sync\\Modules\\Themes',
'Jetpack_Sync_Modules_Users' => 'Automattic\\Jetpack\\Sync\\Modules\\Users',
'Jetpack_Sync_Modules_Import' => 'Automattic\\Jetpack\\Sync\\Modules\\Import',
'Jetpack_Sync_Modules_Posts' => 'Automattic\\Jetpack\\Sync\\Modules\\Posts',
'Jetpack_Sync_Modules_Protect' => 'Automattic\\Jetpack\\Sync\\Modules\\Protect',
'Jetpack_Sync_Modules_Comments' => 'Automattic\\Jetpack\\Sync\\Modules\\Comments',
'Jetpack_Sync_Modules_Updates' => 'Automattic\\Jetpack\\Sync\\Modules\\Updates',
'Jetpack_Sync_Modules_Attachments' => 'Automattic\\Jetpack\\Sync\\Modules\\Attachments',
'Jetpack_Sync_Modules_Meta' => 'Automattic\\Jetpack\\Sync\\Modules\\Meta',
'Jetpack_Sync_Modules_Plugins' => 'Automattic\\Jetpack\\Sync\\Modules\\Plugins',
'Jetpack_Sync_Modules_Stats' => 'Automattic\\Jetpack\\Sync\\Modules\\Stats',
'Jetpack_Sync_Modules_Full_Sync' => 'Automattic\\Jetpack\\Sync\\Modules\\Full_Sync',
);
/**
* Keeps track of initialized sync modules.
*
* @access private
* @static
*
* @var null|array
*/
private static $initialized_modules = null;
/**
* Gets a list of initialized modules.
*
* @access public
* @static
*
* @return array|null
*/
public static function get_modules() {
if ( null === self::$initialized_modules ) {
self::$initialized_modules = self::initialize_modules();
}
return self::$initialized_modules;
}
/**
* Sets defaults for all initialized modules.
*
* @access public
* @static
*/
public static function set_defaults() {
foreach ( self::get_modules() as $module ) {
$module->set_defaults();
}
}
/**
* Gets the name of an initialized module. Returns false if given module has not been initialized.
*
* @access public
* @static
*
* @param string $module_name A module name.
*
* @return bool|Automattic\Jetpack\Sync\Modules\Module
*/
public static function get_module( $module_name ) {
foreach ( self::get_modules() as $module ) {
if ( $module->name() === $module_name ) {
return $module;
}
}
return false;
}
/**
* Loads and sets defaults for all declared modules.
*
* @access public
* @static
*
* @return array
*/
public static function initialize_modules() {
/**
* Filters the list of class names of sync modules.
* If you add to this list, make sure any classes implement the
* Jetpack_Sync_Module interface.
*
* @since 4.2.0
*/
$modules = apply_filters( 'jetpack_sync_modules', self::DEFAULT_SYNC_MODULES );
$modules = array_map( array( 'Automattic\\Jetpack\\Sync\\Modules', 'map_legacy_modules' ), $modules );
$modules = array_map( array( 'Automattic\\Jetpack\\Sync\\Modules', 'load_module' ), $modules );
return array_map( array( 'Automattic\\Jetpack\\Sync\\Modules', 'set_module_defaults' ), $modules );
}
/**
* Returns an instance of the given module class.
*
* @access public
* @static
*
* @param string $module_class The classname of a Jetpack sync module.
*
* @return Automattic\Jetpack\Sync\Modules\Module
*/
public static function load_module( $module_class ) {
return new $module_class();
}
/**
* For backwards compat, takes the classname of a given module pre Jetpack 7.5,
* and returns the new namespaced classname.
*
* @access public
* @static
*
* @param string $module_class The classname of a Jetpack sync module.
*
* @return string
*/
public static function map_legacy_modules( $module_class ) {
$legacy_map = self::LEGACY_SYNC_MODULES_MAP;
if ( isset( $legacy_map[ $module_class ] ) ) {
return $legacy_map[ $module_class ];
}
return $module_class;
}
/**
* Sets defaults for the given instance of a Jetpack sync module.
*
* @access public
* @static
*
* @param Automattic\Jetpack\Sync\Modules\Module $module Instance of a Jetpack sync module.
*
* @return Automattic\Jetpack\Sync\Modules\Module
*/
public static function set_module_defaults( $module ) {
$module->set_defaults();
if ( method_exists( $module, 'set_late_default' ) ) {
add_action( 'init', array( $module, 'set_late_default' ), 90 );
}
return $module;
}
}
|