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

Use WP Autoloader class #1051

Merged
merged 9 commits into from
Dec 9, 2024
22 changes: 20 additions & 2 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
\define( 'ACTIVITYPUB_PLUGIN_FILE', ACTIVITYPUB_PLUGIN_DIR . basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

require_once __DIR__ . '/includes/class-autoloader.php';
require_once __DIR__ . '/includes/compat.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/constants.php';
require_once __DIR__ . '/integration/load.php';

Autoloader::register_path( __NAMESPACE__, __DIR__ . '/includes' );

/**
* Initialize REST routes.
Expand Down Expand Up @@ -81,6 +85,22 @@ function plugin_init() {
}
\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' );

/**
* Add plugin settings link.
*
* @param array $actions The current actions.
*/
function plugin_settings_link( $actions ) {
obenland marked this conversation as resolved.
Show resolved Hide resolved
$settings_link = array();
$settings_link[] = \sprintf(
'<a href="%1s">%2s</a>',
\menu_page_url( 'activitypub', false ),
\__( 'Settings', 'activitypub' )
);

return \array_merge( $settings_link, $actions );
}
\add_filter( 'plugin_action_links_' . ACTIVITYPUB_PLUGIN_BASENAME, __NAMESPACE__ . '\plugin_settings_link' );

/**
* Class Autoloader.
Expand Down Expand Up @@ -144,8 +164,6 @@ function ( $full_class ) {
)
);

// Load integrations.
require_once __DIR__ . '/integration/load.php';
pfefferle marked this conversation as resolved.
Show resolved Hide resolved

/**
* `get_plugin_data` wrapper.
Expand Down
106 changes: 106 additions & 0 deletions includes/class-autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Autoloader for Activitypub.
*
* @package Activitypub
*/

namespace Activitypub;

/**
* An Autoloader that respects WordPress's filename standards.
*/
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 Namespace 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 . '/' );
}

/**
* Registers Autoloader's autoload function.
*
* @throws \Exception When autoload_function cannot be registered.
*
* @param string $prefix Namespace prefix all classes have in common.
* @param string $path Path to the files to be loaded.
*/
public static function register_path( $prefix, $path ) {
$loader = new Autoloader( $prefix, $path );
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
spl_autoload_register( array( $loader, 'load' ) );
}

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

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

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

$path = $this->path . $dir . 'class-' . str_replace( '_', '-', $class_name ) . '.php';

if ( ! file_exists( $path ) ) {
$path = $this->path . $dir . 'interface-' . str_replace( '_', '-', $class_name ) . '.php';
}

if ( ! file_exists( $path ) ) {
$path = $this->path . $dir . 'trait-' . str_replace( '_', '-', $class_name ) . '.php';
}

if ( file_exists( $path ) ) {
require_once $path;
}
}
}
Loading