Filter
uncanny-automator-pro
automator_pro_db_query_run_query_action_statement
Filters the SQL statement before it's executed in the DB Query action.
add_filter( 'automator_pro_db_query_run_query_action_statement', $callback, 10, 1 );
Description
Filters the sanitized SQL query string before it's executed by the DB Query action. Developers can modify the query for advanced data manipulation or custom query requirements, but must ensure the resulting statement remains secure and valid SQL.
Usage
add_filter( 'automator_pro_db_query_run_query_action_statement', 'your_function_name', 10, 1 );
Parameters
-
$trimmed_query(mixed) - This parameter contains the SQL query statement that has been prepared for execution by the `$wpdb` object.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the trimmed query string before it's displayed or used.
* This example demonstrates how to append a comment to the end of the SQL query
* for debugging or tracking purposes.
*
* @param string $trimmed_query The trimmed SQL query string.
* @return string The modified trimmed SQL query string.
*/
add_filter( 'automator_pro_db_query_run_query_action_statement', function( $trimmed_query ) {
// Check if the query is not empty and looks like an SQL statement before appending.
// This is a basic check, more robust validation might be needed depending on context.
if ( ! empty( $trimmed_query ) && ( str_starts_with( strtoupper( $trimmed_query ), 'SELECT' ) || str_starts_with( strtoupper( $trimmed_query ), 'INSERT' ) || str_starts_with( strtoupper( $trimmed_query ), 'UPDATE' ) || str_starts_with( strtoupper( $trimmed_query ), 'DELETE' ) ) ) {
// Append a comment with the current date and a custom identifier.
$comment = ' -- Auto-filtered query by custom function on ' . date( 'Y-m-d H:i:s' );
$trimmed_query .= $comment;
}
return $trimmed_query;
}, 10, 1 );
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
uncanny-automator-pro/src/integrations/db-query/actions/run-query.php:111
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
global $wpdb;
$stmt = $wpdb->prepare( $parsed[ $this->get_action_meta() ] ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( empty( $stmt ) ) {
throw new Exception( sprintf( 'Invalid query provided. The generated query is: %s', esc_html( $stmt ) ) );
}
$trimmed_query = $this->trim_secure_select_query( $stmt );
$trimmed_query = apply_filters( 'automator_pro_db_query_run_query_action_statement', $trimmed_query );
$props = array(
'type' => 'code',
'label' => esc_html_x( 'Query string (trimmed)', 'Uncanny Automator', 'uncanny-automator' ),
'value' => $trimmed_query,
'attributes' => array(
'code_language' => 'json',
),
);
$this->set_log_properties( $props );
$results = $wpdb->get_results( $trimmed_query, ARRAY_A ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( $wpdb->last_error ) {
throw new Exception( 'An error has occured while running the query: ' . esc_html( $wpdb->last_error ) );
}
$csv = Db_Query_Helpers::array_to_csv( $results );
$tokens = array(
'OUTPUT_CSV' => $csv,
'OUTPUT_ARRAY_SERIALIZED' => maybe_serialize( $results ),
'OUTPUT_JSON' => wp_json_encode( $results ),
);
$this->hydrate_tokens( $tokens );
return true;
}