Skip to content

Commit

Permalink
Enhance rocket_url_to_postid function to search only in specific post…
Browse files Browse the repository at this point in the history
… statuses to make it faster (#6446)
  • Loading branch information
wordpressfan committed Mar 15, 2024
1 parent cfbfe6e commit ad5be3d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"wpackagist-plugin/amp": "^1.1.4",
"wpackagist-plugin/hummingbird-performance": "2.0.1",
"wpackagist-plugin/jetpack": "9.3.2",
"wpackagist-plugin/pdf-embedder": "^4.6",
"wpackagist-plugin/pdf-embedder": "4.6.*",
"wpackagist-plugin/simple-custom-css": "^4.0.3",
"wpackagist-plugin/spinupwp": "^1.1",
"wpackagist-plugin/woocommerce": "^8",
Expand Down
10 changes: 2 additions & 8 deletions inc/Engine/Preload/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ private function get_all_private_urls() {
* Exclude private urls.
*
* @param bool $excluded In case we want to exclude that url.
* @param string $url Current URL to test..
* @param string $url Current URL to test.
*
* @return bool Tells if it's excluded or not.
*/
Expand All @@ -550,12 +550,6 @@ public function exclude_private_url( $excluded, string $url ): bool {
return true;
}

$post_id = rocket_url_to_postid( $url );

$post = get_post( $post_id );
if ( $post ) {
return 'private' === $post->post_status;
}
return false;
return ! empty( rocket_url_to_postid( $url, [ 'private' ] ) );
}
}
15 changes: 12 additions & 3 deletions inc/functions/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ function get_rocket_sample_permalink( $id, $title = null, $name = null ) { // ph
/**
* Get the post ID from the URL.
*
* @param string $url URL of the page.
* @param string $url URL of the page.
* @param array|string[] $search_in_post_statuses Post statuses to search in.
* @return float|int Post ID.
*/
function rocket_url_to_postid( string $url ) {
function rocket_url_to_postid( string $url, array $search_in_post_statuses = [ 'publish', 'private' ] ) {
global $wp_rewrite;

/**
Expand Down Expand Up @@ -117,6 +118,9 @@ function rocket_url_to_postid( string $url ) {
if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
$id = absint( $values[2] );
if ( $id ) {
if ( empty( $search_in_post_statuses ) || ! in_array( get_post_status( $id ), $search_in_post_statuses, true ) ) {
return 0;
}
return $id;
}
}
Expand Down Expand Up @@ -147,6 +151,9 @@ function rocket_url_to_postid( string $url ) {
$page_on_front = get_option( 'page_on_front' );

if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
if ( empty( $search_in_post_statuses ) || ! in_array( get_post_status( (int) $page_on_front ), $search_in_post_statuses, true ) ) {
return 0;
}
return (int) $page_on_front;
}
}
Expand Down Expand Up @@ -239,7 +246,9 @@ function rocket_url_to_postid( string $url ) {
// Resolve conflicts between posts with numeric slugs and date archive queries.
$query = wp_resolve_numeric_slug_conflicts( $query );

$query['post_status'] = [ 'publish', 'private' ];
if ( ! empty( $search_in_post_statuses ) ) {
$query['post_status'] = $search_in_post_statuses;
}

/**
* Filters WP_Query class passed args.
Expand Down

0 comments on commit ad5be3d

Please sign in to comment.