$lock_name = 'term_split.lock';
// Try to lock.
$lock_result =
$wpdb->
query( $wpdb->
prepare( "INSERT IGNORE INTO `
$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */",
$lock_name,
time() ) );
if ( !
$lock_result ) { $lock_result =
get_option( $lock_name );
// Bail if we were unable to create a lock, or if the existing lock is still valid.
if ( !
$lock_result || ( $lock_result >
( time() - HOUR_IN_SECONDS
) ) ) { wp_schedule_single_event( time() +
( 5 * MINUTE_IN_SECONDS
), 'wp_split_shared_term_batch'
);
return;
} } // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
update_option( $lock_name,
time() );
// Get a list of shared terms (those with more than one associated row in term_taxonomy).
$shared_terms =
$wpdb->
get_results( "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {
$wpdb->term_taxonomy
} tt
LEFT JOIN {
$wpdb->terms
} t ON t.term_id = tt.term_id
GROUP BY t.term_id
HAVING term_tt_count > 1
LIMIT 10"