Skip to content

Commit

Permalink
Prevent with a cap check
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jul 23, 2023
1 parent 9a5538a commit 0bef718
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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

## 0.1.0 - 202X-XX-XX
## 0.1.0 - 2023-07-22

- Initial release
16 changes: 16 additions & 0 deletions src/class-wp-plugin-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct( public array $plugins = [] ) {
add_filter( 'plugin_action_links', [ $this, 'filter_plugin_action_links' ], 10, 2 );
add_filter( 'option_active_plugins', [ $this, 'filter_option_active_plugins' ] );
add_filter( 'pre_update_option_active_plugins', [ $this, 'filter_pre_update_option_active_plugins' ] );
add_filter( 'map_meta_cap', [ $this, 'prevent_plugin_activation' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -189,4 +190,19 @@ protected function sanitize_plugin_name( string $folder ): string {
$folder = preg_replace( '#([^a-zA-Z0-9-_.]+)#', '', $folder );
return str_replace( '..', '', (string) $folder ); // To prevent going up directories.
}

/**
* Prevent any plugin activations for non-code activated plugins.
*
* @param array<string> $caps Array of capabilities.
* @param string $cap Capability name.
* @return array<string>
*/
public function prevent_plugin_activation( $caps, $cap ) {
if ( $this->prevent_activations && 'activate_plugins' === $cap ) {
return [ 'do_not_allow' ];
}

return $caps;
}
}

0 comments on commit 0bef718

Please sign in to comment.