Completed
Push — update/refactor_autoloader_fun... ( df4d56...25efa2 )
by
unknown
70:40 queued 63:55
created

functions.php ➔ set_up_autoloader()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
global $jetpack_packages_classmap;
5
global $jetpack_packages_filemap;
6
global $jetpack_autoloader_activating_plugins;
7
8
if ( ! is_array( $jetpack_packages_classmap ) ) {
9
	$jetpack_packages_classmap = array();
10
}
11
12
if ( ! is_array( $jetpack_packages_filemap ) ) {
13
	$jetpack_packages_filemap = array();
14
}
15
16
if ( ! is_array( $jetpack_autoloader_activating_plugins ) ) {
17
	$jetpack_autoloader_activating_plugins = array();
18
}
19
20
/**
21
 * Used for autoloading jetpack packages.
22
 *
23
 * @param string $class_name Class Name to load.
24
 */
25
function autoloader( $class_name ) {
26
	global $jetpack_packages_classmap;
27
28
	if ( isset( $jetpack_packages_classmap[ $class_name ] ) ) {
29
		if ( file_exists( $jetpack_packages_classmap[ $class_name ]['path'] ) ) {
30
			require_once $jetpack_packages_classmap[ $class_name ]['path'];
31
			return true;
32
		}
33
	}
34
35
	return false;
36
}
37
38
/**
39
 * Used for running the code that initializes class and file maps.
40
 *
41
 * @param Plugins_Handler $plugins_handler The Plugins_Handler object.
42
 */
43
function enqueue_files( $plugins_handler ) {
44
	require_once __DIR__ . '/class-classes-handler.php';
45
	require_once __DIR__ . '/class-files-handler.php';
46
47
	$classes_handler = new Classes_Handler( $plugins_handler );
48
	$classes_handler->set_class_paths();
49
50
	$files_handler = new Files_Handler( $plugins_handler );
51
	$files_handler->set_file_paths();
52
53
	$files_handler->file_loader();
54
}
55
56
/**
57
 * Finds the latest installed autoloader. If this is the latest autoloader, sets
58
 * up the classmap and filemap.
59
 */
60
function set_up_autoloader() {
61
	global $jetpack_autoloader_latest_version;
62
	global $jetpack_packages_classmap;
63
64
	require_once __DIR__ . '/class-plugins-handler.php';
65
	require_once __DIR__ . '/class-autoloader-handler.php';
66
67
	$plugins_handler    = new Plugins_Handler();
68
	$autoloader_handler = new Autoloader_Handler( $plugins_handler );
69
70
	if ( $plugins_handler->should_autoloader_reset() ) {
71
		/*
72
		 * The autoloader must be reset when an activating plugin that was
73
		 * previously unknown is detected.
74
		 */
75
		$jetpack_autoloader_latest_version = null;
76
		$jetpack_packages_classmap         = array();
77
	}
78
79
	// Find the latest autoloader.
80
	if ( ! $jetpack_autoloader_latest_version ) {
81
		$autoloader_handler->find_latest_autoloader();
82
	}
83
84
	$current_autoloader_version = $autoloader_handler->get_current_autoloader_version();
85
86
	// This is the latest autoloader, so generate the classmap and filemap and register the autoloader function.
87
	if ( empty( $jetpack_packages_classmap ) && $current_autoloader_version === $jetpack_autoloader_latest_version ) {
88
		enqueue_files( $plugins_handler );
89
		$autoloader_handler->update_autoloader_chain();
90
	}
91
}
92