Hi,
Great, however, I discovered that it creates other issue on pages that doesn’t have the media player loaded. You seem to load pretty much everything on everypage even if there is no upload happening on a page.
stockpack-load-admin.js?ver=3.2.4:1 Uncaught TypeError: Cannot read properties of undefined (reading 'search')
at Object.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:20702)
at i (stockpack-load-admin.js?ver=3.2.4:1:110)
at Module.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:51573)
at i (stockpack-load-admin.js?ver=3.2.4:1:110)
at Object.<anonymous> (stockpack-load-admin.js?ver=3.2.4:1:51525)
at i (stockpack-load-admin.js?ver=3.2.4:1:110)
at stockpack-load-admin.js?ver=3.2.4:1:903
at stockpack-load-admin.js?ver=3.2.4:1:912
I do however have a fix AND a tricks that solves the issue.
The idea is to check if did_action(“wp_enqueue_media”) is true
In class-stockpackmedia.php, you can do this, it works
public function enqueue($admin = true, $no_dialog = false)
{
if (did_action('wp_enqueue_media')) {
$this->enqueue_script($admin);
$this->enqueue_style($no_dialog);
$this->enqueue_settings($admin);
}
}
But I think there’s a better way that keeps your exact same logic and doesn’t messed up anything.
The idea is to use stockpack_frontend_load later when enqueue scripts are called. That makes the “frontend_load_stockpack” filter much more useful.
Here’s what I tested successfuly.
public function enqueue($admin = true, $no_dialog = false)
{
if (stockpack_frontend_load()) {
$this->enqueue_script($admin);
$this->enqueue_style($no_dialog);
$this->enqueue_settings($admin);
}
}
public function templates()
{
if (stockpack_frontend_load()) {
include_once(__DIR__ . '/../templates/attachment.php');
include_once(__DIR__ . '/../templates/empty.php');
include_once(__DIR__ . '/../templates/details.php');
include_once(__DIR__ . '/../templates/downloader.php');
include_once(__DIR__ . '/../templates/dialog.php');
include_once(__DIR__ . '/../templates/attribution.php');
include_once(__DIR__ . '/../templates/no-search.php');
}
}
public function enqueue_frontend()
{
if (stockpack_frontend_load()) {
$this->enqueue(false);
}
}
public function load_frontend_style()
{
if (stockpack_frontend_load()) {
wp_enqueue_style('stockpack-frontend', plugins_url('/dist/css/stockpack-frontend.css', STOCKPACK_DIR), false, $this->version);
}
}
And then somebody can filter it out with :
add_filter("frontend_load_stockpack", function ($load) {
if (!did_action("wp_enqueue_media")) {
return false;
} else {
return $load;
}
});
Let me know what you think