Skip to content

Commit

Permalink
Release/1.3.0 (#34)
Browse files Browse the repository at this point in the history
* Fixes incorrect references to "WP_error" #1 (#29)

* Introduces find method in Loader Registry. #28 (#30)

Resolves #28

* Issue/31 - Introduce middleware API (#33)

* Moves has_trait method in Underpin class. #31

This helper method is something that can be used outside of this system. With the trend of adding methods to Underpin as static methods, it makes sense to move this method, as well.

* Introduces Middleware API. #31

* Updates Readme #31
  • Loading branch information
alexstandiford authored Jun 22, 2021
1 parent 36b21bc commit 6689264
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 93 deletions.
99 changes: 91 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,23 +454,106 @@ And then register each one like so:

add_action( 'init', function() {
$post_types = get_post_types( [], 'objects' );
$ignored_types = flare_wp_get_ignored_post_types();

foreach ( $post_types as $post_type ) {
if ( ! in_array( $post_type->name, $ignored_types ) ) {
$this->shortcodes()->add( $post_type->name . '_shortcode', [
'class' => 'Flare_WP\Shortcodes\Post_Type_Shortcode',
'args' => [ $post_type ],
] );
}
}
$this->shortcodes()->add( $post_type->name . '_shortcode', [
'class' => 'Flare_WP\Shortcodes\Post_Type_Shortcode',
'args' => [ $post_type ],
] );
}
} );
```

The key part here is how differently we handled the `add` method. Instead of simply providing a instance name, we
instead provide an array containing the `class`, and an array of ordered `args` to pass directly into the contstructor.
As a result, we register this class to be constructed if it is ever needed.

## Middleware

Some loaders support middleware. This pattern makes it possible to do a set of things when a loader item is registered.
A good example of middleware in-action can be seen in the [script loader](https://github.com/Underpin-WP/script-loader).

```php
// Register script
underpin()->scripts()->add( 'test', [
'handle' => 'test',
'src' => 'path/to/script/src',
'name' => 'test',
'description' => 'The description',
'middlewares' => [
'Underpin_Scripts\Factories\Enqueue_Admin_Script'
]
] );

// Enqueue script
$script = underpin()->scripts()->get('test')->enqueue();
```

The above `middlewares` array would automatically cause the `test` script to be enqueued on the admin page. Multiple
middlewares can be added in the array, and each one would run right after the item is added to the registry.

The `middlewares` array uses `Underpin::make_class` to create the class instances. This means that you can pass either:

1. a string that references an instance of `Script_Middleware` (see example above).
1. An array of arguments to construct an instance of `Script_Middleware` on-the-fly (see example below).

```php
underpin()->scripts()->add( 'test', [
'handle' => 'test',
'src' => 'path/to/script/src',
'name' => 'test',
'description' => 'The description',
'middlewares' => [
'Underpin_Rest_Middleware\Factories\Rest_Middleware', // Will localize script params.
'Underpin_Scripts\Factories\Enqueue_Script', // Will enqueue the script on the front end all the time.
[ // Will instantiate an instance of Script_Middleware_Instance using the provided arguments
'name' => 'Custom setup params',
'description' => 'Sets up custom parameters specific to this script',
'priority' => 10, // Optional. Default 10.
'do_actions_callback' => function ( \Underpin_Scripts\Abstracts\Script $loader_item ) {
// Do actions
},
],
],
] );
```

### Using Middleware In Your Loader

The easiest way to use middleware in your loader is with the `Middleware` trait. Using the shortcode example above again:

```php
class Post_Type_Shortcode extends \Underpin\Abstracts\Shortcode {
use \Underpin\Traits\Middleware;

public function __construct( $post_type ) {
$this->shortcode = $post_type . '_is_the_best';

$this->post_type = $post_type;
}

public function shortcode_actions() {
echo $this->post_type . ' is the best post type';
}
}
```

You could then register much like before, only now you can provide middleware actions.

```php
add_action( 'init', function() {
$post_types = get_post_types( [], 'objects' );

foreach ( $post_types as $post_type ) {
$this->shortcodes()->add( $post_type->name . '_shortcode', [
'class' => 'Flare_WP\Shortcodes\Post_Type_Shortcode',
'args' => [ $post_type ],
'middlewares' => [/* Add middleware references here. */]
] );
}
} );
```

## Template System Trait

This plugin also includes a templating system. This system clearly separates HTML markup from business logic, and
Expand Down
61 changes: 61 additions & 0 deletions lib/abstracts/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* A single instance of Middleware
*
* @since 1.3.0
* @package Underpin\Abstracts
*/
namespace Underpin\Abstracts;


use Underpin\Factories\Loader_Registry_Item;
use Underpin\Traits\Feature_Extension;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Middleware
*
* @since 1.3.0
* @package Underpin\Abstracts
*/
abstract class Middleware {

use Feature_Extension;

/**
* Loader Item
*
* @var Loader_Registry_Item The loader item
*/
protected $loader_item;

/**
* The priority in which this middleware should be ran.
*
* @since 1.3.0
* @var int
*/
protected $priority = 10;

/**
* Sets the middleware's loader.
*
* @since 1.3.0
*
* @param Loader_Registry_Item|string|array $loader_item The loader item to set
*/
public function set_loader( $loader_item ) {
$this->loader_item = $loader_item;
}

public function __get( $key ) {
if ( isset( $this->$key ) ) {
return $this->$key;
} else {
return new \WP_Error( 'middleware_param_not_set', 'The middleware key ' . $key . ' could not be found.' );
}
}
}
2 changes: 1 addition & 1 deletion lib/abstracts/Settings_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function __get( $key ) {
if ( isset( $this->$key ) ) {
return $this->$key;
} else {
return new WP_error( 'batch_task_param_not_set', 'The batch task key ' . $key . ' could not be found.' );
return new WP_Error( 'batch_task_param_not_set', 'The batch task key ' . $key . ' could not be found.' );
}
}

Expand Down
33 changes: 33 additions & 0 deletions lib/abstracts/Underpin.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,39 @@ protected function unsupported_actions() {
add_action( 'admin_notices', array( $this, 'below_version_notice' ) );
}

/**
* Checks to see if the class, or any of its parents, uses the specified trait.
*
* @since 1.3.0
*
* @param string $trait The trait to check for
* @param object|string|false $class The class to check.
* @return bool true if the class uses the specified trait, otherwise false.
*/
public static function has_trait( $trait, $class ) {

if ( false === $class ) {
return false;
}

$traits = class_uses( $class );

if ( in_array( $trait, $traits ) ) {
return true;
}

while ( get_parent_class( $class ) ) {
$class = get_parent_class( $class );

$has_trait = self::has_trait( $trait, $class );

if ( true === $has_trait ) {
return true;
}
}

return false;
}
/**
* Actions that run when this plugin meets the specified minimum requirements.
*
Expand Down
Loading

0 comments on commit 6689264

Please sign in to comment.