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
|
<?php
/**
* Simple wrapper that allows enumerating cached static instances
* of sync modules.
*
* @package automattic/jetpack-sync
*/
namespace Automattic\Jetpack\Sync;
use Automattic\Jetpack\Sync\Modules\Module;
/**
* 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(
'Automattic\\Jetpack\\Sync\\Modules\\Constants',
'Automattic\\Jetpack\\Sync\\Modules\\Callables',
'Automattic\\Jetpack\\Sync\\Modules\\Network_Options',
'Automattic\\Jetpack\\Sync\\Modules\\Options',
'Automattic\\Jetpack\\Sync\\Modules\\Terms',
'Automattic\\Jetpack\\Sync\\Modules\\Menus',
'Automattic\\Jetpack\\Sync\\Modules\\Themes',
'Automattic\\Jetpack\\Sync\\Modules\\Users',
'Automattic\\Jetpack\\Sync\\Modules\\Import',
'Automattic\\Jetpack\\Sync\\Modules\\Posts',
'Automattic\\Jetpack\\Sync\\Modules\\Protect',
'Automattic\\Jetpack\\Sync\\Modules\\Comments',
'Automattic\\Jetpack\\Sync\\Modules\\Updates',
'Automattic\\Jetpack\\Sync\\Modules\\Attachments',
'Automattic\\Jetpack\\Sync\\Modules\\Meta',
'Automattic\\Jetpack\\Sync\\Modules\\Plugins',
'Automattic\\Jetpack\\Sync\\Modules\\Stats',
'Automattic\\Jetpack\\Sync\\Modules\\Full_Sync_Immediately',
'Automattic\\Jetpack\\Sync\\Modules\\Term_Relationships',
);
/**
* 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 Module[]
*/
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( __CLASS__, 'load_module' ), $modules );
return array_map( array( __CLASS__, '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();
}
/**
* 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;
}
}
|