From 4fad1fa4f46c5bbc9c4ee68f8b099f1caded5966 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Wed, 20 Dec 2023 19:17:41 -0600 Subject: [PATCH 01/10] WIP: issue-293 From f81286cfe41f170da0e0858990ddfbe1889bd453 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Wed, 20 Dec 2023 20:35:12 -0600 Subject: [PATCH 02/10] add feature-manager --- plugin.php | 10 +++- src/class-feature-manager.php | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/class-feature-manager.php diff --git a/plugin.php b/plugin.php index 54efe700..7882d540 100644 --- a/plugin.php +++ b/plugin.php @@ -58,11 +58,19 @@ function () { // Load the plugin's main files. require_once __DIR__ . '/src/assets.php'; require_once __DIR__ . '/src/meta.php'; +require_once __DIR__ . '/src/class-feature-manager.php'; /** * Instantiate the plugin. */ function main(): void { - // ... + + $features =[ + 'Create_WordPress_Plugin\Features\Featured_Image_Caption' => [], + ]; + $features = apply_filters( 'create_wordpress_plugin_features', $features ); + + $feature_manager = new Feature_Manager( $features ); + $feature_manager->boot(); } main(); diff --git a/src/class-feature-manager.php b/src/class-feature-manager.php new file mode 100644 index 00000000..bd857573 --- /dev/null +++ b/src/class-feature-manager.php @@ -0,0 +1,105 @@ +features ) ) { + $this->features = []; + } + $this->autoload_features( CREATE_WORDPRESS_PLUGIN_DIR . '/features' ); + + foreach ( $features_to_create as $feature_class => $args ) { + $this->features[] = new $feature_class( ...$args ); + } + } + + /** + * Boot the feature. + */ + public function boot(): void { + // if ( empty( $this->features ) ) { + // $this->features = []; + // } + foreach ( $this->features as $feature ) { + $feature->boot(); + } + $this->booted = true; + } + + /** + * Return the plugin features. + * + * @return Feature[] The plugin features. + */ + public function get_features() { + return $this->features; + } + + /** + * Add a feature to the plugin. + * + * @param Feature $feature The new feature. + * @return void + */ + public function add_feature( Feature $feature ) { + $this->features[] = $feature; + if ( $this->booted ) { + $feature->boot(); + } + } + + /** + * Get a feature by name. + * + * @param string $feature_name The name of the feature to get. + * @return Feature|null The feature or null if it doesn't exist. + */ + public function get_feature( string $feature_name ) { + foreach ( $this->features as $feature ) { + if ( $feature->get_name() === $feature_name ) { + return $feature; + } + } + return null; + } + + /** + * Autoload features from a directory. + * + * @param string $path The directory path. + * @return void + */ + public function autoload_features( string $path ) { + $files = glob( $path . '/*.php' ); + foreach ( $files as $file ) { + require_once $file; + } + } +} From 17627ee3cd87f5fcd54bac76b883f868e7f1c1ab Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 21 Dec 2023 08:34:07 -0600 Subject: [PATCH 03/10] add example --- features/class-hello.php | 82 +++++++++++++++++++++++++++++++++++ plugin.php | 40 ++++++++++++++++- src/class-feature-manager.php | 22 +++++----- 3 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 features/class-hello.php diff --git a/features/class-hello.php b/features/class-hello.php new file mode 100644 index 00000000..b18ddd05 --- /dev/null +++ b/features/class-hello.php @@ -0,0 +1,82 @@ +lyrics ); + + // And then randomly choose a line. + return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] ); + } + + // This just echoes the chosen line, we'll position it later. + public function hello_dolly() { + $chosen = $this->hello_dolly_get_lyric(); + $lang = ''; + if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) { + $lang = ' lang="en"'; + } + + printf( + '

%s %s

', + __( 'Quote from Hello Dolly song, by Jerry Herman:' ), + $lang, + $chosen + ); + } + + // We need some CSS to position the paragraph. + public function dolly_css() { + echo " + + "; + } +} diff --git a/plugin.php b/plugin.php index 7882d540..db43554e 100644 --- a/plugin.php +++ b/plugin.php @@ -64,13 +64,51 @@ function () { * Instantiate the plugin. */ function main(): void { + $lyrics = "Hello, Dolly + Well, hello, Dolly + It's so nice to have you back where you belong + You're lookin' swell, Dolly + I can tell, Dolly + You're still glowin', you're still crowin' + You're still goin' strong + I feel the room swayin' + While the band's playin' + One of our old favorite songs from way back when + So, take her wrap, fellas + Dolly, never go away again + Hello, Dolly + Well, hello, Dolly + It's so nice to have you back where you belong + You're lookin' swell, Dolly + I can tell, Dolly + You're still glowin', you're still crowin' + You're still goin' strong + I feel the room swayin' + While the band's playin' + One of our old favorite songs from way back when + So, golly, gee, fellas + Have a little faith in me, fellas + Dolly, never go away + Promise, you'll never go away + Dolly'll never go away again"; + // Add a feature using the filter. + // add_filter( 'create_wordpress_plugin_features', function ( array $features ) use ( $lyrics ): array { + // $features[ 'Create_WordPress_Plugin\Features\Hello' ] = [ 'lyrics' => $lyrics ]; + // return $features; + // } ); + + // Add a feature using the array on construct. $features =[ - 'Create_WordPress_Plugin\Features\Featured_Image_Caption' => [], + // 'Create_WordPress_Plugin\Features\Hello' => [ 'lyrics' => $lyrics ], ]; $features = apply_filters( 'create_wordpress_plugin_features', $features ); $feature_manager = new Feature_Manager( $features ); + // Add a feature using the add_feature method. + // $feature_manager->add_feature( 'Create_WordPress_Plugin\Features\Hello', [ 'lyrics' => $lyrics ] ); $feature_manager->boot(); + // Get the instance of the feature. + $hello_feature = $feature_manager->get_feature( 'Create_WordPress_Plugin\Features\Hello' ); } main(); diff --git a/src/class-feature-manager.php b/src/class-feature-manager.php index bd857573..bd2d3509 100644 --- a/src/class-feature-manager.php +++ b/src/class-feature-manager.php @@ -15,7 +15,7 @@ class Feature_Manager implements Feature { * * @var Feature[] */ - private array $features; + private array $features = []; /** * Have the features been booted? @@ -30,9 +30,6 @@ class Feature_Manager implements Feature { * @param Feature ...$features Features. */ public function __construct( array $features_to_create = [] ) { - if ( empty( $this->features ) ) { - $this->features = []; - } $this->autoload_features( CREATE_WORDPRESS_PLUGIN_DIR . '/features' ); foreach ( $features_to_create as $feature_class => $args ) { @@ -44,9 +41,6 @@ public function __construct( array $features_to_create = [] ) { * Boot the feature. */ public function boot(): void { - // if ( empty( $this->features ) ) { - // $this->features = []; - // } foreach ( $this->features as $feature ) { $feature->boot(); } @@ -65,25 +59,28 @@ public function get_features() { /** * Add a feature to the plugin. * - * @param Feature $feature The new feature. - * @return void + * @param string $feature_class The feature class to add. + * @param array $args The arguments to pass to the feature constructor. + * @return Feature The feature that was added. */ - public function add_feature( Feature $feature ) { + public function add_feature( string $feature_class, array $args = [] ) { + $feature = new $feature_class( ...$args ); $this->features[] = $feature; if ( $this->booted ) { $feature->boot(); } + return $feature; } /** - * Get a feature by name. + * Get a feature by class name. * * @param string $feature_name The name of the feature to get. * @return Feature|null The feature or null if it doesn't exist. */ public function get_feature( string $feature_name ) { foreach ( $this->features as $feature ) { - if ( $feature->get_name() === $feature_name ) { + if ( get_class( $feature ) === $feature_name ) { return $feature; } } @@ -92,6 +89,7 @@ public function get_feature( string $feature_name ) { /** * Autoload features from a directory. + * This only includes the files. It does not boot the features. * * @param string $path The directory path. * @return void From 83bc11ea6de7853498ef893ba9675b6916229df2 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Thu, 21 Dec 2023 09:24:20 -0600 Subject: [PATCH 04/10] phpcs and example of removing action --- features/class-hello.php | 24 ++++++++++++++++++------ plugin.php | 4 +++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/features/class-hello.php b/features/class-hello.php index b18ddd05..720d9e82 100644 --- a/features/class-hello.php +++ b/features/class-hello.php @@ -9,6 +9,9 @@ use Alley\WP\Types\Feature; +/** + * Hello class file + */ final class Hello implements Feature { /** * Set up. @@ -27,15 +30,22 @@ public function boot(): void { add_action( 'admin_head', [ $this, 'dolly_css' ] ); } + /** + * Gets a random lyric from the lyric string. + * + * @return void + */ public function hello_dolly_get_lyric() { // Here we split the lyrics into lines. $lyrics = explode( "\n", $this->lyrics ); // And then randomly choose a line. - return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] ); + return wptexturize( $lyrics[ wp_rand( 0, count( $lyrics ) - 1 ) ] ); } - // This just echoes the chosen line, we'll position it later. + /** + * Echos the chosen line. + */ public function hello_dolly() { $chosen = $this->hello_dolly_get_lyric(); $lang = ''; @@ -45,13 +55,15 @@ public function hello_dolly() { printf( '

%s %s

', - __( 'Quote from Hello Dolly song, by Jerry Herman:' ), - $lang, - $chosen + esc_html__( 'Quote from Hello Dolly song, by Jerry Herman:' ), + esc_attr( $lang ), + esc_html( $chosen ) ); } - // We need some CSS to position the paragraph. + /** + * Output css to position the paragraph. + */ public function dolly_css() { echo " + "; + } +} ``` \ No newline at end of file diff --git a/src/features/class-hello.php b/src/features/class-hello.php deleted file mode 100644 index a405713c..00000000 --- a/src/features/class-hello.php +++ /dev/null @@ -1,94 +0,0 @@ -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 " - - "; - } -} From b01c6ded423fe4e2a0ddc74c93c5e5b26bde4dd1 Mon Sep 17 00:00:00 2001 From: Greg Marshall Date: Fri, 22 Dec 2023 12:55:44 -0600 Subject: [PATCH 10/10] add example of filter --- src/features/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/features/README.md b/src/features/README.md index cbe4a573..eeafb8bc 100644 --- a/src/features/README.md +++ b/src/features/README.md @@ -33,6 +33,9 @@ Feature_Manager::add_features( $features ); ``` > 💡 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 ); +``` ### Add a feature using the `add_feature` method ```