| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Templately\Core\Importer\FullSiteImport; |
| 6 |
use Templately\Core\Importer\LogHelper; |
| 7 |
use Templately\Core\Importer\Utils\Utils; |
| 8 |
use Templately\Utils\Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* @method string get_name() |
| 12 |
* @method void sse_message(array $data) |
| 13 |
*/ |
| 14 |
trait Loop { |
| 15 |
|
| 16 |
/** |
| 17 |
* Undocumented function |
| 18 |
* |
| 19 |
* @param [type] $items |
| 20 |
* @param [type] $callback($key, $item, $results) |
| 21 |
* @param [type] $unique_id |
| 22 |
* @param boolean $split_to_chunks |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public function loop($items, $callback, $unique_id = null, $split_to_chunks = false) { |
| 26 |
// throw error if the callback is not callable |
| 27 |
if (!is_callable($callback)) { |
| 28 |
throw new \Exception('The callback is not callable'); |
| 29 |
} |
| 30 |
if (!is_array($items)) { |
| 31 |
throw new \Exception('The items should be an array'); |
| 32 |
} |
| 33 |
|
| 34 |
$results = $this->_get_result([], $unique_id); |
| 35 |
$progress = $this->_get_progress([], $unique_id); |
| 36 |
|
| 37 |
|
| 38 |
if(!empty($this->backup_attributes)){ |
| 39 |
$this->_retrieve_attributes($this->backup_attributes, $unique_id); |
| 40 |
} |
| 41 |
|
| 42 |
foreach ($items as $key => $item) { |
| 43 |
// If the template has been processed, skip it |
| 44 |
if (in_array("key_$key", $progress, true)) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$result = $callback($key, $item, $results); |
| 49 |
if($result === 'continue'){ |
| 50 |
// If the callback returns 'continue', skip to the next iteration |
| 51 |
continue; |
| 52 |
} |
| 53 |
$results = Helper::recursive_wp_parse_args($result, $results); |
| 54 |
|
| 55 |
|
| 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); |
| 59 |
|
| 60 |
// If it's not the last item, send the SSE message and exit |
| 61 |
|
| 62 |
$is_last_runner = key( array_slice( $items, -1, 1, true ) ) === $key; |
| 63 |
|
| 64 |
if( (Helper::fsi_should_exit() || $split_to_chunks) && !$is_last_runner && method_exists($this, 'sse_message') ) { |
| 65 |
if(!empty($this->backup_attributes)){ |
| 66 |
$this->_backup_attributes($this->backup_attributes, $unique_id); |
| 67 |
} |
| 68 |
$this->sse_message( [ |
| 69 |
'type' => 'continue', |
| 70 |
'action' => 'continue', |
| 71 |
'name' => method_exists($this, 'get_name') ? $this->get_name() : '', |
| 72 |
'index' => $key, |
| 73 |
'results' => (function() use ($unique_id) {$this->CallingFunctionName($unique_id);})(), |
| 74 |
] ); |
| 75 |
exit; |
| 76 |
} |
| 77 |
} |
| 78 |
return $results; |
| 79 |
} |
| 80 |
|
| 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; |
| 115 |
} |
| 116 |
|
| 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; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public function get_progress($defaults = [], $unique_id = null, $function = true) { |
| 131 |
return $this->_get_progress($defaults, $unique_id, $function); |
| 132 |
} |
| 133 |
|
| 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; |
| 142 |
} |
| 143 |
|
| 144 |
public function get_result($defaults = [], $unique_id = null, $function = true) { |
| 145 |
return $this->_get_result($defaults, $unique_id, $function); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 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 ); |
| 164 |
} |
| 165 |
|
| 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 ); |
| 168 |
} |
| 169 |
|
| 170 |
// Modified get_session_data to use the static version |
| 171 |
protected function get_session_data(): array { |
| 172 |
return Utils::get_session_data($this->session_id); |
| 173 |
} |
| 174 |
|
| 175 |
// Modified update_session_data to use the static version |
| 176 |
protected function update_session_data($data): bool { |
| 177 |
return Utils::update_session_data($this->session_id, $data); |
| 178 |
} |
| 179 |
|
| 180 |
private function _retrieve_attributes($attributes, $unique_id){ |
| 181 |
$calling_class = $this->CallingFunctionName($unique_id, false); |
| 182 |
|
| 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 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
private function _backup_attributes($attributes, $unique_id){ |
| 195 |
$calling_class = $this->CallingFunctionName($unique_id, false); |
| 196 |
$old_data = $this->get_session_data(); |
| 197 |
|
| 198 |
$new_data = []; |
| 199 |
$attr_values = []; |
| 200 |
|
| 201 |
foreach ($attributes as $attribute) { |
| 202 |
if(isset($this->$attribute)){ |
| 203 |
$attr_values[$attribute] = $this->$attribute; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
$new_data['loop']['backup_attributes'] = [$calling_class => $attr_values]; |
| 208 |
|
| 209 |
|
| 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 ); |
| 213 |
} |
| 214 |
|
| 215 |
} |