From 49f49de74c1b3eb7a2f21bac1485e81e9948be12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Thu, 22 Jun 2023 06:25:29 +0000 Subject: [PATCH 1/2] Introduce autoloader --- site-performance-tracker.php | 86 ++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/site-performance-tracker.php b/site-performance-tracker.php index 318fcb5..e476fd8 100644 --- a/site-performance-tracker.php +++ b/site-performance-tracker.php @@ -21,8 +21,8 @@ /** * Admin notice for incompatible versions of PHP. */ -function site_performance_tracker_php_version_error() { - printf( '

%s

', esc_html( site_performance_tracker_php_version_text() ) ); +function xwp_site_performance_tracker_php_version_error() { + printf( '

%s

', esc_html( xwp_site_performance_tracker_php_version_text() ) ); } /** @@ -33,41 +33,10 @@ function site_performance_tracker_php_version_error() { * * @return string */ -function site_performance_tracker_php_version_text() { +function xwp_site_performance_tracker_php_version_text() { return __( 'Site Performance Tracker plugin error: Your version of PHP is too old to run this plugin. You must be running PHP 5.3 or higher.', 'site-performance-tracker' ); } -// If the PHP version is too low, show warning and return. -if ( version_compare( phpversion(), '5.3', '<' ) ) { - if ( defined( 'WP_CLI' ) ) { - WP_CLI::warning( site_performance_tracker_php_version_text() ); - } else { - add_action( 'admin_notices', 'site_performance_tracker_php_version_error' ); - } - - return; -} - -/** - * Use includes to simplify the plugin distribution and usage on - * platforms on platforms that don't use Composer autoloader. - * - * @todo Consider supporting Composer classmap autoload (to match - * the filename requirements per PHPCS) after figuring out - * how to handle built JS and the presence of `vendor` directory. - */ -require_once __DIR__ . '/php/src/class-plugin.php'; -require_once __DIR__ . '/php/src/class-settings.php'; -require_once __DIR__ . '/php/src/class-fieldbase.php'; -require_once __DIR__ . '/php/src/class-dimensionfieldbase.php'; -require_once __DIR__ . '/php/src/class-analyticstypesfield.php'; -require_once __DIR__ . '/php/src/class-analyticsidfield.php'; -require_once __DIR__ . '/php/src/class-measurementversiondimensionfield.php'; -require_once __DIR__ . '/php/src/class-eventmetadimensionfield.php'; -require_once __DIR__ . '/php/src/class-eventdebugdimensionfield.php'; -require_once __DIR__ . '/php/src/class-webvitalstrackingratiofield.php'; -require_once __DIR__ . '/php/helpers.php'; - /** * Global function to provide access to the plugin APIs. * @@ -83,5 +52,54 @@ function xwp_site_performance_tracker() { return $plugin; } +/** + * Global function to provide access to the plugin APIs. + * + * @param class-string $class_name The fully-qualified class name. + * + * @return void + */ +function xwp_site_performance_autoloader( $class_name ) { + $project_namespace = 'XWP\\Site_Performance_Tracker\\'; + $length = strlen( $project_namespace ); + + // Class is not in our namespace. + if ( 0 !== strncmp( $project_namespace, $class_name, $length ) ) { + return; + } + + $relative_class_name = substr( $class_name, $length ); + $name_parts = explode( '\\', strtolower( str_replace( '_', '-', $relative_class_name ) ) ); + $last_part = array_pop( $name_parts ); + + $file = sprintf( + '%1$s/php/src%2$s/class-%3$s.php', + __DIR__, + array() === $name_parts ? '' : '/' . implode( '/', $name_parts ), + $last_part + ); + + if ( ! is_file( $file ) ) { + return; + } + + require $file; +} + +// If the PHP version is too low, show warning and return. +if ( version_compare( phpversion(), '5.3', '<' ) ) { + if ( defined( 'WP_CLI' ) ) { + WP_CLI::warning( xwp_site_performance_tracker_php_version_text() ); + } else { + add_action( 'admin_notices', 'xwp_site_performance_tracker_php_version_error' ); + } + + return; +} + +// Register autoloader and load helpers. +spl_autoload_register( 'xwp_site_performance_autoloader' ); +require_once __DIR__ . '/php/helpers.php'; + // Initialize the plugin. add_action( 'init', array( xwp_site_performance_tracker(), 'init' ) ); From b8fb3f61514ce7c44a2bed3a99f61a1a3ac477b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Thu, 22 Jun 2023 07:14:44 +0000 Subject: [PATCH 2/2] Fix copypasta --- site-performance-tracker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site-performance-tracker.php b/site-performance-tracker.php index e476fd8..1b0aef3 100644 --- a/site-performance-tracker.php +++ b/site-performance-tracker.php @@ -53,7 +53,7 @@ function xwp_site_performance_tracker() { } /** - * Global function to provide access to the plugin APIs. + * Load Site Performance Tracker classes. * * @param class-string $class_name The fully-qualified class name. *