credits.php 5.4 KB

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