'acft_sanitize_settings' ) ); add_settings_section( 'acft_acf-typography-field_section', '', 'acft_settings_section_callback', 'acf-typography-field' ); add_settings_field( 'acft_text_field_0', __( 'Google Fonts Key', 'acf-typography-field' ), 'acft_google_key_field', 'acf-typography-field', 'acft_acf-typography-field_section', array( 'label_for' => 'acft_google_key' ) ); } /** * Sanitize the API key submitted through the settings form. * * @param mixed $settings Submitted option value. * @return array */ function acft_sanitize_settings( $settings ) { if ( in_array( acft_google_api_key_source(), array( 'constant', 'legacy_constant' ), true ) ) { return array( 'google_key' => acft_get_saved_google_api_key() ); } if ( ! is_array( $settings ) || ! isset( $settings['google_key'] ) || ! is_string( $settings['google_key'] ) ) { return array( 'google_key' => '' ); } return array( 'google_key' => sanitize_text_field( $settings['google_key'] ) ); } function acft_google_key_field() { $google_key = acft_get_saved_google_api_key(); $key_source = acft_google_api_key_source(); // readonly, not disabled: a disabled field is not submitted and saving would wipe the stored key $readonly = in_array( $key_source, array( 'constant', 'legacy_constant' ), true ); ?> >
' . wp_kses( __( 'The key is set by the ACFT_GOOGLE_API_KEY constant, which overrides this field.', 'acf-typography-field' ), array( 'code' => array() ) ) . '

'; } elseif ( 'legacy_constant' === $key_source ) { echo '

' . wp_kses( __( 'The key is set by the YOUR_API_KEY constant, which overrides this field. That constant is deprecated and will stop working in 4.0. Please rename it to ACFT_GOOGLE_API_KEY.', 'acf-typography-field' ), array( 'code' => array() ) ) . '

'; } if ( '' !== $key_source ) { $fonts_cache = acft_get_google_fonts_cache(); if ( '' !== $fonts_cache['error'] ) { /* translators: %s: error message returned by the Google Fonts API */ echo '

' . esc_html( sprintf( __( 'Google Fonts could not be loaded: %s', 'acf-typography-field' ), $fonts_cache['error'] ) ) . '

'; } if ( $fonts_cache['fetched'] ) { $fonts_count = count( $fonts_cache['families'] ); $fetched_on = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $fonts_cache['fetched'] ); // after a failed fetch the list is the last good one, not from the key in the field if ( '' !== $fonts_cache['error'] ) { /* translators: 1: number of Google Fonts, 2: date and time the list was fetched */ $fonts_status = sprintf( __( 'Still using the last good list: %1$d Google Fonts, from %2$s.', 'acf-typography-field' ), $fonts_count, $fetched_on ); } else { /* translators: 1: number of Google Fonts, 2: date and time of the last update */ $fonts_status = sprintf( __( '%1$d Google Fonts available, last updated %2$s.', 'acf-typography-field' ), $fonts_count, $fetched_on ); } echo '

' . esc_html( $fonts_status ) . '

'; } } else { /* translators: %s: URL of Google's guide to getting an API key */ echo '

' . wp_kses( sprintf( __( 'No key is saved, so editors can only pick web-safe fonts. Get a Google Fonts API key to offer Google Fonts.', 'acf-typography-field' ), esc_url( 'https://developers.google.com/fonts/docs/developer_api#APIKey' ) ), array( 'a' => array( 'href' => array() ) ) ) . '

'; } ?>
post_type ) ) { return false; } return 'acf' === $screen->post_type || 0 === strpos( $screen->post_type, 'acf-' ); } /** * Refresh the Google Fonts list on ACF screens and on the settings page * * Runs before these screens print the list's status (the notice below, the * message on the settings page), so it is current when an admin looks. Other * pages rely on the daily background refresh. The settings form posts to * options.php, which is not matched here, so a save only fetches once, with the * new key. * * Until the list has been fetched once (e.g. right after an update from 3.2.x), * any admin screen fetches it: on sites where WP-Cron never runs, editors would * otherwise see no Google fonts until someone opened an ACF screen. Any attempt, * good or failed, ends this, and the saved list survives later plugin updates, * so it runs once per site. Without a key nothing is fetched, so the list is not * even read. * * acft_refresh_google_fonts_on_screen() * * @since 3.3.0 * @param WP_Screen $screen Current admin screen. */ add_action( 'current_screen', 'acft_refresh_google_fonts_on_screen' ); function acft_refresh_google_fonts_on_screen( $screen ) { if ( acft_is_acf_admin_screen() || 'settings_page_acf-typography-field' === $screen->id || acft_google_fonts_never_fetched() ) { acft_refresh_google_fonts(); } } /** * Whether a key is set but the Google Fonts list was never fetched or tried * * acft_google_fonts_never_fetched() * * @since 3.3.0 * @return bool */ function acft_google_fonts_never_fetched() { // the source check reads the settings only (autoloaded), and never shows the YOUR_API_KEY notice if ( '' === acft_google_api_key_source() ) { return false; } $cache = acft_get_google_fonts_cache(); return ! $cache['fetched'] && ! $cache['attempted']; } /** * Warn admins on ACF screens when the Google Fonts list could not be loaded * * Without it a broken key goes unnoticed: the field just offers web-safe fonts (or * an old list). Kept to one line; the full error is on the settings page. Not * dismissible; it disappears once a fetch succeeds. * * acft_google_fonts_error_notice() * * @since 3.3.0 */ add_action( 'admin_notices', 'acft_google_fonts_error_notice' ); function acft_google_fonts_error_notice() { if ( ! current_user_can( 'manage_options' ) || ! acft_is_acf_admin_screen() || '' === acft_google_api_key_source() ) { return; } $fonts_cache = acft_get_google_fonts_cache(); if ( '' === $fonts_cache['error'] ) { return; } ?>