/
var
/
tmp
/
parabrincar
/
export
/
Upload File
HOME
<?php require_once __DIR__ . '/../wp-load.php'; require_once __DIR__ . '/../wp-admin/includes/taxonomy.php'; function Generate_Featured_Image($images_url, $images_name, $post_id, $featuredImage = true) { $upload_dir = wp_upload_dir(); $toappend = ""; for($i=0;$i<sizeof($images_url);$i++){ $image_data = file_get_contents($images_url[$i]); $filename = basename($images_url[$i]); echo $filename; 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); if(empty($images_name[$i]) || !strlen($images_name[$i])) $images_name[$i] = $filename; $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($images_name[$i]), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment($attachment, $file, $post_id); // require_once('../../wp-admin/includes/image.php'); require_once('../wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata($attach_id, $file); $res1= wp_update_attachment_metadata($attach_id, $attach_data); // print_r($attach_id); if($i === 0 && $featuredImage) $res2= set_post_thumbnail($post_id, $attach_id); else { $toappend .= $attach_id.","; } } if($toappend!="") update_post_meta( $post_id, '_product_image_gallery', $toappend); return $attach_id; } function postIdByMetaKey($code, $key) { global $wpdb; $postIds = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='$key' AND meta_value='%s' ORDER BY post_id ASC", $code)); // echo $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='$key' AND meta_value='%s'", $code); if($postIds){ return $postIds; } return -1; } /* $file = fopen('export.json', 'r'); $json = fread($file, filesize('export.json')); fclose($file); $products = json_decode($json); $file = fopen('progress2.txt', 'r'); $progress = fread($file, filesize('progress2.txt')); fclose($file); $progress = explode(',', $progress); $progress2Ids = $deleteIds = ''; foreach($products as $id => $post){ // if(in_array($post->ID, $progress)) // continue; $progress2Ids .= ',' . $post->ID; $postMeta = $post->post_meta; $sku = $postMeta->_sku[0]; $posts = postIdByMetaKey($sku, '_sku'); if($posts != -1 && count($posts) > 1){ array_shift($posts); foreach($posts as $post2){ $deleteIds .= ',' . $post2->post_id; } } } $file = fopen('progress2.txt', 'a'); fwrite($file, ',' . $progress2Ids); fclose($file); $file = fopen('deleteIds.txt', 'a'); fwrite($file, ',' . $deleteIds); fclose($file); */ function wpr_prod_remove_db_by_ids($remove_cats, $ids) { global $wpdb; $where = ' AND ('; $where2 = ' AND ('; foreach($ids as $id){ $where .= 'post_id = ' . $id . ' OR '; $where2 .= 'ID = ' . $id . ' OR '; } $where = ' ' . trim($where, ' OR ' ) . ')'; $where2 = ' ' . trim($where2, ' OR ' ) . ')'; // echo $where2;exit; // Current site db prefix $pref = $wpdb->prefix; //delete all product relationships first $wpdb->query( "DELETE relations.*".( $remove_cats ? ", taxes.* , terms.* " : "" ). "FROM ".$pref."term_relationships AS relations INNER JOIN ".$pref."term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN ".$pref."terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT ID FROM ".$pref."posts WHERE post_type='product' $where2) ;" ); //keep taxonomies just fix the link count to be zero //taxonomies that need updating include product_tag, product_type and product_cat // if (!$remove_cats){ // $wpdb->query( // "UPDATE ".$pref."term_taxonomy SET count = 0 WHERE taxonomy LIKE 'product%';" // ); // } //delete product meta data $wpdb->query( "DELETE FROM ".$pref."postmeta WHERE post_id IN (SELECT ID FROM ".$pref."posts WHERE post_type = 'product' $where2);" ); //delete variation meta data // $wpdb->query( // "DELETE FROM ".$pref."postmeta WHERE post_id IN (SELECT ID FROM ".$pref."posts WHERE post_type = 'product_variation');" // ); //delete actual products $prods_count = $wpdb->query( "DELETE FROM ".$pref."posts WHERE post_type = 'product' $where2;" ); //delete product variations // $vars_count = $wpdb->query( // "DELETE FROM ".$pref."posts WHERE post_type = 'product_variation';" // ); if (is_numeric($prods_count)){ if ($prods_count == 0){ echo "No products found."; }else{ echo $prods_count." product(s) successfully removed!"; } }else{ echo 'Error removing products'; } echo '<br />'; if (is_numeric($vars_count)){ if ($vars_count > 0){ echo $vars_count." product variation(s) successfully removed!"; echo '<br />'; } } } $file = fopen('deleteIds.txt', 'r'); $deleteIds = fread($file, filesize('deleteIds.txt')); fclose($file); $deleteIds = explode(',', $deleteIds); wpr_prod_remove_db_by_ids(false, $deleteIds); ?>