| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presenters\Twitter; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Presentations\Indexable_Presentation; |
| 6 |
use Yoast\WP\SEO\Presenters\Abstract_Indexable_Tag_Presenter; |
| 7 |
|
| 8 |
/** |
| 9 |
* Presenter class for the Twitter site tag. |
| 10 |
*/ |
| 11 |
class Site_Presenter extends Abstract_Indexable_Tag_Presenter { |
| 12 |
|
| 13 |
/** |
| 14 |
* The tag key name. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $key = 'twitter:site'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Run the Twitter site through the `wpseo_twitter_site` filter. |
| 22 |
* |
| 23 |
* @return string The filtered Twitter site. |
| 24 |
*/ |
| 25 |
public function get() { |
| 26 |
/** |
| 27 |
* Filter: 'wpseo_twitter_site' - Allow changing the Twitter site account as output in the Twitter card by Yoast SEO. |
| 28 |
* |
| 29 |
* @param string $twitter_site Twitter site account string. |
| 30 |
* @param Indexable_Presentation $presentation The presentation of an indexable. |
| 31 |
*/ |
| 32 |
$twitter_site = \apply_filters( 'wpseo_twitter_site', $this->presentation->twitter_site, $this->presentation ); |
| 33 |
$twitter_site = $this->get_twitter_id( $twitter_site ); |
| 34 |
|
| 35 |
if ( ! \is_string( $twitter_site ) || $twitter_site === '' ) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
|
| 39 |
return '@' . $twitter_site; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Checks if the given id is actually an id or a url and if url, distills the id from it. |
| 44 |
* |
| 45 |
* Solves issues with filters returning urls and theme's/other plugins also adding a user meta |
| 46 |
* twitter field which expects url rather than an id (which is what we expect). |
| 47 |
* |
| 48 |
* @param string $id Twitter ID or url. |
| 49 |
* |
| 50 |
* @return string|bool Twitter ID or false if it failed to get a valid Twitter ID. |
| 51 |
*/ |
| 52 |
private function get_twitter_id( $id ) { |
| 53 |
if ( \preg_match( '`([A-Za-z0-9_]{1,25})$`', $id, $match ) ) { |
| 54 |
return $match[1]; |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
|