-
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
Conversation
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
costdev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes, some thoughts, and a whack with a ruler. This review has everything!
inc/plugins/namespace.php
Outdated
| * | ||
| * @return string|void | ||
| */ | ||
| function get_short_did( $did ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "short DID" as referred to here is known as the "method-specific-id" in the spec. I think we should try to stick to the terminology where possible. Besides "short" is vague 😉. What about get_did_id()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from that, I think there's a great opportunity here for a more general function, parse_did() that returns an array keyed on 'method' and 'id', or false for an improper DID.
So instead of having get_did_id(), you'd instead have:
function parse_did( $did ) {
$parts = explode( ':', $did, 3 );
if (
count( $parts ) !== 3 ||
$parts[0] !== 'did' ||
( $parts[1] !== 'web' && $parts[1] !== 'plc' )
) {
return false;
}
return [
'method' => $parts[1],
'id' => $parts[2],
];
}Usage:
$did = parse_did( $did );
if ( ! $did ) {
// Error or something else.
}
$id = $did['id'];Actually, there's already a parse_did() being introduced in #71 - here's its implementation.
Seems like we'll end up using that function when #71 gets merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored in 5795d00
inc/plugins/namespace.php
Outdated
| $plugins = $packages['plugins'] ?? []; | ||
| foreach ( $plugins as $plugin ) { | ||
| if ( is_plugin_active( plugin_basename( $plugin ) ) ) { | ||
| $plugins[] = get_didless_slug( $plugin ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Careful - $plugin isn't a slug in this situation, it's trailingslashit( WP_PLUGIN_DIR ) . $file - Ref.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I meant the call to get_didless_slug( $plugin ), which expects the argument to be a slug, not a path. Maybe it's get_didless_slug()'s internally referring to $slug that's causing confusion for me.
Would it be clearer to rename get_didless_slug() to remove_did_id() and call the parameter $plugin? That way, $plugin could be a slug, an absolute path, or slug/file.php.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored in 5795d00
inc/plugins/namespace.php
Outdated
| */ | ||
| function get_short_did( $did ) { | ||
| if ( ! empty( $did ) ) { | ||
| if ( str_contains( $did, ':' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be looking at what our minimum WP version should be bumped to, and opening a PR as soon as I can, so this doesn't need to be changed to strpos().
Still, with a current minimum PHP of 7.4 and WP of 5.4, bad Andy for using this function. hits with ruler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str_contains() is in a shim in WP5.9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, and our current minimum is 5.4. We also use wp_admin_notice(), which is WP 6.4, so I know we'll need to at least bump our minimum to 6.4. I'll work out if there are any other uses that need us to bump above 6.4.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides Ryan uses str_starts_with() in parse_did() 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @costdev !
Co-authored-by: Colin Stewart <[email protected]> Signed-off-by: Andy Fragen <[email protected]>
inc/plugins/namespace.php
Outdated
| * | ||
| * @return string|void | ||
| */ | ||
| function get_didless_slug( $slug, $did = '' ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. If we do get_did_id() then this might be better as get_slug_without_did_id()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I sort of did that in 5795d00
inc/plugins/namespace.php
Outdated
| */ | ||
| function get_short_did( $did ) { | ||
| if ( ! empty( $did ) ) { | ||
| if ( str_contains( $did, ':' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @costdev !
Signed-off-by: Andy Fragen <[email protected]>
| // 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' ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need to remove this when ready to merge.
|
I have a better way of getting the method specific ID from the DID. I plan on extending Ryan's |
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
|
|
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
| if ( $args['id'] === 'message' ) { | ||
| $active = get_option( 'active_plugins' ); | ||
| foreach ( $active as $plugin ) { | ||
| if ( str_contains( $message, $plugin ) && str_contains( $markup, 'error' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for the 'error' before the loop, in the initial if - can alternatively use in_array( 'error', $args['additional_classes'], true ) instead of checking for 'error' somewhere in the markup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then I need to also check that the additional_classes is set correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just use:
in_array( 'error', $args['additional_classes'] ?? [], true )There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 message and has the error class.
I think, unfortunately, we'll have to check for __( 'Plugin file does not exist.' ) and add a phpcs:ignore comment for the lack of textdomain since this is a Core string.
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
|
This will error until #71 is merged. It requires |
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
Signed-off-by: Andy Fragen <[email protected]>
|
I will say that I don't love the pattern of hook-and-self-unhook here, and it also doesn't seem like it works consistently - what happens when you call I'd rather we instead a) install consistently into |
Signed-off-by: Andy Fragen <[email protected]>
|
@rmccue consider this more of a POC. It does seem that your hijack and backtrace method might be cleaner. |
This PR will/should make any installed FAIR plugin, that was installed with folder at
slug-did, as an active plugin when usingis_plugin_active( slug ).Yes, I'm making a preference for avoiding name collisions by installing as
slug-did.Requires #150