| @@ -1,11 +1,14 @@ | ||
| 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\Utils; | |
| 5 | 7 | use Templately\Utils\Helper; |
| 6 | 8 | |
| 7 | 9 | trait LogHelper { |
| 10 | + private static $ai_last_progress = 0; | |
| 8 | 11 | private $log_types = [ |
| 9 | 12 | '' |
| 10 | 13 | ]; |
| 11 | 14 | |
| @@ -33,16 +36,12 @@ | ||
| 33 | 36 | 'progress' => 100 |
| 34 | 37 | ] ); |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | - public function sse_message( $data ) { | |
| 40 | + public function sse_message( $data, $ai_content = true ) { | |
| 38 | 41 | // Log the data into debug log file |
| 39 | - $this->debug_log( $data ); | |
| 42 | + $this->sse_log_file( $data ); | |
| 40 | 43 | |
| 41 | - $log = get_transient( 'templately_fsi_log' ) ?: []; | |
| 42 | - $log[] = $data; | |
| 43 | - set_transient( 'templately_fsi_log', $log, 15 * MINUTE_IN_SECONDS ); | |
| 44 | - | |
| 45 | 44 | if(Helper::should_flush()){ |
| 46 | 45 | echo "event: message\n"; |
| 47 | 46 | echo 'data: ' . wp_json_encode( $data ) . "\n\n"; |
| 48 | 47 | |
| @@ -49,15 +48,77 @@ | ||
| 49 | 48 | // Extra padding. |
| 50 | 49 | echo esc_html( ':' . str_repeat( ' ', 2048 ) . "\n\n" ); |
| 51 | 50 | |
| 52 | 51 | flush(); |
| 52 | + if (ob_get_level() > 0) { | |
| 53 | + ob_flush(); | |
| 54 | + } | |
| 53 | 55 | } |
| 54 | - else if($data['action'] === 'complete' || $data['action'] === 'error'){ | |
| 55 | - wp_send_json( $data ); | |
| 56 | - } | |
| 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 | + // } | |
| 57 | 97 | } |
| 58 | 98 | |
| 59 | 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 | + /** | |
| 60 | 121 | * Printing Error Logs in debug.log file. |
| 61 | 122 | * |
| 62 | 123 | * @param mixed $log |
| 63 | 124 | * @return void |
| @@ -63,12 +124,14 @@ | ||
| 63 | 124 | * @return void |
| 64 | 125 | */ |
| 65 | 126 | public function debug_log( $log ){ |
| 66 | 127 | if ( defined('TEMPLATELY_EVENT_LOG') && TEMPLATELY_EVENT_LOG === true ) { |
| 128 | + | |
| 67 | 129 | if ( is_array( $log ) || is_object( $log ) ) { |
| 68 | 130 | error_log( print_r( $log, true ) ); |
| 69 | - } else { | |
| 131 | + } else if($log) { | |
| 70 | 132 | error_log( $log ); |
| 71 | 133 | } |
| 134 | + | |
| 72 | 135 | } |
| 73 | 136 | } |
| 74 | 137 | } |