PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/server/wordpress/wp-content/plugins/wordpress-seo/src/generators/schema/person.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 302 lines | 137 code | 45 blank | 120 comment | 26 complexity | 1ca6433997513e1bac55f8495ea69418 MD5 | raw file
  1. <?php
  2. namespace Yoast\WP\SEO\Generators\Schema;
  3. use WP_User;
  4. use Yoast\WP\SEO\Config\Schema_IDs;
  5. /**
  6. * Returns schema Person data.
  7. */
  8. class Person extends Abstract_Schema_Piece {
  9. /**
  10. * Array of the social profiles we display for a Person.
  11. *
  12. * @var string[]
  13. */
  14. private $social_profiles = [
  15. 'facebook',
  16. 'instagram',
  17. 'linkedin',
  18. 'pinterest',
  19. 'twitter',
  20. 'myspace',
  21. 'youtube',
  22. 'soundcloud',
  23. 'tumblr',
  24. 'wikipedia',
  25. ];
  26. /**
  27. * The Schema type we use for this class.
  28. *
  29. * @var string[]
  30. */
  31. protected $type = [ 'Person', 'Organization' ];
  32. /**
  33. * Determine whether we should return Person schema.
  34. *
  35. * @return bool
  36. */
  37. public function is_needed() {
  38. // Using an author piece instead.
  39. if ( $this->site_represents_current_author() ) {
  40. return false;
  41. }
  42. return $this->context->site_represents === 'person' || $this->context->indexable->object_type === 'user';
  43. }
  44. /**
  45. * Returns Person Schema data.
  46. *
  47. * @return bool|array Person data on success, false on failure.
  48. */
  49. public function generate() {
  50. $user_id = $this->determine_user_id();
  51. if ( ! $user_id ) {
  52. return false;
  53. }
  54. return $this->build_person_data( $user_id );
  55. }
  56. /**
  57. * Determines a User ID for the Person data.
  58. *
  59. * @return bool|int User ID or false upon return.
  60. */
  61. protected function determine_user_id() {
  62. /**
  63. * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
  64. *
  65. * @api int|bool $user_id The user ID currently determined.
  66. */
  67. $user_id = \apply_filters( 'wpseo_schema_person_user_id', $this->context->site_user_id );
  68. // It should to be an integer higher than 0.
  69. if ( \is_int( $user_id ) && $user_id > 0 ) {
  70. return $user_id;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Retrieve a list of social profile URLs for Person.
  76. *
  77. * @param array $same_as_urls Array of SameAs URLs.
  78. * @param int $user_id User ID.
  79. *
  80. * @return string[] A list of SameAs URLs.
  81. */
  82. protected function get_social_profiles( $same_as_urls, $user_id ) {
  83. /**
  84. * Filter: 'wpseo_schema_person_social_profiles' - Allows filtering of social profiles per user.
  85. *
  86. * @param int $user_id The current user we're grabbing social profiles for.
  87. *
  88. * @api string[] $social_profiles The array of social profiles to retrieve. Each should be a user meta field
  89. * key. As they are retrieved using the WordPress function `get_the_author_meta`.
  90. */
  91. $social_profiles = \apply_filters( 'wpseo_schema_person_social_profiles', $this->social_profiles, $user_id );
  92. // We can only handle an array.
  93. if ( ! \is_array( $social_profiles ) ) {
  94. return $same_as_urls;
  95. }
  96. foreach ( $social_profiles as $profile ) {
  97. // Skip non-string values.
  98. if ( ! \is_string( $profile ) ) {
  99. continue;
  100. }
  101. $social_url = $this->url_social_site( $profile, $user_id );
  102. if ( $social_url ) {
  103. $same_as_urls[] = $social_url;
  104. }
  105. }
  106. return $same_as_urls;
  107. }
  108. /**
  109. * Builds our array of Schema Person data for a given user ID.
  110. *
  111. * @param int $user_id The user ID to use.
  112. *
  113. * @return array An array of Schema Person data.
  114. */
  115. protected function build_person_data( $user_id ) {
  116. $user_data = \get_userdata( $user_id );
  117. $data = [
  118. '@type' => $this->type,
  119. '@id' => $this->helpers->schema->id->get_user_schema_id( $user_id, $this->context ),
  120. ];
  121. // Safety check for the `get_userdata` WP function, which could return false.
  122. if ( $user_data === false ) {
  123. return $data;
  124. }
  125. $data['name'] = $this->helpers->schema->html->smart_strip_tags( $user_data->display_name );
  126. $data = $this->add_image( $data, $user_data );
  127. if ( ! empty( $user_data->description ) ) {
  128. $data['description'] = $this->helpers->schema->html->smart_strip_tags( $user_data->description );
  129. }
  130. $data = $this->add_same_as_urls( $data, $user_data, $user_id );
  131. /**
  132. * Filter: 'wpseo_schema_person_data' - Allows filtering of schema data per user.
  133. *
  134. * @param array $data The schema data we have for this person.
  135. * @param int $user_id The current user we're collecting schema data for.
  136. */
  137. $data = \apply_filters( 'wpseo_schema_person_data', $data, $user_id );
  138. return $data;
  139. }
  140. /**
  141. * Returns an ImageObject for the persons avatar.
  142. *
  143. * @param array $data The Person schema.
  144. * @param WP_User $user_data User data.
  145. *
  146. * @return array The Person schema.
  147. */
  148. protected function add_image( $data, $user_data ) {
  149. $schema_id = $this->context->site_url . Schema_IDs::PERSON_LOGO_HASH;
  150. $data = $this->set_image_from_options( $data, $schema_id );
  151. if ( ! isset( $data['image'] ) ) {
  152. $data = $this->set_image_from_avatar( $data, $user_data, $schema_id );
  153. }
  154. if ( \is_array( $this->type ) && \in_array( 'Organization', $this->type, true ) ) {
  155. $data['logo'] = [ '@id' => $schema_id ];
  156. }
  157. return $data;
  158. }
  159. /**
  160. * Generate the person image from our settings.
  161. *
  162. * @param array $data The Person schema.
  163. * @param string $schema_id The string used in the `@id` for the schema.
  164. *
  165. * @return array The Person schema.
  166. */
  167. protected function set_image_from_options( $data, $schema_id ) {
  168. if ( $this->context->site_represents !== 'person' ) {
  169. return $data;
  170. }
  171. if ( \is_array( $this->context->person_logo_meta ) ) {
  172. $data['image'] = $this->helpers->schema->image->generate_from_attachment_meta( $schema_id, $this->context->person_logo_meta, $data['name'] );
  173. }
  174. return $data;
  175. }
  176. /**
  177. * Generate the person logo from gravatar.
  178. *
  179. * @param array $data The Person schema.
  180. * @param WP_User $user_data User data.
  181. * @param string $schema_id The string used in the `@id` for the schema.
  182. *
  183. * @return array The Person schema.
  184. */
  185. protected function set_image_from_avatar( $data, $user_data, $schema_id ) {
  186. // If we don't have an image in our settings, fall back to an avatar, if we're allowed to.
  187. $show_avatars = \get_option( 'show_avatars' );
  188. if ( ! $show_avatars ) {
  189. return $data;
  190. }
  191. $url = \get_avatar_url( $user_data->user_email );
  192. if ( empty( $url ) ) {
  193. return $data;
  194. }
  195. $data['image'] = $this->helpers->schema->image->simple_image_object( $schema_id, $url, $user_data->display_name );
  196. return $data;
  197. }
  198. /**
  199. * Returns an author's social site URL.
  200. *
  201. * @param string $social_site The social site to retrieve the URL for.
  202. * @param mixed $user_id The user ID to use function outside of the loop.
  203. *
  204. * @return string
  205. */
  206. protected function url_social_site( $social_site, $user_id = false ) {
  207. $url = \get_the_author_meta( $social_site, $user_id );
  208. if ( ! empty( $url ) && $social_site === 'twitter' ) {
  209. $url = 'https://twitter.com/' . $url;
  210. }
  211. return $url;
  212. }
  213. /**
  214. * Checks the site is represented by the same person as this indexable.
  215. *
  216. * @return bool True when the site is represented by the same person as this indexable.
  217. */
  218. protected function site_represents_current_author() {
  219. // Can only be the case when the site represents a user.
  220. if ( $this->context->site_represents !== 'person' ) {
  221. return false;
  222. }
  223. // Article post from the same user as the site represents.
  224. if (
  225. $this->context->indexable->object_type === 'post'
  226. && $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
  227. && $this->context->schema_article_type !== 'None'
  228. ) {
  229. return $this->context->site_user_id === $this->context->indexable->author_id;
  230. }
  231. // Author archive from the same user as the site represents.
  232. return $this->context->indexable->object_type === 'user' && $this->context->site_user_id === $this->context->indexable->object_id;
  233. }
  234. /**
  235. * Builds our SameAs array.
  236. *
  237. * @param array $data The Person schema data.
  238. * @param WP_User $user_data The user data object.
  239. * @param int $user_id The user ID to use.
  240. *
  241. * @return array The Person schema data.
  242. */
  243. protected function add_same_as_urls( $data, $user_data, $user_id ) {
  244. $same_as_urls = [];
  245. // Add the "Website" field from WordPress' contact info.
  246. if ( ! empty( $user_data->user_url ) ) {
  247. $same_as_urls[] = $user_data->user_url;
  248. }
  249. // Add the social profiles.
  250. $same_as_urls = $this->get_social_profiles( $same_as_urls, $user_id );
  251. if ( ! empty( $same_as_urls ) ) {
  252. $same_as_urls = \array_values( \array_unique( $same_as_urls ) );
  253. $data['sameAs'] = $same_as_urls;
  254. }
  255. return $data;
  256. }
  257. }