PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.1
Elementor Website Builder – more than just a page builder v3.0.1
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 / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.0.1, at core/logger/manager.php

250 lines 5.9 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;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Core\Common\Modules\Ajax\Module;
6 use Elementor\Core\Logger\Loggers\Logger_Interface;
7 use Elementor\Core\Logger\Items\PHP;
8 use Elementor\Core\Logger\Items\JS;
9 use Elementor\Plugin;
10 use Elementor\Modules\System_Info\Module as System_Info;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 class Manager extends BaseModule {
17
18 protected $loggers = [];
19
20 protected $default_logger = '';
21
22 public function get_name() {
23 return 'log';
24 }
25
26 public function shutdown( $last_error = null ) {
27 if ( ! $last_error ) {
28 $last_error = error_get_last();
29 }
30
31 if ( ! $last_error ) {
32 return;
33 }
34
35 if ( empty( $last_error['file'] ) ) {
36 return;
37 }
38
39 $error_path = ( wp_normalize_path( $last_error['file'] ) );
40 // `untrailingslashit` in order to include other plugins prefixed with elementor.
41 $elementor_path = untrailingslashit( wp_normalize_path( ELEMENTOR_PATH ) );
42
43 if ( false === strpos( $error_path, $elementor_path ) ) {
44 return;
45 }
46
47 $last_error['type'] = $this->get_log_type_from_php_error( $last_error['type'] );
48 $last_error['trace'] = true;
49
50 $item = new PHP( $last_error );
51
52 $this->get_logger()->log( $item );
53 }
54
55 public function rest_error_handler( $error_number, $error_message, $error_file, $error_line ) {
56 $error = new \WP_Error( $error_number, $error_message, [
57 'type' => $error_number,
58 'message' => $error_message,
59 'file' => $error_file,
60 'line' => $error_line,
61 ] );
62
63 // Notify $e.data.
64 if ( ! headers_sent() ) {
65 header( 'Content-Type: application/json; charset=UTF-8' );
66
67 http_response_code( 500 );
68
69 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
70 echo wp_json_encode( $error->get_error_data() );
71 } else {
72 echo wp_json_encode( [
73 'message' => 'Server error, see Elementor => System Info',
74 ] );
75 }
76 }
77
78 $this->shutdown( $error->get_error_data() );
79
80 exit;
81 }
82
83 public function add_system_info_report() {
84 System_Info::add_report(
85 'log', [
86 'file_name' => __DIR__ . '/log-reporter.php',
87 'class_name' => __NAMESPACE__ . '\Log_Reporter',
88 ]
89 );
90 }
91
92 /**
93 * Javascript log.
94 *
95 * Log Elementor errors and save them in the database.
96 *
97 * Fired by `wp_ajax_elementor_js_log` action.
98 *
99 */
100 public function js_log() {
101 /** @var Module $ajax */
102 $ajax = Plugin::$instance->common->get_component( 'ajax' );
103
104 if ( ! $ajax->verify_request_nonce() || empty( $_POST['data'] ) ) {
105 wp_send_json_error();
106 }
107
108 array_walk_recursive( $_POST['data'], function( &$value ) {
109 $value = sanitize_text_field( $value );
110 } );
111
112 foreach ( $_POST['data'] as $error ) {
113 $error['type'] = Logger_Interface::LEVEL_ERROR;
114
115 if ( ! empty( $error['customFields'] ) ) {
116 $error['meta'] = $error['customFields'];
117 }
118
119 $item = new JS( $error );
120 $this->get_logger()->log( $item );
121 }
122
123 wp_send_json_success();
124 }
125
126 public function register_logger( $name, $class ) {
127 $this->loggers[ $name ] = $class;
128 }
129
130 public function set_default_logger( $name ) {
131 if ( ! empty( $this->loggers[ $name ] ) ) {
132 $this->default_logger = $name;
133 }
134 }
135
136 public function register_default_loggers() {
137 $this->register_logger( 'db', 'Elementor\Core\Logger\Loggers\Db' );
138 $this->set_default_logger( 'db' );
139 }
140
141 /**
142 * @param string $name
143 *
144 * @return Logger_Interface
145 */
146 public function get_logger( $name = '' ) {
147 $this->register_loggers();
148
149 if ( empty( $name ) || ! isset( $this->loggers[ $name ] ) ) {
150 $name = $this->default_logger;
151 }
152
153 if ( ! $this->get_component( $name ) ) {
154 $this->add_component( $name, new $this->loggers[ $name ]() );
155 }
156
157 return $this->get_component( $name );
158 }
159
160 /**
161 * @param string $message
162 * @param array $args
163 *
164 * @return void
165 */
166 public function log( $message, $args = [] ) {
167 $this->get_logger()->log( $message, $args );
168 }
169
170 /**
171 * @param string $message
172 * @param array $args
173 *
174 * @return void
175 */
176 public function info( $message, $args = [] ) {
177 $this->get_logger()->info( $message, $args );
178 }
179
180 /**
181 * @param string $message
182 * @param array $args
183 *
184 * @return void
185 */
186 public function notice( $message, $args = [] ) {
187 $this->get_logger()->notice( $message, $args );
188 }
189
190 /**
191 * @param string $message
192 * @param array $args
193 *
194 * @return void
195 */
196 public function warning( $message, $args = [] ) {
197 $this->get_logger()->warning( $message, $args );
198 }
199
200 /**
201 * @param string $message
202 * @param array $args
203 *
204 * @return void
205 */
206 public function error( $message, $args = [] ) {
207 $this->get_logger()->error( $message, $args );
208 }
209
210 private function get_log_type_from_php_error( $type ) {
211 $error_map = [
212 E_CORE_ERROR => Logger_Interface::LEVEL_ERROR,
213 E_ERROR => Logger_Interface::LEVEL_ERROR,
214 E_USER_ERROR => Logger_Interface::LEVEL_ERROR,
215 E_COMPILE_ERROR => Logger_Interface::LEVEL_ERROR,
216 E_RECOVERABLE_ERROR => Logger_Interface::LEVEL_ERROR,
217 E_PARSE => Logger_Interface::LEVEL_ERROR,
218 E_STRICT => Logger_Interface::LEVEL_ERROR,
219
220 E_WARNING => Logger_Interface::LEVEL_WARNING,
221 E_USER_WARNING => Logger_Interface::LEVEL_WARNING,
222 E_CORE_WARNING => Logger_Interface::LEVEL_WARNING,
223 E_COMPILE_WARNING => Logger_Interface::LEVEL_WARNING,
224
225 E_NOTICE => Logger_Interface::LEVEL_NOTICE,
226 E_USER_NOTICE => Logger_Interface::LEVEL_NOTICE,
227 E_DEPRECATED => Logger_Interface::LEVEL_NOTICE,
228 E_USER_DEPRECATED => Logger_Interface::LEVEL_NOTICE,
229 ];
230
231 return isset( $error_map[ $type ] ) ? $error_map[ $type ] : Logger_Interface::LEVEL_ERROR;
232 }
233
234 private function register_loggers() {
235 if ( ! did_action( 'elementor/loggers/register' ) ) {
236 do_action( 'elementor/loggers/register', $this );
237 }
238 }
239
240 public function __construct() {
241 register_shutdown_function( [ $this, 'shutdown' ] );
242
243 add_action( 'admin_init', [ $this, 'add_system_info_report' ], 80 );
244
245 add_action( 'wp_ajax_elementor_js_log', [ $this, 'js_log' ] );
246
247 add_action( 'elementor/loggers/register', [ $this, 'register_default_loggers' ] );
248 }
249 }
250