Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Ensure an Asset Version is Always Present #13

Closed
wants to merge 3 commits into from
Closed

Ensure an Asset Version is Always Present #13

wants to merge 3 commits into from

Conversation

kopepasah
Copy link

Fixes #12

This pull request ensures that when using the $plugin->asset_version() method, a version will always be output.

This extension ensure that we always return some version by checking the file modified time or defaulting to the installed WP Version.

$version = $this->version();

if ( empty( $version ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is a generic helper to bump the asset version number with every plugin update so maybe we could introduce a new helper for getting a file-specific version string based on filemtime()?

if ( ! empty( $file ) && file_exists( $file ) ) {
$version = (string) filemtime( $file );
} else {
$version = $GLOBALS['wp_version'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we pass the WP version string as a dependency to the __constructor() to avoid coupling this class with a global state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I see the value of adding the global check as there should always be a version in the file header. We control that and is expected. If it's not there then we should throw an error in the version() method.

if ( $this->is_debug() || $this->is_script_debug() ) {
    return (string) time();
}

if ( ! empty( $file ) && file_exists( $file ) ) {
    return (string) filemtime( $file );
}

return $this->version();

@@ -164,7 +186,7 @@ public function asset_version() {
public function meta( $field = null ) {
static $meta;

if ( ! isset( $meta ) ) {
if ( ! isset( $meta ) && function_exists( 'get_plugin_data' ) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we switch to using get_file_data() which appears to be always available?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but get_plugin_data does a better job at sane defaults.

If we continue to use it we need to add:

if ( ! function_exists( 'get_plugin_data' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}

* @param string $path_relative Path relative to this plugin directory root.
* @return string The path to the asset.
*/
public function asset_path( $path_relative = '' ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should always expect a valid input similar to asset_url().

* @return string The path to the asset.
*/
public function asset_path( $path_relative = '' ) {
return plugin_dir_path( $this->file ) . $path_relative;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have $this->dir so we could use something like:

return sprintf( '%s/%s', $this->dir, ltrim( $path_relative, '/' ) );

@derekherman
Copy link
Contributor

@kopepasah in the future could you avoid working from a personal fork as that makes it impossible to work collaboratively.

@kasparsd
Copy link
Collaborator

kasparsd commented Dec 4, 2019

Fixed in #16.

@kasparsd kasparsd closed this Dec 4, 2019
@kopepasah
Copy link
Author

kopepasah commented Dec 4, 2019

@kopepasah in the future could you avoid working from a personal fork as that makes it impossible to work collaboratively.

Yep, not a problem. At the time, I did not have any access to do so.

@kopepasah
Copy link
Author

Thanks @kasparsd and @derekherman for pushing this forward!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling $plugin->asset_version() Outside the Admin Throws an Error
3 participants