| 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 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 17 |
*/ |
| 18 |
trait Indexable_Social_Image_Trait { |
| 19 |
|
| 20 |
/** |
| 21 |
* The image helper. |
| 22 |
* |
| 23 |
* @var Image_Helper |
| 24 |
*/ |
| 25 |
protected $image; |
| 26 |
|
| 27 |
/** |
| 28 |
* The Open Graph image helper. |
| 29 |
* |
| 30 |
* @var Open_Graph_Image_Helper |
| 31 |
*/ |
| 32 |
protected $open_graph_image; |
| 33 |
|
| 34 |
/** |
| 35 |
* The Twitter image helper. |
| 36 |
* |
| 37 |
* @var Twitter_Image_Helper |
| 38 |
*/ |
| 39 |
protected $twitter_image; |
| 40 |
|
| 41 |
/** |
| 42 |
* Sets the helpers for the trait. |
| 43 |
* |
| 44 |
* @required |
| 45 |
* |
| 46 |
* @param Image_Helper $image The image helper. |
| 47 |
* @param Open_Graph_Image_Helper $open_graph_image The Open Graph image helper. |
| 48 |
* @param Twitter_Image_Helper $twitter_image The Twitter image helper. |
| 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 |
protected function set_alternative_image( array $alternative_image, Indexable $indexable ) { |
| 67 |
if ( ! empty( $alternative_image['image_id'] ) ) { |
| 68 |
if ( ! $indexable->open_graph_image_source && ! $indexable->open_graph_image_id ) { |
| 69 |
$indexable->open_graph_image_id = $alternative_image['image_id']; |
| 70 |
$indexable->open_graph_image_source = $alternative_image['source']; |
| 71 |
|
| 72 |
$this->set_open_graph_image_meta_data( $indexable ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! $indexable->twitter_image && ! $indexable->twitter_image_id ) { |
| 76 |
$indexable->twitter_image = $this->twitter_image->get_by_id( $alternative_image['image_id'] ); |
| 77 |
$indexable->twitter_image_id = $alternative_image['image_id']; |
| 78 |
$indexable->twitter_image_source = $alternative_image['source']; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! empty( $alternative_image['image'] ) ) { |
| 83 |
if ( ! $indexable->open_graph_image_source && ! $indexable->open_graph_image_id ) { |
| 84 |
$indexable->open_graph_image = $alternative_image['image']; |
| 85 |
$indexable->open_graph_image_source = $alternative_image['source']; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! $indexable->twitter_image && ! $indexable->twitter_image_id ) { |
| 89 |
$indexable->twitter_image = $alternative_image['image']; |
| 90 |
$indexable->twitter_image_source = $alternative_image['source']; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Sets the Open Graph image meta data for an og image |
| 97 |
* |
| 98 |
* @param Indexable $indexable The indexable. |
| 99 |
*/ |
| 100 |
protected function set_open_graph_image_meta_data( Indexable $indexable ) { |
| 101 |
if ( ! $indexable->open_graph_image_id ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$image = $this->open_graph_image->get_image_by_id( $indexable->open_graph_image_id ); |
| 106 |
|
| 107 |
if ( ! empty( $image ) ) { |
| 108 |
$indexable->open_graph_image = $image['url']; |
| 109 |
$indexable->open_graph_image_meta = WPSEO_Utils::format_json_encode( $image ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Handles the social images. |
| 115 |
* |
| 116 |
* @param Indexable $indexable The indexable to handle. |
| 117 |
*/ |
| 118 |
protected function handle_social_images( Indexable $indexable ) { |
| 119 |
// When the image or image id is set. |
| 120 |
if ( $indexable->open_graph_image || $indexable->open_graph_image_id ) { |
| 121 |
$indexable->open_graph_image_source = 'set-by-user'; |
| 122 |
|
| 123 |
$this->set_open_graph_image_meta_data( $indexable ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( $indexable->twitter_image || $indexable->twitter_image_id ) { |
| 127 |
$indexable->twitter_image_source = 'set-by-user'; |
| 128 |
} |
| 129 |
|
| 130 |
if ( $indexable->twitter_image_id ) { |
| 131 |
$indexable->twitter_image = $this->twitter_image->get_by_id( $indexable->twitter_image_id ); |
| 132 |
} |
| 133 |
|
| 134 |
// When image sources are set already. |
| 135 |
if ( $indexable->open_graph_image_source && $indexable->twitter_image_source ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$alternative_image = $this->find_alternative_image( $indexable ); |
| 140 |
if ( ! empty( $alternative_image ) ) { |
| 141 |
$this->set_alternative_image( $alternative_image, $indexable ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Resets the social images. |
| 147 |
* |
| 148 |
* @param Indexable $indexable The indexable to set images for. |
| 149 |
*/ |
| 150 |
protected function reset_social_images( Indexable $indexable ) { |
| 151 |
$indexable->open_graph_image = null; |
| 152 |
$indexable->open_graph_image_id = null; |
| 153 |
$indexable->open_graph_image_source = null; |
| 154 |
$indexable->open_graph_image_meta = null; |
| 155 |
|
| 156 |
$indexable->twitter_image = null; |
| 157 |
$indexable->twitter_image_id = null; |
| 158 |
$indexable->twitter_image_source = null; |
| 159 |
} |
| 160 |
} |
| 161 |
|