PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.5
Elementor Website Builder – more than just a page builder v4.1.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / logger / items / base.php

base.php in Elementor Website Builder – more than just a page builder 4.1.5, at core/logger/items/base.php

179 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Logger\Items;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 class Base implements Log_Item_Interface {
9
10 const FORMAT = 'date [type] message [meta]';
11 const TRACE_FORMAT = '#key: file(line): class type function()';
12 const TRACE_LIMIT = 5;
13
14 protected $date;
15 protected $type;
16 protected $message;
17 protected $meta = [];
18
19 protected $times = 0;
20 protected $times_dates = [];
21 protected $args = [];
22
23 public function __construct( $args ) {
24 $this->date = current_time( 'mysql' );
25 $this->message = ! empty( $args['message'] ) ? esc_html( $args['message'] ) : '';
26 $this->type = ! empty( $args['type'] ) ? $args['type'] : 'info';
27 $this->meta = ! empty( $args['meta'] ) ? $args['meta'] : [];
28 $this->args = $args;
29
30 $this->set_trace();
31 }
32
33 public function __get( $name ) {
34 if ( property_exists( $this, $name ) ) {
35 return $this->{$name};
36 }
37
38 return '';
39 }
40
41 public function __toString() {
42 $vars = get_object_vars( $this );
43 return strtr( static::FORMAT, $vars );
44 }
45
46 #[\ReturnTypeWillChange]
47 public function jsonSerialize() {
48 return [
49 'class' => get_class( $this ),
50 'item' => [
51 'date' => $this->date,
52 'message' => $this->message,
53 'type' => $this->type,
54 'meta' => $this->meta,
55 'times' => $this->times,
56 'times_dates' => $this->times_dates,
57 'args' => $this->args,
58 ],
59 ];
60 }
61
62 public function deserialize( $properties ) {
63 $this->date = ! empty( $properties['date'] ) && is_string( $properties['date'] ) ? $properties['date'] : '';
64 $this->message = ! empty( $properties['message'] ) && is_string( $properties['message'] ) ? $properties['message'] : '';
65 $this->type = ! empty( $properties['type'] ) && is_string( $properties['type'] ) ? $properties['type'] : '';
66 $this->meta = ! empty( $properties['meta'] ) && is_array( $properties['meta'] ) ? $properties['meta'] : [];
67 $this->times = ! empty( $properties['times'] ) && is_string( $properties['times'] ) ? $properties['times'] : '';
68 $this->times_dates = ! empty( $properties['times_dates'] ) && is_array( $properties['times_dates'] ) ? $properties['times_dates'] : [];
69 $this->args = ! empty( $properties['args'] ) && is_array( $properties['args'] ) ? $properties['args'] : [];
70 }
71
72 /**
73 * @return Log_Item_Interface | null
74 */
75 public static function from_json( $str ) {
76 $obj = json_decode( $str, true );
77 if ( ! array_key_exists( 'class', $obj ) ) {
78 return null;
79 }
80 $class = $obj['class'];
81 if ( class_exists( $class ) ) {
82 /** @var Base $item */
83 $item = new $class( $obj['item']['message'] );
84 $item->deserialize( $obj['item'] );
85 return $item;
86 }
87
88 return null;
89 }
90
91 public function to_formatted_string( $output_format = 'html' ) {
92 $vars = get_object_vars( $this );
93 $format = static::FORMAT;
94 if ( 'html' === $output_format ) {
95 $format = str_replace( 'message', '<strong>message</strong>', static::FORMAT );
96 }
97 if ( empty( $vars['meta'] ) ) {
98 $format = str_replace( '[meta]', '', $format );
99 } else {
100 $vars['meta'] = stripslashes( var_export( $vars['meta'], true ) ); // @codingStandardsIgnoreLine
101 }
102 return strtr( $format, $vars );
103 }
104
105 public function get_fingerprint() {
106 $unique_key = $this->type . $this->message . var_export( $this->meta, true ); // @codingStandardsIgnoreLine
107 // Info messages are not be aggregated:
108 if ( 'info' === $this->type ) {
109 $unique_key .= $this->date;
110 }
111 return md5( $unique_key );
112 }
113
114 public function increase_times( $item, $truncate = true ) {
115 ++$this->times;
116 $this->times_dates[] = $item->date;
117
118 if ( $truncate && ( self::MAX_LOG_ENTRIES < count( $this->times_dates ) ) ) {
119 $this->times_dates = array_slice( $this->times_dates, -self::MAX_LOG_ENTRIES );
120 }
121 }
122
123 public function format( $format = 'html' ) {
124 $trace = $this->format_trace();
125 if ( empty( $trace ) ) {
126 return $this->to_formatted_string( $format );
127 }
128 $copy = clone $this;
129 $copy->meta['trace'] = $trace;
130 return $copy->to_formatted_string( $format );
131 }
132
133 public function get_name() {
134 return 'Log';
135 }
136
137 private function format_trace() {
138 $trace = empty( $this->meta['trace'] ) ? '' : $this->meta['trace'];
139
140 if ( is_string( $trace ) ) {
141 return $trace;
142 }
143
144 $trace_str = '';
145 foreach ( $trace as $key => $trace_line ) {
146 $format = static::TRACE_FORMAT;
147 $trace_line['key'] = $key;
148 if ( empty( $trace_line['file'] ) ) {
149 $format = str_replace( 'file(line): ', '', $format );
150 }
151
152 $trace_str .= PHP_EOL . strtr( $format, $trace_line );
153 $trace_str .= empty( $trace_line['args'] ) ? '' : var_export( $trace_line['args'], true ); // @codingStandardsIgnoreLine
154 }
155
156 return $trace_str . PHP_EOL;
157 }
158
159 private function set_trace() {
160 if ( ! empty( $this->args['trace'] ) && true === $this->args['trace'] ) {
161 $limit = empty( $this->args['trace_limit'] ) ? static::TRACE_LIMIT : $this->args['trace_limit'];
162
163 $stack = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // @codingStandardsIgnoreLine
164
165 while ( ! empty( $stack ) && ! empty( $stack[0]['file'] ) && ( false !== strpos( $stack[0]['file'], 'core' . DIRECTORY_SEPARATOR . 'logger' ) ) ) {
166 array_shift( $stack );
167 }
168
169 $this->meta['trace'] = array_slice( $stack, 0, $limit );
170
171 return;
172 }
173
174 if ( is_array( $this->args ) ) {
175 unset( $this->args['trace'] );
176 }
177 }
178 }
179