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

Move main plugin class to a PSR-4 file #29

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"autoload": {
"psr-4": {
"Geniem\\Oopi\\": ["src/", "plugin.php"]
"Geniem\\Oopi\\": "src/"
}
},
"require-dev": {
Expand Down
121 changes: 0 additions & 121 deletions plugin.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the use statements from this class...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 🍏

Original file line number Diff line number Diff line change
Expand Up @@ -34,127 +34,6 @@
require_once __DIR__ . '/vendor/autoload.php';
}

/**
* The base class for the plugin.
*
* @package Geniem
*/
class Plugin {

/**
* Holds the general plugin data.
*
* @var array
*/
protected static $plugin_data = [
'TABLE_NAME' => 'oopi_log',
];

/**
* Run tasks when plugin is activated.
*/
public static function install() {
// Install log handler.
Log::install();

// Register cron jobs.
CronJobs::install();
}


/**
* Run tasks when plugin is deactivated.
*/
public static function uninstall() {
// Remove cron jobs.
CronJobs::uninstall();
}

/**
* A map of importable class names and their corresponding import handlers.
*
* @var Importable[]
*/
private static $importables = [];

/**
* Initialize the plugin.
*/
public static function init() {
// Set the plugin version.
$plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version' ], 'plugin' );
self::$plugin_data = array_merge( $plugin_data, self::$plugin_data );

// Set the basic settings.
Settings::init( self::$plugin_data );

// Initialize the import handlers for importables.
self::set_initial_import_handlers();

// Register the cron job hooks.
CronJobs::init();

// Initialize plugin controllers after plugins are loaded.
add_action( 'wp_loaded', function() {
LanguageUtil::init();
} );
}

/**
* Sets the initial state of global handlers for all importables.
*/
protected static function set_initial_import_handlers() {
static::$importables = [
PostImportable::class => PostImporter::class,
TermImportable::class => TermImporter::class,
AttachmentImportable::class => AttachmentImporter::class,
];
}

/**
* Add or override a global importable and/or its import handler.
*
* @param string $importable_class The importable class name.
* @param string $importer_class The importer class name.
*
* @throws ReflectionException|TypeException If any of the given classes are missing or they are of the wrong type,
* an error is thrown.
*/
public static function set_importable( string $importable_class, string $importer_class ) {
$importable_reflect = new ReflectionClass( $importable_class );
$importer_reflect = new ReflectionClass( $importer_class );

$importable_interface = Importable::class;
$importer_interface = Importer::class;

$valid_classes =
$importable_reflect->implementsInterface( Importable::class )
&& $importer_reflect->implementsInterface( Importer::class );

if ( $valid_classes ) {
static::$importables[ $importable_class ] = $importer_class;
}
else {
throw new TypeException(
"The passed classes must implement the following interfaces:
importable: $importable_interface, importer: $importer_interface."
);
}
}

/**
* Get the global importer for the given importable class name.
*
* @param string $importable_class The importable class name.
*
* @return ?Importer
*/
public static function get_importer( string $importable_class ) : ?Importer {
$importer = new static::$importables[ $importable_class ]();
return $importer;
}
}

// Initialize the plugin.
Plugin::init();

Expand Down
127 changes: 127 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* The main plugin class.
*/

namespace Geniem\Oopi;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... to this class so everything works correctly 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. This is the first time I use a computer 🙃

/**
* The base class for the plugin.
*
* @package Geniem
*/
class Plugin {

/**
* Holds the general plugin data.
*
* @var array
*/
protected static $plugin_data = [
'TABLE_NAME' => 'oopi_log',
];

/**
* Run tasks when plugin is activated.
*/
public static function install() {
// Install log handler.
Log::install();

// Register cron jobs.
CronJobs::install();
}


/**
* Run tasks when plugin is deactivated.
*/
public static function uninstall() {
// Remove cron jobs.
CronJobs::uninstall();
}

/**
* A map of importable class names and their corresponding import handlers.
*
* @var Importable[]
*/
private static $importables = [];

/**
* Initialize the plugin.
*/
public static function init() {
// Set the plugin version.
$plugin_data = get_file_data( dirname( __DIR__ ) . '/plugin.php', [ 'Version' => 'Version' ], 'plugin' );
szepeviktor marked this conversation as resolved.
Show resolved Hide resolved
self::$plugin_data = array_merge( $plugin_data, self::$plugin_data );

// Set the basic settings.
Settings::init( self::$plugin_data );

// Initialize the import handlers for importables.
self::set_initial_import_handlers();

// Register the cron job hooks.
CronJobs::init();

// Initialize plugin controllers after plugins are loaded.
add_action( 'wp_loaded', function() {
LanguageUtil::init();
} );
}

/**
* Sets the initial state of global handlers for all importables.
*/
protected static function set_initial_import_handlers() {
static::$importables = [
PostImportable::class => PostImporter::class,
TermImportable::class => TermImporter::class,
AttachmentImportable::class => AttachmentImporter::class,
];
}

/**
* Add or override a global importable and/or its import handler.
*
* @param string $importable_class The importable class name.
* @param string $importer_class The importer class name.
*
* @throws ReflectionException|TypeException If any of the given classes are missing or they are of the wrong type,
* an error is thrown.
*/
public static function set_importable( string $importable_class, string $importer_class ) {
$importable_reflect = new ReflectionClass( $importable_class );
$importer_reflect = new ReflectionClass( $importer_class );

$importable_interface = Importable::class;
$importer_interface = Importer::class;

$valid_classes =
$importable_reflect->implementsInterface( Importable::class )
&& $importer_reflect->implementsInterface( Importer::class );

if ( $valid_classes ) {
static::$importables[ $importable_class ] = $importer_class;
}
else {
throw new TypeException(
"The passed classes must implement the following interfaces:
importable: $importable_interface, importer: $importer_interface."
);
}
}

/**
* Get the global importer for the given importable class name.
*
* @param string $importable_class The importable class name.
*
* @return ?Importer
*/
public static function get_importer( string $importable_class ) : ?Importer {
$importer = new static::$importables[ $importable_class ]();
return $importer;
}
}
4 changes: 3 additions & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Geniem\Oopi;

use Geniem\Oopi\Interfaces\ErrorHandler;/**
use Geniem\Oopi\Interfaces\ErrorHandler;

/**
* Class Util
* A collection of utility functions.
*
Expand Down