Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Template Name: FixProdLinks
- */
- /* PLEASE USE THIS AT YOUR OWN RISK __ PUT IT ON A PRIVATE PAGE AND UNPUBLISH IT AFTER USE JUST TO BE SAFE!!!! */
- get_header(); ?>
- <?php
- echo "you found me!";
- /* adds nice names for permalinks for products */
- function wpsc_recreate_product_url_names() {
- global $wpdb;
- $product_data = $wpdb->get_results("SELECT `id`, `name` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `active` IN ('1')", ARRAY_A);
- //echo "<pre>".print_r($product_data,true)."</pre>";
- echo "<pre>";
- foreach($product_data as $product_row) {
- $product_id = $product_row['id'];
- $tidied_name = trim($product_row['name']);
- $tidied_name = strtolower($tidied_name);
- $url_name = sanitize_title($tidied_name);
- echo "<strong>Product {$product_id}:</strong> {$product_row['name']}\n";
- echo "Originally Proposed Name: {$url_name}\n";
- $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:]]*$' ");
- if(array_search($url_name, $similar_names) !== false) {
- // If it is, try to add a number to the end, if that is taken, try the next highest number...
- $i = 0;
- do {
- $i++;
- if($i > 100) {
- break;
- }
- echo "Proposed Name No.$i: ".($url_name.$i)."\n";
- } while(array_search(($url_name.$i), $similar_names) !== false);
- // Concatenate the first number found that wasn't taken
- $url_name .= $i;
- }
- echo "Accepted Name: {$url_name}\n";
- $existing_name = get_product_meta($product_id, 'url_name', true);
- if(is_array($existing_name )) {
- $existing_name = array_pop($existing_name);
- }
- if($existing_name != $url_name) {
- update_product_meta($product_id, 'url_name', $url_name);
- }
- echo "\n\n\n";
- }
- }
- wpsc_recreate_product_url_names();
- echo "That's it -- this should be the last of the products -- huzzah!";
- ?>
- <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement