PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / DocumentFontsV1Service.php

DocumentFontsV1Service.php in Depicter — Popup & Slider Builder 1.3.3, at app/src/Services/DocumentFontsV1Service.php

153 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4 class DocumentFontsV1Service
5 {
6 /**
7 * @var array[]
8 */
9 private $fonts = [];
10
11 /**
12 * Get font families
13 *
14 * @param int $documentId The document ID to get belonging faces
15 * @param string $type Repository type of the font. `google`, `typescript`, ..
16 *
17 * @return int[]|string[]
18 */
19 public function getFamilies( $documentId, $type = 'google' ) {
20 return array_keys( $this->getFontsInfo( $documentId, $type ) );
21 }
22
23 /**
24 * Get collected fonts list
25 *
26 * @param int $documentId The document ID to get belonging faces
27 * @param string $type Repository type of the font. `google`, `typescript`, ..
28 *
29 * @return int[]|string[]
30 */
31 public function getFontsInfo( $documentId, $type = 'google' ) {
32 return ! empty( $this->fonts[ $documentId ][ $type ] ) ? $this->fonts[ $documentId ][ $type ] : [];
33 }
34
35 /**
36 * Get font faces
37 *
38 * @param int $documentId The document ID to get belonging faces
39 * @param string $type Repository type of the font. `google`, `typescript`, ..
40 *
41 * @return array
42 */
43 public function getFontsLinkQuery( $documentId, $type = 'google' ) {
44 if( empty( $this->fonts[ $documentId ][ $type ] ) ){
45 return [];
46 }
47
48 $fontFaces = [];
49
50 foreach ( $this->fonts[ $documentId ][ $type ] as $fontFamily => $fontInfo ) {
51
52 if ( $fontFamily == 'inherit' ) {
53 continue;
54 }
55
56 $weights = [];
57 foreach ( $fontInfo['weights'] as $key => $weight ) {
58 $weight = 'regular' == strtolower($weight) ? '400' : $weight;
59 $weights[ $key ] = $weight;
60 }
61
62 // Sort weight number ascending
63 ksort( $weights );
64
65 // Make weight query string for current font
66 $weightQuery = '';
67 foreach ( $weights as $weightKey => $weight ){
68 $weightQuery .= $weight . ',';
69 }
70 $weightQuery = rtrim($weightQuery, ',');
71
72 // Collect font name and weights in a query string
73 $fontFaces[] = $fontInfo['face'] . ':' . $weightQuery;
74 }
75
76 return $fontFaces;
77 }
78
79 /**
80 * Add a font to fonts list
81 *
82 * @param int $documentId The document ID that the font belongs to
83 * @param string $fontName Font family name
84 * @param string|array $fontWeight Font weight
85 * @param string $type Repository type of the font. `google`, `typescript`, ..
86 */
87 public function addFont( $documentId, $fontName, $fontWeight, $type = 'google' ) {
88 if ( empty( $fontName ) ) {
89 return;
90 }
91
92 $fontName = trim( $fontName );
93 $fontWeight = (array) $fontWeight;
94
95 $this->initFontForDocument( $documentId );
96
97 if ( isset( $this->fonts[ $documentId ][ $type ][ $fontName ] ) ) {
98 foreach ( $fontWeight as $key => $weight ) {
99 if ( !in_array( $weight, $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'] ) ) {
100 $this->fonts[ $documentId ][ $type ][ $fontName ]['weights'][] = $weight;
101 }
102 }
103 } else {
104 $this->fonts[ $documentId ][ $type ][ $fontName ] = [
105 'face' => str_replace( ' ', '+', $fontName ),
106 'weights' => $fontWeight
107 ];
108 }
109 }
110
111 protected function initFontForDocument( $documentId ){
112 if( ! isset( $this->fonts[ $documentId ] ) ){
113 $this->fonts[ $documentId ] = [
114 'google' => []
115 ];
116 }
117 }
118
119 /**
120 * Add list of fonts to fonts list
121 *
122 * @param int $documentId The document ID that fonts belong to
123 * @param array $fontList
124 * @param string $type
125 */
126 public function addFonts( $documentId, $fontList, $type = 'google' ) {
127 if ( empty( $fontList ) ) {
128 return;
129 }
130
131 foreach ( $fontList as $fontName => $fontWeight ){
132 $this->addFont( $documentId, $fontName, $fontWeight, $type );
133 }
134 }
135
136 /**
137 * Get fonts load link
138 *
139 * @param int $documentId The document ID to get belonging faces
140 * @param string $type Repository type of the font. `google`, `typescript`, ..
141 *
142 * @return bool|string
143 */
144 public function getFontsLink( $documentId, $type = 'google' ) {
145 if ( $fontsQuery = $this->getFontsLinkQuery( $documentId, $type = 'google' ) ) {
146 $fontsQuery = implode( "|", $fontsQuery );
147 return 'https://fonts.googleapis.com/css?family=' . $fontsQuery . '&display=swap';
148 }
149
150 return '';
151 }
152 }
153