/wp-content/plugins/all-in-one-seo-pack/app/Common/Social/Twitter.php

https://gitlab.com/ebrjose/comcebu · PHP · 261 lines · 141 code · 40 blank · 80 comment · 35 complexity · cc1c6a1d86bec6f8b5eb9ce5093ce2fe MD5 · raw file

  1. <?php
  2. namespace AIOSEO\Plugin\Common\Social;
  3. // Exit if accessed directly.
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit;
  6. }
  7. /**
  8. * Handles the Twitter meta.
  9. *
  10. * @since 4.0.0
  11. */
  12. class Twitter {
  13. /**
  14. * Returns the Twitter URL for the site.
  15. *
  16. * @since 4.0.0
  17. *
  18. * @return string The Twitter URL.
  19. */
  20. public function getTwitterUrl() {
  21. if ( ! aioseo()->options->social->profiles->sameUsername->enable ) {
  22. return aioseo()->options->social->profiles->urls->twitterUrl;
  23. }
  24. $userName = aioseo()->options->social->profiles->sameUsername->username;
  25. return ( $userName && in_array( 'twitterUrl', aioseo()->options->social->profiles->sameUsername->included, true ) )
  26. ? 'https://twitter.com/' . $userName
  27. : '';
  28. }
  29. /**
  30. * Returns the Twitter card type.
  31. *
  32. * @since 4.0.0
  33. *
  34. * @return string $card The card type.
  35. */
  36. public function getCardType() {
  37. if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
  38. return aioseo()->options->social->twitter->homePage->cardType;
  39. }
  40. $metaData = aioseo()->meta->metaData->getMetaData();
  41. return ! empty( $metaData->twitter_card ) && 'default' !== $metaData->twitter_card ? $metaData->twitter_card : aioseo()->options->social->twitter->general->defaultCardType;
  42. }
  43. /**
  44. * Returns the Twitter creator.
  45. *
  46. * @since 4.0.0
  47. *
  48. * @return string The creator.
  49. */
  50. public function getCreator() {
  51. $author = '';
  52. $post = aioseo()->helpers->getPost();
  53. if ( $post && aioseo()->options->social->twitter->general->showAuthor ) {
  54. $twitterUser = get_the_author_meta( 'aioseo_twitter', $post->post_author );
  55. $author = $twitterUser ? $twitterUser : aioseo()->social->twitter->getTwitterUrl();
  56. $author = aioseo()->social->twitter->prepareUsername( $author );
  57. }
  58. return $author;
  59. }
  60. /**
  61. * Returns the Twitter image URL.
  62. *
  63. * @since 4.0.0
  64. *
  65. * @param int $postId The post ID (optional).
  66. * @return string The image URL.
  67. */
  68. public function getImage( $postId = null ) {
  69. $post = aioseo()->helpers->getPost( $postId );
  70. if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
  71. $image = aioseo()->options->social->twitter->homePage->image;
  72. if ( empty( $image ) ) {
  73. $image = aioseo()->options->social->facebook->homePage->image;
  74. }
  75. if ( empty( $image ) ) {
  76. $image = aioseo()->social->image->getImage( 'twitter', aioseo()->options->social->twitter->general->defaultImageSourcePosts, $post );
  77. }
  78. return $image ? $image : aioseo()->social->facebook->getImage();
  79. }
  80. $metaData = aioseo()->meta->metaData->getMetaData( $post );
  81. if ( ! empty( $metaData->twitter_use_og ) ) {
  82. return aioseo()->social->facebook->getImage();
  83. }
  84. $image = '';
  85. if ( ! empty( $metaData ) ) {
  86. $imageSource = ! empty( $metaData->twitter_image_type ) && 'default' !== $metaData->twitter_image_type
  87. ? $metaData->twitter_image_type
  88. : aioseo()->options->social->twitter->general->defaultImageSourcePosts;
  89. $image = aioseo()->social->image->getImage( 'twitter', $imageSource, $post );
  90. }
  91. return $image ? $image : aioseo()->social->facebook->getImage();
  92. }
  93. /**
  94. * Returns the Twitter title for the current page.
  95. *
  96. * @since 4.0.0
  97. *
  98. * @param WP_Post|integer $post The post object or ID (optional).
  99. * @return string The Twitter title.
  100. */
  101. public function getTitle( $post = null ) {
  102. if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
  103. $title = aioseo()->meta->title->helpers->prepare( aioseo()->options->social->twitter->homePage->title );
  104. return $title ? $title : aioseo()->social->facebook->getTitle( $post );
  105. }
  106. $post = aioseo()->helpers->getPost( $post );
  107. $metaData = aioseo()->meta->metaData->getMetaData( $post );
  108. if ( ! empty( $metaData->twitter_use_og ) ) {
  109. return aioseo()->social->facebook->getTitle( $post );
  110. }
  111. $title = '';
  112. if ( ! empty( $metaData->twitter_title ) ) {
  113. $title = aioseo()->meta->title->helpers->prepare( $metaData->twitter_title );
  114. }
  115. return $title ? $title : aioseo()->social->facebook->getTitle( $post );
  116. }
  117. /**
  118. * Returns the Twitter description for the current page.
  119. *
  120. * @since 4.0.0
  121. *
  122. * @param WP_Post|integer $post The post object or ID (optional).
  123. * @return string The Twitter description.
  124. */
  125. public function getDescription( $post = null ) {
  126. if ( is_home() && 'posts' === get_option( 'show_on_front' ) ) {
  127. $description = aioseo()->meta->description->helpers->prepare( aioseo()->options->social->twitter->homePage->description );
  128. return $description ? $description : aioseo()->social->facebook->getDescription( $post );
  129. }
  130. $post = aioseo()->helpers->getPost( $post );
  131. $metaData = aioseo()->meta->metaData->getMetaData( $post );
  132. if ( ! empty( $metaData->twitter_use_og ) ) {
  133. return aioseo()->social->facebook->getDescription( $post );
  134. }
  135. $description = '';
  136. if ( ! empty( $metaData->twitter_description ) ) {
  137. $description = aioseo()->meta->description->helpers->prepare( $metaData->twitter_description );
  138. }
  139. return $description ? $description : aioseo()->social->facebook->getDescription( $post );
  140. }
  141. /**
  142. * Prepare twitter username for public display.
  143. *
  144. * We do things like strip out the URL, etc and return just (at)username.
  145. * At the moment, we'll check for 1 of 3 things... (at)username, username, and https://twitter.com/username.
  146. *
  147. * @since 4.0.0
  148. *
  149. * @param string $profile Twitter username.
  150. * @param boolean $includeAt Whether or not ot include the @ sign.
  151. * @return string Full Twitter username.
  152. */
  153. public function prepareUsername( $profile, $includeAt = true ) {
  154. if ( preg_match( '/^(\@)?[A-Za-z0-9_]+$/', $profile ) ) {
  155. if ( '@' !== $profile[0] && $includeAt ) {
  156. $profile = '@' . $profile;
  157. } elseif ( '@' === $profile[0] && ! $includeAt ) {
  158. $profile = ltrim( $profile, '@' );
  159. }
  160. } elseif ( strpos( $profile, 'twitter.com' ) ) {
  161. $profile = esc_url( $profile );
  162. // extract the twitter username from the url.
  163. $parsedTwitterProfile = wp_parse_url( $profile );
  164. $path = $parsedTwitterProfile['path'];
  165. $pathParts = explode( '/', $path );
  166. $profile = $pathParts[1];
  167. if ( $profile ) {
  168. if ( '@' !== $profile[0] && $includeAt ) {
  169. $profile = '@' . $profile;
  170. } elseif ( '@' === $profile[0] && ! $includeAt ) {
  171. $profile = ltrim( $profile, '@' );
  172. }
  173. }
  174. }
  175. return $profile;
  176. }
  177. /**
  178. * Get additional twitter data.
  179. *
  180. * @since 4.0.0
  181. *
  182. * @return array An array of additional twitter data.
  183. */
  184. public function getAdditionalData() {
  185. if ( ! aioseo()->options->social->twitter->general->additionalData ) {
  186. return [];
  187. }
  188. $data = [];
  189. $post = aioseo()->helpers->getPost();
  190. if ( $post->post_author ) {
  191. $data[] = [
  192. 'label' => __( 'Written by', 'all-in-one-seo-pack' ),
  193. 'value' => get_the_author_meta( 'display_name', $post->post_author )
  194. ];
  195. }
  196. if ( ! empty( $post->post_content ) ) {
  197. $minutes = $this->getReadingTime( $post->post_content );
  198. if ( ! empty( $minutes ) ) {
  199. $data[] = [
  200. 'label' => __( 'Est. reading time', 'all-in-one-seo-pack' ),
  201. // Translators: 1 - The estimated reading time.
  202. 'value' => sprintf( _n( '%1$s minute', '%1$s minutes', $minutes, 'all-in-one-seo-pack' ), $minutes )
  203. ];
  204. }
  205. }
  206. return $data;
  207. }
  208. /**
  209. * Returns the estimated reading time for a string.
  210. *
  211. * @since 4.0.0
  212. *
  213. * @param string $string The string to count.
  214. * @return integer The estimated reading time as an integer.
  215. */
  216. private function getReadingTime( $string ) {
  217. $wpm = 200;
  218. $word = str_word_count( wp_strip_all_tags( $string ) );
  219. return round( $word / $wpm );
  220. }
  221. }