Rimuovi “%” dalle stringhe in array serializzato in wp_postmeta
Domanda
Nel corso della conversione di un blog da Blogger a WP e l'esecuzione di uno script per afferrare le immagini hot-linked per l'hosting, ho finito con alcuni nomi immagini funky come
act%252Bapandas-210x290.png
Questi i nomi delle immagini evitare che l'immagine da visualizzare su una pagina web, a causa della codifica URL finire nel nome del file stesso (non chiedere!). li ho rinominato sul file server, nessun prob, ma i nomi sono anche nei metadati allegato per ogni post.
Come posso rimuovere il "%" da tutti i riferimenti di immagine nella tabella wp_postmeta? * La maggior parte di essi si verificano in array serializzati in meta_values ??per le meta_keys di * _wp_attachment_metadata. Ho avuto fortuna trovare un plugin, e sono sicuri di come istituire una soluzione pura SQL / PHP.
Ecco un esempio di una voce matrice serializzato (più gommata fino dal plugin Smush.it, ugh):
a:7:{s:5:"width";s:3:"210";s:6:"height";s:3:"339";s:14:"hwstring_small";s:22:"height='96' width='59'";s:4:"file";s:27:"2011/02/act%252Bapandas.png";s:5:"sizes";a:6:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"act%252Bapandas-210x290.png";s:5:"width";s:3:"210";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png)";}s:14:"soft-thumbnail";a:4:{s:4:"file";s:27:"act%252Bapandas-179x290.png";s:5:"width";s:3:"179";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png)";}s:14:"mini-thumbnail";a:4:{s:4:"file";s:25:"act%252Bapandas-60x60.png";s:5:"width";s:2:"60";s:6:"height";s:2:"60";s:10:"wp_smushit";s:267:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-60x60.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-60x60.png)";}s:5:"slide";a:4:{s:4:"file";s:27:"act%252Bapandas-210x290.png";s:5:"width";s:3:"210";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x290.png)";}s:10:"soft-slide";a:4:{s:4:"file";s:27:"act%252Bapandas-179x290.png";s:5:"width";s:3:"179";s:6:"height";s:3:"290";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-179x290.png)";}s:10:"mini-slide";a:4:{s:4:"file";s:27:"act%252Bapandas-210x145.png";s:5:"width";s:3:"210";s:6:"height";s:3:"145";s:10:"wp_smushit";s:271:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x145.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas-210x145.png)";}}s:10:"image_meta";a:10:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";}s:10:"wp_smushit";s:255:"Smush.it error: Could not get the image while processing http://new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas.png (/home/xxxxxxxxx/new.xxxxx.com/wp-content/uploads/2011/02/act%252Bapandas.png)";}
Il problema sta cambiando o rimuovendo il carattere "%" e aggiornando la matrice in modo che riporta il numero corretto di caratteri (vale a dire i s: 13 indicherebbe yoursite.com è 13 char []) Sono aperto anche a utilizzare una soluzione PHP! Qualunque cosa può aiutarmi a risolvere questo pasticcio.
soluzione finale
Vedere la mia risposta qui sotto.
Soluzione
idea generale sarebbe quello di un ciclo tra tutti gli allegati per recuperare, modificare e scrivere di nuovo la loro meta.
Qualcosa di simile (test prima di utilizzare su qualsiasi cosa importante):
$posts = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
));
foreach( $posts as $post ) {
// retrieve data, unserialized automatically
$meta = get_post_meta($post->ID,'_wp_attachment_metadata', true);
// do stuff with $meta array
// write it back
update_post_meta($post->ID, '_wp_attachment_metadata', $meta);
}
Altri suggerimenti
$posts = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
));
// Recursive String Replace - recursive_array_replace(mixed, mixed, array);
function recursive_array_replace($find, $replace, $array){
if (!is_array($array)) {
return str_replace($find, $replace, $array);
}
$newArray = array();
foreach ($array as $key => $value) {
$newArray[$key] = recursive_array_replace($find, $replace, $value);
}
return $newArray;
}
foreach ($posts as $post) {
// retrieve data, unserialized automatically
$meta = get_post_meta($post->ID, '_wp_attachment_metadata', true);
// see if file name has "%" in it
if (strpos($meta[file], "%")) {
//recursively scrub "%"
$meta = recursive_array_replace('%', '', $meta);
}
// write it back
update_post_meta($post->ID, '_wp_attachment_metadata', $meta);
}