Skip to content

Commit

Permalink
Merge pull request #11 from alleyinteractive/hotfix/caching
Browse files Browse the repository at this point in the history
Fix caching being enabled
  • Loading branch information
srtfisher authored Apr 24, 2024
2 parents 614c5f7 + 8888531 commit c0363df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `WP Plugin Loader` will be documented in this file.

## 0.1.4 - 2024-04-24

- Fix to actually allow caching to be enabled.

## 0.1.3 - 2024-02-14

- Changes class from `Alley\WP\WP_Plugin_Loader\WP_Plugin_Loader` to
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,15 @@ capability check.

When a plugin is loaded by a directory name the package will attempt to
determine the main plugin file from the directory. This can be a semi-expensive
operation that can be cached with APCu. To enable caching, call
`enable_caching()` or `set_cache_prefix( $prefix )` to specify a custom cache
prefix.
operation that can be cached with APCu. To enable caching, pass `$cache` to the
constructor with a boolean or string prefix:

```php
use Alley\WP\WP_Plugin_Loader;

( new WP_Plugin_Loader( [ ... ] ) )->enable_caching();
new WP_Plugin_Loader( plugins: [ ... ], cache: true );

( new WP_Plugin_Loader( [ ... ] ) )->set_cache_prefix( 'my-prefix' );
new WP_Plugin_Loader( plugins: [ ... ], cache: 'my-prefix' );
```

Note: caching will only be enabled if APCu is available.
Expand Down
12 changes: 9 additions & 3 deletions src/class-wp-plugin-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ class WP_Plugin_Loader {
* Constructor.
*
* @param array<int, string> $plugins Array of plugins to load.
* @param string|bool $cache Whether to enable caching with an optional prefix.
*/
public function __construct( public array $plugins = [] ) {
public function __construct( public array $plugins = [], string|bool $cache = false ) {
if ( did_action( 'plugins_loaded' ) ) {
trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
'WP_Plugin_Loader should be instantiated before the plugins_loaded hook.',
E_USER_WARNING
);
}

if ( $cache ) {
$this->enable_caching( true === $cache ? null : (string) $cache );
}

$this->load_plugins();

add_filter( 'plugin_action_links', [ $this, 'filter_plugin_action_links' ], 10, 2 );
Expand All @@ -68,10 +73,11 @@ public function prevent_activations( bool $prevent = true ): static {
/**
* Enable APCu caching for plugin paths.
*
* @param string $prefix The cache prefix, defaults to 'wp-plugin-loader-'.
* @return static
*/
public function enable_caching(): static {
return $this->set_cache_prefix( 'wp-plugin-loader-' );
public function enable_caching( ?string $prefix = null ): static {
return $this->set_cache_prefix( $prefix ?? 'wp-plugin-loader-' );
}

/**
Expand Down

0 comments on commit c0363df

Please sign in to comment.