PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 All 128 releases
wordpress-seo / src / generators / twitter-image-generator.php

twitter-image-generator.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/generators/twitter-image-generator.php

103 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Generators;
4
5 use Yoast\WP\SEO\Context\Meta_Tags_Context;
6 use Yoast\WP\SEO\Helpers\Image_Helper;
7 use Yoast\WP\SEO\Helpers\Twitter\Image_Helper as Twitter_Image_Helper;
8 use Yoast\WP\SEO\Helpers\Url_Helper;
9 use Yoast\WP\SEO\Models\Indexable;
10 use Yoast\WP\SEO\Values\Twitter\Images;
11
12 /**
13 * Represents the generator class for the Twitter images.
14 */
15 class Twitter_Image_Generator implements Generator_Interface {
16
17 /**
18 * The Twitter image helper.
19 *
20 * @var Twitter_Image_Helper
21 */
22 protected $twitter_image;
23
24 /**
25 * The image helper.
26 *
27 * @var Image_Helper
28 */
29 protected $image;
30
31 /**
32 * The URL helper.
33 *
34 * @var Url_Helper
35 */
36 protected $url;
37
38 /**
39 * Twitter_Image_Generator constructor.
40 *
41 * @codeCoverageIgnore
42 *
43 * @param Image_Helper $image The image helper.
44 * @param Url_Helper $url The url helper.
45 * @param Twitter_Image_Helper $twitter_image The Twitter image helper.
46 */
47 public function __construct( Image_Helper $image, Url_Helper $url, Twitter_Image_Helper $twitter_image ) {
48 $this->image = $image;
49 $this->url = $url;
50 $this->twitter_image = $twitter_image;
51 }
52
53 /**
54 * Retrieves the images for an indexable.
55 *
56 * @param Meta_Tags_Context $context The context.
57 *
58 * @return array<array<string, string|int>> The images.
59 */
60 public function generate( Meta_Tags_Context $context ) {
61 $image_container = $this->get_image_container();
62
63 $this->add_from_indexable( $context->indexable, $image_container );
64
65 return $image_container->get_images();
66 }
67
68 /**
69 * Adds an image based on the given indexable.
70 *
71 * @param Indexable $indexable The indexable.
72 * @param Images $image_container The image container.
73 *
74 * @return void
75 */
76 protected function add_from_indexable( Indexable $indexable, Images $image_container ) {
77 if ( $indexable->twitter_image_id ) {
78 $image_container->add_image_by_id( $indexable->twitter_image_id );
79 return;
80 }
81
82 if ( $indexable->twitter_image ) {
83 $image_container->add_image_by_url( $indexable->twitter_image );
84 }
85 }
86
87 /**
88 * Retrieves an instance of the image container.
89 *
90 * @codeCoverageIgnore
91 *
92 * @return Images The image container.
93 */
94 protected function get_image_container() {
95 $image_container = new Images( $this->image, $this->url );
96 $image_container->image_size = $this->twitter_image->get_image_size();
97
98 $image_container->set_helpers( $this->twitter_image );
99
100 return $image_container;
101 }
102 }
103