save_queries = true; } class SmackQuery { private $display = true; private $starttime, $servertime, $css, $ajax,$enqueued_styles='',$data; protected static $instance = null,$plugin; public $query_row = 0; public $total_time = 0; public $total_qs = 0; public static function getInstance() { if ( null == self::$instance ) { self::$instance = new self; self::$plugin = Plugin::getInstance(); self::$instance->doHooks(); } return self::$instance; } public function doHooks(){ add_action('wp_ajax_get_query_informations', array($this,'get_query_info')); } function __construct() { $css='.query-dd{display:block;line-height:24px;background-color:#c4c4c4;color:#000;font-family:Helvetica,Arial,sans-serif;font-size:18px;text-align:center}.query-bs{margin:0 45px}.query-ss{margin:0 4px;border-left:5px solid #999}#query_dd{position:fixed;left:50%;bottom:0;z-index:9999998;margin-left:-160px;width:475px;height:26px;white-space:nowrap;overflow:hidden;background-color:rgb(159 188 253 / 50%);border-radius:6px;margin-bottom:21px;padding:2px}'; $this->css = '
') . '
ajax) ? '>' : ' id="query_dd">') . "Memory:{$memory_usage}MTime:{$time_usage}sQueries:{$wpdb->num_queries}Q
"; // echo ( $this->css . '
') . '
ajax) ? '>' : ' id="query_dd">') . "Memory:{$memory_usage}MTime:{$time_usage}sQueries:{$wpdb->num_queries}Q
"; $class = array( 'smack-no-js', ); if ( did_action( 'wp_head' ) ) { $class[] = sprintf( 'smack-theme-%s', get_template() ); $class[] = sprintf( 'smack-theme-%s', get_stylesheet() ); } if ( ! is_admin_bar_showing() ) { $class[] = 'smack-peek'; } if (is_user_logged_in()) { echo '
'; echo '
'; echo '
'; echo '

' . esc_html__( 'Smack Query / Asset View', 'all-in-one' ) . '

'; echo ''; echo ''; echo '
'; $this->query_output_view(); $this->asset_output_view(); echo '
'; } } } public function smack_query_view($queryss){ $unserialize=$queryss; foreach ( $unserialize as $name => $db ) { if ( is_a( $db, 'wpdb' ) ) { $response=$this->process_db( $name, $db ); } } } public function get_query_info(){ if (!is_user_logged_in() || !current_user_can('manage_options')) { wp_send_json_error(['message' => 'Unauthorized access.'], 403); return; } $this->data['total_qs'] = 0; $this->data['total_time'] = 0; $this->data['errors'] = array(); // $this->db_objects = get_option('smack_db_queries'); $this->db_objects = apply_filters( 'qm/collect/db_objects', array( '$wpdb' => $GLOBALS['wpdb'], ) ); $unserialize = $this->db_objects; //$this->custom_unserialize($this->db_objects); foreach ( $unserialize as $name => $db ) { if ( is_a( $db, 'wpdb' ) ) { $response=$this->process_db( $name, $db ); $component_times=$this->query_status($response['component_times']); $caller_times=$this->caller_status($response['times']); $select_chart_report= $this->caller_report_status($response['times']); $time_chart_report= $this->chart_report_status($response['component_times']); } else { unset( $unserialize[ $name ] ); } } global $wpdb; $id_to_check = 1; // Replace this with the actual id value you want to check $displayQueryConditions=''; $in_value_check = $wpdb->get_results( $wpdb->prepare( "SELECT display_query FROM {$wpdb->prefix}aio_asset_table_entery WHERE id = %d", $id_to_check ) ); // Check if the query was successful if ($in_value_check !== false) { // Display or process the result foreach ($in_value_check as $result) { // Assuming display_query is a column in the result set $displayQueryConditions=$result->display_query; } } else { // Handle the case where the query failed $wpdb_last_error = $wpdb->last_error; error_log("Database error: $wpdb_last_error"); } echo wp_json_encode(['response' => ['time_chart_report' => $time_chart_report, 'plugin_query_details' => $component_times,'caller_details' => $caller_times,'caller_query_details'=>$select_chart_report ],'displayQueryConditions'=>$displayQueryConditions,'status' => 200, 'success' => true]); wp_die(); } public function chart_report_status($component){ if(isset($component)){ $name = array_column($component, 'component'); $time = array_column($component, 'ltime'); $result = array_combine($name, $time); return $result; } } public function caller_report_status($select){ $name = array_column($select, 'caller'); $time = array_column($select, 'ltime'); $result = array_combine($name, $time); return $result; } public function query_status($plugins){ $result_array = []; $temp = 0; foreach($plugins as $plugin){ $result_array[$temp]['plugin'] = $plugin['component']; $result_array[$temp]['ltime'] = $plugin['ltime']; $result_array[$temp]['SELECT'] = isset($plugin['types']['SELECT']) ? $plugin['types']['SELECT'] : null ; $result_array[$temp]['INSERT'] = isset($plugin['types']['CREATE']) ? $plugin['types']['CREATE'] : null ; $result_array[$temp]['UPDATE'] = isset($plugin['types']['ALTER']) ? $plugin['types']['ALTER'] : null ; $result_array[$temp]['SHOW'] = isset($plugin['types']['SHOW']) ? $plugin['types']['SHOW'] : null ; $temp++; } return $result_array; } public function caller_status($callers){ $caller_array = []; $temp = 0; foreach($callers as $caller){ $caller_array[$temp]['caller'] = $caller['caller']; $caller_array[$temp]['ltime'] = $caller['ltime']; $caller_array[$temp]['SELECT'] = isset($caller['types']['SELECT']) ? $caller['types']['SELECT'] : null ; $caller_array[$temp]['INSERT'] = isset($caller['types']['CREATE']) ? $caller['types']['CREATE'] : null ; $caller_array[$temp]['UPDATE'] = isset($caller['types']['ALTER']) ? $caller['types']['ALTER'] : null ; $caller_array[$temp]['SHOW'] = isset($caller['types']['SHOW']) ? $caller['types']['SHOW'] : null ; $temp++; } return $caller_array; } public function process_db($id,$db){ global $EZSQL_ERROR, $wp_the_query; // With SAVEQUERIES defined as false, `wpdb::queries` is empty but `wpdb::num_queries` is not. if ( empty( $db->queries ) ) { $this->data['total_qs'] += $db->num_queries; return; } $rows = array(); $types = array(); $has_result = false; $has_trace = false; $total_time = $this->total_time; $total_qs = $this->total_qs; $i = 0; $request = isset($wp_the_query->request) ? trim( $wp_the_query->request ) :''; if ( method_exists( $db, 'remove_placeholder_escape' ) ) { $request = $db->remove_placeholder_escape( $request ); } foreach ( (array) $db->queries as $query ) { # @TODO: decide what I want to do with this: if ( false !== strpos( $query[2], 'wp_admin_bar' ) and !isset( $_REQUEST['qm_display_admin_bar'] ) ) { // phpcs:ignore continue; } $sql = $query[0]; $ltime = $query[1]; $stack = $query[2]; $has_start = isset( $query[3] ); $has_trace = isset( $query['trace'] ); $has_result = isset( $query['result'] ); if ( $has_result ) { $result = $query['result']; } else { $result = null; } $this->total_time += $ltime; if ( $has_trace ) { $trace = $query['trace']; $component = $query['trace']->get_component(); $caller = $query['trace']->get_caller(); $caller_name = $caller['display']; $caller = $caller['display']; } else { $trace = null; $component = null; $callers = explode( ',', $stack ); $caller = trim( end( $callers ) ); if ( false !== strpos( $caller, '(' ) ) { $caller_name = substr( $caller, 0, strpos( $caller, '(' ) ) . '()'; } else { $caller_name = $caller; } } $sql = trim( $sql ); $type = self::getting_queries_type( $sql ); $this->type_log( $type ); $this->caller_log( $caller_name, $ltime, $type ); $this->dupe_log( $sql, $i ); if ( $component ) { $this->component_log( $component, $ltime, $type ); } if ( ! isset( $types[ $type ]['total'] ) ) { $types[ $type ]['total'] = 1; } else { $types[ $type ]['total']++; } if ( ! isset( $types[ $type ]['callers'][ $caller ] ) ) { $types[ $type ]['callers'][ $caller ] = 1; } else { $types[ $type ]['callers'][ $caller ]++; } $is_main_query = ( $request === $sql && ( false !== strpos( $stack, ' WP->main,' ) ) ); $row = compact( 'caller', 'caller_name', 'sql', 'ltime', 'result', 'type', 'component', 'trace', 'is_main_query' ); if ( ! isset( $trace ) ) { $row['stack'] = $stack; } if ( is_wp_error( $result ) ) { $this->data['errors'][] = $row; } if ( self::is_expensive( $row ) ) { $this->data['expensive'][] = $row; } $rows[ $i ] = $row; $i++; } if ( '$wpdb' === $id && ! $has_result && ! empty( $EZSQL_ERROR ) && is_array( $EZSQL_ERROR ) ) { // Fallback for displaying database errors when wp-content/db.php isn't in place foreach ( $EZSQL_ERROR as $error ) { $row = array( 'caller' => null, 'caller_name' => null, 'stack' => '', 'sql' => $error['query'], 'ltime' => 0, 'result' => new \WP_Error( 'qmdb', $error['error_str'] ), 'type' => '', 'component' => false, 'trace' => null, 'is_main_query' => false, ); $this->data['errors'][] = $row; } } $this->total_qs = count( $rows ); if (!isset($this->data['total_qs'])) { $this->data['total_qs'] = 0; } if (!isset($this->data['total_time'])) { $this->data['total_time'] = 0; } $this->data['total_qs'] += $this->total_qs; $this->data['total_time'] += $this->total_time; // error_log(print_r(["total_qs"=>$this->data],true),3,'/var/www/html/ram.log'); $has_main_query = wp_list_filter( $rows, array( 'is_main_query' => true, ) ); $this->data['dbs'][ $id ] = (object) compact( 'rows', 'types', 'has_result', 'has_trace', 'total_time', 'total_qs', 'has_main_query' ); // $serialized_data = $this->custom_serialize($this->data); // error_log(print_r(["thisdata"=>$this->data],true),3,'/var/www/html/ram.log'); update_option('data_db',''); return $this->data; } protected function type_log( $type ) { if ( isset( $this->data['types'][ $type ] ) ) { $this->data['types'][ $type ]++; } else { $this->data['types'][ $type ] = 1; } } public static function is_expensive( array $row ) { return $row['ltime'] > 0.05; } protected function caller_log( $caller, $ltime, $type ) { if ( ! isset( $this->data['times'][ $caller ] ) ) { $this->data['times'][ $caller ] = array( 'caller' => $caller, 'ltime' => 0, 'types' => array(), ); } $this->data['times'][ $caller ]['ltime'] += $ltime; if ( isset( $this->data['times'][ $caller ]['types'][ $type ] ) ) { $this->data['times'][ $caller ]['types'][ $type ]++; } else { $this->data['times'][ $caller ]['types'][ $type ] = 1; } } protected function dupe_log( $sql, $i ) { $sql = str_replace( array( "\r\n", "\r", "\n" ), ' ', $sql ); $sql = str_replace( array( "\t", '`' ), '', $sql ); $sql = preg_replace( '/ +/', ' ', $sql ); $sql = trim( $sql ); $sql = rtrim( $sql, ';' ); $this->data['dupes'][ $sql ][] = $i; } protected function component_log( $component, $ltime, $type ) { if ( ! isset( $this->data['component_times'][ $component->name ] ) ) { $this->data['component_times'][ $component->name ] = array( 'component' => $component->name, 'ltime' => 0, 'types' => array(), ); } $this->data['component_times'][ $component->name ]['ltime'] += $ltime; if ( isset( $this->data['component_times'][ $component->name ]['types'][ $type ] ) ) { $this->data['component_times'][ $component->name ]['types'][ $type ]++; } else { $this->data['component_times'][ $component->name ]['types'][ $type ] = 1; } } public static function getting_queries_type( $sql ) { // Trim leading whitespace and brackets $sql = ltrim( $sql, ' \t\n\r\0\x0B(' ); if ( 0 === strpos( $sql, '/*' ) ) { // Strip out leading comments such as `/*NO_SELECT_FOUND_ROWS*/` before calculating the query type $sql = preg_replace( '|^/\*[^\*/]+\*/|', '', $sql ); } $words = preg_split( '/\b/', trim( $sql ), 2, PREG_SPLIT_NO_EMPTY ); $type = strtoupper( $words[0] ); return $type; } public function query_output_view(){ // $db = get_option('data_db'); $overAllQuerys = apply_filters( 'qm/collect/db_objects', array( '$wpdb' => $GLOBALS['wpdb'], ) ); $this->smack_query_view($overAllQuerys); $db=$this->data; $this->query_row = 0; echo ''; } public static function get_query_type( $sql ) { $sql = ltrim( $sql, ' \t\n\r\0\x0B(' ); if ( 0 === strpos( $sql, '/*' ) ) { $sql = preg_replace( '|^/\*[^\*/]+\*/|', '', $sql ); } $words = preg_split( '/\b/', trim( $sql ), 2, PREG_SPLIT_NO_EMPTY ); $type = strtoupper( $words[0] ); return $type; } public static function format_sql( $sql ) { $sql = str_replace( array( "\r\n", "\r", "\n", "\t" ), ' ', $sql ); $sql = esc_html( $sql ); $sql = trim( $sql ); $regex = 'ADD|AFTER|ALTER|AND|BEGIN|COMMIT|CREATE|DELETE|DESCRIBE|DO|DROP|ELSE|END|EXCEPT|EXPLAIN|FROM|GROUP|HAVING|INNER|INSERT|INTERSECT|LEFT|LIMIT|ON|OR|ORDER|OUTER|RENAME|REPLACE|RIGHT|ROLLBACK|SELECT|SET|SHOW|START|THEN|TRUNCATE|UNION|UPDATE|USE|USING|VALUES|WHEN|WHERE|XOR'; $sql = preg_replace( '# (' . $regex . ') #', '
$1 ', $sql ); $keywords = '\b(?:ACTION|ADD|AFTER|ALTER|AND|ASC|AS|AUTO_INCREMENT|BEGIN|BETWEEN|BIGINT|BINARY|BIT|BLOB|BOOLEAN|BOOL|BREAK|BY|CASE|COLLATE|COLUMNS?|COMMIT|CONTINUE|CREATE|DATA(?:BASES?)?|DATE(?:TIME)?|DECIMAL|DECLARE|DEC|DEFAULT|DELAYED|DELETE|DESCRIBE|DESC|DISTINCT|DOUBLE|DO|DROP|DUPLICATE|ELSE|END|ENUM|EXCEPT|EXISTS|EXPLAIN|FIELDS|FLOAT|FOREIGN|FOR|FROM|FULL|FUNCTION|GROUP|HAVING|IF|IGNORE|INDEX|INNER|INSERT|INTEGER|INTERSECT|INTERVAL|INTO|INT|IN|IS|JOIN|KEYS?|LEFT|LIKE|LIMIT|LONG(?:BLOB|TEXT)|MEDIUM(?:BLOB|INT|TEXT)|MERGE|MIDDLEINT|NOT|NO|NULLIF|ON|ORDER|OR|OUTER|PRIMARY|PROC(?:EDURE)?|REGEXP|RENAME|REPLACE|RIGHT|RLIKE|ROLLBACK|SCHEMA|SELECT|SET|SHOW|SMALLINT|START|TABLES?|TEXT(?:SIZE)?|THEN|TIME(?:STAMP)?|TINY(?:BLOB|INT|TEXT)|TRUNCATE|UNION|UNIQUE|UNSIGNED|UPDATE|USE|USING|VALUES?|VAR(?:BINARY|CHAR)|WHEN|WHERE|WHILE|XOR)\b'; $sql = preg_replace( '#' . $keywords . '#', '$0', $sql ); return '' . $sql . ''; } public function asset_output_view() { $page = home_url($_SERVER['REQUEST_URI']); $page_value = parse_url($page); // $current_page = $page_value['path'] . isset($page_value['query']) ? $page_value['query'] :''; // Step 1: Get enqueued scripts and styles for the current page $enqueued_styles = wp_styles()->queue; $enqueued_scripts = wp_scripts()->queue; echo '
'; echo ''; echo '"; echo '
'; echo ''; } } add_action('plugins_loaded', function(){new SmackQuery();}, 1);