PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.239
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.239
1.0.273 1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / helpers / class-attachment.php
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / helpers Last commit date
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