class-twitter.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Frontend
  6. */
  7. /**
  8. * This class handles the Twitter card functionality.
  9. *
  10. * @link https://dev.twitter.com/docs/cards
  11. */
  12. class WPSEO_Twitter {
  13. /**
  14. * Instance of this class.
  15. *
  16. * @var object
  17. */
  18. public static $instance;
  19. /**
  20. * Images.
  21. *
  22. * @var array
  23. */
  24. private $images = [];
  25. /**
  26. * Images.
  27. *
  28. * @var array
  29. */
  30. public $shown_images = [];
  31. /**
  32. * Will hold the Twitter card type being created.
  33. *
  34. * @var string
  35. */
  36. private $type;
  37. /**
  38. * Card types currently allowed by Twitter.
  39. *
  40. * @link https://dev.twitter.com/cards/types
  41. *
  42. * @var array
  43. */
  44. private $valid_types = [
  45. 'summary',
  46. 'summary_large_image',
  47. 'app',
  48. 'player',
  49. ];
  50. /**
  51. * Class constructor.
  52. */
  53. public function __construct() {
  54. $this->twitter();
  55. }
  56. /**
  57. * Outputs the Twitter Card code on singular pages.
  58. */
  59. public function twitter() {
  60. /**
  61. * Filter: 'wpseo_output_twitter_card' - Allow disabling of the Twitter card.
  62. *
  63. * @api bool $enabled Enabled/disabled flag
  64. */
  65. if ( false === apply_filters( 'wpseo_output_twitter_card', true ) ) {
  66. return;
  67. }
  68. wp_reset_query();
  69. $this->type();
  70. $this->description();
  71. $this->title();
  72. $this->site_twitter();
  73. if ( ! post_password_required() ) {
  74. $this->image();
  75. }
  76. if ( is_singular() ) {
  77. $this->author();
  78. }
  79. /**
  80. * Action: 'wpseo_twitter' - Hook to add all Yoast SEO Twitter output to so they're close together.
  81. */
  82. do_action( 'wpseo_twitter' );
  83. }
  84. /**
  85. * Display the Twitter card type.
  86. *
  87. * This defaults to summary but can be filtered using the <code>wpseo_twitter_card_type</code> filter.
  88. *
  89. * @link https://dev.twitter.com/docs/cards
  90. */
  91. protected function type() {
  92. $this->determine_card_type();
  93. $this->sanitize_card_type();
  94. $this->output_metatag( 'card', $this->type );
  95. }
  96. /**
  97. * Determines the twitter card type for the current page.
  98. */
  99. private function determine_card_type() {
  100. $this->type = WPSEO_Options::get( 'twitter_card_type' );
  101. // @todo This should be reworked to use summary_large_image for any fitting image R.
  102. if ( is_singular() && has_shortcode( $GLOBALS['post']->post_content, 'gallery' ) ) {
  103. $this->images = get_post_gallery_images();
  104. if ( count( $this->images ) > 0 ) {
  105. $this->type = 'summary_large_image';
  106. }
  107. }
  108. /**
  109. * Filter: 'wpseo_twitter_card_type' - Allow changing the Twitter Card type as output in the Twitter card by Yoast SEO.
  110. *
  111. * @api string $unsigned The type string.
  112. */
  113. $this->type = apply_filters( 'wpseo_twitter_card_type', $this->type );
  114. }
  115. /**
  116. * Determines whether the card type is of a type currently allowed by Twitter.
  117. *
  118. * @link https://dev.twitter.com/cards/types
  119. */
  120. private function sanitize_card_type() {
  121. if ( ! in_array( $this->type, $this->valid_types, true ) ) {
  122. $this->type = 'summary';
  123. }
  124. }
  125. /**
  126. * Output the metatag.
  127. *
  128. * @param string $name Tag name string.
  129. * @param string $value Tag value string.
  130. * @param bool $escaped Force escape flag.
  131. */
  132. private function output_metatag( $name, $value, $escaped = false ) {
  133. // Escape the value if not escaped.
  134. if ( false === $escaped ) {
  135. $value = esc_attr( $value );
  136. }
  137. /**
  138. * Filter: 'wpseo_twitter_metatag_key' - Make the Twitter metatag key filterable.
  139. *
  140. * @api string $key The Twitter metatag key.
  141. */
  142. $metatag_key = apply_filters( 'wpseo_twitter_metatag_key', 'name' );
  143. // Output meta.
  144. echo '<meta ', esc_attr( $metatag_key ), '="twitter:', esc_attr( $name ), '" content="', $value, '" />', "\n";
  145. }
  146. /**
  147. * Displays the description for Twitter.
  148. *
  149. * Only used when OpenGraph is inactive.
  150. */
  151. protected function description() {
  152. if ( WPSEO_Frontend_Page_Type::is_simple_page() ) {
  153. $meta_desc = $this->single_description( WPSEO_Frontend_Page_Type::get_simple_page_id() );
  154. }
  155. elseif ( is_category() || is_tax() || is_tag() ) {
  156. $meta_desc = $this->taxonomy_description();
  157. }
  158. else {
  159. $meta_desc = $this->fallback_description();
  160. }
  161. $meta_desc = wpseo_replace_vars( $meta_desc, get_queried_object() );
  162. /**
  163. * Filter: 'wpseo_twitter_description' - Allow changing the Twitter description as output in the Twitter card by Yoast SEO.
  164. *
  165. * @api string $twitter The description string.
  166. */
  167. $meta_desc = apply_filters( 'wpseo_twitter_description', $meta_desc );
  168. if ( is_string( $meta_desc ) && $meta_desc !== '' ) {
  169. $this->output_metatag( 'description', $meta_desc );
  170. }
  171. }
  172. /**
  173. * Returns the description for a singular page.
  174. *
  175. * @param int $post_id Post ID.
  176. *
  177. * @return string
  178. */
  179. private function single_description( $post_id = 0 ) {
  180. $meta_desc = trim( WPSEO_Meta::get_value( 'twitter-description', $post_id ) );
  181. if ( is_string( $meta_desc ) && '' !== $meta_desc ) {
  182. return $meta_desc;
  183. }
  184. $meta_desc = $this->fallback_description();
  185. if ( is_string( $meta_desc ) && '' !== $meta_desc ) {
  186. return $meta_desc;
  187. }
  188. return wp_strip_all_tags( get_the_excerpt() );
  189. }
  190. /**
  191. * Getting the description for the taxonomy.
  192. *
  193. * @return bool|mixed|string
  194. */
  195. private function taxonomy_description() {
  196. $meta_desc = WPSEO_Taxonomy_Meta::get_meta_without_term( 'twitter-description' );
  197. if ( ! is_string( $meta_desc ) || $meta_desc === '' ) {
  198. $meta_desc = $this->fallback_description();
  199. }
  200. if ( is_string( $meta_desc ) && $meta_desc !== '' ) {
  201. return $meta_desc;
  202. }
  203. return wp_strip_all_tags( term_description() );
  204. }
  205. /**
  206. * Returns a fallback description.
  207. *
  208. * @return string
  209. */
  210. private function fallback_description() {
  211. return trim( WPSEO_Frontend::get_instance()->metadesc( false ) );
  212. }
  213. /**
  214. * Displays the title for Twitter.
  215. *
  216. * Only used when OpenGraph is inactive.
  217. */
  218. protected function title() {
  219. if ( WPSEO_Frontend_Page_Type::is_simple_page() ) {
  220. $title = $this->single_title( WPSEO_Frontend_Page_Type::get_simple_page_id() );
  221. }
  222. elseif ( is_category() || is_tax() || is_tag() ) {
  223. $title = $this->taxonomy_title();
  224. }
  225. else {
  226. $title = $this->fallback_title();
  227. }
  228. $title = wpseo_replace_vars( $title, get_queried_object() );
  229. /**
  230. * Filter: 'wpseo_twitter_title' - Allow changing the Twitter title as output in the Twitter card by Yoast SEO.
  231. *
  232. * @api string $twitter The title string.
  233. */
  234. $title = apply_filters( 'wpseo_twitter_title', $title );
  235. if ( is_string( $title ) && $title !== '' ) {
  236. $this->output_metatag( 'title', $title );
  237. }
  238. }
  239. /**
  240. * Returns the Twitter title for a single post.
  241. *
  242. * @param int $post_id Post ID.
  243. *
  244. * @return string
  245. */
  246. private function single_title( $post_id = 0 ) {
  247. $title = WPSEO_Meta::get_value( 'twitter-title', $post_id );
  248. if ( ! is_string( $title ) || $title === '' ) {
  249. return $this->fallback_title();
  250. }
  251. return $title;
  252. }
  253. /**
  254. * Getting the title for the taxonomy.
  255. *
  256. * @return bool|mixed|string
  257. */
  258. private function taxonomy_title() {
  259. $title = WPSEO_Taxonomy_Meta::get_meta_without_term( 'twitter-title' );
  260. if ( ! is_string( $title ) || $title === '' ) {
  261. return $this->fallback_title();
  262. }
  263. return $title;
  264. }
  265. /**
  266. * Returns the Twitter title for any page.
  267. *
  268. * @return string
  269. */
  270. private function fallback_title() {
  271. return WPSEO_Frontend::get_instance()->title( '' );
  272. }
  273. /**
  274. * Displays the Twitter account for the site.
  275. */
  276. protected function site_twitter() {
  277. switch ( WPSEO_Options::get( 'company_or_person', '' ) ) {
  278. case 'person':
  279. $user_id = (int) WPSEO_Options::get( 'company_or_person_user_id', false );
  280. $twitter = get_the_author_meta( 'twitter', $user_id );
  281. // For backwards compat reasons, if there is no twitter ID for person, we fall back to site.
  282. if ( empty( $twitter ) ) {
  283. $twitter = WPSEO_Options::get( 'twitter_site' );
  284. }
  285. break;
  286. case 'company':
  287. default:
  288. $twitter = WPSEO_Options::get( 'twitter_site' );
  289. break;
  290. }
  291. /**
  292. * Filter: 'wpseo_twitter_site' - Allow changing the Twitter site account as output in the Twitter card by Yoast SEO.
  293. *
  294. * @api string $unsigned Twitter site account string.
  295. */
  296. $site = apply_filters( 'wpseo_twitter_site', $twitter );
  297. $site = $this->get_twitter_id( $site );
  298. if ( is_string( $site ) && $site !== '' ) {
  299. $this->output_metatag( 'site', '@' . $site );
  300. }
  301. }
  302. /**
  303. * Checks if the given id is actually an id or a url and if url, distills the id from it.
  304. *
  305. * Solves issues with filters returning urls and theme's/other plugins also adding a user meta
  306. * twitter field which expects url rather than an id (which is what we expect).
  307. *
  308. * @param string $id Twitter ID or url.
  309. *
  310. * @return string|bool Twitter ID or false if it failed to get a valid Twitter ID.
  311. */
  312. private function get_twitter_id( $id ) {
  313. if ( preg_match( '`([A-Za-z0-9_]{1,25})$`', $id, $match ) ) {
  314. return $match[1];
  315. }
  316. return false;
  317. }
  318. /**
  319. * Displays the image for Twitter.
  320. *
  321. * Only used when OpenGraph is inactive or Summary Large Image card is chosen.
  322. */
  323. protected function image() {
  324. if ( is_category() || is_tax() || is_tag() ) {
  325. $this->taxonomy_image_output();
  326. }
  327. else {
  328. $this->single_image_output();
  329. }
  330. if ( count( $this->shown_images ) === 0 && WPSEO_Options::get( 'og_default_image', '' ) !== '' ) {
  331. $this->image_output( WPSEO_Options::get( 'og_default_image' ) );
  332. }
  333. }
  334. /**
  335. * Outputs the first image of a gallery.
  336. */
  337. private function gallery_images_output() {
  338. $this->image_output( reset( $this->images ) );
  339. }
  340. /**
  341. * Outputs the Twitter image. Using the Facebook image as fallback.
  342. *
  343. * @return bool
  344. */
  345. private function taxonomy_image_output() {
  346. foreach ( [ 'twitter-image', 'opengraph-image' ] as $tag ) {
  347. $img = WPSEO_Taxonomy_Meta::get_meta_without_term( $tag );
  348. if ( is_string( $img ) && $img !== '' ) {
  349. $this->image_output( $img );
  350. return true;
  351. }
  352. }
  353. /**
  354. * Filter: wpseo_twitter_taxonomy_image - Allow developers to set a custom Twitter image for taxonomies.
  355. *
  356. * @api bool|string $unsigned Return string to supply image to use, false to use no image.
  357. */
  358. $img = apply_filters( 'wpseo_twitter_taxonomy_image', false );
  359. if ( is_string( $img ) && $img !== '' ) {
  360. $this->image_output( $img );
  361. return true;
  362. }
  363. return false;
  364. }
  365. /**
  366. * Takes care of image output when we only need to display a single image.
  367. *
  368. * @return void
  369. */
  370. private function single_image_output() {
  371. if ( $this->homepage_image_output() ) {
  372. return;
  373. }
  374. // Posts page, which won't be caught by is_singular() below.
  375. if ( $this->posts_page_image_output() ) {
  376. return;
  377. }
  378. if ( WPSEO_Frontend_Page_Type::is_simple_page() ) {
  379. $post_id = WPSEO_Frontend_Page_Type::get_simple_page_id();
  380. if ( $this->image_from_meta_values_output( $post_id ) ) {
  381. return;
  382. }
  383. if ( $this->image_of_attachment_page_output( $post_id ) ) {
  384. return;
  385. }
  386. if ( $this->image_thumbnail_output( $post_id ) ) {
  387. return;
  388. }
  389. if ( count( $this->images ) > 0 ) {
  390. $this->gallery_images_output();
  391. return;
  392. }
  393. if ( $this->image_from_content_output( $post_id ) ) {
  394. return;
  395. }
  396. }
  397. }
  398. /**
  399. * Show the front page image.
  400. *
  401. * @return bool
  402. */
  403. private function homepage_image_output() {
  404. if ( is_front_page() ) {
  405. if ( WPSEO_Options::get( 'og_frontpage_image', '' ) !== '' ) {
  406. $this->image_output( WPSEO_Options::get( 'og_frontpage_image' ) );
  407. return true;
  408. }
  409. }
  410. return false;
  411. }
  412. /**
  413. * Show the posts page image.
  414. *
  415. * @return bool
  416. */
  417. private function posts_page_image_output() {
  418. if ( is_front_page() || ! is_home() ) {
  419. return false;
  420. }
  421. $post_id = get_option( 'page_for_posts' );
  422. if ( $this->image_from_meta_values_output( $post_id ) ) {
  423. return true;
  424. }
  425. if ( $this->image_thumbnail_output( $post_id ) ) {
  426. return true;
  427. }
  428. return false;
  429. }
  430. /**
  431. * Outputs a Twitter image tag for a given image.
  432. *
  433. * @param string $img The source URL to the image.
  434. *
  435. * @return bool
  436. */
  437. protected function image_output( $img ) {
  438. /**
  439. * Filter: 'wpseo_twitter_image' - Allow changing the Twitter Card image.
  440. *
  441. * @api string $img Image URL string.
  442. */
  443. $img = apply_filters( 'wpseo_twitter_image', $img );
  444. if ( WPSEO_Utils::is_url_relative( $img ) === true && $img[0] === '/' ) {
  445. $parsed_url = wp_parse_url( home_url() );
  446. $img = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img;
  447. }
  448. $escaped_img = esc_url( $img );
  449. if ( in_array( $escaped_img, $this->shown_images, true ) ) {
  450. return false;
  451. }
  452. if ( is_string( $escaped_img ) && $escaped_img !== '' ) {
  453. $this->output_metatag( 'image', $escaped_img, true );
  454. array_push( $this->shown_images, $escaped_img );
  455. return true;
  456. }
  457. return false;
  458. }
  459. /**
  460. * Retrieve images from the post meta values.
  461. *
  462. * @param int $post_id Optional post ID to use.
  463. *
  464. * @return bool
  465. */
  466. private function image_from_meta_values_output( $post_id = 0 ) {
  467. foreach ( [ 'twitter-image', 'opengraph-image' ] as $tag ) {
  468. $img = WPSEO_Meta::get_value( $tag, $post_id );
  469. if ( $img !== '' ) {
  470. $this->image_output( $img );
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. /**
  477. * Retrieve an attachment page's attachment.
  478. *
  479. * @param string $attachment_id The ID of the attachment for which to retrieve the image.
  480. *
  481. * @return bool
  482. */
  483. private function image_of_attachment_page_output( $attachment_id ) {
  484. if ( get_post_type( $attachment_id ) === 'attachment' ) {
  485. $mime_type = get_post_mime_type( $attachment_id );
  486. switch ( $mime_type ) {
  487. case 'image/jpeg':
  488. case 'image/png':
  489. case 'image/gif':
  490. $this->image_output( wp_get_attachment_url( $attachment_id ) );
  491. return true;
  492. }
  493. }
  494. return false;
  495. }
  496. /**
  497. * Retrieve the featured image.
  498. *
  499. * @param int $post_id Optional post ID to use.
  500. *
  501. * @return bool
  502. */
  503. private function image_thumbnail_output( $post_id = 0 ) {
  504. if ( empty( $post_id ) ) {
  505. $post_id = get_the_ID();
  506. }
  507. if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post_id ) ) {
  508. /**
  509. * Filter: 'wpseo_twitter_image_size' - Allow changing the Twitter Card image size.
  510. *
  511. * @api string $featured_img Image size string.
  512. */
  513. $featured_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), apply_filters( 'wpseo_twitter_image_size', 'full' ) );
  514. if ( $featured_img ) {
  515. $this->image_output( $featured_img[0] );
  516. return true;
  517. }
  518. }
  519. return false;
  520. }
  521. /**
  522. * Retrieve the image from the content.
  523. *
  524. * @param int $post_id The post id to extract the images from.
  525. *
  526. * @return bool True when images output succeeded.
  527. */
  528. private function image_from_content_output( $post_id ) {
  529. $image_url = WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id );
  530. if ( $image_url === null || empty( $image_url ) ) {
  531. return false;
  532. }
  533. $this->image_output( $image_url );
  534. return true;
  535. }
  536. /**
  537. * Displays the authors Twitter account.
  538. */
  539. protected function author() {
  540. $post = get_post();
  541. $twitter = null;
  542. if ( is_object( $post ) ) {
  543. $twitter = ltrim( trim( get_the_author_meta( 'twitter', $post->post_author ) ), '@' );
  544. }
  545. /**
  546. * Filter: 'wpseo_twitter_creator_account' - Allow changing the Twitter account as output in the Twitter card by Yoast SEO.
  547. *
  548. * @api string $twitter The twitter account name string.
  549. */
  550. $twitter = apply_filters( 'wpseo_twitter_creator_account', $twitter );
  551. $twitter = $this->get_twitter_id( $twitter );
  552. if ( is_string( $twitter ) && $twitter !== '' ) {
  553. $this->output_metatag( 'creator', '@' . $twitter );
  554. }
  555. elseif ( WPSEO_Options::get( 'twitter_site', '' ) !== '' && is_string( WPSEO_Options::get( 'twitter_site' ) ) ) {
  556. $this->output_metatag( 'creator', '@' . WPSEO_Options::get( 'twitter_site' ) );
  557. }
  558. }
  559. /**
  560. * Get the singleton instance of this class.
  561. *
  562. * @return object
  563. */
  564. public static function get_instance() {
  565. if ( ! ( self::$instance instanceof self ) ) {
  566. self::$instance = new self();
  567. }
  568. return self::$instance;
  569. }
  570. } /* End of class */