I create an internal plugin that offers a filtering mechanism with AJAX for each post type, depending on the configuration I saved in a JSON.
When the plugin is loaded, I have to set the callback for the AJAX call, and I set that straight away (I'll stick to OOP):
$gfb = FilterBuilder::get_instance(); $action_filter_reg_instance = new ActionFilterRegistration(); $action_filter_reg_instance->add_action( 'wp_ajax_process_filters', $gfb, 'process_filters_callback' );
This sets the callback and it works fine … but this instance of $ gbf is lost when the page that shows the posts is loaded and I have to set other properties when loading the template.
For example, when the page that shows the posts is loaded, I create a new FilterBuilder object and pass the query arguments to it, which are then stored in the instance as a property of the class.
When the callback is called, it is a separate instance of a FilterBuilder, so there is nothing in this class instance that I have defined in the template.
This puts me in a difficult situation because I want to have access to the query arguments that were defined when this particular instance was created.
I initially used transients to share the query arguments that the template instance defined through the callback function, but it just feels wrong. In addition, I believe that transients are of maximum length and can therefore explode if this limit is reached.
What other options are there? Maybe serialize the template class instance into a JSON and load it from the recall? That also sounds … a little more.
I don't want to use global variables … interested in what you think would be an elegant approach to work around this problem.
Thank you for taking the time to read this! 👍🏽