PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.11
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.11
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Admin / ImportExport.php

ImportExport.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.11, at includes/Admin/ImportExport.php

314 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace NotificationX\Admin;
3
4 use NotificationX\Core\Database;
5 use NotificationX\Core\PostType;
6 use NotificationX\Core\Rules;
7 use NotificationX\Extensions\GlobalFields;
8 use NotificationX\GetInstance;
9
10 /**
11 * @method static ImportExport get_instance($args = null)
12 */
13 class ImportExport{
14 use GetInstance;
15
16 public function __construct(){
17 add_filter('nx_settings_tab_miscellaneous', [$this, 'settings_tab_help']);
18 add_filter('upload_mimes', [$this, 'cc_mime_types']);
19 add_filter('nx_settings', [$this, 'save_settings']);
20 }
21
22 public function save_settings($settings) {
23 $remove_before_save = [
24 'export-notification',
25 'export-analytics',
26 'export-status',
27 'export-settings',
28 'run_export',
29 'import',
30 'run_import',
31 ];
32 foreach ($remove_before_save as $key) {
33 if(isset($settings[$key])){
34 unset($settings[$key]);
35 }
36 }
37 return $settings;
38 }
39
40 public function cc_mime_types($mimes) {
41 $mimes['json'] = 'text/plain';
42 return $mimes;
43 }
44
45 public function settings_tab_help($tabs) {
46
47 $tabs['fields']['import-section'] = array(
48 'name' => 'import-section',
49 'type' => "section",
50 'label' => __('Import/Export', 'notificationx'),
51 'priority' => 30,
52 'fields' => array(
53 'export-notification' => [
54 'name' => "export-notification",
55 'type' => 'checkbox',
56 'label' => __('Export Notifications', 'notificationx'),
57 'default' => 0,
58 'priority' => 10,
59 ],
60 'export-analytics' => [
61 'name' => "export-analytics",
62 'type' => 'checkbox',
63 'label' => __('Analytics', 'notificationx'),
64 'default' => 0,
65 'priority' => 15,
66 'rules' => Rules::is( 'export-notification', true ),
67 // 'description' => __('Click, if you want to disable powered by text from notification', 'notificationx'),
68 ],
69 'export-status' => array(
70 'name' => 'export-status',
71 'type' => 'select',
72 'label' => __('Status', 'notificationx'),
73 'priority' => 20,
74 'rules' => Rules::is( 'export-notification', true ),
75 'default' => ['all'],
76 'options' => GlobalFields::get_instance()->normalize_fields([
77 'all' => 'ALL',
78 'enabled' => 'Enabled',
79 'disabled' => 'Disabled',
80 ]),
81 ),
82 'export-settings' => [
83 'name' => "export-settings",
84 'type' => 'checkbox',
85 'label' => __('Export Settings', 'notificationx'),
86 'default' => 0,
87 'priority' => 30,
88 ],
89 'run_export' => array(
90 'name' => 'run_export',
91 // 'label' => __('Import', 'notificationx'),
92 'text' => [
93 'normal' => __('Export', 'notificationx'),
94 'saved' => __('Export', 'notificationx'),
95 'loading' => __('Exporting...', 'notificationx'),
96 ],
97 'type' => 'button',
98 'priority' => 40,
99 // 'rules' => Rules::is( 'import', null, true ),
100 'rules' => Rules::logicalRule([
101 Rules::is( 'export-notification', true ),
102 Rules::is( 'export-settings', true ),
103 ], 'or'),
104 'ajax' => [
105 'on' => 'click',
106 'api' => '/notificationx/v1/export',
107 'data' => [
108 'export-notification' => '@export-notification',
109 'export-settings' => '@export-settings',
110 'export-analytics' => '@export-analytics',
111 'export-status' => '@export-status',
112 ],
113 'swal' => [
114 'text' => __('Export completed successfully.', 'notificationx'),
115 'icon' => 'success',
116 'autoClose' => 2000
117 ],
118 ],
119 ),
120
121 'import' => array(
122 'name' => 'import',
123 'type' => 'jsonuploader',
124 'label' => __('Import (*.json)', 'notificationx'),
125 'reset' => __('Change', 'notificationx'),
126 'priority' => 60,
127 'notImage' => true,
128 ),
129 'run_import' => array(
130 'name' => 'run_import',
131 // 'label' => __('Import', 'notificationx'),
132 'text' => [
133 'normal' => __('Import', 'notificationx'),
134 'saved' => __('Import', 'notificationx'),
135 'loading' => __('Importing...', 'notificationx'),
136 ],
137 'type' => 'button',
138 'priority' => 70,
139 'rules' => Rules::is( 'import', null, true ),
140 'ajax' => [
141 'on' => 'click',
142 'api' => '/notificationx/v1/import',
143 'data' => [
144 'import' => '@import',
145 ],
146 'swal' => [
147 'text' => __('Import completed successfully.', 'notificationx'),
148 'icon' => 'success',
149 'autoClose' => 2000
150 ],
151 ],
152 ),
153 ),
154 );
155
156 return $tabs;
157 }
158
159 public function import($request){
160 @set_time_limit(0);
161 $params = $request->get_params();
162 $status = 'error';
163 if(!empty($params['import'])){
164 try {
165 $data = json_decode($params['import'], true);
166
167 if(!empty($data['settings'])){
168 Settings::get_instance()->set('settings', $data['settings']);
169 $status = 'success';
170 }
171
172 if(!empty($data['notifications'])){
173 $analytics = [];
174 if(!empty($data['analytics'])){
175 $analytics = $this->group_stats_by_nx_id($data['analytics']);
176 }
177 foreach ($data['notifications'] as $key => $post) {
178 $nx_id = $post['nx_id'];
179 unset($post['nx_id']);
180 unset($post['id']);
181
182 if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){
183 $elementor_data = $data['elementor'][$post['elementor_id']];
184 unset($elementor_data['post']['ID']);
185
186 $el_id = wp_insert_post($elementor_data['post']);
187 foreach ($elementor_data['meta'] as $key => $value) {
188 if($key == '_elementor_css') continue;
189 foreach ($value as $s_value) {
190 if($key == '_elementor_data'){
191 $s_value = wp_slash( wp_json_encode(json_decode($s_value)));
192 }
193 add_post_meta($el_id, $key, $s_value);
194 }
195 }
196 $post['elementor_id'] = $el_id;
197
198
199 }
200
201
202 $notification = PostType::get_instance()->save_post($post); //, ['no_hooks' => true]
203 $nx_id_new = $notification['nx_id'];
204
205 if(!empty($analytics[$nx_id])){
206 foreach ($analytics[$nx_id] as $key => $value) {
207 $value['nx_id'] = $nx_id_new;
208 $analytics[$nx_id][$key] = $value;
209 }
210 // Database::get_instance()->insert_posts(Database::$table_stats, array_values($analytics[$nx_id]));
211 }
212 }
213 if(!empty($analytics)){
214 $_analytics = [];
215 foreach ($analytics as $key => $value) {
216 $_analytics = array_merge($_analytics, $value);
217 }
218 Database::get_instance()->insert_posts(Database::$table_stats, array_values($_analytics));
219 }
220
221 $status = 'success';
222 }
223
224 } catch (\Throwable $th) {
225 //throw $th;
226 $status = 'error';
227 }
228 }
229
230 return [
231 'status' => $status,
232 'data' => [
233 'context' => [
234 'import' => null,
235 ]
236 ]
237 ];
238 }
239
240 public function export($request){
241 @set_time_limit(0);
242 $params = $request->get_params();
243 $export = [];
244 if(!empty($params['export-settings'])){
245 $file_name = 'nx-settings-export.json';
246 $export['settings'] = Settings::get_instance()->get('settings');
247 }
248 if(!empty($params['export-notification'])){
249 $where = [];
250 $file_name = 'nx-notification-export.json';
251 if(!empty($params['export-status']) && ($params['export-status'] == 'enabled' || $params['export-status'] == 'disabled')){
252 $where = [
253 'enabled' => $params['export-status'] == 'enabled',
254 ];
255 }
256 if(!empty($params['export-notification-ids']) && is_array($params['export-notification-ids'])){
257 $where = [
258 'nx_id' => [
259 'IN',
260 $params['export-notification-ids'],
261 ],
262 ];
263 }
264 $export['notifications'] = PostType::get_instance()->get_posts($where);
265 if(!empty($params['export-analytics']) && !empty($export['notifications'])){
266 $nx_ids = array_column($export['notifications'], 'nx_id');
267 $export['analytics'] = Database::get_instance()->get_posts(Database::$table_stats, '*', [
268 'nx_id' => [ 'IN', $nx_ids ],
269 ]);
270 }
271
272 if(!empty($export['notifications'])){
273 foreach ($export['notifications'] as $key => $post) {
274 if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){
275 $export['elementor'][$post['elementor_id']]['post'] = get_post($post['elementor_id']);
276 $meta = get_post_meta($post['elementor_id']);
277 foreach ($meta as $key => $value) {
278 $export['elementor'][$post['elementor_id']]['meta'][$key] = array_map('maybe_unserialize', $value);
279 }
280 }
281 }
282 }
283 }
284 if(!empty($params['export-settings']) && !empty($params['export-notification'])){
285 $file_name = 'nx-export.json';
286 }
287 return [
288 'success' => true,
289 'data' => [
290 'filename' => $file_name,
291 'download' => $export,
292 'context' => [
293 'export-notification' => false,
294 'export-settings' => false,
295 'export-analytics' => false,
296 'export-status' => 'all',
297 ]
298 ]
299 ];
300 }
301
302 public function group_stats_by_nx_id($stats){
303 $new_stats = [];
304 if(!empty($stats)){
305 foreach ($stats as $key => $value) {
306 unset($value['stat_id']);
307 $new_stats[$value['nx_id']][] = $value;
308 }
309 }
310
311 return $new_stats;
312 }
313 }
314