| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Debug |
| 4 |
* |
| 5 |
* @editor tungnx |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class LP_Debug { |
| 13 |
/** |
| 14 |
* @var null |
| 15 |
*/ |
| 16 |
private static $_instance = null; |
| 17 |
/** |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
private static $_time = array(); |
| 21 |
/** |
| 22 |
* Constructor for the logger. |
| 23 |
*/ |
| 24 |
protected function __construct() { |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
protected static $_transaction_started = false; |
| 31 |
|
| 32 |
public function output() { |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Set time start |
| 37 |
* |
| 38 |
* @param string $name |
| 39 |
*/ |
| 40 |
public static function time_start( string $name = '' ) { |
| 41 |
if ( ! self::is_debug() ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if ( empty( $name ) ) { |
| 46 |
echo 'Please set unique name'; |
| 47 |
} |
| 48 |
|
| 49 |
self::$_time[ $name ] = microtime( true ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get total time execute |
| 54 |
* |
| 55 |
* @param string $name |
| 56 |
*/ |
| 57 |
public static function time_end( string $name = '' ) { |
| 58 |
if ( ! self::is_debug() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
if ( empty( $name ) ) { |
| 62 |
echo 'Please set unique name'; |
| 63 |
} |
| 64 |
|
| 65 |
$time = microtime( true ) - self::$_time[ $name ]; |
| 66 |
echo esc_html( wp_sprintf( '%s execution time = %s', $name, $time ) ); |
| 67 |
unset( self::$_time[ $name ] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Start a new sql transaction |
| 72 |
* |
| 73 |
* Remove this function on add-on Frontend Editor and update |
| 74 |
*/ |
| 75 |
public static function startTransaction() { |
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
if ( self::$_transaction_started ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$wpdb->query( 'START TRANSACTION;' ); |
| 83 |
|
| 84 |
self::$_transaction_started = true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Rollback a sql transaction |
| 89 |
*/ |
| 90 |
public static function rollbackTransaction() { |
| 91 |
global $wpdb; |
| 92 |
|
| 93 |
if ( ! self::$_transaction_started ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$wpdb->query( 'ROLLBACK;' ); |
| 98 |
|
| 99 |
self::$_transaction_started = false; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Show value of variable |
| 104 |
* |
| 105 |
* @param mixed $variable |
| 106 |
* @param string $file_path |
| 107 |
* @param string $line |
| 108 |
*/ |
| 109 |
public static function var_dump( $variable, string $file_path = '', string $line = '' ) { |
| 110 |
echo wp_kses( '<pre>' . print_r( $variable, true ) . '</pre>', true ); |
| 111 |
echo 'FILE:' . esc_html( $file_path ) . '<br> LINE:' . esc_html( $line ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Check enable debug mode |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
* @since 3.2.8 |
| 119 |
* @editor tungnx |
| 120 |
*/ |
| 121 |
public static function is_debug(): bool { |
| 122 |
return LP_Settings::get_option( 'debug', 'no' ) === 'yes'; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Set error log |
| 127 |
* |
| 128 |
* @param Throwable $e |
| 129 |
* |
| 130 |
* @return void |
| 131 |
* @version 1.0.1 |
| 132 |
* @since 3.0.0 |
| 133 |
*/ |
| 134 |
public static function error_log( Throwable $e ) { |
| 135 |
error_log( sprintf( 'MESSAGE: %s FILE: %s LINE: %s', $e->getMessage(), $e->getFile(), $e->getLine() ) ); |
| 136 |
} |
| 137 |
|
| 138 |
public static function log_to_comment( $comment_content ) { |
| 139 |
wp_insert_comment( |
| 140 |
[ |
| 141 |
'comment_content' => $comment_content, |
| 142 |
'comment_type' => 'lp_debug_log', |
| 143 |
] |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @return LP_Debug|null |
| 149 |
*/ |
| 150 |
public static function instance() { |
| 151 |
if ( ! self::$_instance ) { |
| 152 |
self::$_instance = new self(); |
| 153 |
} |
| 154 |
|
| 155 |
return self::$_instance; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return LP_Debug::instance(); |
| 160 |
|