| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Services; |
| 4 |
|
| 5 |
use Depicter\GuzzleHttp\Client; |
| 6 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 7 |
|
| 8 |
class GoogleFontsService{ |
| 9 |
|
| 10 |
/** |
| 11 |
* Download Google fonts to depicter upload directory |
| 12 |
* |
| 13 |
* @param $cssLink |
| 14 |
* |
| 15 |
* @return array $downloadedFontsLinks |
| 16 |
*/ |
| 17 |
public function download( $cssLink ) { |
| 18 |
$client = new Client(); |
| 19 |
$downloadedFontsLinks = []; |
| 20 |
try { |
| 21 |
$cssContent = $client->get( $cssLink )->getBody()->getContents(); |
| 22 |
preg_match_all( '/@font-face\s{([^}]*)}/', $cssContent, $fontFaces, PREG_SET_ORDER ); |
| 23 |
if ( !empty( $fontFaces ) ) { |
| 24 |
|
| 25 |
$fileSystem = \Depicter::storage()->filesystem(); |
| 26 |
$googleFontsDir = \Depicter::storage()->getPluginUploadsDirectory() . '/fonts/google'; |
| 27 |
if ( ! $fileSystem->isDir( $googleFontsDir ) ) { |
| 28 |
$fileSystem->mkdir( $googleFontsDir, true ); |
| 29 |
} |
| 30 |
|
| 31 |
foreach( $fontFaces as $fontFace ) { |
| 32 |
preg_match( "/font-family:\s'([^\']*)'/", $fontFace[0], $fontFamily ); |
| 33 |
$fontFamilySlug = str_replace( ' ', '-', $fontFamily[1] ); |
| 34 |
preg_match_all( '/url\(([^\)]*)/', $fontFace[0], $urls, PREG_SET_ORDER ); |
| 35 |
if ( !empty( $urls ) ) { |
| 36 |
foreach( $urls as $url ) { |
| 37 |
if ( false !== strpos( $url[1], 'fonts.gstatic' ) ) { |
| 38 |
// Create google font directory if not exist |
| 39 |
if ( ! $fileSystem->isDir( $googleFontsDir . '/' . $fontFamilySlug ) ) { |
| 40 |
$fileSystem->mkdir( $googleFontsDir . '/' . $fontFamilySlug ); |
| 41 |
} |
| 42 |
|
| 43 |
// Download Font File |
| 44 |
$fileName = pathinfo( $url[1],PATHINFO_BASENAME ); |
| 45 |
$savedGoogleFontPath = $googleFontsDir . '/' . $fontFamilySlug . '/' . $fileName; |
| 46 |
if ( !$fileSystem->isFile( $savedGoogleFontPath ) ) { |
| 47 |
$client->get( $url[1], [ 'sink' => $savedGoogleFontPath ] ); |
| 48 |
|
| 49 |
// update font face css file |
| 50 |
$fontFace[0] = str_replace( $url[1], $fontFamilySlug . '/' . $fileName, $fontFace[0] ); |
| 51 |
if ( ! $fileSystem->isFile( $googleFontsDir . '/' . $fontFamilySlug . '.css' ) ) { |
| 52 |
$fileSystem->write( $googleFontsDir . '/' . $fontFamilySlug . '.css', $fontFace[0] . "\n" ); |
| 53 |
} else { |
| 54 |
$fontFaceContent = $fileSystem->read( $googleFontsDir . '/' . $fontFamilySlug . '.css' ); |
| 55 |
$fontFaceContent = $fontFaceContent . $fontFace[0] . "\n"; |
| 56 |
$fileSystem->write( $googleFontsDir . '/' . $fontFamilySlug . '.css', $fontFaceContent ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$downloadedFontsLinks[ $fontFamilySlug ] = \Depicter::storage()->uploads()->getBaseUrl() . '/depicter/fonts/google/' . $fontFamilySlug . '.css'; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $downloadedFontsLinks; |
| 68 |
} catch ( GuzzleException $exception ) { |
| 69 |
preg_match_all( '/family=(.+?)&/', $cssLink, $fontFamilies, PREG_SET_ORDER ); |
| 70 |
if ( !empty( $fontFamilies ) ) { |
| 71 |
$fontFamilies = explode( '|', $fontFamilies[0][1] ); |
| 72 |
foreach( $fontFamilies as $fontFamily ) { |
| 73 |
$fontFamilyName = explode( ':', $fontFamily ); |
| 74 |
$fontFamilySlug = str_replace( '+', '-', $fontFamilyName[0] ); |
| 75 |
if ( file_exists( \Depicter::storage()->uploads()->getBaseDirectory() . '/depicter/fonts/google/' . $fontFamilySlug . '.css' ) ) { |
| 76 |
$downloadedFontsLinks[ $fontFamilySlug ] = \Depicter::storage()->uploads()->getBaseUrl() . '/depicter/fonts/google/' . $fontFamilySlug . '.css'; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
error_log( $exception->getMessage(), 0 ); |
| 81 |
return $downloadedFontsLinks; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function getCssIdForLocalFont( $documentID, $fontFamilySlug ){ |
| 86 |
return 'depicter--' . $documentID . '-local-g-font-' . strtolower( $fontFamilySlug ); |
| 87 |
} |
| 88 |
|
| 89 |
public function swapToLocalLinks( $documentID, $cssLinks ){ |
| 90 |
$localLinks = []; |
| 91 |
|
| 92 |
foreach( $cssLinks as $cssId => $cssLink ){ |
| 93 |
if ( false !== strpos( $cssId, 'google-font' ) ) { |
| 94 |
$links = $this->download( $cssLink ); |
| 95 |
foreach( $links as $fontFamilySlug => $link ) { |
| 96 |
$localLinks[ $this->getCssIdForLocalFont( $documentID, $fontFamilySlug ) ] = $link; |
| 97 |
} |
| 98 |
} else { |
| 99 |
$localLinks[ $cssId ] = $cssLink; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $localLinks; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get list of hosted google fonts |
| 108 |
* |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function getListOfHostedGoogleFonts(): array{ |
| 112 |
$fonts = []; |
| 113 |
|
| 114 |
$fileSystem = \Depicter::storage()->filesystem(); |
| 115 |
$googleFontsDir = \Depicter::storage()->getPluginUploadsDirectory() . '/fonts/google'; |
| 116 |
$files = $fileSystem->scan($googleFontsDir ); |
| 117 |
if ( empty( $files ) ) { |
| 118 |
return $fonts; |
| 119 |
} |
| 120 |
|
| 121 |
foreach( $files as $key => $file ) { |
| 122 |
// if it's a directory then continue to the next file |
| 123 |
if ( $file['type'] == 'd' || false === strpos( $file['name'], '.css' ) ) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$cssFileContent = $fileSystem->read( $googleFontsDir . '/' . $file['name'] ); |
| 128 |
preg_match_all( "/font-family:\s'([^']*)';/", $cssFileContent, $fontFamily, PREG_SET_ORDER ); |
| 129 |
preg_match_all( "/@font-face[^\}]*\}/", $cssFileContent, $fontFaces, PREG_SET_ORDER ); |
| 130 |
if ( empty( $fontFaces ) ) { |
| 131 |
return $fonts; |
| 132 |
} |
| 133 |
|
| 134 |
$variants = []; |
| 135 |
foreach( $fontFaces as $fontFaceKey => $fontFace ) { |
| 136 |
preg_match_all( "/url\(([^\)]*)/", $fontFace[0], $fontSrc, PREG_SET_ORDER ); |
| 137 |
if ( !empty( $fontSrc ) && $fileSystem->isFile( $googleFontsDir . '/' . $fontSrc[0][1] ) ) { |
| 138 |
$variants[ $fontFaceKey ]['src'] = \Depicter::storage()->uploads()->getBaseUrl() . '/depicter/fonts/google/' . $fontSrc[0][1]; |
| 139 |
} else { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
preg_match_all( "/font-style:([^;]*);/", $fontFace[0], $fontStyle, PREG_SET_ORDER ); |
| 144 |
if ( !empty( $fontStyle ) ) { |
| 145 |
$variants[ $fontFaceKey ]['style'] = $fontStyle[0][0]; |
| 146 |
} |
| 147 |
|
| 148 |
preg_match_all( "/font-weight:([^;]*);/", $fontFace[0], $fontWeight, PREG_SET_ORDER ); |
| 149 |
if ( !empty( $fontWeight ) ) { |
| 150 |
$variants[ $fontFaceKey ]['weight'] = trim( $fontWeight[0][1] ); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
$fonts[] = [ |
| 156 |
'family' => $fontFamily[0][1], |
| 157 |
'variants' => $variants |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
return $fonts; |
| 162 |
} |
| 163 |
} |
| 164 |
|