summaryrefslogtreecommitdiff
blob: 3a9638571ce5fc2f88116828caa3bb82fba00be5 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
 * This file `autoload_packages.php`was generated by automattic/jetpack-autoloader.
 *
 * From your plugin include this file with:
 * require_once . plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';
 *
 * @package automattic/jetpack-autoloader
 */

// phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
// phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_ns_cFound

namespace Automattic\Jetpack\Autoloader;

if ( ! function_exists( __NAMESPACE__ . '\enqueue_package_class' ) ) {
	global $jetpack_packages_classes;

	if ( ! is_array( $jetpack_packages_classes ) ) {
		$jetpack_packages_classes = array();
	}
	/**
	 * Adds the version of a package to the $jetpack_packages global array so that
	 * the autoloader is able to find it.
	 *
	 * @param string $class_name Name of the class that you want to autoload.
	 * @param string $version Version of the class.
	 * @param string $path Absolute path to the class so that we can load it.
	 */
	function enqueue_package_class( $class_name, $version, $path ) {
		global $jetpack_packages_classes;

		if ( ! isset( $jetpack_packages_classes[ $class_name ] ) ) {
			$jetpack_packages_classes[ $class_name ] = array(
				'version' => $version,
				'path'    => $path,
			);
		}
		// If we have a @dev version set always use that one!
		if ( 'dev-' === substr( $jetpack_packages_classes[ $class_name ]['version'], 0, 4 ) ) {
			return;
		}

		// Always favour the @dev version. Since that version is the same as bleeding edge.
		// We need to make sure that we don't do this in production!
		if ( 'dev-' === substr( $version, 0, 4 ) ) {
			$jetpack_packages_classes[ $class_name ] = array(
				'version' => $version,
				'path'    => $path,
			);

			return;
		}
		// Set the latest version!
		if ( version_compare( $jetpack_packages_classes[ $class_name ]['version'], $version, '<' ) ) {
			$jetpack_packages_classes[ $class_name ] = array(
				'version' => $version,
				'path'    => $path,
			);
		}
	}
}

if ( ! function_exists( __NAMESPACE__ . '\autoloader' ) ) {
	/**
	 * Used for autoloading jetpack packages.
	 *
	 * @param string $class_name Class Name to load.
	 */
	function autoloader( $class_name ) {
		global $jetpack_packages_classes;

		if ( isset( $jetpack_packages_classes[ $class_name ] ) ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				// TODO ideally we shouldn't skip any of these, see: https://github.com/Automattic/jetpack/pull/12646.
				$ignore = in_array(
					$class_name,
					array(
						'Automattic\Jetpack\JITM',
						'Automattic\Jetpack\Connection\Manager',
						'Automattic\Jetpack\Connection\Manager_Interface',
						'Automattic\Jetpack\Connection\XMLRPC_Connector',
						'Jetpack_IXR_Client',
						'Jetpack_Options',
						'Jetpack_Signature',
						'Jetpack_XMLRPC_Server',
						'Automattic\Jetpack\Sync\Main',
						'Automattic\Jetpack\Constants',
						'Automattic\Jetpack\Tracking',
						'Automattic\Jetpack\Plugin\Tracking',
					),
					true
				);
				if ( ! $ignore && function_exists( 'did_action' ) && ! did_action( 'plugins_loaded' ) ) {
					_doing_it_wrong(
						esc_html( $class_name ),
						sprintf(
							/* translators: %s Name of a PHP Class */
							esc_html__( 'Not all plugins have loaded yet but we requested the class %s', 'jetpack' ),
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							$class_name
						),
						esc_html( $jetpack_packages_classes[ $class_name ]['version'] )
					);
				}
			}

			if ( file_exists( $jetpack_packages_classes[ $class_name ]['path'] ) ) {
				require_once $jetpack_packages_classes[ $class_name ]['path'];

				return true;
			}
		}

		return false;
	}

	// Add the jetpack autoloader.
	spl_autoload_register( __NAMESPACE__ . '\autoloader' );
}
/**
 * Prepare all the classes for autoloading.
 */
function enqueue_packages_e5e1fcbf7e161455e15277144d4cf8d1() {
	$class_map = require_once dirname( __FILE__ ) . '/composer/autoload_classmap_package.php';
	foreach ( $class_map as $class_name => $class_info ) {
		enqueue_package_class( $class_name, $class_info['version'], $class_info['path'] );
	}

	$autoload_file = __DIR__ . '/composer/autoload_files.php';
	$includeFiles = file_exists( $autoload_file )
		? require $autoload_file
		: [];

	foreach ( $includeFiles as $fileIdentifier => $file ) {
		if ( empty( $GLOBALS['__composer_autoload_files'][ $fileIdentifier ] ) ) {
			require $file;

			$GLOBALS['__composer_autoload_files'][ $fileIdentifier ] = true;
		}
	}
}
enqueue_packages_e5e1fcbf7e161455e15277144d4cf8d1();