PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.2.8
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.2.8
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.2.8, at includes/Core/Importer/Runners/Loop.php

211 lines 6.2 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 $results = Helper::recursive_wp_parse_args($result, $results);
50
51
52 // Add the template to the processed templates and update the session data
53 $progress[] = "key_$key";
54 $this->_update_progress( $progress, $result, $unique_id);
55
56 // If it's not the last item, send the SSE message and exit
57
58 $is_last_runner = key( array_slice( $items, -1, 1, true ) ) === $key;
59
60 if( (Helper::fsi_should_exit() || $split_to_chunks) && !$is_last_runner && method_exists($this, 'sse_message') ) {
61 if(!empty($this->backup_attributes)){
62 $this->_backup_attributes($this->backup_attributes, $unique_id);
63 }
64 $this->sse_message( [
65 'type' => 'continue',
66 'action' => 'continue',
67 'name' => method_exists($this, 'get_name') ? $this->get_name() : '',
68 'index' => $key,
69 'results' => (function() use ($unique_id) {$this->CallingFunctionName($unique_id);})(),
70 ] );
71 exit;
72 }
73 }
74 return $results;
75 }
76
77 private function CallingFunctionName($id = null, $function = true, $line = false, $level = 3) {
78 $return = 'unknown';
79 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, ($level + 1));
80
81 // Check if the trace has at least three elements
82 if (isset($trace[$level])) {
83 $final_call = $trace[$level];
84 $return = '';
85
86 if (isset($final_call['object'])) {
87 $return .= get_class($final_call['object']);
88 } elseif (isset($final_call['class'])) {
89 $return .= $final_call['class'];
90 }
91
92 if ($function && isset($final_call['function'])) {
93 $return .= ($return ? '::' : '') . $final_call['function'];
94 }
95
96 if ($line && isset($final_call['line'])) {
97 $return .= ($return ? '::' : '') . $final_call['line'];
98 }
99
100 if (!empty($id)) {
101 $return .= ($return ? '::' : '') . $id;
102 }
103
104 if (!$return) {
105 $return = 'unknown';
106 }
107 }
108 // error_log($return . PHP_EOL, 3, ABSPATH . 'wp-content/debug.log');
109
110 return $return;
111 }
112
113 private function _get_progress($defaults = [], $unique_id = null, $function = true) {
114 $data = $this->get_session_data();
115 $calling_class = $this->CallingFunctionName($unique_id, $function);
116 if(isset($data['loop']['progress'][$calling_class])){
117 return $data['loop']['progress'][$calling_class];
118 }
119 else if(!empty($defaults)){
120 $this->_update_progress($defaults, null, $unique_id, $function);
121 }
122 return $defaults;
123 }
124
125
126 public function get_progress($defaults = [], $unique_id = null, $function = true) {
127 return $this->_get_progress($defaults, $unique_id, $function);
128 }
129
130
131 private function _get_result($defaults = [], $unique_id = null, $function = true) {
132 $data = $this->get_session_data();
133 $calling_class = $this->CallingFunctionName($unique_id, $function);
134 if(isset($data['loop']['result'][$calling_class])){
135 return $data['loop']['result'][$calling_class];
136 }
137 return $defaults;
138 }
139
140 public function get_result($defaults = [], $unique_id = null, $function = true) {
141 return $this->_get_result($defaults, $unique_id, $function);
142 }
143
144
145 private function _update_progress( $progress, $imported_data = null, $unique_id = null, $function = true ): bool {
146 $calling_class = $this->CallingFunctionName($unique_id, $function);
147 $old_data = $this->get_session_data();
148
149 $new_data = [];
150
151 if($progress !== null){
152 $new_data['loop']['progress'] = [$calling_class => $progress];
153 }
154 if($imported_data !== null){
155 $new_data['loop']['result'] = [$calling_class => $imported_data];
156 }
157
158 $new_data = Helper::recursive_wp_parse_args( $new_data, $old_data );
159 return $this->update_session_data( $new_data );
160 }
161
162 public function update_progress( $progress, $imported_data = null, $unique_id = null, $function = true ): bool {
163 return $this->_update_progress( $progress, $imported_data, $unique_id, $function );
164 }
165
166 // Modified get_session_data to use the static version
167 protected function get_session_data(): array {
168 return Utils::get_session_data($this->session_id);
169 }
170
171 // Modified update_session_data to use the static version
172 protected function update_session_data($data): bool {
173 return Utils::update_session_data($this->session_id, $data);
174 }
175
176 private function _retrieve_attributes($attributes, $unique_id){
177 $calling_class = $this->CallingFunctionName($unique_id, false);
178
179 $data = $this->get_session_data();
180 if(isset($data['loop']['backup_attributes'][$calling_class])){
181 $attr_values = $data['loop']['backup_attributes'][$calling_class];
182 foreach ($attributes as $attribute) {
183 if(isset($attr_values[$attribute])){
184 $this->$attribute = $attr_values[$attribute];
185 }
186 }
187 }
188 }
189
190 private function _backup_attributes($attributes, $unique_id){
191 $calling_class = $this->CallingFunctionName($unique_id, false);
192 $old_data = $this->get_session_data();
193
194 $new_data = [];
195 $attr_values = [];
196
197 foreach ($attributes as $attribute) {
198 if(isset($this->$attribute)){
199 $attr_values[$attribute] = $this->$attribute;
200 }
201 }
202
203 $new_data['loop']['backup_attributes'] = [$calling_class => $attr_values];
204
205
206 // @todo: not sure if we need this.
207 $new_data = Helper::recursive_wp_parse_args( $new_data, $old_data );
208 return $this->update_session_data( $new_data );
209 }
210
211 }