| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Templately\Core\Importer\Utils\LogHandler; |
| 6 |
use Templately\Core\Importer\Utils\Utils; |
| 7 |
use Templately\Utils\Helper; |
| 8 |
|
| 9 |
trait LogHelper { |
| 10 |
private static $ai_last_progress = 0; |
| 11 |
private $log_types = [ |
| 12 |
'' |
| 13 |
]; |
| 14 |
|
| 15 |
public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 16 |
$data = [ |
| 17 |
'action' => $action, |
| 18 |
'type' => $type, |
| 19 |
'progress' => $progress, |
| 20 |
'message' => $message |
| 21 |
]; |
| 22 |
|
| 23 |
if ( $progress == 100 && $status == null ) { |
| 24 |
$data['status'] = 'complete'; |
| 25 |
} elseif ( $status != null ) { |
| 26 |
$data['status'] = $status; |
| 27 |
} |
| 28 |
|
| 29 |
$this->sse_message( $data ); |
| 30 |
} |
| 31 |
|
| 32 |
public function removeLog( $type ) { |
| 33 |
$this->sse_message( [ |
| 34 |
'action' => 'removeLog', |
| 35 |
'type' => $type, |
| 36 |
'progress' => 100 |
| 37 |
] ); |
| 38 |
} |
| 39 |
|
| 40 |
public function sse_message( $data, $ai_content = true ) { |
| 41 |
// Log the data into debug log file |
| 42 |
$this->sse_log_file( $data ); |
| 43 |
|
| 44 |
if(Helper::should_flush()){ |
| 45 |
echo "event: message\n"; |
| 46 |
echo 'data: ' . wp_json_encode( $data ) . "\n\n"; |
| 47 |
|
| 48 |
// Extra padding. |
| 49 |
echo esc_html( ':' . str_repeat( ' ', 2048 ) . "\n\n" ); |
| 50 |
|
| 51 |
flush(); |
| 52 |
if (ob_get_level() > 0) { |
| 53 |
ob_flush(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// $data = Utils::get_session_data_by_id(); |
| 58 |
// if($ai_content && !empty($data['process_id']) && $data['process_id'] !== 'undefined' && "null" !== $data['process_id']){ |
| 59 |
|
| 60 |
// // wp_cache_delete( 'templately_ai_processed_pages', 'options' ); |
| 61 |
// global $wpdb; |
| 62 |
// $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", 'templately_ai_processed_pages' ) ); |
| 63 |
// // You might need to manually unserialize the value if it was serialized |
| 64 |
// $processed_pages = maybe_unserialize( $value ); |
| 65 |
// // $processed_pages = get_option( "templately_ai_processed_pages", [] ); |
| 66 |
|
| 67 |
// $updated_ids = $processed_pages[$data['process_id']]['pages'] ?? []; |
| 68 |
// $ai_page_ids = array_reduce($data['ai_page_ids'], 'array_merge', array()); |
| 69 |
|
| 70 |
// // $ai_page_ids = array_filter(array_map('intval', explode(',', $data['ai_page_ids'] ?? ''))); |
| 71 |
// // Calculate progress percentage |
| 72 |
|
| 73 |
// $total_pages = count($ai_page_ids); |
| 74 |
// $updated_pages = count($updated_ids); |
| 75 |
// $progress_percentage = $total_pages > 0 ? round(($updated_pages / $total_pages) * 100) : 0; |
| 76 |
|
| 77 |
// if( $progress_percentage != self::$ai_last_progress ) { |
| 78 |
// self::$ai_last_progress = $progress_percentage; |
| 79 |
// $this->sse_message( [ |
| 80 |
// 'type' => 'ai-content', |
| 81 |
// 'action' => 'updateLog', |
| 82 |
// 'progress' => $progress_percentage, |
| 83 |
// // 'name' => method_exists($runner, 'get_name') ? $runner->get_name() : '', |
| 84 |
// 'processed_pages' => $processed_pages, |
| 85 |
// // 'asdfg' => $updated_ids, |
| 86 |
// ], false ); |
| 87 |
// } |
| 88 |
// } |
| 89 |
// else { |
| 90 |
// $log = get_option( 'templately_fsi_log' ) ?: []; |
| 91 |
// $log[] = $data; |
| 92 |
// update_option( 'templately_fsi_log', $log ); |
| 93 |
// } |
| 94 |
// else if($data['action'] === 'complete' || $data['action'] === 'downloadComplete' || $data['action'] === 'error'){ |
| 95 |
// wp_send_json( $data ); |
| 96 |
// } |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Printing Error Logs in debug.log file or in option. |
| 101 |
* |
| 102 |
* @param mixed $log |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function sse_log_file( $log ){ |
| 106 |
$request_params = Utils::get_session_data_by_id(); |
| 107 |
|
| 108 |
if (is_array($log)) { |
| 109 |
$log['timestamp'] = date('Y-m-d H:i:s'); |
| 110 |
} |
| 111 |
|
| 112 |
if (isset($request_params['log_type']) && $request_params['log_type'] == 'file') { |
| 113 |
LogHandler::sse_log_file($log); |
| 114 |
} else { |
| 115 |
$_log = get_option('templately_fsi_log') ?: []; |
| 116 |
$_log[] = $log; |
| 117 |
update_option('templately_fsi_log', $_log, false); |
| 118 |
} |
| 119 |
} |
| 120 |
/** |
| 121 |
* Printing Error Logs in debug.log file. |
| 122 |
* |
| 123 |
* @param mixed $log |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function debug_log( $log ){ |
| 127 |
if ( defined('TEMPLATELY_EVENT_LOG') && TEMPLATELY_EVENT_LOG === true ) { |
| 128 |
|
| 129 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 130 |
error_log( print_r( $log, true ) ); |
| 131 |
} else if($log) { |
| 132 |
error_log( $log ); |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
} |