PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.7
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.7
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / Runners / Loop.php

Loop.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.4.7, at includes/Core/Importer/Runners/Loop.php

215 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }