| 1 |
<?php |
| 2 |
namespace Elementor\Core\Logger\Loggers; |
| 3 |
|
| 4 |
use Elementor\Core\Logger\Items\Log_Item_Interface as Log_Item; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
class Db extends Base { |
| 11 |
|
| 12 |
public function save_log( Log_Item $item ) { |
| 13 |
$log = $this->maybe_truncate_log(); |
| 14 |
|
| 15 |
$id = $item->get_fingerprint(); |
| 16 |
|
| 17 |
if ( empty( $log[ $id ] ) ) { |
| 18 |
$log[ $id ] = $item; |
| 19 |
} |
| 20 |
|
| 21 |
$log[ $id ]->increase_times( $item ); |
| 22 |
|
| 23 |
update_option( self::LOG_NAME, $log, 'no' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function clear() { |
| 27 |
delete_option( self::LOG_NAME ); |
| 28 |
} |
| 29 |
|
| 30 |
private function maybe_truncate_log() { |
| 31 |
/** @var Log_Item[] $log */ |
| 32 |
$log = $this->get_log(); |
| 33 |
|
| 34 |
if ( Log_Item::MAX_LOG_ENTRIES < count( $log ) ) { |
| 35 |
$log = array_slice( $log, -Log_Item::MAX_LOG_ENTRIES ); |
| 36 |
} |
| 37 |
|
| 38 |
return $log; |
| 39 |
} |
| 40 |
|
| 41 |
public function get_log() { |
| 42 |
// Clear cache. |
| 43 |
wp_cache_delete( self::LOG_NAME, 'options' ); |
| 44 |
|
| 45 |
$log = get_option( self::LOG_NAME, [] ); |
| 46 |
|
| 47 |
// In case the DB log is corrupted. |
| 48 |
if ( ! is_array( $log ) ) { |
| 49 |
$log = []; |
| 50 |
} |
| 51 |
|
| 52 |
return $log; |
| 53 |
} |
| 54 |
} |
| 55 |
|