credits.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * WordPress Credits Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Retrieve the contributor credits.
  11. *
  12. * @since 3.2.0
  13. * @since 5.6.0 Added the `$version` and `$locale` parameters.
  14. *
  15. * @param string $version WordPress version. Defaults to the current version.
  16. * @param string $locale WordPress locale. Defaults to the current user's locale.
  17. * @return array|false A list of all of the contributors, or false on error.
  18. */
  19. function wp_credits( $version = '', $locale = '' ) {
  20. if ( ! $version ) {
  21. // Include an unmodified $wp_version.
  22. require ABSPATH . WPINC . '/version.php';
  23. $version = $wp_version;
  24. }
  25. if ( ! $locale ) {
  26. $locale = get_user_locale();
  27. }
  28. $results = get_site_transient( 'wordpress_credits_' . $locale );
  29. if ( ! is_array( $results )
  30. || false !== strpos( $version, '-' )
  31. || ( isset( $results['data']['version'] ) && strpos( $version, $results['data']['version'] ) !== 0 )
  32. ) {
  33. $url = "http://api.wordpress.org/core/credits/1.1/?version={$version}&locale={$locale}";
  34. $options = array( 'user-agent' => 'WordPress/' . $version . '; ' . home_url( '/' ) );
  35. if ( wp_http_supports( array( 'ssl' ) ) ) {
  36. $url = set_url_scheme( $url, 'https' );
  37. }
  38. $response = wp_remote_get( $url, $options );
  39. if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
  40. return false;
  41. }
  42. $results = json_decode( wp_remote_retrieve_body( $response ), true );
  43. if ( ! is_array( $results ) ) {
  44. return false;
  45. }
  46. set_site_transient( 'wordpress_credits_' . $locale, $results, DAY_IN_SECONDS );
  47. }
  48. return $results;
  49. }
  50. /**
  51. * Retrieve the link to a contributor's WordPress.org profile page.
  52. *
  53. * @access private
  54. * @since 3.2.0
  55. *
  56. * @param string $display_name The contributor's display name (passed by reference).
  57. * @param string $username The contributor's username.
  58. * @param string $profiles URL to the contributor's WordPress.org profile page.
  59. */
  60. function _wp_credits_add_profile_link( &$display_name, $username, $profiles ) {
  61. $display_name = '<a href="' . esc_url( sprintf( $profiles, $username ) ) . '">' . esc_html( $display_name ) . '</a>';
  62. }
  63. /**
  64. * Retrieve the link to an external library used in WordPress.
  65. *
  66. * @access private
  67. * @since 3.2.0
  68. *
  69. * @param string $data External library data (passed by reference).
  70. */
  71. function _wp_credits_build_object_link( &$data ) {
  72. $data = '<a href="' . esc_url( $data[1] ) . '">' . esc_html( $data[0] ) . '</a>';
  73. }
  74. /**
  75. * Displays the title for a given group of contributors.
  76. *
  77. * @since 5.3.0
  78. *
  79. * @param array $group_data The current contributor group.
  80. */
  81. function wp_credits_section_title( $group_data = array() ) {
  82. if ( ! count( $group_data ) ) {
  83. return;
  84. }
  85. if ( $group_data['name'] ) {
  86. if ( 'Translators' === $group_data['name'] ) {
  87. // Considered a special slug in the API response. (Also, will never be returned for en_US.)
  88. $title = _x( 'Translators', 'Translate this to be the equivalent of English Translators in your language for the credits page Translators section' );
  89. } elseif ( isset( $group_data['placeholders'] ) ) {
  90. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  91. $title = vsprintf( translate( $group_data['name'] ), $group_data['placeholders'] );
  92. } else {
  93. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  94. $title = translate( $group_data['name'] );
  95. }
  96. echo '<h2 class="wp-people-group-title">' . esc_html( $title ) . "</h2>\n";
  97. }
  98. }
  99. /**
  100. * Displays a list of contributors for a given group.
  101. *
  102. * @since 5.3.0
  103. *
  104. * @param array $credits The credits groups returned from the API.
  105. * @param string $slug The current group to display.
  106. */
  107. function wp_credits_section_list( $credits = array(), $slug = '' ) {
  108. $group_data = isset( $credits['groups'][ $slug ] ) ? $credits['groups'][ $slug ] : array();
  109. $credits_data = $credits['data'];
  110. if ( ! count( $group_data ) ) {
  111. return;
  112. }
  113. if ( ! empty( $group_data['shuffle'] ) ) {
  114. shuffle( $group_data['data'] ); // We were going to sort by ability to pronounce "hierarchical," but that wouldn't be fair to Matt.
  115. }
  116. switch ( $group_data['type'] ) {
  117. case 'list':
  118. array_walk( $group_data['data'], '_wp_credits_add_profile_link', $credits_data['profiles'] );
  119. echo '<p class="wp-credits-list">' . wp_sprintf( '%l.', $group_data['data'] ) . "</p>\n\n";
  120. break;
  121. case 'libraries':
  122. array_walk( $group_data['data'], '_wp_credits_build_object_link' );
  123. echo '<p class="wp-credits-list">' . wp_sprintf( '%l.', $group_data['data'] ) . "</p>\n\n";
  124. break;
  125. default:
  126. $compact = 'compact' === $group_data['type'];
  127. $classes = 'wp-people-group ' . ( $compact ? 'compact' : '' );
  128. echo '<ul class="' . $classes . '" id="wp-people-group-' . $slug . '">' . "\n";
  129. foreach ( $group_data['data'] as $person_data ) {
  130. echo '<li class="wp-person" id="wp-person-' . esc_attr( $person_data[2] ) . '">' . "\n\t";
  131. echo '<a href="' . esc_url( sprintf( $credits_data['profiles'], $person_data[2] ) ) . '" class="web">';
  132. $size = $compact ? 80 : 160;
  133. $data = get_avatar_data( $person_data[1] . '@md5.gravatar.com', array( 'size' => $size ) );
  134. $data2x = get_avatar_data( $person_data[1] . '@md5.gravatar.com', array( 'size' => $size * 2 ) );
  135. echo '<span class="wp-person-avatar"><img src="' . esc_url( $data['url'] ) . '" srcset="' . esc_url( $data2x['url'] ) . ' 2x" class="gravatar" alt="" /></span>' . "\n";
  136. echo esc_html( $person_data[0] ) . "</a>\n\t";
  137. if ( ! $compact && ! empty( $person_data[3] ) ) {
  138. // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
  139. echo '<span class="title">' . translate( $person_data[3] ) . "</span>\n";
  140. }
  141. echo "</li>\n";
  142. }
  143. echo "</ul>\n";
  144. break;
  145. }
  146. }