Filter
uncanny-automator
automator_rest_routes_log_endpoint_logs_factory
Filters the logs factory instance used to retrieve endpoint logs in the REST API before they are fetched.
add_filter( 'automator_rest_routes_log_endpoint_logs_factory', $callback, 10, 1 );
Description
This filter hook fires when the Logs Factory object is being prepared for the REST API log endpoint. Developers can use it to replace the default `Logs_Factory` instance with a custom implementation, allowing for advanced log retrieval logic. This is useful for extending or modifying how logs are accessed via the API.
Usage
add_filter( 'automator_rest_routes_log_endpoint_logs_factory', 'your_function_name', 10, 1 );
Parameters
-
$logs_factory(mixed) - This parameter contains an instance of the `Logs_Factory` class, which is responsible for retrieving various log objects.
Return Value
The filtered value.
Examples
/**
* Example filter to modify the Logs_Factory instance before it's used by the Log_Endpoint.
*
* This filter could be used to, for instance, add a custom log retrieval method
* or modify how existing log types are accessed.
*
* @param Logs_Factory $logs_factory The current instance of the Logs_Factory.
*
* @return Logs_Factory The modified or original Logs_Factory instance.
*/
add_filter( 'automator_rest_routes_log_endpoint_logs_factory', function ( $logs_factory ) {
// Example: If you had a custom log type that needs to be available through the factory.
// Let's assume you've registered a new log resource handler elsewhere.
// For demonstration purposes, we'll simulate adding a new resource.
// In a real scenario, $logs_factory might have a method like add_log_resource()
// or get_resources() which you could manipulate.
// Since we don't have the exact internal methods of Logs_Factory from the snippet,
// we'll demonstrate a conceptual modification.
// Imagine Logs_Factory has a protected property $recipe_logs_resources,
// $trigger_logs_resources, etc., and a method to add a new one or replace one.
// As a safe example, let's assume we can get the existing resources and add a new one.
// Note: This is a hypothetical example assuming certain methods/properties exist
// within the Logs_Factory class that are not explicitly shown in the provided snippet.
// You would need to consult the actual Logs_Factory class definition for precise implementation.
// If $logs_factory has a method like get_registered_resources() which returns an array
// of resource objects (e.g., Recipe_Logs_Resource, Trigger_Logs_Resource),
// you could potentially add a new one.
// Example: Add a hypothetical 'custom_log_resource' if the factory supports it.
// if ( method_exists( $logs_factory, 'add_log_resource' ) ) {
// $custom_resource = new Custom_Log_Resource(); // Assuming this class exists.
// $logs_factory->add_log_resource( $custom_resource );
// }
// For this example, let's assume a simpler scenario: if there's a specific
// type of log we want to exclude or prioritize based on some external condition.
// This is more realistic if $logs_factory has methods to configure its behavior.
// Let's say we want to ensure that for this specific endpoint,
// we only retrieve logs that have been processed within the last hour.
// This would likely involve modifying how the underlying resources fetch data.
// If $logs_factory had a method like `set_default_fetch_parameters()`:
// if ( method_exists( $logs_factory, 'set_default_fetch_parameters' ) ) {
// $current_params = $logs_factory->get_default_fetch_parameters(); // Hypothetical getter
// $current_params['date_query'] = array(
// 'after' => date( 'Y-m-d H:i:s', strtotime( '-1 hour' ) ),
// );
// $logs_factory->set_default_fetch_parameters( $current_params );
// }
// A more robust and common use case for filters is to conditionally return a completely
// different factory or a modified version of it based on dynamic conditions.
// For simplicity, and to show a direct modification, we'll just return the
// potentially modified factory.
// If you need to return a different instance, you would instantiate it here
// and return it, ensuring it's compatible with what Log_Endpoint expects.
// For example:
// $modified_factory = new Custom_Log_Factory_Extension( $logs_factory );
// return $modified_factory;
// In this realistic example, we'll just return the original factory instance,
// but the structure is there to perform modifications or replacements.
// This filter is often used by premium add-ons or custom integrations
// to integrate their custom log types or fetch mechanisms.
// Example: If there's a requirement to filter out logs from a specific user role
// for this REST API endpoint. This would likely require access to the user
// making the request, which might not be directly available here without more context.
// However, if the $logs_factory had a method to accept such a filter:
// global $current_user;
// if ( ! current_user_can( 'manage_automator_options' ) ) {
// $logs_factory->exclude_logs_from_role( 'contributor' ); // Hypothetical method
// }
// For the purpose of a simple, runnable example, we will return the original
// factory instance without modification, but demonstrating the filter's existence.
return $logs_factory;
}, 10, 1 ); // Priority 10, accepts 1 argument
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/services/rest-routes.php:172
$conditions = new Conditions_Helper();
$action_logs_resources->set_conditions( $conditions );
// Logs Factory is a class for retrieving various logs objects.
$logs_factory = new Logs_Factory( $recipe_logs_resources, $trigger_logs_resources, $action_logs_resources, $loops_logs_resources );
// Allow for customizing the logs factory.
$logs_factory = apply_filters( 'automator_rest_routes_log_endpoint_logs_factory', $logs_factory );
// The main endpoint controller.
$log_endpoint = new Log_Endpoint( $automator_factory, $logs_factory );
$log_endpoint->set_utils( $utils );
// The wiring of the objects above can be simplified with DiC.