Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching being enabled #11

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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