: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Integration for GoogleFonts
* @copyright Copyright (c) 2023, Code Atlantic LLC
class PUM_Integration_GoogleFonts {
private static $default_api_key = 'AIzaSyA1Q0uFOhEh3zv_Pk31FqlACArFquyBeQU';
public static function init() {
// Set the API key based on options first then default second.
self::$api_key = pum_get_option( 'google_fonts_api_key', self::$default_api_key );
add_filter( 'pum_theme_font_family_options', [ __CLASS__, 'font_family_options' ], 20 );
* Loads a static backup list of Google Fonts in case the API is not responding.
* @return array|mixed|object
public static function load_backup_fonts() {
$json_data = file_get_contents( Popup_Maker::$DIR . 'includes/google-fonts.json' );
return json_decode( $json_data, true );
* Fetch list of Google Fonts or fallback to local list.
public static function fetch_fonts( $sort = 'alpha' ) {
if ( false !== $font_list = get_site_transient( 'pum_google_fonts_list' ) ) {
$google_api_url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . self::$api_key . '&sort=' . $sort;
$response = wp_remote_retrieve_body( wp_remote_get( $google_api_url, [ 'sslverify' => false ] ) );
if ( ! is_wp_error( $response ) ) {
$data = json_decode( $response, true );
// Store transient for a long time after fetching from Google to save API key hits.
$transient_time = self::$api_key === self::$default_api_key ? 8 * WEEK_IN_SECONDS : 1 * WEEK_IN_SECONDS;
if ( ! empty( $data['errors'] ) || empty( $data['items'] ) ) {
$data = self::load_backup_fonts();
// Store transient for short period.
$transient_time = 1 * DAY_IN_SECONDS;
foreach ( $items as $item ) {
$font_list[ $item['family'] ] = $item;
set_site_transient( 'pum_google_fonts_list', $font_list, $transient_time );
* Adds options to the font family dropdowns.
public static function font_family_options( $options ) {
$font_list = self::fetch_fonts();
if ( empty( $font_list ) ) {
// $options = array_merge( $options, array(
// '' => __( 'Google Web Fonts', 'popup-maker' ) . ' ⤵',
foreach ( $font_list as $font_family => $font ) {
$new_options[ $font_family ] = $font_family;
$options[ __( 'Google Web Fonts', 'popup-maker' ) ] = $new_options;