| 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 |
// Importing/exporting many notifications can exceed the default limit. |
| 161 |
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged |
| 162 |
@set_time_limit(0); |
| 163 |
$params = $request->get_params(); |
| 164 |
$status = 'error'; |
| 165 |
if(!empty($params['import'])){ |
| 166 |
try { |
| 167 |
$data = json_decode($params['import'], true); |
| 168 |
|
| 169 |
if(!empty($data['settings'])){ |
| 170 |
Settings::get_instance()->set('settings', $data['settings']); |
| 171 |
$status = 'success'; |
| 172 |
} |
| 173 |
|
| 174 |
if(!empty($data['notifications'])){ |
| 175 |
$analytics = []; |
| 176 |
if(!empty($data['analytics'])){ |
| 177 |
$analytics = $this->group_stats_by_nx_id($data['analytics']); |
| 178 |
} |
| 179 |
foreach ($data['notifications'] as $key => $post) { |
| 180 |
$nx_id = $post['nx_id']; |
| 181 |
unset($post['nx_id']); |
| 182 |
unset($post['id']); |
| 183 |
|
| 184 |
if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){ |
| 185 |
$elementor_data = $data['elementor'][$post['elementor_id']]; |
| 186 |
unset($elementor_data['post']['ID']); |
| 187 |
|
| 188 |
$el_id = wp_insert_post($elementor_data['post']); |
| 189 |
foreach ($elementor_data['meta'] as $key => $value) { |
| 190 |
if($key == '_elementor_css') continue; |
| 191 |
foreach ($value as $s_value) { |
| 192 |
if($key == '_elementor_data'){ |
| 193 |
$s_value = wp_slash( wp_json_encode(json_decode($s_value))); |
| 194 |
} |
| 195 |
add_post_meta($el_id, $key, $s_value); |
| 196 |
} |
| 197 |
} |
| 198 |
$post['elementor_id'] = $el_id; |
| 199 |
|
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
$notification = PostType::get_instance()->save_post($post); //, ['no_hooks' => true] |
| 205 |
$nx_id_new = $notification['nx_id']; |
| 206 |
|
| 207 |
if(!empty($analytics[$nx_id])){ |
| 208 |
foreach ($analytics[$nx_id] as $key => $value) { |
| 209 |
$value['nx_id'] = $nx_id_new; |
| 210 |
$analytics[$nx_id][$key] = $value; |
| 211 |
} |
| 212 |
// Database::get_instance()->insert_posts(Database::$table_stats, array_values($analytics[$nx_id])); |
| 213 |
} |
| 214 |
} |
| 215 |
if(!empty($analytics)){ |
| 216 |
$_analytics = []; |
| 217 |
foreach ($analytics as $key => $value) { |
| 218 |
$_analytics = array_merge($_analytics, $value); |
| 219 |
} |
| 220 |
Database::get_instance()->insert_posts(Database::$table_stats, array_values($_analytics)); |
| 221 |
} |
| 222 |
|
| 223 |
$status = 'success'; |
| 224 |
} |
| 225 |
|
| 226 |
} catch (\Throwable $th) { |
| 227 |
//throw $th; |
| 228 |
$status = 'error'; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return [ |
| 233 |
'status' => $status, |
| 234 |
'data' => [ |
| 235 |
'context' => [ |
| 236 |
'import' => null, |
| 237 |
] |
| 238 |
] |
| 239 |
]; |
| 240 |
} |
| 241 |
|
| 242 |
public function export($request){ |
| 243 |
// Importing/exporting many notifications can exceed the default limit. |
| 244 |
// phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged |
| 245 |
@set_time_limit(0); |
| 246 |
$params = $request->get_params(); |
| 247 |
$export = []; |
| 248 |
if(!empty($params['export-settings'])){ |
| 249 |
$file_name = 'nx-settings-export.json'; |
| 250 |
$export['settings'] = Settings::get_instance()->get('settings'); |
| 251 |
} |
| 252 |
if(!empty($params['export-notification'])){ |
| 253 |
$where = []; |
| 254 |
$file_name = 'nx-notification-export.json'; |
| 255 |
if(!empty($params['export-status']) && ($params['export-status'] == 'enabled' || $params['export-status'] == 'disabled')){ |
| 256 |
$where = [ |
| 257 |
'enabled' => $params['export-status'] == 'enabled', |
| 258 |
]; |
| 259 |
} |
| 260 |
if(!empty($params['export-notification-ids']) && is_array($params['export-notification-ids'])){ |
| 261 |
$where = [ |
| 262 |
'nx_id' => [ |
| 263 |
'IN', |
| 264 |
$params['export-notification-ids'], |
| 265 |
], |
| 266 |
]; |
| 267 |
} |
| 268 |
$export['notifications'] = PostType::get_instance()->get_posts($where); |
| 269 |
if(!empty($params['export-analytics']) && !empty($export['notifications'])){ |
| 270 |
$nx_ids = array_column($export['notifications'], 'nx_id'); |
| 271 |
$export['analytics'] = Database::get_instance()->get_posts(Database::$table_stats, '*', [ |
| 272 |
'nx_id' => [ 'IN', $nx_ids ], |
| 273 |
]); |
| 274 |
} |
| 275 |
|
| 276 |
if(!empty($export['notifications'])){ |
| 277 |
foreach ($export['notifications'] as $key => $post) { |
| 278 |
if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){ |
| 279 |
$export['elementor'][$post['elementor_id']]['post'] = get_post($post['elementor_id']); |
| 280 |
$meta = get_post_meta($post['elementor_id']); |
| 281 |
foreach ($meta as $key => $value) { |
| 282 |
$export['elementor'][$post['elementor_id']]['meta'][$key] = array_map('maybe_unserialize', $value); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
if(!empty($params['export-settings']) && !empty($params['export-notification'])){ |
| 289 |
$file_name = 'nx-export.json'; |
| 290 |
} |
| 291 |
return [ |
| 292 |
'success' => true, |
| 293 |
'data' => [ |
| 294 |
'filename' => $file_name, |
| 295 |
'download' => $export, |
| 296 |
'context' => [ |
| 297 |
'export-notification' => false, |
| 298 |
'export-settings' => false, |
| 299 |
'export-analytics' => false, |
| 300 |
'export-status' => 'all', |
| 301 |
] |
| 302 |
] |
| 303 |
]; |
| 304 |
} |
| 305 |
|
| 306 |
public function group_stats_by_nx_id($stats){ |
| 307 |
$new_stats = []; |
| 308 |
if(!empty($stats)){ |
| 309 |
foreach ($stats as $key => $value) { |
| 310 |
unset($value['stat_id']); |
| 311 |
$new_stats[$value['nx_id']][] = $value; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
return $new_stats; |
| 316 |
} |
| 317 |
} |
| 318 |
|