-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathclass-wp-font-library.php
More file actions
122 lines (110 loc) · 2.74 KB
/
class-wp-font-library.php
File metadata and controls
122 lines (110 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Font Library class.
*
* This file contains the Font Library class definition.
*
* @package WordPress
* @subpackage Font Library
* @since 6.4.0
*/
if ( class_exists( 'WP_Font_Library' ) ) {
return;
}
/**
* Font Library class.
*
* @since 6.4.0
*/
class WP_Font_Library {
const ALLOWED_FONT_MIME_TYPES = array(
'otf' => 'font/otf',
'ttf' => 'font/ttf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
);
/**
* Font collections.
*
* @since 6.4.0
*
* @var array
*/
private static $collections = array();
/**
* Register a new font collection.
*
* @since 6.4.0
*
* @param string $id Font collection id.
* @param array $config Font collection config options.
* @return WP_Font_Collection|WP_Error A font collection is it was registered succesfully and a WP_Error otherwise.
*/
public static function register_font_collection( $id, $config ) {
if ( isset( self::$collections[ $id ] ) ) {
return new WP_Error( 'font_collection_registration_error', 'Font collection already registered.' );
}
$new_collection = new WP_Font_Collection( $id, $config );
if ( is_wp_error( $new_collection ) ) {
return $new_collection;
}
self::$collections[ $id ] = $new_collection;
return $new_collection;
}
/**
* Gets all the font collections available.
*
* @since 6.4.0
*
* @return array List of font collections.
*/
public static function get_font_collections() {
return self::$collections;
}
/**
* Gets a font collection.
*
* @since 6.4.0
*
* @param string $id Font collection id.
* @return array List of font collections.
*/
public static function get_font_collection( $id ) {
if ( array_key_exists( $id, self::$collections ) ) {
return self::$collections[ $id ];
}
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
}
/**
* Gets the upload directory for fonts.
*
* @since 6.4.0
*
* @return string Path of the upload directory for fonts.
*/
public static function get_fonts_dir() {
return wp_upload_dir()['basedir'] . '/fonts';
}
/**
* Sets the upload directory for fonts.
*
* @since 6.4.0
*
* @param array $defaults {
* Default upload directory.
*
* @type string $path Path to the directory.
* @type string $url URL for the directory.
* @type string $subdir Sub-directory of the directory.
* @type string $basedir Base directory.
* @type string $baseurl Base URL.
* }
* @return array Modified upload directory.
*/
public static function set_upload_dir( $defaults ) {
$defaults['subdir'] = '/fonts';
$defaults['path'] = $defaults['basedir'] . $defaults['subdir'];
$defaults['url'] = $defaults['baseurl'] . $defaults['subdir'];
return $defaults;
}
}