/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/frontend/class-opengraph-image.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Frontend
 */

/**
 * Class WPSEO_OpenGraph_Image.
 */
class WPSEO_OpenGraph_Image {

    
/**
     * @var string
     */
    
const EXTERNAL_IMAGE_ID '-1';

    
/**
     * Holds the images that have been put out as OG image.
     *
     * @var array
     */
    
protected $images = array();

    
/**
     * Holds the WPSEO_OpenGraph instance, so we can call og_tag.
     *
     * @var WPSEO_OpenGraph
     */
    
private $opengraph;

    
/**
     * Image tags that we output for each image.
     *
     * @var array
     */
    
private $image_tags = array(
        
'width'     => 'width',
        
'height'    => 'height',
        
'mime-type' => 'type',
    );

    
/**
     * The parameters we have for Facebook images.
     *
     * @var array
     */
    
private $image_params = array(
        
'min_width'  => 200,
        
'max_width'  => 2000,
        
'min_height' => 200,
        
'max_height' => 2000,
    );

    
/**
     * Image types that are supported by OpenGraph.
     *
     * @var array
     */
    
private $valid_image_types = array( 'image/jpeg''image/gif''image/png' );

    
/**
     * Image extensions that are supported by OpenGraph.
     *
     * @var array
     */
    
private $valid_image_extensions = array( 'jpeg''jpg''gif''png' );

    
/**
     * Constructor.
     *
     * @param null|string     $image     Optional. The Image to use.
     * @param WPSEO_OpenGraph $opengraph Optional. The OpenGraph object.
     */
    
public function __construct$image nullWPSEO_OpenGraph $opengraph null ) {
        if ( 
$opengraph === null ) {
            global 
$wpseo_og;

            
// Use the global if available.
            
if ( empty( $wpseo_og ) ) {
                
$wpseo_og = new WPSEO_OpenGraph();
            }

            
$opengraph $wpseo_og;
        }

        
$this->opengraph $opengraph;

        if ( ! empty( 
$image ) && is_string$image ) ) {
            
$this->add_image_by_url$image );
        }

        if ( ! 
post_password_required() ) {
            
$this->set_images();
        }
    }

    
/**
     * Outputs the images.
     *
     * @return void
     */
    
public function show() {
        foreach ( 
$this->get_images() as $image => $image_meta ) {
            
$this->og_image_tag$image );
            
$this->show_image_meta$image_meta );
        }
    }

    
/**
     * Output the image metadata.
     *
     * @param array $image_meta Image meta data to output.
     *
     * @return void
     */
    
private function show_image_meta$image_meta ) {
        foreach ( 
$this->image_tags as $key => $value ) {
            if ( ! empty( 
$image_meta$key ] ) ) {
                
$this->opengraph->og_tag'og:image:' $key$image_meta$key ] );
            }
        }
    }

    
/**
     * Outputs an image tag based on whether it's https or not.
     *
     * @param string $image_url The image URL.
     *
     * @return void
     */
    
private function og_image_tag$image_url ) {
        
$this->opengraph->og_tag'og:image'esc_url$image_url ) );

        
// Add secure URL if detected. Not all services implement this, so the regular one also needs to be rendered.
        
if ( strpos$image_url'https://' ) === ) {
            
$this->opengraph->og_tag'og:image:secure_url'esc_url$image_url ) );
        }
    }

    
/**
     * Return the images array.
     *
     * @return array The images.
     */
    
public function get_images() {
        return 
$this->images;
    }

    
/**
     * Check whether we have images or not.
     *
     * @return bool True if we have images, false if we don't.
     */
    
public function has_images() {
        return ! empty( 
$this->images );
    }

    
/**
     * Display an OpenGraph image tag.
     *
     * @param string|array $attachment Attachment array.
     *
     * @return void
     */
    
public function add_image$attachment ) {
        
// In the past `add_image` accepted an image url, so leave this for backwards compatibility.
        
if ( is_string$attachment ) ) {
            
$attachment = array( 'url' => $attachment );
        }

        if ( ! 
is_array$attachment ) || empty( $attachment['url'] ) ) {
            return;
        }

        
// If the URL ends in `.svg`, we need to return.
        
if ( ! $this->is_valid_image_url$attachment['url'] ) ) {
            return;
        }

        
/**
         * Filter: 'wpseo_opengraph_image' - Allow changing the OpenGraph image.
         *
         * @api string - The URL of the OpenGraph image.
         */
        
$image_url trimapply_filters'wpseo_opengraph_image'$attachment['url'] ) );
        if ( empty( 
$image_url ) ) {
            return;
        }

        if ( 
WPSEO_Utils::is_url_relative$image_url ) === true ) {
            
$image_url WPSEO_Image_Utils::get_relative_path$image_url );
        }

        if ( 
array_key_exists$image_url$this->images ) ) {
            return;
        }

        
$this->images$image_url ] = $attachment;
    }

    
/**
     * Adds an image by ID if possible and by URL if the ID isn't present.
     *
     * @param string   $image_id   The image ID as set in the database.
     * @param string   $image_url  The saved URL for the image.
     * @param callable $on_save_id Function to call to save the ID if it needs to be saved.
     *
     * @return void
     */
    
private function add_image_by_id_or_url$image_id$image_url$on_save_id ) {
        switch ( 
$image_id ) {
            case 
self::EXTERNAL_IMAGE_ID:
                
// Add image by URL, but skip attachment_to_id call. We already know this is an external image.
                
$this->add_image( array( 'url' => $image_url ) );
                break;

            case 
'':
                
// Add image by URL, try to save the ID afterwards. So we can use the ID the next time.
                
$attachment_id $this->add_image_by_url$image_url );

                if ( 
$attachment_id !== null ) {
                    
call_user_func$on_save_id$attachment_id );
                }
                break;

            default:
                
// Add the image by ID. This is our ideal scenario.
                
$this->add_image_by_id$image_id );
                break;
        }
    }

    
/**
     * Saves the ID to the frontpage Open Graph image ID.
     *
     * @param string $attachment_id The ID to save.
     *
     * @return void
     */
    
private function save_frontpage_image_id$attachment_id ) {
        
WPSEO_Options::set'og_frontpage_image_id'$attachment_id );
    }

    
/**
     * If the frontpage image exists, call add_image.
     *
     * @return void
     */
    
private function set_front_page_image() {
        if ( 
get_option'show_on_front' ) === 'page' ) {
            
$this->set_user_defined_image();

            
// Don't fall back to the frontpage image below, as that's not set for this situation, so we should fall back to the default image.
            
return;
        }


        
$frontpage_image_url WPSEO_Options::get'og_frontpage_image' );
        
$frontpage_image_id  WPSEO_Options::get'og_frontpage_image_id' );

        
$this->add_image_by_id_or_url$frontpage_image_id$frontpage_image_url, array( $this'save_frontpage_image_id' ) );
    }

    
/**
     * Get the images of the posts page.
     *
     * @return void
     */
    
private function set_posts_page_image() {
        
$post_id get_option'page_for_posts' );

        
$this->set_image_post_meta$post_id );

        if ( 
$this->has_images() ) {
            return;
        }

        
$this->set_featured_image$post_id );
    }

    
/**
     * Get the images of the singular post.
     *
     * @param null|int $post_id The post id to get the images for.
     *
     * @return void
     */
    
private function set_singular_image$post_id null ) {
        if ( 
$post_id === null ) {
            
$post_id WPSEO_Frontend_Page_Type::get_simple_page_id();
        }

        
$this->set_user_defined_image$post_id );

        if ( 
$this->has_images() ) {
            return;
        }

        
$this->add_first_usable_content_image$post_id );
    }

    
/**
     * Gets the user-defined image of the post.
     *
     * @param null|int $post_id The post id to get the images for.
     *
     * @return void
     */
    
private function set_user_defined_image$post_id null ) {
        if ( 
$post_id === null ) {
            
$post_id WPSEO_Frontend_Page_Type::get_simple_page_id();
        }

        
$this->set_image_post_meta$post_id );

        if ( 
$this->has_images() ) {
            return;
        }

        
$this->set_featured_image$post_id );
    }

    
/**
     * Saves the default image ID for Open Graph images to the database.
     *
     * @param string $attachment_id The ID to save.
     *
     * @return void
     */
    
private function save_default_image_id$attachment_id ) {
        
WPSEO_Options::set'og_default_image_id'$attachment_id );
    }

    
/**
     * Get default image and call add_image.
     *
     * @return void
     */
    
private function maybe_set_default_image() {
        if ( 
$this->has_images() ) {
            return;
        }

        
$default_image_url WPSEO_Options::get'og_default_image''' );
        
$default_image_id  WPSEO_Options::get'og_default_image_id''' );

        if ( 
$default_image_url === '' && $default_image_id === '' ) {
            return;
        }

        
$this->add_image_by_id_or_url$default_image_id$default_image_url, array( $this'save_default_image_id' ) );
    }

    
/**
     * Saves the Open Graph image meta to the database for the current post.
     *
     * @param string $attachment_id The ID to save.
     *
     * @return void
     */
    
private function save_opengraph_image_id_meta$attachment_id ) {
        
$post_id WPSEO_Frontend_Page_Type::get_simple_page_id();

        
WPSEO_Meta::set_value'opengraph-image-id', (string) $attachment_id$post_id );
    }

    
/**
     * If opengraph-image is set, call add_image and return true.
     *
     * @param int $post_id Optional post ID to use.
     *
     * @return void
     */
    
private function set_image_post_meta$post_id ) {
        
$image_id  WPSEO_Meta::get_value'opengraph-image-id'$post_id );
        
$image_url WPSEO_Meta::get_value'opengraph-image'$post_id );

        
$this->add_image_by_id_or_url$image_id$image_url, array( $this'save_opengraph_image_id_meta' ) );
    }

    
/**
     * Check if taxonomy has an image and add this image.
     *
     * @return void
     */
    
private function set_taxonomy_image() {
        
$image_url WPSEO_Taxonomy_Meta::get_meta_without_term'opengraph-image' );
        
$this->add_image_by_url$image_url );
    }

    
/**
     * If there is a featured image, check image size. If image size is correct, call add_image and return true.
     *
     * @param int $post_id The post ID.
     *
     * @return void
     */
    
private function set_featured_image$post_id ) {
        if ( 
has_post_thumbnail$post_id ) ) {
            
$attachment_id get_post_thumbnail_id$post_id );
            
$this->add_image_by_id$attachment_id );
        }
    }

    
/**
     * If this is an attachment page, call add_image with the attachment.
     *
     * @return void
     */
    
private function set_attachment_page_image() {
        
$post_id WPSEO_Frontend_Page_Type::get_simple_page_id();
        if ( 
wp_attachment_is_image$post_id ) ) {
            
$this->add_image_by_id$post_id );
        }
    }

    
/**
     * Adds an image based on a given URL, and attempts to be smart about it.
     *
     * @param string $url The given URL.
     *
     * @return null|number Returns the found attachment ID if it exists. Otherwise -1.
     *                     If the URL is empty we return null.
     */
    
public function add_image_by_url$url ) {
        if ( empty( 
$url ) ) {
            return 
null;
        }

        
$attachment_id WPSEO_Image_Utils::get_attachment_by_url$url );

        if ( 
$attachment_id ) {
            
$this->add_image_by_id$attachment_id );
            return 
$attachment_id;
        }

        
$this->add_image( array( 'url' => $url ) );

        return -
1;
    }

    
/**
     * Returns the overridden image size if it has been overridden.
     *
     * @return null|string The overridden image size or null.
     */
    
protected function get_overridden_image_size() {
        
/**
         * Filter: 'wpseo_opengraph_image_size' - Allow overriding the image size used
         * for OpenGraph sharing. If this filter is used, the defined size will always be
         * used for the og:image. The image will still be rejected if it is too small.
         *
         * Only use this filter if you manually want to determine the best image size
         * for the `og:image` tag.
         *
         * Use the `wpseo_image_sizes` filter if you want to use our logic. That filter
         * can be used to add an image size that needs to be taken into consideration
         * within our own logic.
         *
         * @api string $size Size string.
         */
        
return apply_filters'wpseo_opengraph_image_size'null );
    }

    
/**
     * Determines if the OpenGraph image size should overridden.
     *
     * @return bool Whether the size should be overridden.
     */
    
protected function is_size_overridden() {
        return 
$this->get_overridden_image_size() !== null;
    }

    
/**
     * Adds the possibility to short-circuit all the optimal variation logic with
     * your own size.
     *
     * @param int $attachment_id The attachment ID that is used.
     *
     * @return void
     */
    
protected function get_overridden_image$attachment_id ) {
        
$attachment WPSEO_Image_Utils::get_image$attachment_id$this->get_overridden_image_size() );

        if ( 
$attachment ) {
            
$this->add_image$attachment );
        }
    }

    
/**
     * Adds an image to the list by attachment ID.
     *
     * @param int $attachment_id The attachment ID to add.
     *
     * @return void
     */
    
public function add_image_by_id$attachment_id ) {
        if ( ! 
$this->is_valid_attachment$attachment_id ) ) {
            return;
        }

        if ( 
$this->is_size_overridden() ) {
            
$this->get_overridden_image$attachment_id );
            return;
        }

        
$variations WPSEO_Image_Utils::get_variations$attachment_id );
        
$variations WPSEO_Image_Utils::filter_usable_dimensions$this->image_params$variations );
        
$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;
        }

        
// The variations are ordered so the first variations is by definition the best one.
        
$attachment $variations[0];

        if ( 
$attachment ) {
            
$this->add_image$attachment );
        }
    }

    
/**
     * Sets the images based on the page type.
     *
     * @return void
     */
    
protected function set_images() {
        
/**
         * Filter: wpseo_add_opengraph_images - Allow developers to add images to the OpenGraph tags.
         *
         * @api WPSEO_OpenGraph_Image The current object.
         */
        
do_action'wpseo_add_opengraph_images'$this );

        switch ( 
true ) {
            case 
is_front_page():
                
$this->set_front_page_image();
                break;
            case 
is_home():
                
$this->set_posts_page_image();
                break;
            case 
is_attachment():
                
$this->set_attachment_page_image();
                break;
            case 
WPSEO_Frontend_Page_Type::is_simple_page():
                
$this->set_singular_image();
                break;
            case 
is_category():
            case 
is_tag():
            case 
is_tax():
                
$this->set_taxonomy_image();
        }

        
/**
         * Filter: wpseo_add_opengraph_additional_images - Allows to add additional images to the OpenGraph tags.
         *
         * @api WPSEO_OpenGraph_Image The current object.
         */
        
do_action'wpseo_add_opengraph_additional_images'$this );

        
$this->maybe_set_default_image();
    }

    
/**
     * 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.
     */
    
protected function is_valid_attachment$attachment_id ) {
        
$attachment get_post_mime_type$attachment_id );

        if ( 
$attachment === false ) {
            return 
false;
        }

        return 
$this->is_valid_image_type$attachment );
    }

    
/**
     * 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.
     */
    
protected function is_valid_image_type$mime_type ) {
        return 
in_array$mime_type$this->valid_image_typestrue );
    }

    
/**
     * Determines whether the passed URL is considered valid.
     *
     * @param string $url The URL to check.
     *
     * @return bool Whether or not the URL is a valid image.
     */
    
protected function is_valid_image_url$url ) {
        if ( ! 
is_string$url ) ) {
            return 
false;
        }

        
$image_extension $this->get_extension_from_url$url );

        
$is_valid in_array$image_extension$this->valid_image_extensionstrue );

        
/**
         * Filter: 'wpseo_opengraph_is_valid_image_url' - Allows extra validation for an image url.
         *
         * @api bool - Current validation result.
         *
         * @param string $url The image url to validate.
         */
        
return apply_filters'wpseo_opengraph_is_valid_image_url'$is_valid$url );
    }

    
/**
     * Gets the image path from the passed URL.
     *
     * @param string $url The URL to get the path from.
     *
     * @return string The path of the image URL. Returns an empty string if URL parsing fails.
     */
    
protected function get_image_url_path$url ) {
        return (string) 
wp_parse_url$urlPHP_URL_PATH );
    }

    
/**
     * Determines the file extension of the passed URL.
     *
     * @param string $url The URL.
     *
     * @return string The extension.
     */
    
protected function get_extension_from_url$url ) {
        
$extension '';
        
$path      $this->get_image_url_path$url );

        if ( 
$path === '' ) {
            return 
$extension;
        }

        
$parts explode'.'$path );

        if ( ! empty( 
$parts ) ) {
            
$extension end$parts );
        }

        return 
$extension;
    }

    
/**
     * Adds the first usable attachment image from the post content.
     *
     * @param int $post_id The post id.
     *
     * @return void
     */
    
private function add_first_usable_content_image$post_id ) {
        
$image_url WPSEO_Image_Utils::get_first_usable_content_image_for_post$post_id );

        if ( 
$image_url === null || empty( $image_url ) ) {
            return;
        }

        
$this->add_image( array( 'url' => $image_url ) );
    }
}