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

Add an Autoloader based on the Plugin Directory, for autoloading of utilities #331

Merged
merged 6 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
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
93 changes: 93 additions & 0 deletions mu-plugins/autoloader/class-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace WordPressdotorg\Autoload;

/**
* An Autoloader which respects WordPress's filename standards.
*
* @package WordPressdotorg\MU_Plugins\Utilities\Autoloader
*/
class Autoloader {

/**
* Namespace separator.
*/
const NS_SEPARATOR = '\\';

/**
* The prefix to compare classes against.
*
* @var string
* @access protected
*/
protected $prefix;

/**
* Length of the prefix string.
*
* @var int
* @access protected
*/
protected $prefix_length;

/**
* Path to the file to be loaded.
*
* @var string
* @access protected
*/
protected $path;

/**
* Constructor.
*
* @param string $prefix Prefix all classes have in common.
* @param string $path Path to the files to be loaded.
*/
public function __construct( $prefix, $path ) {
$this->prefix = $prefix;
$this->prefix_length = strlen( $prefix );
$this->path = rtrim( $path . '/' ) . '/';
}

/**
* Loads a class if it starts with `$this->prefix`.
*
* @param string $class The class to be loaded.
*/
public function load( $class ) {
if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
return;
}

// Strip prefix from the start (ala PSR-4)
$class = substr( $class, $this->prefix_length + 1 );
$class = strtolower( $class );
$file = '';

if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
$namespace = substr( $class, 0, $last_ns_pos );
$namespace = str_replace( '_', '-', $namespace );
$class = substr( $class, $last_ns_pos + 1 );
$file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}

$file .= 'class-' . str_replace( '_', '-', $class ) . '.php';

$path = $this->path . $file;

if ( file_exists( $path ) ) {
require $path;
}
}
}

/**
* Registers Autoloader's autoload function.
*
* @param string $prefix
* @param string $path
*/
function register_class_path( $prefix, $path ) {
$loader = new Autoloader( $prefix, $path );
spl_autoload_register( array( $loader, 'load' ) );
}
9 changes: 9 additions & 0 deletions mu-plugins/loader.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?php
namespace WordPressdotorg\MU_Plugins;
use WordPressdotorg\Autoload;

/**
* Load mu-plugins.
*
* `utilities/` aren't loaded automatically since they're not used globally.
*/

// Load and register the Autoloader.
if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) {
require_once __DIR__ . '/autoloader/class-autoloader.php';
}

Autoload\register_class_path( __NAMESPACE__, __DIR__ );

require_once __DIR__ . '/helpers/helpers.php';
require_once __DIR__ . '/blocks/global-header-footer/blocks.php';
require_once __DIR__ . '/blocks/horizontal-slider/horizontal-slider.php';
Expand Down