| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Templately\Core\Importer\Utils\LogHandler; |
| 6 |
use Templately\Utils\Helper; |
| 7 |
|
| 8 |
trait LogHelper { |
| 9 |
private $log_types = [ |
| 10 |
'' |
| 11 |
]; |
| 12 |
|
| 13 |
public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 14 |
$data = [ |
| 15 |
'action' => $action, |
| 16 |
'type' => $type, |
| 17 |
'progress' => $progress, |
| 18 |
'message' => $message |
| 19 |
]; |
| 20 |
|
| 21 |
if ( $progress == 100 && $status == null ) { |
| 22 |
$data['status'] = 'complete'; |
| 23 |
} elseif ( $status != null ) { |
| 24 |
$data['status'] = $status; |
| 25 |
} |
| 26 |
|
| 27 |
$this->sse_message( $data ); |
| 28 |
} |
| 29 |
|
| 30 |
public function removeLog( $type ) { |
| 31 |
$this->sse_message( [ |
| 32 |
'action' => 'removeLog', |
| 33 |
'type' => $type, |
| 34 |
'progress' => 100 |
| 35 |
] ); |
| 36 |
} |
| 37 |
|
| 38 |
public function sse_message( $data ) { |
| 39 |
// Log the data into debug log file |
| 40 |
$this->sse_log_file( $data ); |
| 41 |
|
| 42 |
if(Helper::should_flush()){ |
| 43 |
echo "event: message\n"; |
| 44 |
echo 'data: ' . wp_json_encode( $data ) . "\n\n"; |
| 45 |
|
| 46 |
// Extra padding. |
| 47 |
echo esc_html( ':' . str_repeat( ' ', 2048 ) . "\n\n" ); |
| 48 |
|
| 49 |
flush(); |
| 50 |
if (ob_get_level() > 0) { |
| 51 |
ob_flush(); |
| 52 |
} |
| 53 |
} |
| 54 |
// else { |
| 55 |
// $log = get_option( 'templately_fsi_log' ) ?: []; |
| 56 |
// $log[] = $data; |
| 57 |
// update_option( 'templately_fsi_log', $log ); |
| 58 |
// } |
| 59 |
// else if($data['action'] === 'complete' || $data['action'] === 'downloadComplete' || $data['action'] === 'error'){ |
| 60 |
// wp_send_json( $data ); |
| 61 |
// } |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Printing Error Logs in debug.log file or in option. |
| 66 |
* |
| 67 |
* @param mixed $log |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function sse_log_file( $log ){ |
| 71 |
$request_params = FullSiteImport::_get_session_data(); |
| 72 |
|
| 73 |
if (is_array($log)) { |
| 74 |
$log['timestamp'] = date('Y-m-d H:i:s'); |
| 75 |
} |
| 76 |
|
| 77 |
if (isset($request_params['log_type']) && $request_params['log_type'] == 'file') { |
| 78 |
LogHandler::sse_log_file($log); |
| 79 |
} else { |
| 80 |
$_log = get_option('templately_fsi_log') ?: []; |
| 81 |
$_log[] = $log; |
| 82 |
update_option('templately_fsi_log', $_log, false); |
| 83 |
} |
| 84 |
} |
| 85 |
/** |
| 86 |
* Printing Error Logs in debug.log file. |
| 87 |
* |
| 88 |
* @param mixed $log |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function debug_log( $log ){ |
| 92 |
if ( defined('TEMPLATELY_EVENT_LOG') && TEMPLATELY_EVENT_LOG === true ) { |
| 93 |
|
| 94 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 95 |
error_log( print_r( $log, true ) ); |
| 96 |
} else if($log) { |
| 97 |
error_log( $log ); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
} |