class-twitter.php 16 KB

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