Skip to content

Commit

Permalink
Merge pull request #354 from alleyinteractive/feature/remove-feature-…
Browse files Browse the repository at this point in the history
…manager

Remove feature manager
  • Loading branch information
dlh01 authored May 13, 2024
2 parents 4da5932 + 90fd76a commit 3655d0d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 190 deletions.
11 changes: 5 additions & 6 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Create_WordPress_Plugin;

use Alley\WP\Features\Group;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand Down Expand Up @@ -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();
80 changes: 0 additions & 80 deletions src/class-feature-manager.php

This file was deleted.

206 changes: 102 additions & 104 deletions src/features/README.md
Original file line number Diff line number Diff line change
@@ -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.

```
Expand All @@ -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.

```
<?php
/**
Expand All @@ -68,83 +59,90 @@ use Alley\WP\Types\Feature;
* Hello class file
*/
final class Hello implements Feature {
/**
* Set up.
*
* @param string $lyrics The lyrics to Hello Dolly.
*/
public function __construct(
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' ] );
}
/**
* Gets a random lyric from the lyric string.
*
* @return string
*/
public function hello_dolly_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 ) ] );
}
/**
* 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(
'<p id="dolly"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
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 "
<style type='text/css'>
#dolly {
float: right;
padding: 5px 10px;
margin: 0;
font-size: 12px;
line-height: 1.6666;
}
.rtl #dolly {
float: left;
}
.block-editor-page #dolly {
display: none;
}
@media screen and (max-width: 782px) {
#dolly,
.rtl #dolly {
float: none;
padding-left: 0;
padding-right: 0;
}
}
</style>
";
}
/**
* 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(
'<p id="dolly"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
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 "
<style type='text/css'>
#dolly {
float: right;
padding: 5px 10px;
margin: 0;
font-size: 12px;
line-height: 1.6666;
}
.rtl #dolly {
float: left;
}
.block-editor-page #dolly {
display: none;
}
@media screen and (max-width: 782px) {
#dolly,
.rtl #dolly {
float: none;
padding-left: 0;
padding-right: 0;
}
}
</style>
";
}
/**
* 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 ) ] );
}
}
```
```

0 comments on commit 3655d0d

Please sign in to comment.