record.php
358 lines
| 1 | <?php |
| 2 | |
| 3 | class PMXE_Export_Record extends PMXE_Model_Record { |
| 4 | |
| 5 | /** |
| 6 | * Initialize model instance |
| 7 | * @param array[optional] $data Array of record data to initialize object with |
| 8 | */ |
| 9 | public function __construct($data = array()) { |
| 10 | parent::__construct($data); |
| 11 | $this->setTable(PMXE_Plugin::getInstance()->getTablePrefix() . 'exports'); |
| 12 | } |
| 13 | |
| 14 | public function set_html_content_type(){ |
| 15 | return 'text/html'; |
| 16 | } |
| 17 | |
| 18 | public function generate_bundle( $debug = false) |
| 19 | { |
| 20 | // do not generate export bundle if not supported |
| 21 | if ( ! self::is_bundle_supported($this->options) ) return; |
| 22 | |
| 23 | $uploads = wp_upload_dir(); |
| 24 | |
| 25 | //generate temporary folder |
| 26 | $export_dir = wp_all_export_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY, $this->id ) . DIRECTORY_SEPARATOR; |
| 27 | $bundle_dir = $export_dir . 'bundle' . DIRECTORY_SEPARATOR; |
| 28 | |
| 29 | // clear tmp dir |
| 30 | wp_all_export_rrmdir($bundle_dir); |
| 31 | |
| 32 | @mkdir($bundle_dir); |
| 33 | |
| 34 | $friendly_name = sanitize_file_name($this->friendly_name); |
| 35 | |
| 36 | $template = "WP All Import Template - " . $friendly_name . ".txt"; |
| 37 | |
| 38 | $templates = array(); |
| 39 | |
| 40 | $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); |
| 41 | |
| 42 | if ( ! $is_secure_import) |
| 43 | { |
| 44 | $filepath = get_attached_file($this->attch_id); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | $filepath = wp_all_export_get_absolute_path($this->options['filepath']); |
| 49 | } |
| 50 | |
| 51 | @copy( $filepath, $bundle_dir . basename($filepath) ); |
| 52 | |
| 53 | if ( ! empty($this->options['tpl_data'])) |
| 54 | { |
| 55 | $template_data = array($this->options['tpl_data']); |
| 56 | |
| 57 | $template_data[0]['source_file_name'] = basename($filepath); |
| 58 | |
| 59 | $template_options = maybe_unserialize($template_data[0]['options']); |
| 60 | |
| 61 | $templates[$template_options['custom_type']] = $template_data; |
| 62 | |
| 63 | $readme = __("The other two files in this zip are the export file containing all of your data and the import template for WP All Import. \n\nTo import this data, create a new import with WP All Import and upload this zip file.", "wp_all_export_plugin"); |
| 64 | |
| 65 | file_put_contents($bundle_dir . 'readme.txt', $readme); |
| 66 | } |
| 67 | |
| 68 | // [ Add child exports to the bundle] |
| 69 | $exportList = new PMXE_Export_List(); |
| 70 | |
| 71 | foreach ($exportList->getBy('parent_id', $this->id)->convertRecords() as $child_export) |
| 72 | { |
| 73 | $is_generate_child_template = true; |
| 74 | |
| 75 | switch ($child_export->export_post_type) |
| 76 | { |
| 77 | case 'product': |
| 78 | if ( ! $this->options['order_include_poducts'] ) $is_generate_child_template = false; |
| 79 | break; |
| 80 | case 'shop_coupon': |
| 81 | if ( ! $this->options['order_include_coupons'] ) $is_generate_child_template = false; |
| 82 | break; |
| 83 | case 'shop_customer': |
| 84 | if ( ! $this->options['order_include_customers'] ) $is_generate_child_template = false; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | if ( ! $is_generate_child_template ) continue; |
| 89 | |
| 90 | if ( ! $is_secure_import) |
| 91 | { |
| 92 | $filepath = get_attached_file($child_export->attch_id); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | $filepath = wp_all_export_get_absolute_path($child_export->options['filepath']); |
| 97 | } |
| 98 | |
| 99 | if ( ! empty($child_export->options['tpl_data'])) |
| 100 | { |
| 101 | $template_data = array($child_export->options['tpl_data']); |
| 102 | |
| 103 | $template_data[0]['source_file_name'] = basename($filepath); |
| 104 | |
| 105 | $template_key = ($child_export->export_post_type == 'shop_customer') ? 'import_users' : $child_export->export_post_type; |
| 106 | |
| 107 | $templates[$template_key] = $template_data; |
| 108 | } |
| 109 | |
| 110 | @copy( $filepath, $bundle_dir . basename($filepath) ); |
| 111 | } |
| 112 | // \[ Add child exports to the bundle] |
| 113 | |
| 114 | file_put_contents($bundle_dir . $template, json_encode($templates)); |
| 115 | |
| 116 | // if ($this->options['creata_a_new_export_file'] && ! empty($this->options['cpt']) and class_exists('WooCommerce') and in_array('shop_order', $this->options['cpt']) and empty($this->parent_id) ) |
| 117 | // { |
| 118 | // $bundle_path = $export_dir . $friendly_name . '-' . ($this->iteration + 1) . '.zip'; |
| 119 | // } |
| 120 | // else |
| 121 | // { |
| 122 | // $bundle_path = $export_dir . $friendly_name . '.zip'; |
| 123 | // } |
| 124 | |
| 125 | $bundle_path = $export_dir . $friendly_name . '.zip'; |
| 126 | |
| 127 | if ( @file_exists($bundle_path)) |
| 128 | { |
| 129 | @unlink($bundle_path); |
| 130 | } |
| 131 | |
| 132 | PMXE_Zip::zipDir($bundle_dir, $bundle_path); |
| 133 | |
| 134 | // clear tmp dir |
| 135 | wp_all_export_rrmdir($bundle_dir); |
| 136 | |
| 137 | $exportOptions = $this->options; |
| 138 | $exportOptions['bundlepath'] = wp_all_export_get_relative_path($bundle_path); |
| 139 | $this->set(array( |
| 140 | 'options' => $exportOptions |
| 141 | ))->save(); |
| 142 | |
| 143 | return $bundle_path; |
| 144 | } |
| 145 | |
| 146 | public function fix_template_options() |
| 147 | { |
| 148 | // migrate media options since @version 1.2.4 |
| 149 | if ( empty($this->options['migration']) ) |
| 150 | { |
| 151 | $options = $this->options; |
| 152 | |
| 153 | $options['migration'] = PMXE_VERSION; |
| 154 | |
| 155 | $is_migrate_media = false; |
| 156 | |
| 157 | foreach ($options['ids'] as $ID => $value) |
| 158 | { |
| 159 | if ( in_array($options['cc_type'][$ID], array('media', 'attachments'))) |
| 160 | { |
| 161 | $is_migrate_media = true; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if ( ! $is_migrate_media ) |
| 167 | { |
| 168 | $this->set(array('options' => $options))->save(); |
| 169 | |
| 170 | return $this; |
| 171 | } |
| 172 | |
| 173 | $fields = array(); |
| 174 | |
| 175 | foreach ($options['ids'] as $ID => $value) |
| 176 | { |
| 177 | $field = array( |
| 178 | 'cc_label' => empty($options['cc_label'][$ID]) ? '' : $options['cc_label'][$ID], |
| 179 | 'cc_php' => empty($options['cc_php'][$ID]) ? '' : $options['cc_php'][$ID], |
| 180 | 'cc_code' => empty($options['cc_code'][$ID]) ? '' : $options['cc_code'][$ID], |
| 181 | 'cc_sql' => empty($options['cc_sql'][$ID]) ? '' : $options['cc_sql'][$ID], |
| 182 | 'cc_type' => empty($options['cc_type'][$ID]) ? '' : $options['cc_type'][$ID], |
| 183 | 'cc_options' => empty($options['cc_options'][$ID]) ? '' : $options['cc_options'][$ID], |
| 184 | 'cc_value' => empty($options['cc_value'][$ID]) ? '' : $options['cc_value'][$ID], |
| 185 | 'cc_name' => empty($options['cc_name'][$ID]) ? '' : $options['cc_name'][$ID], |
| 186 | 'cc_settings' => empty($options['cc_settings'][$ID]) ? '' : $options['cc_settings'][$ID], |
| 187 | ); |
| 188 | |
| 189 | switch ($field['cc_type']) |
| 190 | { |
| 191 | case 'media': |
| 192 | |
| 193 | switch ($field['cc_options']) |
| 194 | { |
| 195 | case 'urls': |
| 196 | $field['cc_label'] = 'url'; |
| 197 | $field['cc_value'] = 'url'; |
| 198 | $field['cc_type'] = 'image_url'; |
| 199 | break; |
| 200 | case 'filenames': |
| 201 | $field['cc_label'] = 'filename'; |
| 202 | $field['cc_value'] = 'filename'; |
| 203 | $field['cc_type'] = 'image_filename'; |
| 204 | break; |
| 205 | case 'filepaths': |
| 206 | $field['cc_label'] = 'path'; |
| 207 | $field['cc_value'] = 'path'; |
| 208 | $field['cc_type'] = 'image_path'; |
| 209 | break; |
| 210 | default: |
| 211 | $field['cc_label'] = 'url'; |
| 212 | $field['cc_value'] = 'url'; |
| 213 | $field['cc_type'] = 'image_url'; |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | $field_name = $field['cc_name']; |
| 218 | $field['cc_name'] .= '_images'; |
| 219 | $field['cc_options'] = '{"is_export_featured":true,"is_export_attached":true,"image_separator":"|"}'; |
| 220 | |
| 221 | $fields[] = $field; |
| 222 | |
| 223 | $new_fields = array('title', 'caption', 'description', 'alt'); |
| 224 | |
| 225 | foreach ($new_fields as $value) |
| 226 | { |
| 227 | $new_field = array( |
| 228 | 'cc_label' => $value, |
| 229 | 'cc_php' => empty($options['cc_php'][$ID]) ? '' : $options['cc_php'][$ID], |
| 230 | 'cc_code' => empty($options['cc_code'][$ID]) ? '' : $options['cc_code'][$ID], |
| 231 | 'cc_sql' => empty($options['cc_sql'][$ID]) ? '' : $options['cc_sql'][$ID], |
| 232 | 'cc_type' => 'image_' . $value, |
| 233 | 'cc_options' => '{"is_export_featured":true,"is_export_attached":true,"image_separator":"|"}', |
| 234 | 'cc_value' => $value, |
| 235 | 'cc_name' => $field_name . '_' . $value, |
| 236 | 'cc_settings' => '' |
| 237 | ); |
| 238 | |
| 239 | $fields[] = $new_field; |
| 240 | } |
| 241 | |
| 242 | break; |
| 243 | |
| 244 | case 'attachments': |
| 245 | $field['cc_type'] = 'attachment_url'; |
| 246 | $field['cc_options'] = ''; |
| 247 | $fields[] = $field; |
| 248 | break; |
| 249 | |
| 250 | default: |
| 251 | $fields[] = $field; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // reset fields settings |
| 257 | $options['ids'] = array(); |
| 258 | $options['cc_label'] = array(); |
| 259 | $options['cc_php'] = array(); |
| 260 | $options['cc_code'] = array(); |
| 261 | $options['cc_sql'] = array(); |
| 262 | $options['cc_type'] = array(); |
| 263 | $options['cc_options'] = array(); |
| 264 | $options['cc_value'] = array(); |
| 265 | $options['cc_name'] = array(); |
| 266 | $options['cc_settings'] = array(); |
| 267 | |
| 268 | // apply new field settings |
| 269 | foreach ($fields as $ID => $field) { |
| 270 | $options['ids'][] = 1; |
| 271 | $options['cc_label'][] = $field['cc_label']; |
| 272 | $options['cc_php'][] = $field['cc_php']; |
| 273 | $options['cc_code'][] = $field['cc_code']; |
| 274 | $options['cc_sql'][] = $field['cc_sql']; |
| 275 | $options['cc_type'][] = $field['cc_type']; |
| 276 | $options['cc_options'][] = $field['cc_options']; |
| 277 | $options['cc_value'][] = $field['cc_value']; |
| 278 | $options['cc_name'][] = $field['cc_name']; |
| 279 | $options['cc_settings'][] = $field['cc_settings']; |
| 280 | } |
| 281 | |
| 282 | $this->set(array('options' => $options))->save(); |
| 283 | } |
| 284 | |
| 285 | return $this; |
| 286 | } |
| 287 | |
| 288 | public static function is_bundle_supported( $options ) |
| 289 | { |
| 290 | $options += PMXE_Plugin::get_default_import_options(); |
| 291 | |
| 292 | // custom XML template do not support import bundle |
| 293 | if ( $options['export_to'] == 'xml' && ! empty($options['xml_template_type']) && in_array($options['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ) return false; |
| 294 | |
| 295 | // Export only parent product do not support import bundle |
| 296 | if ( ! empty($options['cpt']) and in_array($options['cpt'][0], array('product', 'product_variation')) and class_exists('WooCommerce') and $options['export_variations'] != XmlExportEngine::VARIABLE_PRODUCTS_EXPORT_PARENT_AND_VARIATION){ |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | $unsupported_post_types = array('comments'); |
| 301 | return ( empty($options['cpt']) and ! in_array($options['wp_query_selector'], array('wp_comment_query')) or ! empty($options['cpt']) and ! in_array($options['cpt'][0], $unsupported_post_types) ) ? true : false; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Clear associations with posts |
| 306 | * @return PMXE_Import_Record |
| 307 | * @chainable |
| 308 | */ |
| 309 | public function deletePosts() { |
| 310 | $post = new PMXE_Post_List(); |
| 311 | $this->wpdb->query($this->wpdb->prepare('DELETE FROM ' . $post->getTable() . ' WHERE export_id = %s', $this->id)); |
| 312 | return $this; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Delete associated sub exports |
| 317 | * @return PMXE_Export_Record |
| 318 | * @chainable |
| 319 | */ |
| 320 | public function deleteChildren(){ |
| 321 | $exportList = new PMXE_Export_List(); |
| 322 | foreach ($exportList->getBy('parent_id', $this->id)->convertRecords() as $i) { |
| 323 | $i->delete(); |
| 324 | } |
| 325 | return $this; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @see parent::delete() |
| 330 | */ |
| 331 | public function delete() { |
| 332 | $this->deletePosts()->deleteChildren(); |
| 333 | if ( ! empty($this->options['import_id']) and wp_all_export_is_compatible()){ |
| 334 | $import = new PMXI_Import_Record(); |
| 335 | $import->getById($this->options['import_id']); |
| 336 | if ( ! $import->isEmpty() and $import->parent_import_id == 99999 ){ |
| 337 | $import->delete(); |
| 338 | } |
| 339 | } |
| 340 | $export_file_path = wp_all_export_get_absolute_path($this->options['filepath']); |
| 341 | if ( @file_exists($export_file_path) ){ |
| 342 | wp_all_export_remove_source($export_file_path); |
| 343 | } |
| 344 | if ( ! empty($this->attch_id) ){ |
| 345 | wp_delete_attachment($this->attch_id, true); |
| 346 | } |
| 347 | |
| 348 | $wp_uploads = wp_upload_dir(); |
| 349 | |
| 350 | $file_for_remote_access = $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXE_Plugin::UPLOADS_DIRECTORY . DIRECTORY_SEPARATOR . md5(PMXE_Plugin::getInstance()->getOption('cron_job_key') . $this->id) . '.' . $this->options['export_to']; |
| 351 | |
| 352 | if ( @file_exists($file_for_remote_access)) @unlink($file_for_remote_access); |
| 353 | |
| 354 | return parent::delete(); |
| 355 | } |
| 356 | |
| 357 | } |
| 358 |