Ticket #5218: wpdb-get-caller-r6256-a.diff

File wpdb-get-caller-r6256-a.diff, 1.3 KB (added by tellyworth, 5 years ago)
  • wordpress/wp-includes/wp-db.php

     
    210210                ++$this->num_queries; 
    211211 
    212212                if (SAVEQUERIES) 
    213                         $this->queries[] = array( $query, $this->timer_stop() ); 
     213                        $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 
    214214 
    215215                // If there is an error then take note of it.. 
    216216                if ( mysql_error($this->dbh) ) { 
     
    457457        { 
    458458                return ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ); 
    459459        } 
     460 
     461        /** 
     462         * Get the name of the function that called wpdb. 
     463         * @return string the name of the calling function 
     464         */ 
     465        function get_caller() { 
     466                // requires PHP 4.3+ 
     467                if ( !is_callable('debug_backtrace') ) 
     468                        return ''; 
     469 
     470                $bt = debug_backtrace(); 
     471                $caller = ''; 
     472 
     473                foreach ( $bt as $trace ) { 
     474                        if ( @$trace['class'] == __CLASS__ ) 
     475                                continue; 
     476                        elseif ( strtolower(@$trace['function']) == 'call_user_func_array' ) 
     477                                continue; 
     478                        elseif ( strtolower(@$trace['function']) == 'apply_filters' ) 
     479                                continue; 
     480                        elseif ( strtolower(@$trace['function']) == 'do_action' ) 
     481                                continue; 
     482         
     483                        $caller = $trace['function']; 
     484                        break; 
     485                } 
     486                return $caller; 
     487        } 
     488 
    460489} 
    461490 
    462491if ( ! isset($wpdb) )