Composers are only executed when the view they are connected to is used. Your view almost certainly isnât being called during an AJAX request, so this code is inaccessible.
Composers are not meant to be a place where you collect functionality. They are for âcomposingâ data for the view only. If you have functionality you want to use in a Composer and somewhere else, you should extract that functionality from the Composer and put it somewhere else. Then you can call it in the Composer, in an AJAX request, or anywhere else that you might need to use it.
It seems like maybe there was a disconnect, though: Youâre still trying to access the Composer class for your AJAX filters. Generally you should never be accessing or using your Composers directly. If you find yourself doing that, something about your approach is probably wrong.
I donât know what the exactly functionality youâre try to implement here is, but you should be pulling the functionality out of the Composer and into something else, i.e.:
// sage/app/helpers.php
namespace App;
function get_some_news() {
return get_news_posts();
}
// sage/app/View/Composers/News.php
namespace App\View\Composers;
use Roots\Acorn\View\Composer;
class News extends Composer {
public function with() {
return [ 'news' => \App\get_some_news() ];
}
}
// sage/app/filters.php
namespace App;
add_action('wp_ajax_news_filter', __NAMESPACE__ . '\\get_some_news' );
add_action('wp_ajax_nopriv_news_filter', __NAMESPACE__ . '\\get_some_news' );