| 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 |
/** |
| 29 |
* @var bool |
| 30 |
*/ |
| 31 |
protected static $_transaction_started = false; |
| 32 |
|
| 33 |
public function output() { |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Set time start |
| 39 |
* |
| 40 |
* @param string $name |
| 41 |
*/ |
| 42 |
public static function time_start( string $name = '' ) { |
| 43 |
if ( ! self::is_debug() ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if ( empty( $name ) ) { |
| 48 |
echo 'Please set unique name'; |
| 49 |
} |
| 50 |
|
| 51 |
self::$_time[ $name ] = microtime( true ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get total time execute |
| 56 |
* |
| 57 |
* @param string $name |
| 58 |
*/ |
| 59 |
public static function time_end( string $name = '' ) { |
| 60 |
if ( ! self::is_debug() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
if ( empty( $name ) ) { |
| 64 |
echo 'Please set unique name'; |
| 65 |
} |
| 66 |
|
| 67 |
$time = microtime( true ) - self::$_time[ $name ]; |
| 68 |
echo esc_html( wp_sprintf( '%s execution time = %s', $name, $time ) ); |
| 69 |
unset( self::$_time[ $name ] ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Start a new sql transaction |
| 74 |
* |
| 75 |
* Remove this function on add-on Frontend Editor and update |
| 76 |
*/ |
| 77 |
public static function startTransaction() { |
| 78 |
global $wpdb; |
| 79 |
|
| 80 |
if ( self::$_transaction_started ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$wpdb->query( 'START TRANSACTION;' ); |
| 85 |
|
| 86 |
self::$_transaction_started = true; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Rollback a sql transaction |
| 91 |
*/ |
| 92 |
public static function rollbackTransaction() { |
| 93 |
global $wpdb; |
| 94 |
|
| 95 |
if ( ! self::$_transaction_started ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$wpdb->query( 'ROLLBACK;' ); |
| 100 |
|
| 101 |
self::$_transaction_started = false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Show value of variable |
| 106 |
* |
| 107 |
* @param mixed $variable |
| 108 |
* @param string $file_path |
| 109 |
* @param string $line |
| 110 |
*/ |
| 111 |
public static function var_dump( $variable, string $file_path = '', string $line = '' ) { |
| 112 |
echo wp_kses( '<pre>' . print_r( $variable, true ) . '</pre>', true ); |
| 113 |
echo 'FILE:' . esc_html( $file_path ) . '<br> LINE:' . esc_html( $line ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Check enable debug mode |
| 118 |
* |
| 119 |
* @return bool |
| 120 |
* @since 3.2.8 |
| 121 |
* @editor tungnx |
| 122 |
*/ |
| 123 |
public static function is_debug(): bool { |
| 124 |
return LP_Settings::get_option( 'debug', 'no' ) === 'yes'; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Set error log |
| 129 |
* |
| 130 |
* @param string $message |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public static function error_log( string $message ) { |
| 135 |
if ( LP_Debug::is_debug() ) { |
| 136 |
error_log( $message ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return LP_Debug|null |
| 142 |
*/ |
| 143 |
public static function instance() { |
| 144 |
if ( ! self::$_instance ) { |
| 145 |
self::$_instance = new self(); |
| 146 |
} |
| 147 |
|
| 148 |
return self::$_instance; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return LP_Debug::instance(); |
| 153 |
|