File: //var/www/innodrive/migrations/migrate_images.php
<?php
if( php_sapi_name() !== 'cli' ) {
die("Meant to be run from command line");
}
if (!empty($argv[1])) {
define('APPLICATION_ENV', $argv[1]);
} else {
define('APPLICATION_ENV', 'production');
}
define( 'BASE_PATH', './public/' );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
require(BASE_PATH . 'wp-load.php');
require(BASE_PATH . 'wp-content/plugins/custom-search/custom-search.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Product.php');
$products = $wpdb->get_results(' SELECT * FROM products WHERE brand_id = 1');
function isImageExists($url) {
$uploadsDir = wp_get_upload_dir();
$ch = curl_init( $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $info;
}
function getRedirect($url) {
$uploadsDir = wp_get_upload_dir();
$ch = curl_init( $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function request($url) {
$uploadsDir = wp_get_upload_dir();
$ch = curl_init( $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15');
curl_setopt($ch,CURLOPT_COOKIEFILE,ROOT . $uploadsDir['basedir'] . '/maxoncookie5.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR,ROOT . $uploadsDir['basedir'] . '/maxoncookie5.txt');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
return $response;
} else {
print_r($info);
die('!');
}
}
function isExternal($url) {
return strpos($url, 'http') === 0;
}
function parseImage($product) {
$cats = ['motor', 'gear', 'control', 'sensor', 'accessory'];
$catname = $cats[$product->category_id - 1];
$url = 'https://www.maxongroup.com/maxon/view/category/prodfilter/' . $catname . '?q=' . $product->sku;
return [
$product->id, $product->sku, $product->title, Product::getMeta($product->id, 'LebenszyklusStatus'), $url
];
}
$cache = [] ;
$toUpdate = [];
$toUpdateCounter = 0;
foreach($products as $product) {
$bigImage = Product::getMeta($product->id, 'Picture URL');
echo $product->id . ': ' . $product->title . PHP_EOL;
if (isExternal($bigImage)) {
echo $bigImage . PHP_EOL;
if (!empty($cache[$bigImage])) {
Product::updateMeta($product->id, 'Picture URL', $cache[$bigImage]);
echo ' - external (saved from cache)' . PHP_EOL;
} else {
$imageHeaders = isImageExists($bigImage);
if ($imageHeaders['http_code'] == 200 && in_array($imageHeaders['content_type'], ['image/jpg', 'image/jpeg', 'image/png'])) {
$image = getRedirect($bigImage);
$storeDir = BASE_PATH . 'wp-content/uploads/images/';
$pathInfo = pathinfo($bigImage);
$storeFullPath = $storeDir . $product->id . '-' . $pathInfo['basename'];
file_put_contents($storeFullPath, $image);
$webPath = '/wp-content/uploads/images/' . $product->id . '-' . $pathInfo['basename'];
Product::updateMeta($product->id, 'Picture URL', $webPath);
echo ' - external (exists, saved)' . PHP_EOL;
$cache[$bigImage] = $webPath;
} else {
if ($imageHeaders['http_code'] == 200 && $imageHeaders['content_type'] == 'text/html; charset=utf-8') {
$content = getRedirect($bigImage);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($content);
if (in_array($mimeType, ['image/jpg', 'image/jpeg', 'image/png'])) {
$pathInfo = pathinfo($bigImage);
$storeDir = BASE_PATH . 'wp-content/uploads/images/';
$storeFullPath = $storeDir . $product->id . '-' . $pathInfo['basename'];
file_put_contents($storeFullPath, $content);
$webPath = '/wp-content/uploads/images/' . $product->id . '-' . $pathInfo['basename'];
Product::updateMeta($product->id, 'Picture URL', $webPath);
echo ' - external (exists, saved)' . PHP_EOL;
} else {
print_r($mimeType);
echo ' - external (not found)' . PHP_EOL;
$toUpdate[] = parseImage($product);
$toUpdateCounter++;
}
} else {
echo ' - external (not found)' . PHP_EOL;
print_r($imageHeaders);
$toUpdate[] = parseImage($product);
$toUpdateCounter++;
}
}
}
} else {
echo ' - iternal (' . $bigImage . ')' . PHP_EOL;
}
}
echo $toUpdateCounter . ' – to update' . PHP_EOL;
$fp = fopen('file.csv', 'w');
foreach ($toUpdate as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
echo 'DONE' . PHP_EOL . PHP_EOL;