PurpleTurtleCreative / completionist

Asana integration plugin for WordPress.
https://purpleturtlecreative.com/completionist/
GNU General Public License v3.0
1 stars 0 forks source link

Use plugins_url() instead of cached constant PLUGIN_URL #146

Closed MichelleBlanchette closed 8 months ago

MichelleBlanchette commented 1 year ago

Original https://github.com/PurpleTurtleCreative/completionist-pro/issues/23

As expected, using the cached constant avoids filters applied to plugins_url such as when wanting to map to a different host name.

I believe it'd be like this:

$wrong   = PLUGIN_URL . '/build/index_ShortcodeAsanaProject.jsx.js';
$correct = plugins_url( 'build/index_ShortcodeAsanaProject.jsx.js', PLUGIN_FILE );
MichelleBlanchette commented 9 months ago

I believe this is a non-issue because you should be able to hook earlier from within an mu-plugin. This should be confirmed before closing this issue.

The problem here is that the initial call to plugins_url() is executed in Completionist's main file, so the timing sequence is "random" for other plugins trying to hook into it. At least, that's my hypothesis. As stated, this needs to be confirmed.

MichelleBlanchette commented 8 months ago

Oh, that's funny. WordPress's documentation actually notes this (source):

The plugins_url() function should not be called in the global context of plugins, but rather in a hook like “init” or “admin_init” to ensure that the “plugins_url” filters are already hooked at the time the function is called. This is vital for many site configurations to work, and if plugins_url() is called in the global context of a plugin file it cannot be filtered by other plugins (though mu-plugins are able to filter it because they run before any other plugins).

Regardless, I did confirm the usage of an mu-plugin catches it appropriately:

<?php
// wp-content/mu-plugins/test.php
add_filter(
    'plugins_url',
    function ( $url ) {
        error_log( '*** ' . $url );
        return $url;
    }
);