-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* The main plugin class. | ||
*/ | ||
|
||
namespace Geniem\Oopi; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... to this class so everything works correctly 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 🍏