-
Notifications
You must be signed in to change notification settings - Fork 0
dev: implement PSR-4 and reduce Singleton usage #29
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| * @package OneDesign | ||
| */ | ||
|
|
||
| declare(strict_types = 1); | ||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace OneDesign; | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,25 @@ | ||
| <?php | ||
| /** | ||
| * Interface for Registrable classes. | ||
| * | ||
| * Registrable classes are those that register hooks (actions/filters) with WordPress. | ||
| * | ||
| * @package OneDesign\Contracts\Interfaces | ||
| */ | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace OneDesign\Contracts\Interfaces; | ||
|
|
||
| /** | ||
| * Interface - Registrable | ||
| */ | ||
| interface Registrable { | ||
|
|
||
| /** | ||
| * Registers class methods to WordPress. | ||
| * | ||
| * WordPress actions/filters should be included here. | ||
| */ | ||
| public function register_hooks(): void; | ||
| } |
This file contains hidden or 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,72 @@ | ||
| <?php | ||
| /** | ||
| * Singleton trait. | ||
| * | ||
| * @package OneDesign\Contracts\Traits | ||
| */ | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace OneDesign\Contracts\Traits; | ||
|
|
||
| /** | ||
| * Singleton trait. | ||
| */ | ||
| trait Singleton { | ||
| /** | ||
| * Instance of the class. | ||
| * | ||
| * @var ?static | ||
| */ | ||
| protected static $instance; | ||
|
|
||
| /** | ||
| * Prevent the class from being instantiated directly. | ||
| */ | ||
| protected function __construct() { | ||
| // To be implemented by the class using the trait. | ||
| } | ||
|
|
||
| /** | ||
| * Get the instance of the class. | ||
| * | ||
| * @return static | ||
| */ | ||
| final public static function instance() { | ||
| if ( ! isset( static::$instance ) ) { | ||
| static::$instance = new static(); | ||
| } | ||
|
|
||
| return static::$instance; | ||
| } | ||
|
|
||
| /** | ||
| * Prevent the class from being cloned. | ||
| */ | ||
| final public function __clone() { | ||
| _doing_it_wrong( | ||
| __FUNCTION__, | ||
| sprintf( | ||
| // translators: %s: Class name. | ||
| esc_html__( 'The %s class should not be cloned.', 'onedesign' ), | ||
| esc_html( static::class ), | ||
| ), | ||
| '0.0.1' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Prevent the class from being deserialized. | ||
| */ | ||
| final public function __wakeup() { | ||
| _doing_it_wrong( | ||
| __FUNCTION__, | ||
| sprintf( | ||
| // translators: %s: Class name. | ||
| esc_html__( 'De-serializing instances of %s is not allowed.', 'onedesign' ), | ||
| esc_html( static::class ), | ||
| ), | ||
| '0.0.1' | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,116 @@ | ||
| <?php | ||
| /** | ||
| * The main plugin file. | ||
| * | ||
| * @package OneDesign | ||
| */ | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace OneDesign; | ||
|
|
||
| use OneDesign\Contracts\Traits\Singleton; | ||
|
|
||
| /** | ||
| * Class - Main | ||
| */ | ||
| final class Main { | ||
| use Singleton; | ||
|
|
||
| /** | ||
| * Registrable classes are entrypoints that "hook" into WordPress. | ||
| * They should implement the Registrable interface. | ||
| * | ||
| * @var class-string<\OneDesign\Contracts\Interfaces\Registrable>[] | ||
| */ | ||
| private const REGISTRABLE_CLASSES = [ | ||
| Modules\Core\Assets::class, | ||
| Modules\Settings\Admin::class, | ||
| Modules\Multisite\Multisite::class, | ||
| Modules\Rest\Rest::class, | ||
| Modules\Rest\Basic_Options_Controller::class, | ||
| Modules\Rest\Patterns_Controller::class, | ||
| Modules\Rest\Templates_Controller::class, | ||
| Modules\Rest\Multisite_Controller::class, | ||
| Modules\Post_Types\Pattern::class, | ||
| Modules\Post_Types\Template::class, | ||
| Modules\Post_Types\Meta::class, | ||
| Hooks::class, | ||
| CPT_Restriction::class, | ||
| Plugin_Configs\Secret_Key::class, | ||
| ]; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public static function instance(): self { | ||
| if ( ! isset( self::$instance ) ) { | ||
| self::$instance = new self(); | ||
| self::$instance->setup(); | ||
| } | ||
|
|
||
| return self::$instance; | ||
| } | ||
|
|
||
| /** | ||
| * Setup the plugin. | ||
| */ | ||
| private function setup(): void { | ||
| // Ensure pretty permalinks are enabled. | ||
| if ( ! $this->has_pretty_permalinks() ) { | ||
| return; | ||
| } | ||
|
|
||
| // Load the plugin classes. | ||
| $this->load(); | ||
|
|
||
| // Do other stuff here like dep-checking, telemetry, etc. | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether pretty permalinks are enabled. | ||
| * | ||
| * Will also render an admin notice if not enabled. | ||
| */ | ||
| private function has_pretty_permalinks(): bool { | ||
| if ( ! empty( get_option( 'permalink_structure' ) ) ) { | ||
| return true; | ||
| } | ||
|
|
||
| foreach ( [ | ||
| 'admin_notices', | ||
| 'network_admin_notices', | ||
| ] as $hook ) { | ||
| add_action( | ||
| $hook, | ||
| static function () { | ||
| wp_admin_notice( | ||
| sprintf( | ||
| /* translators: 1: Plugin name */ | ||
| __( 'OneDesign: The plugin requires pretty permalinks to be enabled. Please go to <a href="%s">Permalink Settings</a> and enable an option other than <code>Plain</code>.', 'onedesign' ), | ||
| admin_url( 'options-permalink.php' ), | ||
| ), | ||
| [ | ||
| 'type' => 'error', | ||
| 'dismissible' => false, | ||
| ] | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Load the plugin classes. | ||
| */ | ||
| private function load(): void { | ||
| foreach ( self::REGISTRABLE_CLASSES as $class_name ) { | ||
| $instance = new $class_name(); | ||
| $instance->register_hooks(); | ||
| } | ||
|
|
||
| // Do other generalizable stuff here. | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.