-
Notifications
You must be signed in to change notification settings - Fork 38
Fix for is_plugin_active() with slug-hash folder name #151
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
Changes from 5 commits
d3b51e5
fa47c07
a794e80
a393954
d2fce51
147f576
5795d00
829ae96
ed04e7d
2f0f59d
108c5a6
f66e4f7
c5d57a5
8286b67
90c79e2
ba17062
267505a
153fdf6
dcc9df0
01aa00b
05bbaf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
| /** | ||
| * Sets DID-less plugin slug as active. | ||
| * | ||
| * @package FAIR | ||
| */ | ||
|
|
||
| namespace FAIR\Plugins; | ||
|
|
||
| use function FAIR\Updater\get_packages; | ||
|
|
||
| /** | ||
| * Bootstrap | ||
| * | ||
| * @return void | ||
| */ | ||
| function bootstrap() { | ||
| add_filter( 'option_active_plugins', __NAMESPACE__ . '\\set_as_active' ); | ||
| add_filter( 'wp_admin_notice_markup', __NAMESPACE__ . '\\hide_notice', 10, 3 ); | ||
afragen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // just for testing. | ||
| if ( ! function_exists( 'is_plugin_active' ) ) { | ||
| require_once ABSPATH . 'wp-admin/includes/plugin.php'; | ||
| } | ||
| if ( is_plugin_active( 'git-updater/git-updater.php' ) ) { | ||
| wp_admin_notice( 'Git Updater is active' ); | ||
| } | ||
|
Comment on lines
+21
to
+27
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need to remove this when ready to merge. |
||
| } | ||
|
|
||
| /** | ||
| * Set FAIR plugins as active using DID-less slug. | ||
| * | ||
| * @param array $active_plugins Array of active plugins. | ||
| * | ||
| * @return array | ||
| */ | ||
| function set_as_active( $active_plugins ) { | ||
| remove_filter( 'option_active_plugins', __NAMESPACE__ . '\\set_as_active' ); | ||
| $packages = get_packages(); | ||
| $plugins = $packages['plugins'] ?? []; | ||
| foreach ( $plugins as $plugin ) { | ||
| if ( is_plugin_active( plugin_basename( $plugin ) ) ) { | ||
| $plugins[] = get_didless_slug( $plugin ); | ||
|
||
| } | ||
| } | ||
| $active_plugins = array_map( 'plugin_basename', array_merge( $active_plugins, $plugins ) ); | ||
|
|
||
| return array_unique( $active_plugins ); | ||
| } | ||
|
|
||
| /** | ||
| * Return shortened DID withou `did:plc|web'. | ||
| * | ||
| * @param string $did Full DID. | ||
| * | ||
| * @return string|void | ||
| */ | ||
| function get_short_did( $did ) { | ||
|
||
| if ( ! empty( $did ) ) { | ||
| if ( str_contains( $did, ':' ) ) { | ||
|
||
| $did = (string) explode( ':', $did )[2]; | ||
| } | ||
| return $did; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return slug without DID. | ||
| * | ||
| * @param string $slug Current slug. | ||
| * @param string $did Full DID. | ||
| * | ||
| * @return string|void | ||
| */ | ||
| function get_didless_slug( $slug, $did = '' ) { | ||
|
||
| if ( empty( $did ) ) { | ||
| $did = get_file_data( $slug, [ 'PluginID' => 'Plugin ID' ] )['PluginID']; | ||
| $did = get_short_did( $did ); | ||
| } | ||
| if ( ! empty( $did ) ) { | ||
| $slug = str_replace( '-' . get_short_did( $did ), '', $slug ); | ||
| } | ||
|
|
||
| return $slug; | ||
| } | ||
afragen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Hide notice reporting DID-less plugin is inactive because it doesn't exist. | ||
| * | ||
| * @param string $markup Markup of notice. | ||
| * @param string $message Message of notice. | ||
| * @param array $args Args of notice. | ||
| * | ||
| * @return string | ||
| */ | ||
| function hide_notice( $markup, $message, $args ) { | ||
| $active = get_option( 'active_plugins' ); | ||
| if ( $args['id'] === 'message' ) { | ||
| foreach ( $active as $plugin ) { | ||
afragen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( str_contains( $message, $plugin ) && str_contains( $markup, 'error' ) ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then I need to also check that the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just use: in_array( 'error', $args['additional_classes'] ?? [], true )
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm noticing that there's another error that has an ID of I think, unfortunately, we'll have to check for |
||
| remove_filter( 'wp_admin_notice_markup', __NAMESPACE__ . '\\hide_notice' ); | ||
| return ''; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $markup; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.