| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class CF_Guid_Fix |
| 4 |
* |
| 5 |
* 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>. |
| 6 |
* |
| 7 |
* @source http://crowdfavorite.com/wordpress/plugins/cf-guid-fix/ |
| 8 |
* |
| 9 |
*/ |
| 10 |
namespace UsabilityDynamics\Utility { |
| 11 |
|
| 12 |
if( !class_exists( 'UsabilityDynamics\Utility\Guid_Fix' ) ) { |
| 13 |
|
| 14 |
class Guid_Fix { |
| 15 |
|
| 16 |
function __construct() { |
| 17 |
|
| 18 |
if( is_admin() ) { |
| 19 |
$this->add_actions(); |
| 20 |
} |
| 21 |
|
| 22 |
$this->menu_page_slug = 'cf_guid_fix'; |
| 23 |
$this->page_url = admin_url( 'tools.php?page=' . $this->menu_page_slug ); |
| 24 |
$this->errors = array(); |
| 25 |
$this->error_msgs = array( |
| 26 |
'1' => __( 'Could not create index on your posts table for the `guid` column', 'cf_guid_fix' ), |
| 27 |
'2' => __( 'Could not update a post’s GUID', 'cf_guid_fix' ), |
| 28 |
'3' => __( 'Could not remove the index from `guid`', 'cf_guid_fix' ), |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
function add_actions() { |
| 33 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 34 |
add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); |
| 35 |
add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 ); |
| 36 |
} |
| 37 |
|
| 38 |
function plugin_action_links( $links, $file ) { |
| 39 |
$plugin_file = basename( __FILE__ ); |
| 40 |
if( basename( $file ) == $plugin_file ) { |
| 41 |
$tools_link = '<a href="' . $this->page_url . '">' . __( 'Run GUID-fix', 'cf_guid_fix' ) . '</a>'; |
| 42 |
array_unshift( $links, $tools_link ); |
| 43 |
} |
| 44 |
|
| 45 |
return $links; |
| 46 |
} |
| 47 |
|
| 48 |
function register_admin_menu() { |
| 49 |
add_submenu_page( |
| 50 |
'tools.php', // parent slug |
| 51 |
'GUID Fix', // page title |
| 52 |
'GUID Fix', // menu title |
| 53 |
'manage_options', // capability |
| 54 |
$this->menu_page_slug, // menu item slug |
| 55 |
array( $this, 'output_admin_page' ) // callback function |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
function output_admin_page() { |
| 60 |
?> |
| 61 |
<div class="wrap"> |
| 62 |
<h2><?php _e( 'CF GUID-Fix', 'cf_guid_fix' ); ?></h2> |
| 63 |
<?php |
| 64 |
if( isset( $_GET[ 'success' ] ) ) { |
| 65 |
?> |
| 66 |
<div class="updated"><p><?php _e( 'Post GUIDs are now unique! <b>You should now disable and remove this plugin.</b>', 'cf_guid_fix' ); ?></p></div> |
| 67 |
<?php |
| 68 |
} else if( isset( $_GET[ 'error' ] ) ) { |
| 69 |
$error_str = $_GET[ 'error' ]; |
| 70 |
$error_nums = explode( ',', $error_str ); |
| 71 |
?> |
| 72 |
<div class="error"> |
| 73 |
<?php |
| 74 |
foreach( $error_nums as $num ) { |
| 75 |
echo '<p>' . esc_html( $this->error_msgs[ intval( $num ) ] ) . '</p>'; |
| 76 |
} |
| 77 |
?> |
| 78 |
</div> |
| 79 |
<?php |
| 80 |
} |
| 81 |
?> |
| 82 |
<p class="help"><strong><?php _e( 'What does this plugin do?', 'cf_guid_fix' ); ?></strong> <?php _e( 'Corrects 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>.', 'cf_guid_fix' ); ?></p> |
| 83 |
<p class="instructions"><?php _e( 'Clicking the button below runs the plugin to ensure your guids are unique.', 'cf_guid_fix' ); ?></p> |
| 84 |
<form name="cf_guid_fix_settings_form" action="<?php echo $this->page_url; ?>" method="post"> |
| 85 |
<input type="hidden" name="cf_action" id="cf_action" value="guid_fix"/> |
| 86 |
<button type="submit" class="button-primary"><?php _e( 'Fix Your GUIDs', 'cf_guid_fix' ); ?></button> |
| 87 |
<?php wp_nonce_field( 'cf_guid_fix_run' ); ?> |
| 88 |
</form> |
| 89 |
</div> |
| 90 |
<?php |
| 91 |
} |
| 92 |
|
| 93 |
function admin_init() { |
| 94 |
if( isset( $_POST[ 'cf_action' ] ) ) { |
| 95 |
switch( $_POST[ 'cf_action' ] ) { |
| 96 |
case 'guid_fix': |
| 97 |
// Validate request and nonce |
| 98 |
if( !check_admin_referer( 'cf_guid_fix_run' ) ) { |
| 99 |
error_log( 'Unauthorized Request for CF GUID Fix Plugin' ); |
| 100 |
wp_die( __( 'You should not be here.', 'cf_guid_fix' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
$this->fix_guids(); |
| 104 |
if( empty( $this->errors ) ) { |
| 105 |
wp_redirect( add_query_arg( array( 'success' => '' ), $this->page_url ) ); |
| 106 |
} else { |
| 107 |
wp_redirect( add_query_arg( array( 'error' => implode( ',', $this->errors ) ), $this->page_url ) ); |
| 108 |
} |
| 109 |
exit; |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private function fix_guids() { |
| 116 |
global $wpdb; |
| 117 |
|
| 118 |
// add index on GUID column |
| 119 |
$r = $wpdb->query( " |
| 120 |
ALTER TABLE $wpdb->posts |
| 121 |
ADD INDEX (guid) |
| 122 |
" ); |
| 123 |
|
| 124 |
// Error Handling |
| 125 |
if( $r === false ) { |
| 126 |
$this->add_error( 1 ); |
| 127 |
} |
| 128 |
|
| 129 |
// find non-unique GUID values |
| 130 |
|
| 131 |
// NOTE: not including revisions since that's a whole other mess to |
| 132 |
// try to get the numbering right if we change the GUID |
| 133 |
|
| 134 |
// this gets the GUIDs |
| 135 |
// SELECT guid |
| 136 |
// FROM $wpdb->posts |
| 137 |
// WHERE post_type != 'revision' |
| 138 |
// GROUP BY guid |
| 139 |
// HAVING COUNT(guid) > 1 |
| 140 |
|
| 141 |
// WORKS - but is very slow |
| 142 |
// SELECT ID |
| 143 |
// FROM $wpdb->posts |
| 144 |
// WHERE guid IN ( |
| 145 |
// SELECT guid |
| 146 |
// FROM $wpdb->posts |
| 147 |
// WHERE post_type != 'revision' |
| 148 |
// GROUP BY guid |
| 149 |
// HAVING COUNT(guid) > 1 |
| 150 |
// ) |
| 151 |
// AND post_type != 'revision' |
| 152 |
|
| 153 |
$non_unique_guids = $wpdb->get_col( $wpdb->prepare( " |
| 154 |
SELECT p1.ID |
| 155 |
FROM $wpdb->posts p1 |
| 156 |
WHERE 1 < ( |
| 157 |
SELECT COUNT(ID) |
| 158 |
FROM $wpdb->posts p2 |
| 159 |
WHERE p1.post_type != 'revision' |
| 160 |
AND p1.guid = p2.guid |
| 161 |
) |
| 162 |
AND p1.post_type != 'revision' |
| 163 |
" ) ); |
| 164 |
|
| 165 |
// make them unique |
| 166 |
if( count( $non_unique_guids ) ) { |
| 167 |
foreach( $non_unique_guids as $post_id ) { |
| 168 |
$url = site_url( '?p=' . $post_id ); |
| 169 |
$r = $wpdb->query( $wpdb->prepare( " |
| 170 |
UPDATE $wpdb->posts |
| 171 |
SET guid = %s |
| 172 |
WHERE ID = %d |
| 173 |
", $url, $post_id ) ); |
| 174 |
|
| 175 |
// Error Handling |
| 176 |
if( $r === false ) { |
| 177 |
$this->add_error( 2 ); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// remove index from GUID column |
| 183 |
$r = $wpdb->query( " |
| 184 |
ALTER TABLE $wpdb->posts |
| 185 |
DROP INDEX guid |
| 186 |
" ); |
| 187 |
|
| 188 |
// Error Handling |
| 189 |
if( $r === false ) { |
| 190 |
$this->add_error( 3 ); |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
function add_error( $error_num ) { |
| 196 |
if( !in_array( $error_num, $this->errors ) ) { |
| 197 |
$this->errors[ ] = intval( $error_num ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|