Skip to content

Instantly share code, notes, and snippets.

@wtmujeebu
Created October 13, 2021 07:18
Show Gist options
  • Save wtmujeebu/970b6638a05e7289995f66657b5e8596 to your computer and use it in GitHub Desktop.
Save wtmujeebu/970b6638a05e7289995f66657b5e8596 to your computer and use it in GitHub Desktop.
ReviewX review additional meta import and export
<?php //do not copy this line
// Add in exporting site
add_filter('wt_alter_product_reviews_export_csv_data', 'wt_alter_product_reviews_export_csv_data');
function wt_alter_product_reviews_export_csv_data($csv_row) {
if (isset($csv_row['meta:reviewx_attachments']) && !empty($csv_row['meta:reviewx_attachments'])) {
$images_url = array();
$review_images_ids = json_decode($csv_row['meta:reviewx_attachments'], true);
foreach ($review_images_ids['images'] as $img_id) {
$review_images = wp_get_attachment_image_src($img_id, 'full');
$images_url['images'][] = $review_images[0];
}
$csv_row['meta:reviewx_attachments'] = json_encode($images_url);
}
return $csv_row;
}
// Add in importing site
function wtier_insert_attachment_from_url($image_url) {
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
add_filter('wt_iew_importer_skip_from_evaluation', 'fn_wt_iew_importer_skip_from_evaluation');
function fn_wt_iew_importer_skip_from_evaluation($evl_arra) {
$fields = array('meta:reviewx_rating', 'meta:reviewx_attachments');
$evl_arra = array_merge($evl_arra, $fields);
return $evl_arra;
}
add_filter('wt_woocommerce_product_review_importer_pre_parse_data', 'wt_woocommerce_product_review_importer_pre_parse_data');
function wt_woocommerce_product_review_importer_pre_parse_data($csv_row) {
if (isset($csv_row['meta_mapping_fields']) && !empty($csv_row['meta_mapping_fields'])) {
if (isset($csv_row['meta_mapping_fields']['meta']['meta:reviewx_attachments'])) {
$images_ids = array();
$image_urls = json_decode($csv_row['meta_mapping_fields']['meta']['meta:reviewx_attachments'], true);
foreach ($image_urls['images'] as $img_url) {
$review_images = wtier_insert_attachment_from_url($img_url);
$images_ids['images'][] = $review_images;
}
$csv_row['meta_mapping_fields']['meta']['meta:reviewx_attachments'] = json_encode($images_ids);
}
}
return $csv_row;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment