WordPress.org

Make WordPress Core

Ticket #18286: cf-guid-fix.php

File cf-guid-fix.php, 1.9 KB (added by alexkingorg, 3 years ago)

Proof of concept plugin to uniquify GUIDs in possts table.

Line 
1<?php
2
3/*
4Plugin Name: CF GUID Fix
5Plugin URI: http://crowdfavorite.com/wordpress/plugins/cf-guid-fix/
6Description: Correct duplicate GUID values created with versions of WordPress that exhibited a <a href="http://core.trac.wordpress.org/ticket/15041">non-unique GUID bug</a>.
7Version: 1.0
8Author: Crowd Favorite
9Author URI: http://crowdfavorite.com
10*/
11
12function cf_guid_fix_admin_init() {
13        if (isset($_GET['cf_action'])) {
14                switch ($_GET['cf_action']) {
15                        case 'guid-fix':
16                                cf_guid_fix();
17                                wp_die('Post GUIDs should be unique now.');
18                        break;
19                }
20        }
21}
22add_action('admin_init', 'cf_guid_fix_admin_init');
23
24function cf_guid_fix() {
25        global $wpdb;
26
27// add index on GUID column
28        $wpdb->query("
29                ALTER TABLE $wpdb->posts
30                ADD INDEX (guid)
31        ");
32
33// find non-unique GUID values
34
35// NOTE: not including revisions since that's a whole other mess to
36// try to get the numbering right if we change the GUID
37
38// this gets the GUIDs
39//              SELECT guid
40//              FROM $wpdb->posts
41//              WHERE post_type != 'revision'
42//              GROUP BY guid
43//              HAVING COUNT(guid) > 1
44
45// WORKS - but is very slow
46//              SELECT ID
47//              FROM $wpdb->posts
48//              WHERE guid IN (
49//                      SELECT guid
50//                      FROM $wpdb->posts
51//                      WHERE post_type != 'revision'
52//                      GROUP BY guid
53//                      HAVING COUNT(guid) > 1
54//              )
55//              AND post_type != 'revision'
56
57        $non_unique_guids = $wpdb->get_col($wpdb->prepare("
58                SELECT p1.ID
59                FROM $wpdb->posts p1
60                WHERE 1 < (
61                        SELECT COUNT(ID)
62                        FROM $wpdb->posts p2
63                        WHERE p1.post_type != 'revision'
64                        AND p1.guid = p2.guid
65                )
66                AND p1.post_type != 'revision'
67        "));
68
69// make them unique
70
71        if (count($non_unique_guids)) {
72                foreach ($non_unique_guids as $post_id) {
73                        $url = site_url('?p='.$post_id);
74                        $wpdb->query($wpdb->prepare("
75                                UPDATE $wpdb->posts
76                                SET guid = %s
77                                WHERE ID = %d
78                        ", $url, $post_id));
79                }
80        }
81
82// remove index from GUID column
83        $wpdb->query("
84                ALTER TABLE $wpdb->posts
85                DROP INDEX guid
86        ");
87
88}