modules
3 weeks ago
replicastore
3 months ago
sync-queue
5 months ago
class-actions.php
1 month ago
class-activity-log-event.php
3 weeks ago
class-data-settings.php
1 month ago
class-dedicated-sender.php
7 months ago
class-default-filter-settings.php
4 years ago
class-defaults.php
2 days ago
class-functions.php
1 month ago
class-health.php
4 months ago
class-json-deflate-array-codec.php
7 months ago
class-listener.php
1 month ago
class-lock.php
4 years ago
class-main.php
2 months ago
class-modules.php
1 year ago
class-package-version.php
2 days ago
class-queue-buffer.php
4 years ago
class-queue.php
1 month ago
class-replicastore.php
1 month ago
class-rest-endpoints.php
4 months ago
class-rest-sender.php
1 month ago
class-sender.php
1 month ago
class-server.php
4 months ago
class-settings.php
3 months ago
class-simple-codec.php
7 months ago
class-users.php
3 months ago
class-utils.php
4 years ago
interface-codec.php
4 years ago
interface-replicastore.php
1 month ago
class-modules.php
166 lines
| 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 | $modules = array_unique( $modules ); |
| 128 | |
| 129 | $modules = array_map( array( __CLASS__, 'load_module' ), $modules ); |
| 130 | return array_map( array( __CLASS__, 'set_module_defaults' ), $modules ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns an instance of the given module class. |
| 135 | * |
| 136 | * @access public |
| 137 | * @static |
| 138 | * |
| 139 | * @param string $module_class The classname of a Jetpack sync module. |
| 140 | * |
| 141 | * @return \Automattic\Jetpack\Sync\Modules\Module |
| 142 | */ |
| 143 | public static function load_module( $module_class ) { |
| 144 | return new $module_class(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets defaults for the given instance of a Jetpack sync module. |
| 149 | * |
| 150 | * @access public |
| 151 | * @static |
| 152 | * |
| 153 | * @param \Automattic\Jetpack\Sync\Modules\Module $module Instance of a Jetpack sync module. |
| 154 | * |
| 155 | * @return \Automattic\Jetpack\Sync\Modules\Module |
| 156 | */ |
| 157 | public static function set_module_defaults( $module ) { |
| 158 | $module->set_defaults(); |
| 159 | if ( method_exists( $module, 'set_late_default' ) ) { |
| 160 | // @phan-suppress-next-line PhanUndeclaredMethodInCallable -- https://github.com/phan/phan/issues/1204 |
| 161 | add_action( 'init', array( $module, 'set_late_default' ), 90 ); |
| 162 | } |
| 163 | return $module; |
| 164 | } |
| 165 | } |
| 166 |