ont families to install. * @param array $font_collection Font collection. * @return array */ private static function install_font_families( $slug_font_families_to_install, $font_collection ) { return array_reduce( $slug_font_families_to_install, function( $carry, $slug ) use ( $font_collection ) { $font_family_from_collection = self::get_font_family_by_slug_from_font_collection( $slug, $font_collection ); $font_family_name = $font_family_from_collection['fontFamily']; $font_family_installed = FontFamily::get_font_family_by_name( $font_family_name ); if ( $font_family_installed ) { return array_merge( $carry, array( $slug => $font_family_installed ) ); } $font_family_settings = array( 'fontFamily' => $font_family_from_collection['fontFamily'], 'preview' => $font_family_from_collection['preview'], 'slug' => $font_family_from_collection['slug'], 'name' => $font_family_from_collection['name'], ); $font_family_id = FontFamily::insert_font_family( $font_family_settings ); if ( is_wp_error( $font_family_id ) ) { if ( 'duplicate_font_family' !== $font_family_id->get_error_code() ) { wc_get_logger()->error( sprintf( 'Font Family installation error: %s', $font_family_id->get_error_message(), ), array( 'source' => self::SOURCE_LOGGER ) ); } return $carry; } return array_merge( $carry, array( $slug => get_post( $font_family_id ) ) ); }, array(), ); } /** * Install font faces. * * @param array $slug_font_families_to_install Font families to install. * @param array $installed_font_families Installed font families. * @param array $font_faces_from_collection Font faces from collection. */ private static function install_font_faces( $slug_font_families_to_install, $installed_font_families, $font_faces_from_collection ) { foreach ( $slug_font_families_to_install as $slug ) { $font_family = $installed_font_families[ $slug ]; $font_faces = $font_faces_from_collection[ $slug ]; $font_faces_to_install = self::FONT_FAMILIES_TO_INSTALL[ $slug ]['fontWeights']; foreach ( $font_faces as $font_face ) { if ( ! in_array( $font_face['fontWeight'], $font_faces_to_install, true ) ) { continue; } $slug = \WP_Font_Utils::get_font_face_slug( $font_face ); $font_face_installed = FontFace::get_installed_font_faces_by_slug( $slug ); if ( $font_face_installed ) { continue; } $wp_error = FontFace::insert_font_face( $font_face, $font_family->ID ); if ( is_wp_error( $wp_error ) ) { wc_get_logger()->error( sprintf( /* translators: %s: error message */ __( 'Font Face installation error: %s', 'woocommerce' ), $wp_error->get_error_message() ), array( 'source' => self::SOURCE_LOGGER ) ); } } } } /** * Get font faces data from font collection. * * @param array $slug_font_families_to_install Font families to install. * @param array $font_collection Font collection. * @return array */ private static function get_font_faces_data_from_font_collection( $slug_font_families_to_install, $font_collection ) { return array_reduce( $slug_font_families_to_install, function( $carry, $slug ) use ( $font_collection ) { $font_family = self::get_font_family_by_slug_from_font_collection( $slug, $font_collection ); if ( ! $font_family ) { return $carry; } return array_merge( $carry, array( $slug => $font_family['fontFace'] ) ); }, array() ); } /** * Get font family by slug from font collection. * * @param string $slug Font slug. * @param array $font_families_collection Font families collection. * @return array|null */ private static function get_font_family_by_slug_from_font_collection( $slug, $font_families_collection ) { $font_family = null; foreach ( $font_families_collection as $font_family ) { if ( $font_family['font_family_settings']['slug'] === $slug ) { $font_family = $font_family['font_family_settings']; break; } } return $font_family; } }