PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-logger.php

class-mainwp-logger.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-logger.php

868 lines 22.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Logger
4 *
5 * For custom read/write logging file.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_Logger
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_Logger {
18
19 // phpcs:disable WordPress.WP.AlternativeFunctions -- for custom read/write logging file.
20
21 const UPDATE_CHECK_LOG_PRIORITY = 10;
22 const EXECUTION_TIME_LOG_PRIORITY = 15;
23 const LOGS_AUTO_PURGE_LOG_PRIORITY = 16;
24 const COST_TRACKER_LOG_PRIORITY = 20230112;
25 const API_BACKUPS_LOG_PRIORITY = 20240130;
26
27 const DISABLED = - 1;
28 const LOG = 0;
29 const WARNING = 1;
30 const INFO = 2;
31 const DEBUG = 3;
32
33 const LOG_COLOR = '#999999';
34 const DEBUG_COLOR = '#666666';
35 const INFO_COLOR = '#276f86';
36 const WARNING_COLOR = '#9f3a38;';
37
38 /**
39 * Private variable to hold time start.
40 *
41 * @var int
42 */
43 private static $time_start = null;
44
45 /**
46 * Private varibale to hold the log file prefix.
47 *
48 * @var string Default 'mainwp'
49 */
50 private $logFileNamePrefix = 'mainwp';
51
52 /**
53 * Private varibale to hold the log file suffix.
54 *
55 * @var string Default '.log'
56 */
57 private $logFileNameSuffix = '.log';
58
59 /**
60 * Private varibale to hold the log file max size.
61 *
62 * @var int Default 0.5
63 */
64 private $logMaxMB = 0.5;
65
66 /**
67 * Private varibale to hold the log file date format.
68 *
69 * @var string Default 'Y-m-d H:i:s'
70 */
71 private $logDateFormat = 'Y-m-d H:i:s';
72
73 /**
74 * Private varibale to hold the log file output directory.
75 *
76 * @var mixed Default null
77 */
78 private $logDirectory = null;
79
80 /**
81 * Private varibale to hold the log file priotrity.
82 *
83 * @var string Disabled
84 */
85 private $logPriority = self::DISABLED;
86
87 /**
88 * Private varibale to hold the log Specific priotrity.
89 *
90 * @var string Disabled
91 */
92 private $logSpecific = 0;
93
94
95 /**
96 * Private static varibale to hold the instance.
97 *
98 * @var mixed Default null
99 */
100 private static $instance = null;
101
102 /**
103 * Method instance()
104 *
105 * Returns new MainWP_Logger instance.
106 *
107 * @return self MainWP_Logger
108 *
109 * @uses \MainWP\Dashboard\MainWP_Logger
110 */
111 public static function instance() {
112 if ( null === self::$instance ) {
113 self::$instance = new self();
114 }
115 return self::$instance;
116 }
117
118 /**
119 * MainWP_Logger constructor.
120 *
121 * Run each time the class is called.
122 *
123 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_dir()
124 */
125 private function __construct() {
126
127 $enabled = $this->get_log_status();
128 $specific = $this->get_log_specific();
129
130 $enabled = apply_filters( 'mainwp_log_status', $enabled );
131 $specific = apply_filters( 'mainwp_log_specific', $specific );
132
133 $this->set_log_priority( $enabled, $specific );
134 }
135
136 /**
137 * Method set_log_priority()
138 *
139 * Sets the log priority.
140 *
141 * @param mixed $logPriority Log priority value.
142 * @param mixed $spec_log Specific log.
143 */
144 public function set_log_priority( $logPriority, $spec_log = 0 ) {
145 $this->logPriority = (int) $logPriority;
146 $this->logSpecific = (int) $spec_log; // 1 - specific log, 0 - not specific log.
147 }
148
149 /**
150 * Method get_log_status()
151 *
152 * Get log status.
153 *
154 * @return mixed $enabled log status.
155 */
156 public function get_log_status() {
157 $enabled = get_option( 'mainwp_actionlogs' );
158
159 if ( false === $enabled ) {
160 $sites_count = MainWP_DB::instance()->get_websites_count();
161 if ( empty( $sites_count ) ) {
162 $enabled = self::DEBUG;
163 set_transient( 'mainwp_transient_action_logs', true, 2 * WEEK_IN_SECONDS );
164 } elseif ( get_transient( 'mainwp_transient_action_logs' ) ) {
165 $enabled = self::DEBUG;
166 } else {
167 $enabled = self::DISABLED;
168 }
169 }
170 return $enabled;
171 }
172
173
174 /**
175 * Method get_log_specific()
176 *
177 * Get log specific status.
178 *
179 * @return mixed $enabled log status.
180 */
181 public function get_log_specific() {
182 return get_option( 'mainwp_specific_logs', 0 );
183 }
184
185 /**
186 * Method get_log_type_info()
187 *
188 * Get log type info.
189 *
190 * @param int $type Log type value.
191 * @param int $logcolor Log color value.
192 *
193 * @return int $currentColor log color code.
194 */
195 public function get_log_type_info( $type, $logcolor ) {
196 $currentColor = '';
197 $prefix = '';
198 if ( self::DEBUG === $type || self::DEBUG === $logcolor ) {
199 $currentColor = self::DEBUG_COLOR;
200 $prefix = '[DEBUG]';
201 } elseif ( self::INFO === $type || self::INFO === $logcolor ) {
202 $currentColor = self::INFO_COLOR;
203 $prefix = '[INFO]';
204 } elseif ( self::WARNING === $type || self::WARNING === $logcolor ) {
205 $currentColor = self::WARNING_COLOR;
206 $prefix = '[WARNING]';
207 } elseif ( self::LOG === $type || self::LOG === $logcolor ) {
208 $currentColor = self::LOG_COLOR;
209 $prefix = '[LOG]';
210 }
211 return array(
212 'log_color' => $currentColor,
213 'log_prefix' => $prefix,
214 );
215 }
216
217 /**
218 * Method debug()
219 *
220 * Grab debug.
221 *
222 * @param string $text Debug message text.
223 *
224 * @return string Log debug message.
225 */
226 public function debug( $text ) {
227 return $this->log( $text, self::DEBUG );
228 }
229
230 /**
231 * Method info()
232 *
233 * Grab info.
234 *
235 * @param string $text Info message text.
236 *
237 * @return string Log info message.
238 */
239 public function info( $text ) {
240 return $this->log( $text, self::INFO );
241 }
242
243 /**
244 * Method warning()
245 *
246 * Grab warning information.
247 *
248 * @param string $text Warning message text.
249 *
250 * @return string Log warning message.
251 */
252 public function warning( $text ) {
253 return $this->log( $text, self::WARNING );
254 }
255
256 /**
257 * Method actions()
258 *
259 * Grab actions information.
260 *
261 * @param string $text Warning message text.
262 * @param int $priority priority message.
263 * @param int $log_color Set color: 0 - LOG, 1 - WARNING, 2 - INFO, 3- DEBUG.
264 * @param bool $forced forced logging.
265 *
266 * @return string Log warning message.
267 */
268 public function log_action( $text, $priority, $log_color = 0, $forced = false ) {
269 return $this->log( $text, $priority, $log_color, $forced );
270 }
271
272
273 /**
274 * Method log_update_check().
275 *
276 * @param string $text Log update check.
277 */
278 public function log_update_check( $text = '' ) {
279 $this->log_action( $text, self::UPDATE_CHECK_LOG_PRIORITY );
280 }
281
282 /**
283 * Method debug_for_website()
284 *
285 * Grab website debug and info.
286 *
287 * @param object $website Child site object.
288 * @param string $action Performed action.
289 * @param string $message Debug message.
290 *
291 * @return mixed Website debug info.
292 *
293 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
294 */
295 public function debug_for_website( $website, $action, $message ) {
296 if ( empty( $website ) ) {
297 return $this->log( '[-] [-] ::' . $action . ':: ' . $message, self::DEBUG );
298 }
299
300 return $this->log( '[' . $website->name . '] [' . MainWP_Utility::get_nice_url( $website->url ) . '] ::' . $action . ':: ' . $message, self::DEBUG, 0, false, $website );
301 }
302
303 /**
304 * Method info_for_website()
305 *
306 * Grab Website Info.
307 *
308 * @param object $website Child site object.
309 * @param string $action Performed action.
310 * @param string $message Info message.
311 *
312 * @return mixed Website Info.
313 *
314 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
315 */
316 public function info_for_website( $website, $action, $message ) {
317 if ( empty( $website ) ) {
318 return $this->log( '[-] [-] ::' . $action . ':: ' . $message, self::INFO );
319 }
320
321 return $this->log( '[' . $website->name . '] [' . MainWP_Utility::get_nice_url( $website->url ) . '] ::' . $action . ':: ' . $message, self::INFO );
322 }
323
324 /**
325 * Method warning_for_website()
326 *
327 * Grab Website Warnings.
328 *
329 * @param object $website Child site object.
330 * @param string $action Performed action.
331 * @param string $message Warning message.
332 * @param bool $addStackTrace Add or Don't add stack trace.
333 *
334 * @return string Website warnings.
335 */
336 public function warning_for_website( $website, $action, $message, $addStackTrace = true ) {
337 $stackTrace = '';
338 if ( $addStackTrace ) {
339 ob_start();
340 // phpcs:ignore -- for debugging.
341 debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
342 $stackTrace = "\n" . ob_get_clean();
343 }
344 if ( empty( $website ) ) {
345 return $this->log( '[-] [-] :: ' . $action . ' :: ' . $message . $stackTrace, self::WARNING );
346 }
347
348 return $this->log( '[' . $website->name . '] [' . MainWP_Utility::get_nice_url( $website->url ) . '] ::' . $action . ':: ' . $message . $stackTrace, self::WARNING );
349 }
350
351 /**
352 * Method log_to_db()
353 *
354 * Log to database.
355 *
356 * @param string $text Log record text.
357 * @param int $priority Set priority.
358 * @param int $log_color Set color.
359 * @param bool $forced forced logging.
360 * @param mixed $website website object.
361 *
362 * @return bool true|false Default is False.
363 */
364 private function log_to_db( $text, $priority, $log_color = 0, $forced = false, $website = false ) {
365
366 if ( self::DISABLED === $this->logPriority ) {
367 return;
368 }
369
370 $priority = (int) apply_filters( 'mainwp_log_to_db_priority', $priority, $website );
371
372 $do_log = false;
373
374 if ( 1 === $this->logSpecific ) { // 1 - specific log, 0 - not specific log.
375 if ( $this->logPriority === $priority ) { // specific priority number saved setting.
376 $do_log = true;
377 }
378 } elseif ( $this->logPriority >= $priority ) {
379 $do_log = true;
380 }
381
382 $do_log = apply_filters( 'mainwp_log_do_to_db', $do_log, $website );
383
384 $text = $this->prepare_log_info( $text );
385
386 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
387 if ( 'CRON' !== strtoupper( substr( $text, 0, 4 ) ) ) {
388 $text = 'CRON :: ' . $text;
389 }
390 }
391
392 if ( $forced || $do_log ) {
393
394 $time = gmdate( $this->logDateFormat );
395
396 /**
397 * Current user global.
398 *
399 * @global string
400 */
401 global $current_user;
402
403 $user = '';
404 if ( ! empty( $current_user ) && ! empty( $current_user->user_login ) ) {
405 $user = $current_user->user_login;
406 } elseif ( defined( 'WP_CLI' ) ) {
407 $user = 'WP_CLI';
408 } elseif ( defined( 'DOING_CRON' ) ) {
409 $user = 'DOING_CRON';
410 }
411
412 $data = array();
413
414 $data['log_content'] = $text;
415 $data['log_user'] = $user;
416 $data['log_type'] = $priority;
417 $data['log_color'] = intval( $log_color );
418 $data['log_timestamp'] = time();
419
420 $data = apply_filters( 'mainwp_log_to_db_data', $data );
421
422 MainWP_DB_Common::instance()->insert_action_log( $data );
423
424 return true;
425 }
426 return false;
427 }
428
429 /**
430 * Method log()
431 *
432 * Create Log File.
433 *
434 * @param string $text Log record text.
435 * @param int $priority Set priority.
436 * @param int $log_color Set color.
437 * @param bool $forced forced logging.
438 * @param mixed $website Site object.
439 *
440 * @return bool true|false Default is False.
441 */
442 private function log( $text, $priority, $log_color = 0, $forced = false, $website = false ) { // phpcs:ignore -- complex function.
443
444 if ( self::DISABLED === $this->logPriority ) {
445 return;
446 }
447
448 $log_to_db = apply_filters( 'mainwp_logger_to_db', true, $website );
449
450 if ( $log_to_db ) {
451 return $this->log_to_db( $text, $priority, $log_color, $forced, $website );
452 }
453
454 $text = $this->prepare_log_info( $text );
455
456 $do_log = false;
457 if ( 1 === $this->logSpecific ) { // 1 - specific log, 0 - not specific log.
458 if ( $this->logPriority === $priority ) { // specific priority number saved setting.
459 $do_log = true;
460 }
461 } elseif ( $this->logPriority >= $priority ) {
462 $do_log = true;
463 }
464
465 if ( $do_log ) {
466 $this->logCurrentFile = $this->get_log_file();
467 $logCurrentHandle = fopen( $this->logCurrentFile, 'a+' );
468
469 if ( $logCurrentHandle ) {
470 $time = gmdate( $this->logDateFormat );
471 $prefix = '[' . $this->get_log_text( $priority ) . ']';
472
473 /**
474 * Current user global.
475 *
476 * @global string
477 */
478 global $current_user;
479
480 if ( ! empty( $current_user ) && ! empty( $current_user->user_login ) ) {
481 $prefix .= ' [administrator]';
482 }
483
484 fwrite( $logCurrentHandle, $time . ' ' . $prefix . ' ' . $text . "\n" );
485 fclose( $logCurrentHandle );
486 }
487
488 if ( filesize( $this->logCurrentFile ) > ( $this->logMaxMB * 1048576 ) ) {
489 $logCurrentHandle = fopen( $this->logCurrentFile, 'a+' );
490 if ( $logCurrentHandle ) {
491 fseek( $logCurrentHandle, 0 );
492 $newLogFile = $this->logCurrentFile . '.tmp';
493 $newLogHandle = false;
494 $chunkSize = filesize( $this->logCurrentFile ) - ( $this->logMaxMB * 1048576 );
495 while ( is_resource( $logCurrentHandle ) && ! feof( $logCurrentHandle ) && ( $chunkSize > 0 ) ) {
496 $content = fread( $logCurrentHandle, $chunkSize );
497 if ( false === $content ) {
498 break;
499 }
500 $pos = strrpos( $content, "\n" );
501 if ( $newLogHandle ) {
502 fwrite( $newLogHandle, $content );
503 } elseif ( $pos ) {
504 if ( ! $newLogHandle ) {
505 $newLogHandle = fopen( $newLogFile, 'w+' );
506 }
507 fwrite( $newLogHandle, substr( $content, $pos + 1 ) );
508 }
509 }
510
511 if ( is_resource( $logCurrentHandle ) ) {
512 fclose( $logCurrentHandle );
513 }
514
515 if ( $newLogHandle ) {
516 fclose( $newLogHandle );
517 wp_delete_file( $this->logCurrentFile );
518 if ( file_exists( $newLogFile ) ) {
519 rename( $newLogFile, $this->logCurrentFile );
520 }
521 }
522 }
523 }
524
525 return true;
526 }
527
528 return false;
529 }
530
531
532 /**
533 * Method prepare_log_info()
534 *
535 * Prepare log data.
536 *
537 * @param mixed $data Log data.
538 *
539 * @return mixed $data filtered data.
540 */
541 public function prepare_log_info( $data ) {
542 $patterns[0] = '/user=([^\&]+)\&/';
543 $replacement[0] = 'user=xxxxxx&';
544 $patterns[1] = '/alt_user=([^\&]+)\&/';
545 $replacement[1] = 'alt_user=xxxxxx&';
546 $patterns[2] = '/\&server=([^\&]+)\&/';
547 $replacement[2] = '&server=xxxxxx&';
548 $data = preg_replace( $patterns, $replacement, $data );
549 return $data;
550 }
551
552 /**
553 * Method prepend()
554 *
555 * Prepend content to log file.
556 *
557 * @param mixed $str Custom string.
558 * @param mixed $filename Filename.
559 */
560 public function prepend( $str, $filename ) {
561 $context = stream_context_create();
562 $fp = fopen( $filename, 'r', 1, $context );
563 $tmpname = md5( $str );
564 file_put_contents( $tmpname, $str );
565 file_put_contents( $tmpname, $fp, FILE_APPEND );
566 fclose( $fp );
567 wp_delete_file( $filename );
568 rename( $tmpname, $filename );
569 }
570
571 /**
572 * Method init_execution_time().
573 *
574 * @param string $time_index index for timer.
575 *
576 * Init execution time start value.
577 */
578 public function init_execution_time( $time_index = '' ) {
579 if ( null === self::$time_start || ! is_array( self::$time_start ) ) {
580 self::$time_start = array( 'start' => microtime( true ) );
581 $this->log_action( 'execution time :: init :: [start]', self::EXECUTION_TIME_LOG_PRIORITY );
582 }
583
584 if ( ! empty( $time_index ) && is_string( $time_index ) && 'start' !== $time_index ) {
585 self::$time_start[ $time_index ] = microtime( true );
586 $this->log_action( 'execution time :: init :: [' . $time_index . ']', self::EXECUTION_TIME_LOG_PRIORITY );
587 }
588 }
589
590 /**
591 * Method log_execution_time().
592 *
593 * @param string $text Log record text.
594 *
595 * Log the execution time value.
596 */
597 public function log_execution_time( $text = '' ) {
598 $exec_time = $this->get_execution_time();
599 $this->log_action( 'execution time :: ' . ( ! empty( $text ) ? (string) $text : '<empty>' ) . ' :: [time=' . round( $exec_time, 4 ) . '](seconds)', self::EXECUTION_TIME_LOG_PRIORITY );
600 }
601
602
603 /**
604 * Method get_execution_time().
605 *
606 * Get the execution time value.
607 *
608 * @param string $time_index Index for timer.
609 *
610 * @return int execution time.
611 */
612 private function get_execution_time( $time_index = '' ) {
613
614 if ( empty( self::$time_start ) ) {
615 return 0;
616 }
617
618 $start = 0;
619 if ( ! empty( $time_index ) ) {
620 $start = is_array( self::$time_start ) && isset( self::$time_start[ $time_index ] ) ? self::$time_start[ $time_index ] : 0;
621 }
622
623 if ( empty( $start ) ) {
624 $start = is_array( self::$time_start ) && isset( self::$time_start['start'] ) ? self::$time_start['start'] : 0;
625 }
626
627 if ( empty( $start ) ) {
628 return 0;
629 }
630 return microtime( true ) - $start; // seconds.
631 }
632
633 /**
634 * Method get_log_file()
635 *
636 * Grab Log File.
637 *
638 * @return mixed Log File.
639 */
640 public function get_log_file() {
641 if ( empty( $this->logDirectory ) ) {
642 $this->logDirectory = MainWP_System_Utility::get_mainwp_dir();
643 $this->logDirectory = $this->logDirectory[0];
644 }
645 return $this->logDirectory . $this->logFileNamePrefix . $this->logFileNameSuffix;
646 }
647
648 /**
649 * Method get_log_text()
650 *
651 * Grab what type of log entry.
652 *
653 * @param mixed $priority Set priority.
654 *
655 * @return string LOG -OR- DISABLED|DEBUG|INFO|WARNING|INFO UPDATE
656 */
657 public function get_log_text( $priority ) {
658 switch ( $priority ) {
659 case self::DISABLED:
660 return 'DISABLED';
661 case self::DEBUG:
662 return 'DEBUG';
663 case self::INFO:
664 return 'INFO';
665 case self::WARNING:
666 return 'WARNING';
667 default:
668 return 'SPEC LOG';
669 }
670 }
671
672 /**
673 * Method check_log_daily()
674 *
675 * Daily checks to clear the log file.
676 */
677 public function check_log_daily() {
678 $status = (int) $this->get_log_status();
679 if ( 0 >= $status ) {
680 return;
681 }
682
683 $today_m_y = date_i18n( 'd/m/Y' ); //phpcs:ignore -- local time.
684 // one time per day.
685 if ( get_option( 'mainwp_logger_check_daily' ) !== $today_m_y ) {
686 $num_days = apply_filters( 'mainwp_logger_keep_days', 7 );
687 MainWP_DB_Common::instance()->delete_action_log( $num_days );
688 MainWP_Utility::update_option( 'mainwp_logger_check_daily', $today_m_y );
689
690 $enabled_time = get_option( 'mainwp_actionlogs_enabled_timestamp', false );
691 if ( false === $enabled_time ) {
692 MainWP_Utility::update_option( 'mainwp_actionlogs_enabled_timestamp', time() );
693 } elseif ( $enabled_time + $num_days * DAY_IN_SECONDS < time() ) {
694 MainWP_Utility::update_option( 'mainwp_actionlogs', self::DISABLED );
695 }
696 }
697 }
698
699 /**
700 * Method clear_log_db()
701 *
702 * Clear the log file.
703 */
704 public function clear_log_db() {
705 MainWP_DB_Common::instance()->delete_action_log();
706 }
707
708 /**
709 * Method clear_log()
710 *
711 * Clear the log file.
712 */
713 public function clear_log() {
714 $logFile = $this->get_log_file();
715 wp_delete_file( $logFile );
716 $fh = fopen( $logFile, 'w' );
717 if ( false === $fh ) {
718 return;
719 }
720 fclose( $fh );
721 }
722
723 /**
724 * Method show_log_db()
725 *
726 * Grab log file and build output to screen.
727 */
728 public function show_log_db() {
729
730 echo '<div class="ui hidden divider"></div>';
731 echo '<div class="ui divided padded relaxed list" local-datetime="' . date( 'Y-m-d H:i:s' ) . '">'; // phpcs:ignore -- local time.
732
733 $rows = MainWP_DB::instance()->query( MainWP_DB_Common::instance()->get_sql_log() );
734
735 $start_wrapper = '<span class="ui mini label mainwp-action-log-show-more">Click to See Response</span><div class="mainwp-action-log-site-response" style="display: none;">';
736 $end_wrapper = '</div>';
737
738 while ( $rows && ( $row = MainWP_DB::fetch_object( $rows ) ) ) {
739 $type = $row->log_type;
740 $line = $row->log_content;
741 if ( 120 * 1024 < strlen( $line ) ) {
742 $line = '[Data row too long]';
743 }
744
745 $time = gmdate( $this->logDateFormat, $row->log_timestamp );
746
747 $showInfo = $this->get_log_type_info( $row->log_type, $row->log_color );
748 $currentColor = $showInfo['log_color'];
749
750 $prefix = $time . ' ' . $showInfo['log_prefix'];
751
752 $line = htmlentities( $line );
753
754 if ( false !== strpos( $line, '[data-start]' ) ) {
755 $line = str_replace( '[data-start]', $start_wrapper, $line );
756 $line = str_replace( '[data-end]', $end_wrapper, $line );
757 }
758
759 echo '<div class="item" style="color:' . esc_html( $currentColor ) . '"><div class="mainwpactionlogsline">';
760
761 echo $prefix . ' ' . $line; // phpcs:ignore WordPress.Security.EscapeOutput
762
763 echo '</div></div>';
764 }
765
766 echo '</div></div>';
767
768 echo '</div>';
769
770 ?>
771 <div class="ui large modal" id="mainwp-action-log-response-modal">
772 <i class="close icon mainwp-reload"></i>
773 <div class="header"><?php esc_html_e( 'Child Site Response', 'mainwp' ); ?></div>
774 <div class="content">
775 <div class="ui info message"><?php esc_html_e( 'To see the response in a more readable way, you can copy it and paste it into some HTML render tool, such as Codepen.io.', 'mainwp' ); ?>
776 </div>
777 </div>
778 <div class="scrolling content content-response"></div>
779 <div class="actions">
780 <button class="ui green button mainwp-response-copy-button"><?php esc_html_e( 'Copy Response', 'mainwp' ); ?></button>
781 </div>
782 </div>
783 <?php
784 }
785
786 /**
787 * Method show_log_file()
788 *
789 * Grab log file and build output to screen.
790 */
791 public function show_log_file() {
792 $logFile = $this->get_log_file();
793
794 if ( ! file_exists( $logFile ) ) {
795 return;
796 }
797
798 $fh = fopen( $logFile, 'r' );
799 if ( false === $fh ) {
800 return;
801 }
802
803 $previousColor = '';
804 $fontOpen = false;
805 $firstLinePassedProcessed = false;
806 echo '<div class="ui hidden divider"></div>';
807 echo '<div class="ui divided padded relaxed list">';
808 while ( false !== ( $line = fgets( $fh ) ) ) {
809 $currentColor = $previousColor;
810 if ( stristr( $line, '[DEBUG]' ) ) {
811 $currentColor = self::DEBUG_COLOR;
812 $firstLinePassed = true;
813 } elseif ( stristr( $line, '[INFO]' ) ) {
814 $currentColor = self::INFO_COLOR;
815 $firstLinePassed = true;
816 } elseif ( stristr( $line, '[WARNING]' ) ) {
817 $currentColor = self::WARNING_COLOR;
818 $firstLinePassed = true;
819 } elseif ( stristr( $line, '[LOG]' ) ) {
820 $currentColor = self::LOG_COLOR;
821 $firstLinePassed = true;
822 } else {
823 $firstLinePassed = false;
824 }
825
826 if ( $firstLinePassedProcessed && ! $firstLinePassed ) {
827 echo ' <span class="ui mini label mainwp-action-log-show-more">Click to See Response</span></div><div class="mainwp-action-log-site-response" style="display: none;">';
828 }
829
830 $firstLinePassedProcessed = $firstLinePassed;
831
832 if ( $currentColor !== $previousColor ) {
833 if ( $fontOpen ) {
834 echo '</div></div>';
835 }
836
837 echo '<div class="item" style="color:' . esc_html( $currentColor ) . '"><div class="mainwpactionlogsline">';
838 $fontOpen = true;
839 }
840
841 echo esc_html( htmlentities( $line ) );
842 }
843
844 if ( $fontOpen ) {
845 echo '</div></div>';
846 }
847
848 echo '</div>';
849
850 ?>
851 <div class="ui large modal" id="mainwp-action-log-response-modal">
852 <i class="close icon mainwp-reload"></i>
853 <div class="header"><?php esc_html_e( 'Child Site Response', 'mainwp' ); ?></div>
854 <div class="content">
855 <div class="ui info message"><?php esc_html_e( 'To see the response in a more readable way, you can copy it and paste it into some HTML render tool, such as Codepen.io.', 'mainwp' ); ?>
856 </div>
857 </div>
858 <div class="scrolling content content-response"></div>
859 <div class="actions">
860 <button class="ui green button mainwp-response-copy-button"><?php esc_html_e( 'Copy Response', 'mainwp' ); ?></button>
861 </div>
862 </div>
863 <?php
864
865 fclose( $fh );
866 }
867 }
868