summaryrefslogtreecommitdiff
blob: 170c4ba83fdbb2c1f83359d3de4119693041e3c1 (plain)
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
<?php

/**
 * simple wrapper that allows enumerating cached static instances
 * of sync modules
 */

require_once dirname( __FILE__ ) . '/class.jetpack-sync-module.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-posts.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-comments.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-constants.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-callables.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-options.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-network-options.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-updates.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-users.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-themes.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-menus.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-attachments.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-meta.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-terms.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-plugins.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-full-sync.php';
require_once dirname( __FILE__ ) . '/class.jetpack-sync-module-stats.php';

class Jetpack_Sync_Modules {

	private static $default_sync_modules = array(
		'Jetpack_Sync_Module_Constants',
		'Jetpack_Sync_Module_Callables',
		'Jetpack_Sync_Module_Options',
		'Jetpack_Sync_Module_Network_Options',
		'Jetpack_Sync_Module_Terms',
		'Jetpack_Sync_Module_Themes',
		'Jetpack_Sync_Module_Menus',
		'Jetpack_Sync_Module_Users',
		'Jetpack_Sync_Module_Posts',
		'Jetpack_Sync_Module_Comments',
		'Jetpack_Sync_Module_Updates',
		'Jetpack_Sync_Module_Attachments',
		'Jetpack_Sync_Module_Meta',
		'Jetpack_Sync_Module_Plugins',
		'Jetpack_Sync_Module_Full_Sync',
		'Jetpack_Sync_Module_Stats',
	);

	private static $initialized_modules = null;

	public static function get_modules() {
		if ( null === self::$initialized_modules ) {
			self::$initialized_modules = self::initialize_modules();
		}

		return self::$initialized_modules;
	}

	public static function set_defaults() {
		foreach ( self::get_modules() as $module ) {
			$module->set_defaults();
		}
	}

	public static function get_module( $module_name ) {
		foreach ( self::get_modules() as $module ) {
			if ( $module->name() === $module_name ) {
				return $module;
			}
		}

		return false;
	}

	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( 'Jetpack_Sync_Modules', 'load_module' ), $modules );

		return array_map( array( 'Jetpack_Sync_Modules', 'set_module_defaults' ), $modules );
	}

	static function load_module( $module_name ) {
		return new $module_name;
	}

	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;
	}

}