| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
|
| 7 |
trait LogHelper { |
| 8 |
private $log_types = [ |
| 9 |
'' |
| 10 |
]; |
| 11 |
|
| 12 |
public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 13 |
$data = [ |
| 14 |
'action' => $action, |
| 15 |
'type' => $type, |
| 16 |
'progress' => $progress, |
| 17 |
'message' => $message |
| 18 |
]; |
| 19 |
|
| 20 |
if ( $progress == 100 && $status == null ) { |
| 21 |
$data['status'] = 'complete'; |
| 22 |
} elseif ( $status != null ) { |
| 23 |
$data['status'] = $status; |
| 24 |
} |
| 25 |
|
| 26 |
$this->sse_message( $data ); |
| 27 |
} |
| 28 |
|
| 29 |
public function removeLog( $type ) { |
| 30 |
$this->sse_message( [ |
| 31 |
'action' => 'removeLog', |
| 32 |
'type' => $type, |
| 33 |
'progress' => 100 |
| 34 |
] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function sse_message( $data ) { |
| 38 |
// Log the data into debug log file |
| 39 |
$this->debug_log( $data ); |
| 40 |
|
| 41 |
echo "event: message\n"; |
| 42 |
echo 'data: ' . wp_json_encode( $data ) . "\n\n"; |
| 43 |
|
| 44 |
// Extra padding. |
| 45 |
echo esc_html( ':' . str_repeat( ' ', 2048 ) . "\n\n" ); |
| 46 |
|
| 47 |
flush(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Printing Error Logs in debug.log file. |
| 52 |
* |
| 53 |
* @param mixed $log |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function debug_log( $log ){ |
| 57 |
if ( defined('TEMPLATELY_EVENT_LOG') && TEMPLATELY_EVENT_LOG === true ) { |
| 58 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 59 |
error_log( print_r( $log, true ) ); |
| 60 |
} else { |
| 61 |
error_log( $log ); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |