• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / _wp_batch_split_terms() – Splits a batch of shared taxonomy terms.

You appear to be a bot. Output may be restricted

Description

Splits a batch of shared taxonomy terms.

Usage

_wp_batch_split_terms();

Parameters

Returns

void

Source

File name: wordpress/wp-includes/taxonomy.php


Lines:

1 to 90 of 90
function _wp_batch_split_terms() {
  global $wpdb;

  $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"
  );

  // No more terms, we're done here.
  if ( ! $shared_terms ) {
    update_option( 'finished_splitting_shared_terms', true );
    delete_option( $lock_name );
    return;
  }

  // Shared terms found? We'll need to run this script again.
  wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );

  // Rekey shared term array for faster lookups.
  $_shared_terms = array();
  foreach ( $shared_terms as $shared_term ) {
    $term_id                   = (int) $shared_term->term_id;
    $_shared_terms[ $term_id ] = $shared_term;
  }
  $shared_terms = $_shared_terms;

  // Get term taxonomy data for all shared terms.
  $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
  $shared_tts      = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );

  // Split term data recording is slow, so we do it just once, outside the loop.
  $split_term_data    = get_option( '_split_terms', array() );
  $skipped_first_term = array();
  $taxonomies         = array();
  foreach ( $shared_tts as $shared_tt ) {
    $term_id = (int) $shared_tt->term_id;

    // Don't split the first tt belonging to a given term_id.
    if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
      $skipped_first_term[ $term_id ] = 1;
      continue;
    }

    if ( ! isset( $split_term_data[ $term_id ] ) ) {
      $split_term_data[ $term_id ] = array();
    }

    // Keep track of taxonomies whose hierarchies need flushing.
    if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
      $taxonomies[ $shared_tt->taxonomy ] = 1;
    }

    // Split the term.
    $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
  }

  // Rebuild the cached hierarchy for each affected taxonomy.
  foreach ( array_keys( $taxonomies ) as $tax ) {
    delete_option( "{$tax}_children" );
    _get_term_hierarchy( $tax );
  }

  update_option( '_split_terms', $split_term_data );

  delete_option( $lock_name );
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: _wp_batch_split_terms
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
API type: private
Deprecated?: No
API Letters: _,B,S,T,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites