-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add asset cache version helper function. #585
Conversation
Remarking from #440 as it relates to the changes here:
Per documentation, it sounds like |
index.php
Outdated
$version = false; | ||
|
||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && $file ) { | ||
if ( file_exists( plugin_dir_path( __FILE__ ) . $file ) ) { |
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.
Nice addition of the file_exists
. I think I'd seen some discussion where this was previously logging a warning if the file didn't exist.
index.php
Outdated
$version = $plugin['Version']; | ||
} | ||
} else { | ||
$version = false; |
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.
It doesn't seem to follow that $value
could be anything other than $false
at this point (except manual tampering of the transient). Do we need to assign this?
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.
And if we do eliminate the else
, we could probably collapse the function_exists
check into the ! $version
condition to bump down the indentation one.
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.
You are correct. Originally I had set the transient outside the brackets and forgot to remove after moving.
index.php
Outdated
if ( function_exists( 'get_plugin_data' ) ) { | ||
$plugin = get_plugin_data( __FILE__, false ); | ||
|
||
if ( isset( $plugin['Version'] ) ) { |
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.
In my testing, it appears the Version
key will always be set in the return value from get_plugin_data
, but could be empty. Therefore we'll probably want ! empty
instead of isset
here.
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.
! empty
sound like a good addition.
@aduth I pushed up a few changes to address your comments. |
@kopepasah I'm not seeing any changes. Did you forget to push? |
index.php
Outdated
$version = $plugin['Version']; | ||
} | ||
|
||
set_transient( 'gutenberg_version', $version, DAY_IN_SECONDS ); |
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.
With the logic here, if the version is not assigned, we'll perform a get_transient
and set_transient
on every request. I think we'll either want to:
- Only
set_transient
ifVersion
is not empty (move into condition above) - Allow transient to be set to an empty string, but strictly check this before calling
get_plugin_data
My preference is toward the second, which is a matter of changing ! $version
to false === $version
to reflect we only want the condition to match if get_transient
returns it's empty state, but not for the empty string.
If the transient does not exist, does not have a value, or has expired, then get_transient will return
false
.
https://codex.wordpress.org/Function_Reference/get_transient
Practically speaking, we know that the version will be assigned (it already is), but if we were so confident, we'd also not need the ! empty( $plugin['Version'] )
check. So one way or another, we should be consistent.
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 agree the second option is probably best and being more explicit by using false === $version
is a good idea; however, allowing an empty value (while it may never be empty in this case) would not be ideal if we want the transient to update if the value were to return not empty (after set as empty).
How about something like:
if ( false === $version && function_exists( 'get_plugin_data' ) ) {
$plugin = get_plugin_data( __FILE__, false );
if ( ! empty( $plugin['Version'] ) ) {
$version = $plugin['Version'];
set_transient( 'gutenberg_version', $version, DAY_IN_SECONDS );
}
}
How do we ensure that the stored version transient will be invalidated on plugin upgrade (or install/reinstall)? I think that needs to be done here. |
At that point, I wonder if it's all worth pulling, caching, and invalidating the plugin metadata vs. just |
We should implement activate/deactivate hooks to set the plugin version.
While I typically do not like defining constants for version numbers, I agree it might be worth considering in this case. |
Such hooks can be considered harmful. See WordPress/WordPress-Coding-Standards#881 (comment) |
@westonruter thanks for pointing that out. At this point I would say it is probably best to use a constant for the version number. |
This was implemented in #1025 as part of the plugin zip build process. This PR can be simplified significantly as a result: use the |
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.
There was no activity for a quite some time and now the changed file is conflicting. @kopepasah, do you plan to resolve those conflicts and address all above comments?
This was superseded by #1025. |
Never mind. I should have read the comments including my latest more carefully :) |
Sorry, have been away. I can resolve these changes in the next 24 hours. |
# Conflicts: # index.php
After review, I can see now that Before I make all of these updates, are we certain we want to use |
Not familiar with this part of Gutenberg.
@pento can you help with a review here or help find someone else? |
@pento I am happy to make the changes, but want to verify just to make sure we are going this route before spending the time. |
Codecov Report
@@ Coverage Diff @@
## master #585 +/- ##
==========================================
+ Coverage 33.69% 33.72% +0.03%
==========================================
Files 185 191 +6
Lines 5594 5903 +309
Branches 976 1043 +67
==========================================
+ Hits 1885 1991 +106
- Misses 3141 3308 +167
- Partials 568 604 +36
Continue to review full report at Codecov.
|
So, I'm pondering if we should modify
|
Wouldn't it be better to use a function which returns the mtime in development mode? Otherwise things may be cached in between commits. |
A function seems like a bit of overkill, and I'm not wild about the pattern of writing out the filename twice. Also, if you're actively editing code, your dev environment should either be not caching, or responding with an appropriate |
The reasoning behind using filemtime is that in practice this will often not be the case, and we should ensure that dev environments behave predictably anyway. Edit: The above is just to explain the reasoning behind the existing decision. I won't be working on this further and don't have a strong opinion about the best way forward. |
This PR has zero changes - close? |
This helper function will use
filemtime
while usingWP_DEBUG
for development and Gutenberg's version for package releases.