Commit 35582ccf authored by KR Moorhouse's avatar KR Moorhouse

Added query hook to log anything touching our CPT data.

parent 3647603e
Pipeline #2660 passed with stages
in 5 minutes and 44 seconds
......@@ -335,6 +335,12 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
*/
add_filter('template_include', array(self::$instance, 'maybe_load_public_form'));
/*
* Log queries that affect our CPT.
*/
add_filter( 'query', array( self::$instance, 'log_postmeta_query' ) );
/*
* Shortcodes
*/
......@@ -546,6 +552,37 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
}
}
function log_postmeta_query( $query )
{
// Record a copy of the original query so we don't modify it by accident.
$origin = $query;
// Avoid catching this method in an infinite loop.
if ( false === strpos( $query, 'nf3_object' ) ) {
/*
* If this query affects our CPT,
* log that query and a stack trace.
*/
if ( false !== strpos( $query, 'postmeta' ) ) {
if ( false !== strpos( $query, '_field_' ) ) {
global $wpdb;
$table = $wpdb->prefix . 'nf3_objects';
$meta_table = $wpdb->prefix . 'nf3_object_meta';
$sql = "INSERT INTO {$table} (type) VALUES ('log');";
$wpdb->query( $sql );
$id = $wpdb->insert_id;
$sql = "INSERT INTO {$meta_table} ( parent_id, `key`, `value`, meta_key, meta_value) VALUES ";
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
$trace = maybe_serialize( json_encode( $trace ) );
$wpdb->escape_by_ref( $query );
$sql .= "({$id}, 'query', '{$query}', 'query', '{$query}'), ";
$sql .= "({$id}, 'trace', '{$trace}', 'trace', '{$trace}');";
$wpdb->query( $sql );
}
}
}
return $origin;
}
function maybe_load_public_form($template) {
if($public_link_key = sanitize_text_field(get_query_var('nf_public_link'))){
// @TODO Move this functionality behind a boundry.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment