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