Last active
August 29, 2015 14:20
-
-
Save mattheu/d821b76c4824fcfce6f5 to your computer and use it in GitHub Desktop.
WordPress Split Term Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Split Shared Term Checker. | |
* Plugin URI: https://gist.github.com/mattheu/d821b76c4824fcfce6f5 | |
* Description: Check for shared terms and track when they are split. Find in the tools sub menu. | |
* Version: 0.0.1 | |
* Author: Matthew Haines-Young | |
* Author URI: http://matth.eu | |
* License: GPL2 | |
*/ | |
namespace Matts_Split_Terms_Info; | |
add_action( 'split_shared_term', function( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { | |
$data = (array) json_decode( get_option( 'mph_split_shared_terms', '[]' ) ); | |
array_push( $data, (object) array( | |
'old_term_id' => $old_term_id, | |
'new_term_id' => $new_term_id, | |
'term_taxonomy_id' => $term_taxonomy_id, | |
'taxonomy' => $taxonomy, | |
) ); | |
update_option( 'mph_split_shared_terms', json_encode( $data ) ); | |
}, 10, 4 ); | |
add_action('admin_menu', function() { | |
add_submenu_page( 'tools.php', 'Split Shared Terms', 'Split Shared Terms', 'read', 'mph-split-shared-terms', __NAMESPACE__ . '\mph_split_shared_terms_page' ); | |
} ); | |
function mph_split_shared_terms_page() { | |
echo '<div class="wrap">'; | |
// Show all terms that have been split. | |
printf( '<h2>%s</h2>', esc_html__( 'Split Terms', 'mph_split_shared_terms' ) ); | |
printf( '<p class="description">%s</p>', 'All terms that have been split whilst this plugin is active.' ); | |
print_table( get_split_terms() ); | |
printf( '<h2>%s</h2>', esc_html__( 'Current Shared Terms', 'mph_split_shared_terms' ) ); | |
printf( '<p class="description">%s</p>', 'All terms that share data. They will be split when they are next updated. This may cause issues if you are using the <code>term_id</code> anywhere, either in templates or storing them in the database.' ); | |
print_table( get_terms_to_split() ); | |
echo '</div>'; | |
} | |
/** | |
* Output an HTML table of results. | |
* Columns - One for each header. | |
* If headers are not provided - all properties | |
* from the first item are used. | |
* | |
* @param array $data Array of objects. | |
* @return [type] [description] | |
*/ | |
function print_table( $data, $headers = null ) { | |
if ( ! $headers ) { | |
$headers = array_keys( get_object_vars( reset( $data ) ) ); | |
} | |
add_action( 'admin_footer', __NAMESPACE__ . '\table_styles' ); | |
echo '<table class="mph-results-table">'; | |
echo '<thead>'; | |
echo '<tr>'; | |
foreach ( $headers as $header ) { | |
printf( '<th>%s</th>', esc_html( $header ) ); | |
} | |
echo '</tr>'; | |
echo '</thead>'; | |
echo '<tbody>'; | |
foreach ( $data as $row ) { | |
echo '<tr>'; | |
foreach ( $headers as $header ) { | |
if ( isset( $row->$header ) ) { | |
printf( '<td>%s</td>', wp_kses_post( $row->$header ) ); | |
} | |
} | |
echo '</tr>'; | |
} | |
echo '</tbody>'; | |
echo '</table>'; | |
} | |
function table_styles() { | |
?> | |
<style> | |
.mph-results-table { | |
border: 1px solid #CCC; | |
border-collapse: collapse; | |
margin: 15px 0; | |
} | |
.mph-results-table td, | |
.mph-results-table th { | |
border: 1px solid #CCC; | |
padding: 8px; | |
text-align: left; | |
vertical-align: top; | |
background: #FFF; | |
} | |
.mph-results-table th { | |
background: #FFF; | |
} | |
.mph-results-table tbody tr:nth-child(odd) td { | |
background-color: #EEE; | |
} | |
</style> | |
<?php | |
} | |
/** | |
* Get all terms that have been split. | |
* Note - only returns terms split whilst this plugin is active. | |
* | |
* @return array split terms. | |
*/ | |
function get_split_terms() { | |
return (array) json_decode( get_option( 'mph_split_shared_terms', '[]' ) ); | |
} | |
/** | |
* Get all terms that are still to be split. | |
* | |
* @return null | |
*/ | |
function get_terms_to_split() { | |
global $wpdb; | |
/** | |
* Get all terms that share a term_id. | |
*/ | |
$query = "SELECT name, $wpdb->term_taxonomy.term_id, COUNT(*)"; | |
$query .= "FROM $wpdb->term_taxonomy "; | |
$query .= "INNER JOIN $wpdb->terms "; | |
$query .= "ON $wpdb->term_taxonomy.term_id=$wpdb->terms.term_id "; | |
$query .= "WHERE $wpdb->term_taxonomy.term_id != '' AND $wpdb->term_taxonomy.term_id IS NOT NULL "; | |
$query .= "GROUP BY $wpdb->term_taxonomy.term_id ASC "; | |
$query .= "HAVING COUNT(*) >= 2 "; | |
$results = $wpdb->get_results( $query ); | |
/** | |
* Format the Results. | |
*/ | |
$results = array_map( function( $result ) { | |
global $wpdb; | |
// Get the Taxonomies and Term Taxonomy IDs for terms with the same term_id. | |
$query = $wpdb->get_results( $wpdb->prepare( "SELECT taxonomy, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id=%d", $result->term_id ) ); | |
// Format results. | |
$terms = array_map( function( $term ) { | |
return sprintf( '%s (%d)', $term->taxonomy, $term->term_taxonomy_id ); | |
}, $query ); | |
// Return Formatted Object. | |
return (object) array( | |
'Term (term_id)' => sprintf( '%s (%d)', $result->name, $result->term_id ), | |
'Taxonomies (term_taxonomy_id)' => implode( ", <br/>", $terms ) | |
); | |
}, $results ); | |
return $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment