Skip to content

Commit

Permalink
Deprecate wpcom_vip_get_page_by_path(), wpcom_vip_term_exists() and w…
Browse files Browse the repository at this point in the history
…pcom_vip_get_page_by_title()
  • Loading branch information
rebeccahum committed Dec 14, 2023
1 parent b4dad7f commit 7fb8ff8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 152 deletions.
2 changes: 1 addition & 1 deletion jetpack
Submodule jetpack updated 904 files
150 changes: 0 additions & 150 deletions vip-helpers/vip-caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
* 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
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid
// phpcs:disable WordPressVIPMinimum.Functions.RestrictedFunctions.url_to_postid_url_to_postid
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
Expand Down Expand Up @@ -80,67 +77,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 All @@ -161,92 +97,6 @@ function wpcom_vip_get_term_link( $term, $taxonomy = null ) {
return get_term_link( $term_object );
}

/**
* Cached version of get_page_by_title so that we're not making unnecessary SQL all the time
*
* @param string $page_title Page title
* @param string $output Optional. Output type; OBJECT*, ARRAY_N, or ARRAY_A.
* @param string $post_type Optional. Post type; default is 'page'.
* @return WP_Post|null WP_Post on success or null on failure
* @link https://docs.wpvip.com/technical-references/caching/uncached-functions/ Uncached Functions
*/
function wpcom_vip_get_page_by_title( $title, $output = OBJECT, $post_type = 'page' ) {
global $wp_version;
if ( version_compare( $wp_version, '6.2', '<' ) ) {
_deprecated_function( __FUNCTION__, '6.2', 'WP_Query' );
}

$cache_key = $post_type . '_' . sanitize_key( $title );
$page_id = wp_cache_get( $cache_key, 'get_page_by_title' );

if ( false === $page_id ) {
if ( ! function_exists( 'is_user_logged_in' ) ) {
// If too early to call `WP_Query`, fallback to deprecated `get_page_by_title`
// phpcs:ignore WordPress.WP.DeprecatedFunctions.get_page_by_titleFound
$page = get_page_by_title( $title, OBJECT, $post_type );
$page_id = $page ? $page->ID : 0;
} else {
// WP 6.2 deprecates `get_page_by_title` in favor of `WP_Query`
$query = new WP_Query(
array(
'title' => $title,
'post_type' => $post_type,
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'ASC',
'no_found_rows' => true,
'fields' => 'ids',
),
);
$page_id = ! empty( $query->posts ) ? $query->posts[0] : 0;
}
wp_cache_set( $cache_key, $page_id, 'get_page_by_title', 3 * HOUR_IN_SECONDS ); // We only store the ID to keep our footprint small
}

if ( $page_id ) {
return get_post( $page_id, $output );
}

return null;
}

/**
* Cached version of get_page_by_path so that we're not making unnecessary SQL all the time
*
* @param string $page_path Page path
* @param string $output Optional. Output type; OBJECT*, ARRAY_N, or ARRAY_A.
* @param string|array $post_type Optional. Post type; default is 'page'.
* @return WP_Post|null WP_Post on success or null on failure
* @link https://docs.wpvip.com/technical-references/caching/uncached-functions/ Uncached Functions
*/
function wpcom_vip_get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
global $wp_version;
if ( version_compare( $wp_version, '6.1', '<' ) ) {
_deprecated_function( __FUNCTION__, '6.1', 'get_page_by_path' );
}

// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$cache_key = md5( $page_path . serialize( $post_type ) );
$page_id = wp_cache_get( $cache_key, 'wpcom_vip_get_page_by_path' );

if ( false === $page_id ) {
$page = get_page_by_path( $page_path, $output, $post_type );
$page_id = $page ? $page->ID : 0;
if ( 0 === $page_id ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand, WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined
wp_cache_set( $cache_key, $page_id, 'wpcom_vip_get_page_by_path', ( 1 * HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) ) ); // We only store the ID to keep our footprint small
} else {
wp_cache_set( $cache_key, $page_id, 'wpcom_vip_get_page_by_path', 0 ); // We only store the ID to keep our footprint small
}
}

if ( $page_id ) {
return get_post( $page_id, $output );
}

return null;
}

/**
* Flush the cache for published pages so we don't end up with stale data
*
Expand Down
68 changes: 68 additions & 0 deletions vip-helpers/vip-deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -1307,3 +1307,71 @@ function wpcom_vip_load_geolocation_styles_only_when_needed() {
function wpcom_vip_disable_instapost() {
_deprecated_function( __FUNCTION__, '2.0.0' );
}

/**
* `get_page_by_path()` is now cached and no longer calls direct SQL.
*
* @deprecated Since WP 6.1
*
* @param string $page_path Page path
* @param string $output Optional. Output type; OBJECT*, ARRAY_N, or ARRAY_A.
* @param string|array $post_type Optional. Post type; default is 'page'.
* @return WP_Post|null WP_Post on success or null on failure
*/
function wpcom_vip_get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
_deprecated_function( __FUNCTION__, '6.1', 'get_page_by_path' );

return get_page_by_path( $page_path, $output, $post_type ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_path_get_page_by_path
}

/**
* `get_page_by_title()` is deprecated in favour of WP_Query.
*
* @deprecated Since WP 6.2
*
* @param string $page_title Page title
* @param string $output Optional. Output type; OBJECT*, ARRAY_N, or ARRAY_A.
* @param string $post_type Optional. Post type; default is 'page'.
* @return WP_Post|null WP_Post on success or null on failure
*/
function wpcom_vip_get_page_by_title( $title, $output = OBJECT, $post_type = 'page' ) {
_deprecated_function( __FUNCTION__, '6.2', 'WP_Query' );

$query = new WP_Query(
array(
'title' => $title,
'post_type' => $post_type,
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'ASC',
'no_found_rows' => true,
'fields' => 'ids',
),
);
$page_id = ! empty( $query->posts ) ? $query->posts[0] : 0;

if ( $page_id ) {
return get_post( $page_id, $output );
}

return null;
}

/**
* `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
}
2 changes: 1 addition & 1 deletion wp-parsely
Submodule wp-parsely updated 153 files

0 comments on commit 7fb8ff8

Please sign in to comment.