Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce autoloader #104

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 52 additions & 34 deletions site-performance-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
/**
* Admin notice for incompatible versions of PHP.
*/
function site_performance_tracker_php_version_error() {
printf( '<div class="error"><p>%s</p></div>', esc_html( site_performance_tracker_php_version_text() ) );
function xwp_site_performance_tracker_php_version_error() {
printf( '<div class="error"><p>%s</p></div>', esc_html( xwp_site_performance_tracker_php_version_text() ) );
}

/**
Expand All @@ -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 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.
*
Expand All @@ -83,5 +52,54 @@ function xwp_site_performance_tracker() {
return $plugin;
}

/**
* Load Site Performance Tracker classes.
*
* @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' ) );