PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 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 All 129 releases
wordpress-seo / src / builders / indexable-social-image-trait.php

indexable-social-image-trait.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/builders/indexable-social-image-trait.php

169 lines 4.8 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\Builders;
4
5 use WPSEO_Utils;
6 use Yoast\WP\SEO\Helpers\Image_Helper;
7 use Yoast\WP\SEO\Helpers\Open_Graph\Image_Helper as Open_Graph_Image_Helper;
8 use Yoast\WP\SEO\Helpers\Twitter\Image_Helper as Twitter_Image_Helper;
9 use Yoast\WP\SEO\Models\Indexable;
10
11 /**
12 * Trait for determine the social image to use in the indexable.
13 *
14 * Represents the trait used in builders for handling social images.
15 */
16 trait Indexable_Social_Image_Trait {
17
18 /**
19 * The image helper.
20 *
21 * @var Image_Helper
22 */
23 protected $image;
24
25 /**
26 * The Open Graph image helper.
27 *
28 * @var Open_Graph_Image_Helper
29 */
30 protected $open_graph_image;
31
32 /**
33 * The Twitter image helper.
34 *
35 * @var Twitter_Image_Helper
36 */
37 protected $twitter_image;
38
39 /**
40 * Sets the helpers for the trait.
41 *
42 * @required
43 *
44 * @param Image_Helper $image The image helper.
45 * @param Open_Graph_Image_Helper $open_graph_image The Open Graph image helper.
46 * @param Twitter_Image_Helper $twitter_image The Twitter image helper.
47 *
48 * @return void
49 */
50 public function set_social_image_helpers(
51 Image_Helper $image,
52 Open_Graph_Image_Helper $open_graph_image,
53 Twitter_Image_Helper $twitter_image
54 ) {
55 $this->image = $image;
56 $this->open_graph_image = $open_graph_image;
57 $this->twitter_image = $twitter_image;
58 }
59
60 /**
61 * Sets the alternative on an indexable.
62 *
63 * @param array $alternative_image The alternative image to set.
64 * @param Indexable $indexable The indexable to set image for.
65 *
66 * @return void
67 */
68 protected function set_alternative_image( array $alternative_image, Indexable $indexable ) {
69 if ( ! empty( $alternative_image['image_id'] ) ) {
70 if ( ! $indexable->open_graph_image_source && ! $indexable->open_graph_image_id ) {
71 $indexable->open_graph_image_id = $alternative_image['image_id'];
72 $indexable->open_graph_image_source = $alternative_image['source'];
73
74 $this->set_open_graph_image_meta_data( $indexable );
75 }
76
77 if ( ! $indexable->twitter_image && ! $indexable->twitter_image_id ) {
78 $indexable->twitter_image = $this->twitter_image->get_by_id( $alternative_image['image_id'] );
79 $indexable->twitter_image_id = $alternative_image['image_id'];
80 $indexable->twitter_image_source = $alternative_image['source'];
81 }
82 }
83
84 if ( ! empty( $alternative_image['image'] ) ) {
85 if ( ! $indexable->open_graph_image_source && ! $indexable->open_graph_image_id ) {
86 $indexable->open_graph_image = $alternative_image['image'];
87 $indexable->open_graph_image_source = $alternative_image['source'];
88 }
89
90 if ( ! $indexable->twitter_image && ! $indexable->twitter_image_id ) {
91 $indexable->twitter_image = $alternative_image['image'];
92 $indexable->twitter_image_source = $alternative_image['source'];
93 }
94 }
95 }
96
97 /**
98 * Sets the Open Graph image meta data for an og image
99 *
100 * @param Indexable $indexable The indexable.
101 *
102 * @return void
103 */
104 protected function set_open_graph_image_meta_data( Indexable $indexable ) {
105 if ( ! $indexable->open_graph_image_id ) {
106 return;
107 }
108
109 $image = $this->open_graph_image->get_image_by_id( $indexable->open_graph_image_id );
110
111 if ( ! empty( $image ) ) {
112 $indexable->open_graph_image = $image['url'];
113 $indexable->open_graph_image_meta = WPSEO_Utils::format_json_encode( $image );
114 }
115 }
116
117 /**
118 * Handles the social images.
119 *
120 * @param Indexable $indexable The indexable to handle.
121 *
122 * @return void
123 */
124 protected function handle_social_images( Indexable $indexable ) {
125 // When the image or image id is set.
126 if ( $indexable->open_graph_image || $indexable->open_graph_image_id ) {
127 $indexable->open_graph_image_source = 'set-by-user';
128
129 $this->set_open_graph_image_meta_data( $indexable );
130 }
131
132 if ( $indexable->twitter_image || $indexable->twitter_image_id ) {
133 $indexable->twitter_image_source = 'set-by-user';
134 }
135
136 if ( $indexable->twitter_image_id ) {
137 $indexable->twitter_image = $this->twitter_image->get_by_id( $indexable->twitter_image_id );
138 }
139
140 // When image sources are set already.
141 if ( $indexable->open_graph_image_source && $indexable->twitter_image_source ) {
142 return;
143 }
144
145 $alternative_image = $this->find_alternative_image( $indexable );
146 if ( ! empty( $alternative_image ) ) {
147 $this->set_alternative_image( $alternative_image, $indexable );
148 }
149 }
150
151 /**
152 * Resets the social images.
153 *
154 * @param Indexable $indexable The indexable to set images for.
155 *
156 * @return void
157 */
158 protected function reset_social_images( Indexable $indexable ) {
159 $indexable->open_graph_image = null;
160 $indexable->open_graph_image_id = null;
161 $indexable->open_graph_image_source = null;
162 $indexable->open_graph_image_meta = null;
163
164 $indexable->twitter_image = null;
165 $indexable->twitter_image_id = null;
166 $indexable->twitter_image_source = null;
167 }
168 }
169