Account for package metadata that doesn't include filename when deriving hashed filename#496
Account for package metadata that doesn't include filename when deriving hashed filename#496kasparsd wants to merge 6 commits into
Conversation
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
This sample metadata doesn’t have filename returned, for reference https://github.com/fairpm/fair-plugin/releases/latest/download/fair-metadata.json Signed-off-by: Kaspars Dambis <hi@kaspars.net>
| $type = str_replace( 'wp-', '', $metadata->type ); | ||
| $did_hash = '-' . get_did_hash( $metadata->id ); | ||
|
|
||
| list( $slug, $file ) = explode( '/', $filename, 2 ); |
There was a problem hiding this comment.
When $metadata->filename isn't specified (such as https://github.com/fairpm/fair-plugin/releases/latest/download/fair-metadata.json), the explode() failed with:
Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in /inc/packages/namespace.php on line 694
and the $file was undefined which caused:
Warning: Undefined array key 1 in /inc/packages/namespace.php on line 694
There was a problem hiding this comment.
I think the problem is that $metadata->filename is supposed to exist and that the fair-metadata.json was incorrectly created.
There was a problem hiding this comment.
Yeah, the general package spec doesn’t require it so I think it makes sense that we support that scenario as a fallback.
Arguably, WP determines the bootstrap file dynamically based on header presence (and themes don’t even need it) so I think should probably refactor this eventually.
There was a problem hiding this comment.
We did update the spec and it now requires it.
There was a problem hiding this comment.
@afragen I think the spec shouldn't require it. None of the WP packages have that concept (at least from the existing WP-org API perspective).
I created #500 to illustrate how WP core resolves the plugin_file dynamically during install and never gets it from the update API. Asking package publishers to include the filename is simply unnecessary.
In #500 I demonstrate how we can resolve all the necessary filename data from the canonical places in WP without having to assume or deal with mismatches on the filesystem (what active_plugins thinks) and what the metadata reports.
| function get_default_repo_domain() : string { | ||
| if ( defined( 'FAIR_DEFAULT_REPO_DOMAIN' ) ) { | ||
| return FAIR_DEFAULT_REPO_DOMAIN; | ||
| return (string) \FAIR_DEFAULT_REPO_DOMAIN; |
There was a problem hiding this comment.
Ensure the output type is always correct and use the constant value from the global namespace since that is what defined( 'FAIR_DEFAULT_REPO_DOMAIN' ) checks for.
Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
Signed-off-by: Kaspars Dambis <hi@kaspars.net>
…the DID hash appended Signed-off-by: Kaspars Dambis <hi@kaspars.net>
|
All the code review feedback has been addressed. Added a few additional phpunit test cases to account for the edge cases resolved. |
| } | ||
|
|
||
| return $filename; | ||
| return $slug; |
There was a problem hiding this comment.
Also, not sure I understand why a function named get_hashed_filename() no longer returns $filename.
There was a problem hiding this comment.
It returns the filename only for wp-plugin type and returns the slug in all other cases. That is what this updated approach makes clear with a conditional return for that one edge case for WP plugins.
Feel free to push an update or I can update it if you can advise on the updated logic here.
|
@costdev sorry–I didn't mean to trigger a review from you as I saw your earlier comment. I was attempting to get rid of resolved conversations pertaining to the previous review. Blame it on my github skillz. :) |
|
No worries haha! |
|
Is there a test for the current state of the FAIR Connect where fair-plugin/plugin.php doesn’t list a filename in the metadata? |
|
@afragen Yes, this pull request adds those test cases, including this one with the missing My proposal is to remove the |
Fixes #492.
Updated
get_hashed_filename()method to build a stable, hashed install path for a FAIR package from its metadata.It works in this order:
filenameinto two parts: a slug-like directory name and a plugin file name.slug.php.wp-plugin, it returns a WordPress-style plugin path in the formslug-didhash/file.php.The intent of this version is:
filenamewhen availablefilenameis missing or incompletedirectory/file.phpExamples for this implementation:
filename = example/example.phpreturnsexample-<hash>/example.phpexample-<hash>/example.phpexamplereturnsexample-<hash>