| @@ -1,13 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Templately\Core\Importer\Runners; |
| 4 | 4 | |
| 5 | -use Exception; | |
| 6 | -use Templately\Core\Importer\Exception\SkippableErrorException; | |
| 7 | 5 | use Templately\Core\Importer\FullSiteImport; |
| 8 | 6 | use Templately\Core\Importer\LogHelper; |
| 9 | -use Templately\Core\Importer\Utils\SessionData; | |
| 10 | 7 | use Templately\Core\Importer\Utils\Utils; |
| 11 | 8 | use Templately\Utils\Helper; |
| 12 | 9 | |
| 13 | 10 | /** |
| @@ -15,11 +12,8 @@ | ||
| 15 | 12 | * @method void sse_message(array $data) |
| 16 | 13 | */ |
| 17 | 14 | trait Loop { |
| 18 | 15 | |
| 19 | - public static $max_error_attempts = 2; | |
| 20 | - public static $max_consecutive_skips = 5; | |
| 21 | - | |
| 22 | 16 | /** |
| 23 | 17 | * Undocumented function |
| 24 | 18 | * |
| 25 | 19 | * @param [type] $items |
| @@ -36,10 +30,12 @@ | ||
| 36 | 30 | if (!is_array($items)) { |
| 37 | 31 | throw new \Exception('The items should be an array'); |
| 38 | 32 | } |
| 39 | 33 | |
| 40 | - $results = $this->_get_loop_result([], $unique_id); | |
| 34 | + $results = $this->_get_result([], $unique_id); | |
| 35 | + $progress = $this->_get_progress([], $unique_id); | |
| 41 | 36 | |
| 37 | + | |
| 42 | 38 | if(!empty($this->backup_attributes)){ |
| 43 | 39 | $this->_retrieve_attributes($this->backup_attributes, $unique_id); |
| 44 | 40 | } |
| 45 | 41 | |
| @@ -44,60 +40,24 @@ | ||
| 44 | 40 | } |
| 45 | 41 | |
| 46 | 42 | foreach ($items as $key => $item) { |
| 47 | 43 | // If the template has been processed, skip it |
| 48 | - if ($this->_is_key_processed($key, $unique_id)) { | |
| 44 | + if (in_array("key_$key", $progress, true)) { | |
| 49 | 45 | continue; |
| 50 | 46 | } |
| 51 | 47 | |
| 52 | - // Skip-on-error: Check if feature is enabled and item should be skipped | |
| 53 | - if ($this->_is_skip_feature_enabled()) { | |
| 54 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 55 | - $error_attempts = SessionData::get_error_attempts($this->session_id, $calling_class, $key); | |
| 56 | - | |
| 57 | - // Skip if error attempts >= MAX_ERROR_ATTEMPTS | |
| 58 | - if ($error_attempts >= self::$max_error_attempts) { | |
| 59 | - $this->_mark_key_skipped($key, $unique_id, 'Max error attempts reached'); | |
| 60 | - $this->_increment_consecutive_skips(); | |
| 61 | - continue; | |
| 62 | - } | |
| 48 | + $result = $callback($key, $item, $results); | |
| 49 | + if($result === 'continue'){ | |
| 50 | + // If the callback returns 'continue', skip to the next iteration | |
| 51 | + continue; | |
| 63 | 52 | } |
| 53 | + $results = Helper::recursive_wp_parse_args($result, $results); | |
| 64 | 54 | |
| 65 | - // Wrap callback in try-catch for skip-on-error handling | |
| 66 | - try { | |
| 67 | - $result = $callback($key, $item, $results); | |
| 68 | - if($result === 'continue'){ | |
| 69 | - // If the callback returns 'continue', skip to the next iteration | |
| 70 | - continue; | |
| 71 | - } | |
| 72 | - $results = $result; | |
| 73 | 55 | |
| 74 | - // Success - reset consecutive skip counter | |
| 75 | - if ($this->_is_skip_feature_enabled()) { | |
| 76 | - $this->_reset_consecutive_skips(); | |
| 77 | - } | |
| 78 | - } catch (SkippableErrorException $e) { | |
| 79 | - // Catchable skip error - increment attempts and mark as skipped | |
| 80 | - if ($this->_is_skip_feature_enabled()) { | |
| 81 | - $this->_increment_error_attempts($key, $unique_id); | |
| 82 | - $this->_mark_key_skipped($key, $unique_id, $e->getMessage()); | |
| 83 | - continue; | |
| 84 | - } else { | |
| 85 | - // Feature disabled - throw as normal exception | |
| 86 | - throw new Exception($e->getMessage(), $e->getCode(), $e); | |
| 87 | - } | |
| 88 | - } catch (Exception $e) { | |
| 89 | - // Other exceptions - increment error attempts then re-throw | |
| 90 | - if ($this->_is_skip_feature_enabled()) { | |
| 91 | - $this->_increment_error_attempts($key, $unique_id); | |
| 92 | - } | |
| 93 | - throw $e; | |
| 94 | - } | |
| 56 | + // Add the template to the processed templates and update the session data | |
| 57 | + $progress[] = "key_$key"; | |
| 58 | + $this->_update_progress( $progress, $result, $unique_id); | |
| 95 | 59 | |
| 96 | - // Mark as processed and save result | |
| 97 | - $this->_mark_key_processed($key, $unique_id); | |
| 98 | - $this->_set_loop_result($results, $unique_id); | |
| 99 | - | |
| 100 | 60 | // If it's not the last item, send the SSE message and exit |
| 101 | 61 | |
| 102 | 62 | $is_last_runner = key( array_slice( $items, -1, 1, true ) ) === $key; |
| 103 | 63 | |
| @@ -109,9 +69,9 @@ | ||
| 109 | 69 | 'type' => 'continue', |
| 110 | 70 | 'action' => 'continue', |
| 111 | 71 | 'name' => method_exists($this, 'get_name') ? $this->get_name() : '', |
| 112 | 72 | 'index' => $key, |
| 113 | - 'results' => SessionData::get_calling_identifier($unique_id, true, false, 3), | |
| 73 | + 'results' => (function() use ($unique_id) {$this->CallingFunctionName($unique_id);})(), | |
| 114 | 74 | ] ); |
| 115 | 75 | exit; |
| 116 | 76 | } |
| 117 | 77 | } |
| @@ -117,98 +77,126 @@ | ||
| 117 | 77 | } |
| 118 | 78 | return $results; |
| 119 | 79 | } |
| 120 | 80 | |
| 121 | - /** | |
| 122 | - * Check if a key has been processed (internal) | |
| 123 | - * | |
| 124 | - * @param mixed $key The item key | |
| 125 | - * @param string|null $unique_id Optional unique identifier | |
| 126 | - * @return bool True if processed | |
| 127 | - */ | |
| 128 | - private function _is_key_processed($key, $unique_id = null): bool { | |
| 129 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 130 | - return SessionData::is_key_processed($this->session_id, $calling_class, $key); | |
| 81 | + private function CallingFunctionName($id = null, $function = true, $line = false, $level = 3) { | |
| 82 | + $return = 'unknown'; | |
| 83 | + $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, ($level + 1)); | |
| 84 | + | |
| 85 | + // Check if the trace has at least three elements | |
| 86 | + if (isset($trace[$level])) { | |
| 87 | + $final_call = $trace[$level]; | |
| 88 | + $return = ''; | |
| 89 | + | |
| 90 | + if (isset($final_call['object'])) { | |
| 91 | + $return .= get_class($final_call['object']); | |
| 92 | + } elseif (isset($final_call['class'])) { | |
| 93 | + $return .= $final_call['class']; | |
| 94 | + } | |
| 95 | + | |
| 96 | + if ($function && isset($final_call['function'])) { | |
| 97 | + $return .= ($return ? '::' : '') . $final_call['function']; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ($line && isset($final_call['line'])) { | |
| 101 | + $return .= ($return ? '::' : '') . $final_call['line']; | |
| 102 | + } | |
| 103 | + | |
| 104 | + if (!empty($id)) { | |
| 105 | + $return .= ($return ? '::' : '') . $id; | |
| 106 | + } | |
| 107 | + | |
| 108 | + if (!$return) { | |
| 109 | + $return = 'unknown'; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + // error_log($return . PHP_EOL, 3, ABSPATH . 'wp-content/debug.log'); | |
| 113 | + | |
| 114 | + return $return; | |
| 131 | 115 | } |
| 132 | 116 | |
| 133 | - /** | |
| 134 | - * Check if a key has been processed (public wrapper) | |
| 135 | - */ | |
| 136 | - public function is_key_processed($key, $unique_id = null): bool { | |
| 137 | - return $this->_is_key_processed($key, $unique_id); | |
| 117 | + private function _get_progress($defaults = [], $unique_id = null, $function = true) { | |
| 118 | + $data = $this->get_session_data(); | |
| 119 | + $calling_class = $this->CallingFunctionName($unique_id, $function); | |
| 120 | + if(isset($data['loop']['progress'][$calling_class])){ | |
| 121 | + return $data['loop']['progress'][$calling_class]; | |
| 122 | + } | |
| 123 | + else if(!empty($defaults)){ | |
| 124 | + $this->_update_progress($defaults, null, $unique_id, $function); | |
| 125 | + } | |
| 126 | + return $defaults; | |
| 138 | 127 | } |
| 139 | 128 | |
| 140 | - /** | |
| 141 | - * Mark a key as processed (internal) | |
| 142 | - * | |
| 143 | - * @param mixed $key The item key | |
| 144 | - * @param string|null $unique_id Optional unique identifier | |
| 145 | - * @return bool Success status | |
| 146 | - */ | |
| 147 | - private function _mark_key_processed($key, $unique_id = null): bool { | |
| 148 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 149 | - return SessionData::mark_key_processed($this->session_id, $calling_class, $key); | |
| 129 | + | |
| 130 | + public function get_progress($defaults = [], $unique_id = null, $function = true) { | |
| 131 | + return $this->_get_progress($defaults, $unique_id, $function); | |
| 150 | 132 | } |
| 151 | 133 | |
| 152 | - /** | |
| 153 | - * Mark a key as processed (public wrapper) | |
| 154 | - */ | |
| 155 | - public function mark_key_processed($key, $unique_id = null): bool { | |
| 156 | - return $this->_mark_key_processed($key, $unique_id); | |
| 134 | + | |
| 135 | + private function _get_result($defaults = [], $unique_id = null, $function = true) { | |
| 136 | + $data = $this->get_session_data(); | |
| 137 | + $calling_class = $this->CallingFunctionName($unique_id, $function); | |
| 138 | + if(isset($data['loop']['result'][$calling_class])){ | |
| 139 | + return $data['loop']['result'][$calling_class]; | |
| 140 | + } | |
| 141 | + return $defaults; | |
| 157 | 142 | } |
| 158 | 143 | |
| 159 | - /** | |
| 160 | - * Set loop result (internal) | |
| 161 | - * | |
| 162 | - * @param array $result The result data | |
| 163 | - * @param string|null $unique_id Optional unique identifier | |
| 164 | - * @return bool Success status | |
| 165 | - */ | |
| 166 | - private function _set_loop_result($result, $unique_id = null): bool { | |
| 167 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 168 | - return SessionData::set_loop_result($this->session_id, $calling_class, $result); | |
| 144 | + public function get_result($defaults = [], $unique_id = null, $function = true) { | |
| 145 | + return $this->_get_result($defaults, $unique_id, $function); | |
| 169 | 146 | } |
| 170 | 147 | |
| 171 | - /** | |
| 172 | - * Set loop result (public wrapper) | |
| 173 | - */ | |
| 174 | - public function set_loop_result($result, $unique_id = null): bool { | |
| 175 | - return $this->_set_loop_result($result, $unique_id); | |
| 176 | - } | |
| 177 | 148 | |
| 178 | - private function _get_loop_result($defaults = [], $unique_id = null, $function = true) { | |
| 179 | - $calling_class = SessionData::get_calling_identifier($unique_id, $function, false); | |
| 180 | - return SessionData::get_loop_result($this->session_id, $calling_class, $defaults); | |
| 149 | + private function _update_progress( $progress, $imported_data = null, $unique_id = null, $function = true ): bool { | |
| 150 | + $calling_class = $this->CallingFunctionName($unique_id, $function); | |
| 151 | + $old_data = $this->get_session_data(); | |
| 152 | + | |
| 153 | + $new_data = []; | |
| 154 | + | |
| 155 | + if($progress !== null){ | |
| 156 | + $new_data['loop']['progress'] = [$calling_class => $progress]; | |
| 157 | + } | |
| 158 | + if($imported_data !== null){ | |
| 159 | + $new_data['loop']['result'] = [$calling_class => $imported_data]; | |
| 160 | + } | |
| 161 | + | |
| 162 | + $new_data = Helper::recursive_wp_parse_args( $new_data, $old_data ); | |
| 163 | + return $this->update_session_data( $new_data ); | |
| 181 | 164 | } |
| 182 | 165 | |
| 183 | - public function get_loop_result($defaults = [], $unique_id = null, $function = true) { | |
| 184 | - return $this->_get_loop_result($defaults, $unique_id, $function); | |
| 166 | + public function update_progress( $progress, $imported_data = null, $unique_id = null, $function = true ): bool { | |
| 167 | + return $this->_update_progress( $progress, $imported_data, $unique_id, $function ); | |
| 185 | 168 | } |
| 186 | 169 | |
| 187 | - // Modified get_session_data to use SessionData | |
| 170 | + // Modified get_session_data to use the static version | |
| 188 | 171 | protected function get_session_data(): array { |
| 189 | - return SessionData::get_data($this->session_id); | |
| 172 | + return Utils::get_session_data($this->session_id); | |
| 190 | 173 | } |
| 191 | 174 | |
| 192 | - // Modified update_session_data to use SessionData (deprecated - use specific methods) | |
| 175 | + // Modified update_session_data to use the static version | |
| 193 | 176 | protected function update_session_data($data): bool { |
| 194 | - $existing = SessionData::get_data($this->session_id); | |
| 195 | - return SessionData::save($this->session_id, array_merge($existing, $data)); | |
| 177 | + return Utils::update_session_data($this->session_id, $data); | |
| 196 | 178 | } |
| 197 | 179 | |
| 198 | 180 | private function _retrieve_attributes($attributes, $unique_id){ |
| 199 | - $calling_class = SessionData::get_calling_identifier($unique_id, false, false, 4); | |
| 200 | - $attr_values = SessionData::get($this->session_id, "loop.backup_attributes.{$calling_class}", []); | |
| 181 | + $calling_class = $this->CallingFunctionName($unique_id, false); | |
| 201 | 182 | |
| 202 | - foreach ($attributes as $attribute) { | |
| 203 | - if(isset($attr_values[$attribute])){ | |
| 204 | - $this->$attribute = $attr_values[$attribute]; | |
| 183 | + $data = $this->get_session_data(); | |
| 184 | + if(isset($data['loop']['backup_attributes'][$calling_class])){ | |
| 185 | + $attr_values = $data['loop']['backup_attributes'][$calling_class]; | |
| 186 | + foreach ($attributes as $attribute) { | |
| 187 | + if(isset($attr_values[$attribute])){ | |
| 188 | + $this->$attribute = $attr_values[$attribute]; | |
| 189 | + } | |
| 205 | 190 | } |
| 206 | 191 | } |
| 207 | 192 | } |
| 208 | 193 | |
| 209 | 194 | private function _backup_attributes($attributes, $unique_id){ |
| 210 | - $calling_class = SessionData::get_calling_identifier($unique_id, false, false, 4); | |
| 195 | + $calling_class = $this->CallingFunctionName($unique_id, false); | |
| 196 | + $old_data = $this->get_session_data(); | |
| 197 | + | |
| 198 | + $new_data = []; | |
| 211 | 199 | $attr_values = []; |
| 212 | 200 | |
| 213 | 201 | foreach ($attributes as $attribute) { |
| 214 | 202 | if(isset($this->$attribute)){ |
| @@ -215,85 +203,13 @@ | ||
| 215 | 203 | $attr_values[$attribute] = $this->$attribute; |
| 216 | 204 | } |
| 217 | 205 | } |
| 218 | 206 | |
| 219 | - return SessionData::set($this->session_id, "loop.backup_attributes.{$calling_class}", $attr_values); | |
| 220 | - } | |
| 207 | + $new_data['loop']['backup_attributes'] = [$calling_class => $attr_values]; | |
| 221 | 208 | |
| 222 | - // ============================================================================ | |
| 223 | - // Skip-on-Error Helper Methods | |
| 224 | - // ============================================================================ | |
| 225 | 209 | |
| 226 | - /** | |
| 227 | - * Check if skip-on-error feature is enabled | |
| 228 | - * | |
| 229 | - * @return bool True if enabled | |
| 230 | - */ | |
| 231 | - protected function _is_skip_feature_enabled(): bool { | |
| 232 | - return (bool) get_option('templately_enable_fsi_skip_on_error', false); | |
| 233 | - } | |
| 234 | - | |
| 235 | - /** | |
| 236 | - * Increment error attempts for a loop item | |
| 237 | - * | |
| 238 | - * @param mixed $key The item key | |
| 239 | - * @param string|null $unique_id Optional unique identifier | |
| 240 | - * @return int New error attempt count | |
| 241 | - */ | |
| 242 | - private function _increment_error_attempts($key, $unique_id = null): int { | |
| 243 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 244 | - return SessionData::increment_error_attempts($this->session_id, $calling_class, $key); | |
| 245 | - } | |
| 246 | - | |
| 247 | - /** | |
| 248 | - * Get error attempts for a loop item | |
| 249 | - * | |
| 250 | - * @param mixed $key The item key | |
| 251 | - * @param string|null $unique_id Optional unique identifier | |
| 252 | - * @return int Error attempt count | |
| 253 | - */ | |
| 254 | - private function _get_error_attempts($key, $unique_id = null): int { | |
| 255 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 256 | - return SessionData::get_error_attempts($this->session_id, $calling_class, $key); | |
| 257 | - } | |
| 258 | - | |
| 259 | - /** | |
| 260 | - * Mark a loop item as skipped | |
| 261 | - * | |
| 262 | - * @param mixed $key The item key | |
| 263 | - * @param string|null $unique_id Optional unique identifier | |
| 264 | - * @param string $reason The reason for skipping | |
| 265 | - * @return bool Success status | |
| 266 | - */ | |
| 267 | - private function _mark_key_skipped($key, $unique_id = null, $reason = ''): bool { | |
| 268 | - $calling_class = SessionData::get_calling_identifier($unique_id, true, false); | |
| 269 | - return SessionData::mark_key_skipped($this->session_id, $calling_class, $key, $reason); | |
| 270 | - } | |
| 271 | - | |
| 272 | - /** | |
| 273 | - * Increment consecutive skip counter | |
| 274 | - * | |
| 275 | - * @return int New consecutive skip count | |
| 276 | - */ | |
| 277 | - private function _increment_consecutive_skips(): int { | |
| 278 | - return SessionData::increment_consecutive_skips($this->session_id); | |
| 279 | - } | |
| 280 | - | |
| 281 | - /** | |
| 282 | - * Reset consecutive skip counter | |
| 283 | - * | |
| 284 | - * @return bool Success status | |
| 285 | - */ | |
| 286 | - private function _reset_consecutive_skips(): bool { | |
| 287 | - return SessionData::reset_consecutive_skips($this->session_id); | |
| 288 | - } | |
| 289 | - | |
| 290 | - /** | |
| 291 | - * Get consecutive skip count | |
| 292 | - * | |
| 293 | - * @return int Consecutive skip count | |
| 294 | - */ | |
| 295 | - private function _get_consecutive_skips(): int { | |
| 296 | - return SessionData::get_consecutive_skips($this->session_id); | |
| 210 | + // @todo: not sure if we need this. | |
| 211 | + $new_data = Helper::recursive_wp_parse_args( $new_data, $old_data ); | |
| 212 | + return $this->update_session_data( $new_data ); | |
| 297 | 213 | } |
| 298 | 214 | |
| 299 | 215 | } |