/wp-content/plugins/wordpress-seo/frontend/class-twitter.php

https://bitbucket.org/carloskikea/helpet · PHP · 653 lines · 309 code · 102 blank · 242 comment · 77 complexity · 5b5c8581e4ca6ef12c748fe565b69d64 MD5 · raw file

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