From 90fd76a78a74426712c2ce7ba601ca30344e82ba Mon Sep 17 00:00:00 2001 From: David Herrera Date: Thu, 9 May 2024 23:47:06 -0400 Subject: [PATCH] Remove feature manager --- plugin.php | 11 +- src/class-feature-manager.php | 80 ------------- src/features/README.md | 206 +++++++++++++++++----------------- 3 files changed, 107 insertions(+), 190 deletions(-) delete mode 100644 src/class-feature-manager.php diff --git a/plugin.php b/plugin.php index 11793e97..6c1f73b1 100644 --- a/plugin.php +++ b/plugin.php @@ -17,6 +17,8 @@ namespace Create_WordPress_Plugin; +use Alley\WP\Features\Group; + if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -63,11 +65,8 @@ function () { * Instantiate the plugin. */ function main(): void { - // This should be an array with keys set to feature classnames and arguments. - $features = [ - // Add initial features here. - ]; - $features = apply_filters( 'create_wordpress_plugin_features', $features ); - Feature_Manager::add_features( $features ); + // Add features here. + $plugin = new Group(); + $plugin->boot(); } main(); diff --git a/src/class-feature-manager.php b/src/class-feature-manager.php deleted file mode 100644 index 3b1248b6..00000000 --- a/src/class-feature-manager.php +++ /dev/null @@ -1,80 +0,0 @@ - $args ) { - self::add_feature( $feature_class, $args ); - } - } - - /** - * Return the plugin features. - * - * @return object[] The plugin features. - */ - public static function get_features(): array { - return self::$features; - } - - /** - * Add a feature to the plugin. - * - * @param string $feature_class The feature class to add. - * @param array $args The arguments to pass to the feature constructor. - * - * @phpstan-param array{string?: mixed} $args - * - * @return object The instatiated feature that was added. - * @throws \Exception If the feature class does not implement Feature. - */ - public static function add_feature( string $feature_class, array $args = [] ): object { - if ( ! in_array( Feature::class, class_implements( $feature_class ) ?: [], true ) ) { - throw new \Exception( 'Feature class must implement Feature interface.' ); - } - $feature = new $feature_class( ...$args ); - $feature->boot(); // @phpstan-ignore-line - self::$features[] = $feature; - return $feature; - } - - /** - * Get a feature by class name. - * - * @param string $feature_name The name of the feature to get. - * @return object|null The feature or null if it doesn't exist. - */ - public static function get_feature( string $feature_name ): ?object { - foreach ( self::$features as $feature ) { - if ( get_class( $feature ) === $feature_name ) { - return $feature; - } - } - return null; - } -} diff --git a/src/features/README.md b/src/features/README.md index eeafb8bc..b60ecc2b 100644 --- a/src/features/README.md +++ b/src/features/README.md @@ -1,10 +1,9 @@ # Features + Features should be PHP classes that implement the [Alley\WP\Types\Feature interface](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/types/interface-feature.php). Features should be located in the `src/features` directory of the plugin and have namespace `Create_WordPress_Plugin\Features;` -Files in the features directory will be autoloaded, but features will not be instantiated. Features are instantiated via the `Feature_Manager` static class. - The following variable is passed to the `Class_Hello` feature in each of the following examples. This shows how we can remove any business logic from the feature and pass it in when the feature is added. ``` @@ -22,36 +21,28 @@ $lyrics = "Hello, Dolly Dolly, never go away again"; ``` -## There are two ways to add a feature: -### Add a feature using the `add_features` method -``` -$features = [ - 'Create_WordPress_Plugin\Features\Hello' => [ 'lyrics' => $lyrics ], -]; +## Adding a feature -Feature_Manager::add_features( $features ); -``` +Files in the features directory will be autoloaded, but features will not be automatically instantiated. Features are typically instantiated in the plugin's `main()` function. -> 💡 If you `apply_filters` to the features array before passing it to `add_features`, you can modify it with a filter. ``` -$features = apply_filters( 'create_wordpress_plugin_features', $features ); +function main(): void { + // Add features here. + $plugin = new Group( + new Features\Hello( + title: 'Hello, Dolly', + artist: 'Jerry Herman', + lyrics: $lyrics, + ), + ); + $plugin->boot(); +} ``` -### Add a feature using the `add_feature` method -``` -Feature_Manager::add_feature( 'Create_WordPress_Plugin\Features\Hello', [ 'lyrics' => $lyrics ] ); -``` -## Get the instance of an added feature with the `get_feature` method -``` -$hello_feature = Feature_Manager::get_feature( 'Create_WordPress_Plugin\Features\Hello' ); -``` -## Once we have the instance, we can remove hooks from inside the instance -``` -remove_action( 'admin_head', [ $hello_feature, 'dolly_css' ] ); -``` +## Example feature class -## Example feature class: This is a port of the infamous WordPress `hello.php` plugin to a feature. The lyrics would be passed in when the feature was called, as shown above. + ``` lyrics ); - - // And then randomly choose a line. - return wptexturize( $lyrics[ wp_rand( 0, count( $lyrics ) - 1 ) ] ); - } - - /** - * Echos the chosen line. - */ - public function hello_dolly(): void { - $chosen = $this->hello_dolly_get_lyric(); - $lang = ''; - if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) { - $lang = ' lang="en"'; - } - - printf( - '

%s %s

', - esc_html__( 'Quote from Hello Dolly song, by Jerry Herman:' ), - esc_attr( $lang ), - esc_html( $chosen ) - ); - } - - /** - * Output css to position the paragraph. - */ - public function dolly_css(): void { - echo " - - "; - } + /** + * Set up. + * + * @param string $title The song title. + * @param string $artist The song artist. + * @param string $lyrics The song lyrics. + */ + public function __construct( + private readonly string $title, + private readonly string $artist, + private readonly string $lyrics, + ) {} + + /** + * Boot the feature. + */ + public function boot(): void { + add_action( 'admin_notices', [ $this, 'hello_dolly' ] ); + add_action( 'admin_head', [ $this, 'dolly_css' ] ); + } + + /** + * Echos the chosen line. + */ + public function hello_dolly(): void { + $chosen = $this->get_lyric(); + $lang = ''; + if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) { + $lang = ' lang="en"'; + } + + printf( + '

%s %s

', + esc_html( + /* translators: 1: song title, 2: song artist */ + sprintf( __( 'Quote from %1$s song, by %2$s:' ), $this->title, $this->artist ), + ), + esc_attr( $lang ), + esc_html( $chosen ) + ); + } + + /** + * Output css to position the paragraph. + */ + public function dolly_css(): void { + echo " + + "; + } + + /** + * Gets a random lyric from the lyric string. + * + * @return string + */ + private function get_lyric(): string { + // Here we split the lyrics into lines. + $lyrics = explode( "\n", $this->lyrics ); + + // And then randomly choose a line. + return wptexturize( $lyrics[ wp_rand( 0, count( $lyrics ) - 1 ) ] ); + } } -``` \ No newline at end of file +```