WPSEO_Image_Utils::attachment_url_to_postid()protected staticYoast 1.0

Implements the attachment_url_to_postid with use of WP Cache.

Метод класса: WPSEO_Image_Utils{}

Хуков нет.

Возвращает

int. The Post ID belonging to the attachment, 0 if not found.

Использование

$result = WPSEO_Image_Utils::attachment_url_to_postid( $url );
$url(строка) (обязательный)
The attachment URL for which we want to know the Post ID.

Код WPSEO_Image_Utils::attachment_url_to_postid() Yoast 22.4

protected static function attachment_url_to_postid( $url ) {
	$cache_key = sprintf( 'yoast_attachment_url_post_id_%s', md5( $url ) );

	// Set the ID based on the hashed URL in the cache.
	$id = wp_cache_get( $cache_key );

	if ( $id === 'not_found' ) {
		return 0;
	}

	// ID is found in cache, return.
	if ( $id !== false ) {
		return $id;
	}

	// Note: We use the WP COM version if we can, see above.
	$id = attachment_url_to_postid( $url );

	if ( empty( $id ) ) {
		/**
		 * If no ID was found, maybe we're dealing with a scaled big image. So, let's try that.
		 *
		 * @see https://core.trac.wordpress.org/ticket/51058
		 */
		$id = self::get_scaled_image_id( $url );
	}

	if ( empty( $id ) ) {
		wp_cache_set( $cache_key, 'not_found', '', ( 12 * HOUR_IN_SECONDS + wp_rand( 0, ( 4 * HOUR_IN_SECONDS ) ) ) );
		return 0;
	}

	// We have the Post ID, but it's not in the cache yet. We do that here and return.
	wp_cache_set( $cache_key, $id, '', ( 24 * HOUR_IN_SECONDS + wp_rand( 0, ( 12 * HOUR_IN_SECONDS ) ) ) );
	return $id;
}