PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
jetpack / jetpack_vendor / automattic / jetpack-sync / src / class-modules.php

class-modules.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.1, at jetpack_vendor/automattic/jetpack-sync/src/class-modules.php

167 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Simple wrapper that allows enumerating cached static instances
4 * of sync modules.
5 *
6 * @package automattic/jetpack-sync
7 */
8
9 namespace Automattic\Jetpack\Sync;
10
11 use Automattic\Jetpack\Sync\Modules\Module;
12
13 /**
14 * A class to handle loading of sync modules.
15 */
16 class Modules {
17
18 /**
19 * Lists classnames of sync modules we load by default.
20 *
21 * @access public
22 *
23 * @var array
24 */
25 const DEFAULT_SYNC_MODULES = array(
26 'Automattic\\Jetpack\\Sync\\Modules\\Constants',
27 'Automattic\\Jetpack\\Sync\\Modules\\Callables',
28 'Automattic\\Jetpack\\Sync\\Modules\\Network_Options',
29 'Automattic\\Jetpack\\Sync\\Modules\\Options',
30 'Automattic\\Jetpack\\Sync\\Modules\\Terms',
31 'Automattic\\Jetpack\\Sync\\Modules\\Menus',
32 'Automattic\\Jetpack\\Sync\\Modules\\Themes',
33 'Automattic\\Jetpack\\Sync\\Modules\\Users',
34 'Automattic\\Jetpack\\Sync\\Modules\\Import',
35 'Automattic\\Jetpack\\Sync\\Modules\\Posts',
36 'Automattic\\Jetpack\\Sync\\Modules\\Protect',
37 'Automattic\\Jetpack\\Sync\\Modules\\Comments',
38 'Automattic\\Jetpack\\Sync\\Modules\\Updates',
39 'Automattic\\Jetpack\\Sync\\Modules\\Attachments',
40 'Automattic\\Jetpack\\Sync\\Modules\\Meta',
41 'Automattic\\Jetpack\\Sync\\Modules\\Plugins',
42 'Automattic\\Jetpack\\Sync\\Modules\\Stats',
43 'Automattic\\Jetpack\\Sync\\Modules\\Full_Sync_Immediately',
44 'Automattic\\Jetpack\\Sync\\Modules\\Term_Relationships',
45 );
46
47 /**
48 * Keeps track of initialized sync modules.
49 *
50 * @access private
51 * @static
52 *
53 * @var null|array
54 */
55 private static $initialized_modules = null;
56
57 /**
58 * Gets a list of initialized modules.
59 *
60 * @access public
61 * @static
62 *
63 * @return Module[]
64 */
65 public static function get_modules() {
66 if ( null === self::$initialized_modules ) {
67 self::$initialized_modules = self::initialize_modules();
68 }
69
70 return self::$initialized_modules;
71 }
72
73 /**
74 * Sets defaults for all initialized modules.
75 *
76 * @access public
77 * @static
78 */
79 public static function set_defaults() {
80 foreach ( self::get_modules() as $module ) {
81 $module->set_defaults();
82 }
83 }
84
85 /**
86 * Gets the name of an initialized module. Returns false if given module has not been initialized.
87 *
88 * @access public
89 * @static
90 *
91 * @param string $module_name A module name.
92 *
93 * @return bool|\Automattic\Jetpack\Sync\Modules\Module
94 */
95 public static function get_module( $module_name ) {
96 // @todo Better type hinting for Phan if https://github.com/phan/phan/issues/3842 gets fixed. Then clean up the `@phan-var` on all the callers.
97
98 foreach ( self::get_modules() as $module ) {
99 if ( $module->name() === $module_name ) {
100 return $module;
101 }
102 }
103
104 return false;
105 }
106
107 /**
108 * Loads and sets defaults for all declared modules.
109 *
110 * @access public
111 * @static
112 *
113 * @return array
114 */
115 public static function initialize_modules() {
116
117 /**
118 * Filters the list of class names of sync modules.
119 * If you add to this list, make sure any classes implement the
120 * Jetpack_Sync_Module interface.
121 *
122 * @since 1.6.3
123 * @since-jetpack 4.2.0
124 */
125 $modules = apply_filters( 'jetpack_sync_modules', self::DEFAULT_SYNC_MODULES );
126
127 // Exact class duplicates keep the position of their first contribution.
128 $modules = array_unique( $modules );
129
130 $modules = array_map( array( __CLASS__, 'load_module' ), $modules );
131 return array_map( array( __CLASS__, 'set_module_defaults' ), $modules );
132 }
133
134 /**
135 * Returns an instance of the given module class.
136 *
137 * @access public
138 * @static
139 *
140 * @param string $module_class The classname of a Jetpack sync module.
141 *
142 * @return \Automattic\Jetpack\Sync\Modules\Module
143 */
144 public static function load_module( $module_class ) {
145 return new $module_class();
146 }
147
148 /**
149 * Sets defaults for the given instance of a Jetpack sync module.
150 *
151 * @access public
152 * @static
153 *
154 * @param \Automattic\Jetpack\Sync\Modules\Module $module Instance of a Jetpack sync module.
155 *
156 * @return \Automattic\Jetpack\Sync\Modules\Module
157 */
158 public static function set_module_defaults( $module ) {
159 $module->set_defaults();
160 if ( method_exists( $module, 'set_late_default' ) ) {
161 // @phan-suppress-next-line PhanUndeclaredMethodInCallable -- https://github.com/phan/phan/issues/1204
162 add_action( 'init', array( $module, 'set_late_default' ), 90 );
163 }
164 return $module;
165 }
166 }
167