Completed
Push — add/autoloader-meta ( 27b77b...a00e63 )
by
unknown
28:53 queued 22:12
created

autoload.php ➔ enqueue_files()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 24
nop 0
dl 0
loc 32
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file `autoload_packages.php`was generated by automattic/jetpack-autoloader.
4
 *
5
 * From your plugin include this file with:
6
 * require_once . plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';
7
 *
8
 * @package automattic/jetpack-autoloader
9
 */
10
11
// phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
12
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
13
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_ns_cFound
14
15
namespace Automattic\Jetpack\Autoloader; // Will be amended with a suffix in the generated file.
16
17
global $jetpack_packages_classes;
18
global $jetpack_packages_files;
19
20
if ( ! is_array( $jetpack_packages_classes ) ) {
21
	$jetpack_packages_classes = array();
22
}
23
24
if ( ! is_array( $jetpack_packages_files ) ) {
25
	$jetpack_packages_files = array();
26
}
27
28
/**
29
 * Adds the version of a package to the $jetpack_packages global array so that
30
 * the autoloader is able to find it.
31
 *
32
 * @param string $class_name Name of the class that you want to autoload.
33
 * @param string $version Version of the class.
34
 * @param string $path Absolute path to the class so that we can load it.
35
 */
36
function enqueue_package_class( $class_name, $version, $path ) {
37
	global $jetpack_packages_classes;
38
39
	if ( ! isset( $jetpack_packages_classes[ $class_name ] ) ) {
40
		$jetpack_packages_classes[ $class_name ] = array(
41
			'version' => $version,
42
			'path'    => $path,
43
		);
44
45
		return;
46
	}
47
	// If we have a @dev version set always use that one!
48
	if ( 'dev-' === substr( $jetpack_packages_classes[ $class_name ]['version'], 0, 4 ) ) {
49
		return;
50
	}
51
52
	// Always favour the @dev version. Since that version is the same as bleeding edge.
53
	// We need to make sure that we don't do this in production!
54
	if ( 'dev-' === substr( $version, 0, 4 ) ) {
55
		$jetpack_packages_classes[ $class_name ] = array(
56
			'version' => $version,
57
			'path'    => $path,
58
		);
59
60
		return;
61
	}
62
	// Set the latest version!
63
	if ( version_compare( $jetpack_packages_classes[ $class_name ]['version'], $version, '<' ) ) {
64
		$jetpack_packages_classes[ $class_name ] = array(
65
			'version' => $version,
66
			'path'    => $path,
67
		);
68
	}
69
}
70
71
/**
72
 * Adds the version of a package file to the $jetpack_packages_files global array so that
73
 * we can load the most recent version after 'plugins_loaded'.
74
 *
75
 * @param string $file_identifier Unique id to file assigned by composer based on package name and filename.
76
 * @param string $version Version of the file.
77
 * @param string $path Absolute path to the file so that we can load it.
78
 */
79
function enqueue_package_file( $file_identifier, $version, $path ) {
80
	global $jetpack_packages_files;
81
82
	if ( ! isset( $jetpack_packages_files[ $file_identifier ] ) ) {
83
		$jetpack_packages_files[ $file_identifier ] = array(
84
			'version' => $version,
85
			'path'    => $path,
86
		);
87
88
		return;
89
	}
90
	// If we have a @dev version set always use that one!
91
	if ( 'dev-' === substr( $jetpack_packages_files[ $file_identifier ]['version'], 0, 4 ) ) {
92
		return;
93
	}
94
95
	// Always favour the @dev version. Since that version is the same as bleeding edge.
96
	// We need to make sure that we don't do this in production!
97
	if ( 'dev-' === substr( $version, 0, 4 ) ) {
98
		$jetpack_packages_files[ $file_identifier ] = array(
99
			'version' => $version,
100
			'path'    => $path,
101
		);
102
103
		return;
104
	}
105
	// Set the latest version!
106
	if ( version_compare( $jetpack_packages_files[ $file_identifier ]['version'], $version, '<' ) ) {
107
		$jetpack_packages_files[ $file_identifier ] = array(
108
			'version' => $version,
109
			'path'    => $path,
110
		);
111
	}
112
}
113
114
/**
115
 * Include latest version of all enqueued files. Should be called after all plugins are loaded.
116
 */
117
function file_loader() {
118
	global $jetpack_packages_files;
119
	foreach ( $jetpack_packages_files as $file_identifier => $file_data ) {
120
		if ( empty( $GLOBALS['__composer_autoload_files'][ $file_identifier ] ) ) {
121
			require $file_data['path'];
122
123
			$GLOBALS['__composer_autoload_files'][ $file_identifier ] = true;
124
		}
125
	}
126
}
127
128
/**
129
 * Used for autoloading jetpack packages.
130
 *
131
 * @param string $class_name Class Name to load.
132
 */
133
function autoloader( $class_name ) {
134
	global $jetpack_packages_classes;
135
136
	if ( isset( $jetpack_packages_classes[ $class_name ] ) ) {
137
		if ( file_exists( $jetpack_packages_classes[ $class_name ]['path'] ) ) {
138
			require_once $jetpack_packages_classes[ $class_name ]['path'];
139
			return true;
140
		}
141
	}
142
143
	return false;
144
}
145
146
// Add the jetpack autoloader.
147
spl_autoload_register( __NAMESPACE__ . '\autoloader' );
148
149
/**
150
 * Used for running the code that initializes class and file maps.
151
 */
152
function enqueue_files() {
153
	$class_map = require dirname( __FILE__ ) . '/composer/jetpack_autoload_classmap.php';
154
155
	foreach ( $class_map as $class_name => $class_info ) {
156
		enqueue_package_class( $class_name, $class_info['version'], $class_info['path'] );
157
	}
158
159
	$autoload_file = dirname( __FILE__ ) . '/composer/jetpack_autoload_filemap.php';
160
161
	$include_files = file_exists( $autoload_file ) ? require $autoload_file : array();
162
163
	foreach ( $include_files as $file_identifier => $file_data ) {
164
		enqueue_package_file( $file_identifier, $file_data['version'], $file_data['path'] );
165
	}
166
167
	if (
168
		function_exists( 'has_action' )
169
		&& function_exists( 'did_action' )
170
		&& ! did_action( 'plugins_loaded' )
171
		&& false === has_action( 'plugins_loaded', __NAMESPACE__ . '\file_loader' )
172
	) {
173
		// Add action if it has not been added and has not happened yet.
174
		// Priority -10 to load files as early as possible in case plugins try to use them during `plugins_loaded`.
175
		add_action( 'plugins_loaded', __NAMESPACE__ . '\file_loader', 0, -10 );
176
177
	} elseif (
178
		! function_exists( 'did_action' )
179
		|| did_action( 'plugins_loaded' )
180
	) {
181
		file_loader(); // Either WordPress is not loaded or plugin is doing it wrong. Either way we'll load the files so nothing breaks.
182
	}
183
}
184