class-arr.php
2 years ago
class-attachment.php
2 years ago
class-conditional.php
2 years ago
class-db.php
2 years ago
class-html.php
2 years ago
class-param.php
2 years ago
class-str.php
2 years ago
class-url.php
2 years ago
class-wordpress.php
2 years ago
index.php
2 years ago
class-attachment.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Attachment helpers. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Helpers |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Helpers; |
| 12 | |
| 13 | /** |
| 14 | * Attachment class. |
| 15 | */ |
| 16 | class Attachment { |
| 17 | |
| 18 | /** |
| 19 | * Grabs an image alt text. |
| 20 | * |
| 21 | * @param int $attachment_id The attachment ID. |
| 22 | * |
| 23 | * @return string The image alt text. |
| 24 | */ |
| 25 | public static function get_alt_tag( $attachment_id ) { |
| 26 | return (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the relative path of the image. |
| 31 | * |
| 32 | * @codeCoverageIgnore |
| 33 | * |
| 34 | * @param string $img Image URL. |
| 35 | * |
| 36 | * @return string The expanded image URL. |
| 37 | */ |
| 38 | public static function get_relative_path( $img ) { |
| 39 | if ( '/' !== $img[0] ) { |
| 40 | return $img; |
| 41 | } |
| 42 | |
| 43 | // If it's a relative URL, it's relative to the domain, not necessarily to the WordPress install, we |
| 44 | // want to preserve domain name and URL scheme (http / https) though. |
| 45 | $parsed_url = wp_parse_url( home_url() ); |
| 46 | |
| 47 | return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Find an attachment ID for a given URL. |
| 52 | * |
| 53 | * @codeCoverageIgnore |
| 54 | * |
| 55 | * @param string $url The URL to find the attachment for. |
| 56 | * |
| 57 | * @return int The found attachment ID, or 0 if none was found. |
| 58 | */ |
| 59 | public static function get_by_url( $url ) { |
| 60 | // Because get_by_url won't work on resized versions of images, we strip out the size part of an image URL. |
| 61 | $url = preg_replace( '/(.*)-\d+x\d+\.(jpg|png|gif)$/', '$1.$2', $url ); |
| 62 | |
| 63 | $id = function_exists( 'wpcom_vip_attachment_url_to_postid' ) ? wpcom_vip_attachment_url_to_postid( $url ) : self::url_to_postid( $url ); |
| 64 | |
| 65 | return absint( $id ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Implements the attachment_url_to_postid with use of WP Cache. |
| 70 | * |
| 71 | * @codeCoverageIgnore |
| 72 | * |
| 73 | * @link https://dotlayer.com/20-wordpress-core-functions-that-dont-scale-and-how-to-work-around-it/ |
| 74 | * |
| 75 | * @param string $url The attachment URL for which we want to know the Post ID. |
| 76 | * |
| 77 | * @return int The Post ID belonging to the attachment, 0 if not found. |
| 78 | */ |
| 79 | private static function url_to_postid( $url ) { |
| 80 | $cache_key = sprintf( 'mythemeshop_attachment_url_post_id_%s', md5( $url ) ); |
| 81 | |
| 82 | // Set the ID based on the hashed url in the cache. |
| 83 | $id = wp_cache_get( $cache_key ); |
| 84 | |
| 85 | if ( 'not_found' === $id ) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | // ID is found in cache, return. |
| 90 | if ( false !== $id ) { |
| 91 | return $id; |
| 92 | } |
| 93 | |
| 94 | // phpcs:ignore WordPress.VIP.RestrictedFunctions -- We use the WP COM version if we can, see above. |
| 95 | $id = attachment_url_to_postid( $url ); |
| 96 | |
| 97 | if ( empty( $id ) ) { |
| 98 | wp_cache_set( $cache_key, 'not_found', 'default', ( 12 * HOUR_IN_SECONDS + mt_rand( 0, ( 4 * HOUR_IN_SECONDS ) ) ) ); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | // We have the Post ID, but it's not in the cache yet. We do that here and return. |
| 103 | wp_cache_set( $cache_key, $id, 'default', ( 24 * HOUR_IN_SECONDS + mt_rand( 0, ( 12 * HOUR_IN_SECONDS ) ) ) ); |
| 104 | |
| 105 | return $id; |
| 106 | } |
| 107 | } |
| 108 |