SHARE
TWEET

WPEC 3.7.8 - Fix product permalinks (url_name)

PepperHorn Jun 6th, 2012 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Template Name: FixProdLinks
  4.  */
  5. /* PLEASE USE THIS AT YOUR OWN RISK __ PUT IT ON A PRIVATE PAGE AND UNPUBLISH IT AFTER USE JUST TO BE SAFE!!!! */
  6. get_header(); ?>
  7.  
  8. <?php
  9. echo "you found me!";
  10. /* adds nice names for permalinks for products */
  11.  
  12.         function wpsc_recreate_product_url_names() {
  13.   global $wpdb;
  14.         $product_data = $wpdb->get_results("SELECT `id`, `name` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `active` IN ('1')", ARRAY_A);
  15.         //echo "<pre>".print_r($product_data,true)."</pre>";
  16.         echo "<pre>";
  17.         foreach($product_data as $product_row) {
  18.                 $product_id = $product_row['id'];
  19.                 $tidied_name = trim($product_row['name']);
  20.                 $tidied_name = strtolower($tidied_name);
  21.                 $url_name = sanitize_title($tidied_name);
  22.  
  23.                 echo "<strong>Product {$product_id}:</strong> {$product_row['name']}\n";
  24.  
  25.                 echo "Originally Proposed Name: {$url_name}\n";
  26.                 $similar_names = (array)$wpdb->get_col("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `product_id` NOT IN('{$product_id}}') AND `meta_key` IN ('url_name') AND `meta_value` REGEXP '^(".$wpdb->escape(preg_quote($url_name))."){1}[[:digit:]]*$' ");
  27.  
  28.                 if(array_search($url_name, $similar_names) !== false) {
  29.                         // If it is, try to add a number to the end, if that is taken, try the next highest number...
  30.                         $i = 0;
  31.                         do {
  32.                                 $i++;
  33.                                 if($i > 100) {
  34.                                         break;
  35.                                 }
  36.                                 echo "Proposed Name No.$i: ".($url_name.$i)."\n";
  37.                         } while(array_search(($url_name.$i), $similar_names) !== false);
  38.                         // Concatenate the first number found that wasn't taken
  39.                         $url_name .= $i;
  40.                 }
  41.  
  42.                 echo "Accepted Name: {$url_name}\n";
  43.                 $existing_name = get_product_meta($product_id, 'url_name', true);
  44.                 if(is_array($existing_name )) {
  45.                         $existing_name = array_pop($existing_name);
  46.                 }
  47.                 if($existing_name != $url_name) {
  48.                         update_product_meta($product_id, 'url_name', $url_name);
  49.                 }
  50.  
  51.                 echo "\n\n\n";
  52.         }
  53.  
  54. }
  55. wpsc_recreate_product_url_names();
  56. echo "That's it -- this should be the last of the products -- huzzah!";
  57. ?>
  58. <?php get_footer(); ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top