Completed
Push — update/move_rest_auth_to_conne... ( 7fd674...1e3e36 )
by
unknown
14:33 queued 06:47
created

functions.php ➔ reset_maps_after_update()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 3
dl 0
loc 36
rs 9.0328
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_paths;
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_paths ) ) {
17
	$jetpack_autoloader_activating_plugins_paths = array();
18
}
19
20
/**
21
 * Used for autoloading jetpack packages.
22
 *
23
 * @param string $class_name Class Name to load.
24
 *
25
 * @return Boolean Whether the class_name was found in the classmap.
26
 */
27
function autoloader( $class_name ) {
28
	global $jetpack_packages_classmap;
29
30
	if ( isset( $jetpack_packages_classmap[ $class_name ] ) ) {
31
		require_once $jetpack_packages_classmap[ $class_name ]['path'];
32
		return true;
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
 * @param Version_Selector $version_selector The Version_Selector object.
43
 */
44
function enqueue_files( $plugins_handler, $version_selector ) {
45
	require_once __DIR__ . '/../class-classes-handler.php';
46
	require_once __DIR__ . '/../class-files-handler.php';
47
48
	$classes_handler = new Classes_Handler( $plugins_handler, $version_selector );
49
	$classes_handler->set_class_paths();
50
51
	$files_handler = new Files_Handler( $plugins_handler, $version_selector );
52
	$files_handler->set_file_paths();
53
54
	$files_handler->file_loader();
55
}
56
57
/**
58
 * Finds the latest installed autoloader. If this is the latest autoloader, sets
59
 * up the classmap and filemap.
60
 */
61
function set_up_autoloader() {
62
	global $jetpack_autoloader_latest_version;
63
	global $jetpack_packages_classmap;
64
65
	require_once __DIR__ . '/../class-plugins-handler.php';
66
	require_once __DIR__ . '/../class-version-selector.php';
67
	require_once __DIR__ . '/../class-autoloader-handler.php';
68
69
	$plugins_handler    = new Plugins_Handler();
70
	$version_selector   = new Version_Selector();
71
	$autoloader_handler = new Autoloader_Handler( $plugins_handler, $version_selector );
72
73
	if ( $plugins_handler->should_autoloader_reset() ) {
74
		/*
75
		 * The autoloader must be reset when an activating plugin that was
76
		 * previously unknown is detected.
77
		 */
78
		$jetpack_autoloader_latest_version = null;
79
		$jetpack_packages_classmap         = array();
80
	}
81
82
	// Find the latest autoloader.
83
	if ( ! $jetpack_autoloader_latest_version ) {
84
		$autoloader_handler->find_latest_autoloader();
85
	}
86
87
	$current_autoloader_version = $autoloader_handler->get_current_autoloader_version();
88
89
	// This is the latest autoloader, so generate the classmap and filemap and register the autoloader function.
90
	if ( empty( $jetpack_packages_classmap ) && $current_autoloader_version === $jetpack_autoloader_latest_version ) {
91
		enqueue_files( $plugins_handler, $version_selector );
92
		$autoloader_handler->update_autoloader_chain();
93
		add_filter( 'upgrader_post_install', __NAMESPACE__ . '\reset_maps_after_update', 0, 3 );
94
	}
95
}
96
97
/**
98
 * Resets the autoloader after a plugin update.
99
 *
100
 * @param bool  $response   Installation response.
101
 * @param array $hook_extra Extra arguments passed to hooked filters.
102
 * @param array $result     Installation result data.
103
 *
104
 * @return bool The passed in $response param.
105
 */
106
function reset_maps_after_update( $response, $hook_extra, $result ) {
107
	global $jetpack_packages_classmap;
108
109
	if ( isset( $hook_extra['plugin'] ) ) {
110
		/*
111
		 * $hook_extra['plugin'] is the path to the plugin file relative to the plugins directory:
112
		 * https://core.trac.wordpress.org/browser/tags/5.4/src/wp-admin/includes/class-wp-upgrader.php#L701
113
		 */
114
		$plugin = $hook_extra['plugin'];
115
116
		if ( false === strpos( $plugin, '/', 1 ) ) {
117
			// Single-file plugins don't use packages, so bail.
118
			return $response;
119
		}
120
121
		if ( ! is_plugin_active( $plugin ) ) {
122
			// The updated plugin isn't active, so bail.
123
			return $response;
124
		}
125
126
		/*
127
		 * $plugin is the path to the plugin file relative to the plugins directory.
128
		 */
129
		$plugin_dir  = str_replace( '\\', '/', WP_PLUGIN_DIR );
130
		$plugin_path = trailingslashit( $plugin_dir ) . trailingslashit( explode( '/', $plugin )[0] );
131
132
		if ( is_readable( $plugin_path . 'vendor/jetpack-autoloader/autoload_functions.php' ) ) {
133
			// The plugin has a >=v2.2 autoloader, so reset the classmap.
134
			$jetpack_packages_classmap = array();
135
136
			set_up_autoloader();
137
		}
138
	}
139
140
	return $response;
141
}
142