C:\xampp\htdocs\landing\wp-content\plugins\wordpress-seo\src\helpers\image-helper.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php

namespace Yoast\WP\SEO\Helpers;

use 
WPSEO_Image_Utils;
use 
Yoast\WP\SEO\Repositories\Indexable_Repository;

/**
 * A helper object for images.
 */
class Image_Helper {

    
/**
     * Image types that are supported by Open Graph.
     *
     * @var array
     */
    
protected static $valid_image_types = [ 'image/jpeg''image/gif''image/png' ];

    
/**
     * Image extensions that are supported by Open Graph.
     *
     * @var array
     */
    
protected static $valid_image_extensions = [ 'jpeg''jpg''gif''png' ];

    
/**
     * Represents the indexables repository.
     *
     * @var Indexable_Repository
     */
    
protected $indexable_repository;

    
/**
     * Image_Helper constructor.
     *
     * @param Indexable_Repository $indexable_repository The indexable repository.
     */
    
public function __constructIndexable_Repository $indexable_repository ) {
        
$this->indexable_repository $indexable_repository;
    }

    
/**
     * Determines whether or not the wanted attachment is considered valid.
     *
     * @param int $attachment_id The attachment ID to get the attachment by.
     *
     * @return bool Whether or not the attachment is valid.
     */
    
public function is_valid_attachment$attachment_id ) {
        if ( ! \
wp_attachment_is_image$attachment_id ) ) {
            return 
false;
        }

        
$post_mime_type = \get_post_mime_type$attachment_id );
        if ( 
$post_mime_type === false ) {
            return 
false;
        }

        return 
$this->is_valid_image_type$post_mime_type );
    }

    
/**
     * Checks if the given extension is a valid extension
     *
     * @param string $image_extension The image extension.
     *
     * @return bool True when valid.
     */
    
public function is_extension_valid$image_extension ) {
        return \
in_array$image_extension, static::$valid_image_extensionstrue );
    }

    
/**
     * Determines whether the passed mime type is a valid image type.
     *
     * @param string $mime_type The detected mime type.
     *
     * @return bool Whether or not the attachment is a valid image type.
     */
    
public function is_valid_image_type$mime_type ) {
        return \
in_array$mime_type, static::$valid_image_typestrue );
    }

    
/**
     * Retrieves the image source for an attachment.
     *
     * @param int    $attachment_id The attachment.
     * @param string $image_size    The image size to retrieve.
     *
     * @return string The image url or an empty string when not found.
     */
    
public function get_attachment_image_source$attachment_id$image_size 'full' ) {
        
$attachment = \wp_get_attachment_image_src$attachment_id$image_size );

        if ( ! 
$attachment ) {
            return 
'';
        }

        return 
$attachment[0];
    }

    
/**
     * Retrieves the ID of the featured image.
     *
     * @param int $post_id The post id to get featured image id for.
     *
     * @return int|bool ID when found, false when not.
     */
    
public function get_featured_image_id$post_id ) {
        if ( ! \
has_post_thumbnail$post_id ) ) {
            return 
false;
        }

        return \
get_post_thumbnail_id$post_id );
    }

    
/**
     * Gets the image url from the content.
     *
     * @param int $post_id The post id to extract the images from.
     *
     * @return string The image url or an empty string when not found.
     */
    
public function get_post_content_image$post_id ) {
        
$image_url $this->get_first_usable_content_image_for_post$post_id );

        if ( 
$image_url === null ) {
            return 
'';
        }

        return 
$image_url;
    }

    
/**
     * Gets the first image url of a gallery.
     *
     * @param int $post_id Post ID to use.
     *
     * @return string The image url or an empty string when not found.
     */
    
public function get_gallery_image$post_id ) {
        
$post = \get_post$post_id );
        if ( \
strpos$post->post_content'[gallery' ) === false ) {
            return 
'';
        }

        
$images = \get_post_gallery_images$post );
        if ( empty( 
$images ) ) {
            return 
'';
        }

        return \
reset$images );
    }

    
/**
     * Gets the image url from the term content.
     *
     * @param int $term_id The term id to extract the images from.
     *
     * @return string The image url or an empty string when not found.
     */
    
public function get_term_content_image$term_id ) {
        
$image_url $this->get_first_content_image_for_term$term_id );

        if ( 
$image_url === null ) {
            return 
'';
        }

        return 
$image_url;
    }

    
/**
     * Retrieves the caption for an attachment.
     *
     * @param int $attachment_id Attachment ID.
     *
     * @return string The caption when found, empty string when no caption is found.
     */
    
public function get_caption$attachment_id ) {
        
$caption = \wp_get_attachment_caption$attachment_id );
        if ( ! empty( 
$caption ) ) {
            return 
$caption;
        }

        
$caption = \get_post_meta$attachment_id'_wp_attachment_image_alt'true );
        if ( ! empty( 
$caption ) ) {
            return 
$caption;
        }

        return 
'';
    }

    
/**
     * Retrieves the attachment metadata.
     *
     * @param int $attachment_id Attachment ID.
     *
     * @return array The metadata, empty array when no metadata is found.
     */
    
public function get_metadata$attachment_id ) {
        
$metadata = \wp_get_attachment_metadata$attachment_id );
        if ( ! 
$metadata || ! \is_array$metadata ) ) {
            return [];
        }

        return 
$metadata;
    }

    
/**
     * Retrieves the attachment image url.
     *
     * @param int    $attachment_id Attachment ID.
     * @param string $size          The size to get.
     *
     * @return string The url when found, empty string otherwise.
     */
    
public function get_attachment_image_url$attachment_id$size ) {
        
$url = \wp_get_attachment_image_url$attachment_id$size );
        if ( ! 
$url ) {
            return 
'';
        }

        return 
$url;
    }

    
/**
     * Find the right version of an image based on size.
     *
     * @param int    $attachment_id Attachment ID.
     * @param string $size          Size name.
     *
     * @codeCoverageIgnore - We have to write test when this method contains own code.
     *
     * @return array|false Returns an array with image data on success, false on failure.
     */
    
public function get_image$attachment_id$size ) {
        return 
WPSEO_Image_Utils::get_image$attachment_id$size );
    }

    
/**
     * Retrieves the best attachment variation for the given attachment.
     *
     * @param int $attachment_id The attachment id.
     *
     * @codeCoverageIgnore - We have to write test when this method contains own code.
     *
     * @return bool|string The attachment url or false when no variations found.
     */
    
public function get_best_attachment_variation$attachment_id ) {
        
$variations WPSEO_Image_Utils::get_variations$attachment_id );
        
$variations WPSEO_Image_Utils::filter_usable_file_size$variations );

        
// If we are left without variations, there is no valid variation for this attachment.
        
if ( empty( $variations ) ) {
            return 
false;
        }

        
// The variations are ordered so the first variations is by definition the best one.
        
return \reset$variations );
    }

    
/**
     * Find an attachment ID for a given URL.
     *
     * @param string $url The URL to find the attachment for.
     *
     * @return int The found attachment ID, or 0 if none was found.
     */
    
public function get_attachment_by_url$url ) {
        
// Strip out the size part of an image URL.
        
$url = \preg_replace'/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/''$1.$2'$url );

        
// Don't try to do this for external URLs.
        
if ( \strpos$url, \get_site_url() ) !== ) {
            return 
0;
        }

        
$indexable $this->indexable_repository->find_by_permalink$url );

        if ( 
$indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) {
            return 
$indexable->object_id;
        }

        
$post_id WPSEO_Image_Utils::get_attachment_by_url$url );

        if ( 
$post_id !== ) {
            
// Find the indexable, this triggers creating it so it can be found next time.
            
$this->indexable_repository->find_by_id_and_type$post_id'post' );
        }

        return 
$post_id;
    }

    
/**
     * Retrieves an attachment ID for an image uploaded in the settings.
     *
     * Due to self::get_attachment_by_url returning 0 instead of false.
     * 0 is also a possibility when no ID is available.
     *
     * @param string $setting The setting the image is stored in.
     *
     * @codeCoverageIgnore - We have to write test when this method contains own code.
     *
     * @return int|bool The attachment id, or false or 0 if no ID is available.
     */
    
public function get_attachment_id_from_settings$setting ) {
        return 
WPSEO_Image_Utils::get_attachment_id_from_settings$setting );
    }

    
/**
     * Retrieves the first usable content image for a post.
     *
     * @param int $post_id The post id to extract the images from.
     *
     * @codeCoverageIgnore - We have to write test when this method contains own code.
     *
     * @return string|null
     */
    
protected function get_first_usable_content_image_for_post$post_id ) {
        return 
WPSEO_Image_Utils::get_first_usable_content_image_for_post$post_id );
    }

    
/**
     * Gets the term's first usable content image. Null if none is available.
     *
     * @codeCoverageIgnore - We have to write test when this method contains own code.
     *
     * @param int $term_id The term id.
     *
     * @return string|null The image URL.
     */
    
protected function get_first_content_image_for_term$term_id ) {
        return 
WPSEO_Image_Utils::get_first_content_image_for_term$term_id );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586