Skip to content

Commit

Permalink
Deprecate wpcom_vip_term_exists() (#5100)
Browse files Browse the repository at this point in the history
* Deprecate `wpcom_vip_term_exists()`

* Add `function_exists()` check for non-atomic deploys
  • Loading branch information
rebeccahum authored Dec 14, 2023
1 parent b4dad7f commit 4e7fa74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 62 deletions.
62 changes: 0 additions & 62 deletions vip-helpers/vip-caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* This file contains a bunch of helper functions that handle add caching to core WordPress functions.
*/

// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.term_exists_term_exists
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.count_user_posts_count_user_posts
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_title_get_page_by_title
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_path_get_page_by_path
Expand Down Expand Up @@ -80,67 +79,6 @@ function wp_flush_get_term_by_cache( $term_id, $taxonomy ) {
}
}

/**
* Cached version of term_exists()
*
* Term exists calls can pile up on a single pageload.
* This function adds a layer of caching to prevent lots of queries.
*
* @param int|string $term The term to check can be id, slug or name.
* @param string $taxonomy The taxonomy name to use
* @param int $parent Optional. ID of parent term under which to confine the exists search.
* @return mixed Returns null if the term does not exist. Returns the term ID
* if no taxonomy is specified and the term ID exists. Returns
* an array of the term ID and the term taxonomy ID the taxonomy
* is specified and the pairing exists.
*/

function wpcom_vip_term_exists( $term, $taxonomy = '', $parent = null ) {
global $wp_version;
if ( version_compare( $wp_version, '6.0', '<' ) ) {
_deprecated_function( __FUNCTION__, '6.0', 'term_exists' );
}

// If $parent is not null, let's skip the cache.
if ( null !== $parent ) {
return term_exists( $term, $taxonomy, $parent );
}

if ( ! empty( $taxonomy ) ) {
$cache_key = $term . '|' . $taxonomy;
} else {
$cache_key = $term;
}

$cache_value = wp_cache_get( $cache_key, 'term_exists' );

// term_exists frequently returns null, but (happily) never false
if ( false === $cache_value ) {
$term_exists = term_exists( $term, $taxonomy );
wp_cache_set( $cache_key, $term_exists, 'term_exists', 3 * HOUR_IN_SECONDS );
} else {
$term_exists = $cache_value;
}

if ( is_wp_error( $term_exists ) ) {
$term_exists = null;
}

return $term_exists;
}

/**
* Properly clear wpcom_vip_term_exists() cache when a term is updated
*/
add_action( 'delete_term', 'wp_flush_term_exists', 10, 4 );
function wp_flush_term_exists( $term, $tt_id, $taxonomy, $deleted_term ) {
foreach ( array( 'term_id', 'name', 'slug' ) as $field ) {
$cache_key = $deleted_term->$field . '|' . $taxonomy;
$cache_group = 'term_exists';
wp_cache_delete( $cache_key, $cache_group );
}
}

/**
* Optimized version of get_term_link that adds caching for slug-based lookups.
*
Expand Down
21 changes: 21 additions & 0 deletions vip-helpers/vip-deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -1307,3 +1307,24 @@ function wpcom_vip_load_geolocation_styles_only_when_needed() {
function wpcom_vip_disable_instapost() {
_deprecated_function( __FUNCTION__, '2.0.0' );
}

if ( ! function_exists( 'wpcom_vip_term_exists' ) ) {
/**
* `term_exists()` now uses `get_terms()` and is cached.
*
* @deprecated Since WP 6.0
*
* @param int|string $term The term to check can be id, slug or name.
* @param string $taxonomy The taxonomy name to use
* @param int $parent Optional. ID of parent term under which to confine the exists search.
* @return mixed Returns null if the term does not exist. Returns the term ID
* if no taxonomy is specified and the term ID exists. Returns
* an array of the term ID and the term taxonomy ID the taxonomy
* is specified and the pairing exists.
*/
function wpcom_vip_term_exists( $term, $taxonomy = '', $parent = null ) {
_deprecated_function( __FUNCTION__, '6.0', 'term_exists' );

return term_exists( $term, $taxonomy, $parent ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.term_exists_term_exists
}
}

0 comments on commit 4e7fa74

Please sign in to comment.