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