| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Editors\Framework\Seo\Posts; |
| 6 |
|
| 7 |
use Yoast\WP\SEO\Editors\Domain\Seo\Seo_Plugin_Data_Interface; |
| 8 |
use Yoast\WP\SEO\Editors\Domain\Seo\Social; |
| 9 |
use Yoast\WP\SEO\Editors\Framework\Seo\Social_Data_Provider_Interface; |
| 10 |
use Yoast\WP\SEO\Helpers\Image_Helper; |
| 11 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Describes if the social SEO data. |
| 15 |
*/ |
| 16 |
class Social_Data_Provider extends Abstract_Post_Seo_Data_Provider implements Social_Data_Provider_Interface { |
| 17 |
|
| 18 |
/** |
| 19 |
* The image helper. |
| 20 |
* |
| 21 |
* @var Image_Helper |
| 22 |
*/ |
| 23 |
private $image_helper; |
| 24 |
|
| 25 |
/** |
| 26 |
* Whether we must return social templates values. |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private $use_social_templates = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* The options helper. |
| 34 |
* |
| 35 |
* @var Options_Helper |
| 36 |
*/ |
| 37 |
private $options_helper; |
| 38 |
|
| 39 |
/** |
| 40 |
* The constructor. |
| 41 |
* |
| 42 |
* @param Options_Helper $options_helper The options helper. |
| 43 |
* @param Image_Helper $image_helper The image helper. |
| 44 |
*/ |
| 45 |
public function __construct( Options_Helper $options_helper, Image_Helper $image_helper ) { |
| 46 |
$this->options_helper = $options_helper; |
| 47 |
$this->use_social_templates = $this->use_social_templates(); |
| 48 |
$this->image_helper = $image_helper; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Determines whether the social templates should be used. |
| 53 |
* |
| 54 |
* @return bool Whether the social templates should be used. |
| 55 |
*/ |
| 56 |
private function use_social_templates(): bool { |
| 57 |
return $this->options_helper->get( 'opengraph', false ) === true; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Gets the image url. |
| 62 |
* |
| 63 |
* @return string|null |
| 64 |
*/ |
| 65 |
public function get_image_url(): ?string { |
| 66 |
return $this->image_helper->get_post_content_image( $this->post->ID ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Retrieves the social title template. |
| 71 |
* |
| 72 |
* @return string The social title template. |
| 73 |
*/ |
| 74 |
public function get_social_title_template(): string { |
| 75 |
if ( $this->use_social_templates ) { |
| 76 |
return $this->get_social_template( 'title' ); |
| 77 |
} |
| 78 |
|
| 79 |
return ''; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Retrieves the social description template. |
| 84 |
* |
| 85 |
* @return string The social description template. |
| 86 |
*/ |
| 87 |
public function get_social_description_template(): string { |
| 88 |
if ( $this->use_social_templates ) { |
| 89 |
return $this->get_social_template( 'description' ); |
| 90 |
} |
| 91 |
|
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Retrieves the social image template. |
| 97 |
* |
| 98 |
* @return string The social description template. |
| 99 |
*/ |
| 100 |
public function get_social_image_template(): string { |
| 101 |
if ( $this->use_social_templates ) { |
| 102 |
return $this->get_social_template( 'image-url' ); |
| 103 |
} |
| 104 |
|
| 105 |
return ''; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieves a social template. |
| 110 |
* |
| 111 |
* @param string $template_option_name The name of the option in which the template you want to get is saved. |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
private function get_social_template( $template_option_name ) { |
| 116 |
/** |
| 117 |
* Filters the social template value for a given post type. |
| 118 |
* |
| 119 |
* @param string $template The social template value, defaults to empty string. |
| 120 |
* @param string $template_option_name The subname of the option in which the template you want to get is saved. |
| 121 |
* @param string $post_type The name of the post type. |
| 122 |
*/ |
| 123 |
return \apply_filters( 'wpseo_social_template_post_type', '', $template_option_name, $this->post->post_type ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Method to return the Social domain object with SEO data. |
| 128 |
* |
| 129 |
* @return Seo_Plugin_Data_Interface The specific seo data. |
| 130 |
*/ |
| 131 |
public function get_data(): Seo_Plugin_Data_Interface { |
| 132 |
return new Social( $this->get_social_title_template(), $this->get_social_description_template(), $this->get_social_image_template(), $this->get_image_url() ); |
| 133 |
} |
| 134 |
} |
| 135 |
|