PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 2.0.0
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v2.0.0
2.0.3 2.0.2 2.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.3 2.0.0
easyfonts / easyfonts.php

easyfonts.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 2.0.0, at easyfonts.php

180 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Easy Fonts
4 * Plugin URI: https://fluxpress.io
5 * Description: Detect, self-host, preload, and trim Google Fonts automatically — reliably, with zero-CLS metric-matched fallbacks. Built on WordPress's native HTML API.
6 * Version: 2.0.0
7 * Author: FluxPress
8 * Author URI: https://fluxpress.io
9 * License: GPL-2.0-or-later
10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 * Text Domain: easyfonts
12 * Domain Path: /languages
13 * Requires at least: 6.4
14 * Requires PHP: 7.4
15 *
16 * @package EasyFonts
17 */
18
19 defined( 'ABSPATH' ) || exit;
20
21 define( 'EASYFONTS_VERSION', '2.0.0' );
22 define( 'EASYFONTS_FILE', __FILE__ );
23 define( 'EASYFONTS_DIR', plugin_dir_path( __FILE__ ) );
24 define( 'EASYFONTS_URL', plugin_dir_url( __FILE__ ) );
25 define( 'EASYFONTS_BASENAME', plugin_basename( __FILE__ ) );
26
27 /**
28 * PSR-4 autoloader for the EasyFonts\ namespace.
29 */
30 spl_autoload_register(
31 static function ( $class ) {
32 $prefix = 'EasyFonts\\';
33 $len = strlen( $prefix );
34
35 if ( 0 !== strncmp( $prefix, $class, $len ) ) {
36 return;
37 }
38
39 $relative = substr( $class, $len );
40 $path = EASYFONTS_DIR . 'src/' . str_replace( '\\', '/', $relative ) . '.php';
41
42 if ( is_readable( $path ) ) {
43 require_once $path;
44 }
45 }
46 );
47
48 /**
49 * Resolve the upload directory + URL once. Scheme-correct, multisite-safe.
50 *
51 * @return array{path:string,url:string}
52 */
53 function easyfonts_paths(): array {
54 static $paths = null;
55
56 if ( null === $paths ) {
57 $upload = wp_upload_dir();
58 $paths = array(
59 'path' => trailingslashit( $upload['basedir'] ) . 'easyfonts',
60 'url' => set_url_scheme( trailingslashit( $upload['baseurl'] ) . 'easyfonts' ),
61 );
62 }
63
64 return $paths;
65 }
66
67 /**
68 * Boot the plugin on plugins_loaded.
69 */
70 add_action(
71 'plugins_loaded',
72 static function () {
73 $paths = easyfonts_paths();
74
75 if ( ! defined( 'EASYFONTS_CACHE_DIR' ) ) {
76 define( 'EASYFONTS_CACHE_DIR', $paths['path'] );
77 }
78 if ( ! defined( 'EASYFONTS_CACHE_URL' ) ) {
79 define( 'EASYFONTS_CACHE_URL', $paths['url'] );
80 }
81
82 \EasyFonts\Plugin::instance();
83 }
84 );
85
86 /**
87 * WP-CLI registration (loaded outside the normal request lifecycle).
88 */
89 if ( defined( 'WP_CLI' ) && WP_CLI ) {
90 add_action(
91 'cli_init',
92 static function () {
93 \WP_CLI::add_command( 'easyfonts', \EasyFonts\Cli\Command::class );
94 }
95 );
96 }
97
98 /**
99 * Activation — create tables, seed defaults, mark for rewrite.
100 *
101 * On multisite the activation hook fires once even for a network-wide enable,
102 * so when network-activated we install on every existing site. New sites
103 * created later are handled by the wp_initialize_site hook below, and any site
104 * that somehow slips through self-heals on its first load (Plugin::maybe_migrate).
105 *
106 * @param bool $network_wide Whether the plugin is being network-activated.
107 */
108 register_activation_hook(
109 __FILE__,
110 static function ( $network_wide = false ) {
111 require_once EASYFONTS_DIR . 'src/Database/Migrator.php';
112
113 $install = static function () {
114 ( new \EasyFonts\Database\Migrator() )->install();
115 add_option( 'easyfonts_activated_at', time() );
116 };
117
118 if ( is_multisite() && $network_wide ) {
119 $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
120
121 foreach ( $site_ids as $site_id ) {
122 switch_to_blog( (int) $site_id );
123 $install();
124 restore_current_blog();
125 }
126
127 return;
128 }
129
130 $install();
131 }
132 );
133
134 /**
135 * Multisite: install tables when a brand-new subsite is created, if the plugin
136 * is network-active. (WP 5.1+ passes a WP_Site; older signatures are tolerated.)
137 *
138 * @param mixed $site New site (WP_Site) or blog id.
139 */
140 add_action(
141 'wp_initialize_site',
142 static function ( $site ) {
143 if ( ! is_multisite() ) {
144 return;
145 }
146
147 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
148 require_once ABSPATH . 'wp-admin/includes/plugin.php';
149 }
150
151 if ( ! is_plugin_active_for_network( EASYFONTS_BASENAME ) ) {
152 return;
153 }
154
155 $blog_id = is_object( $site ) && isset( $site->blog_id ) ? (int) $site->blog_id : (int) $site;
156
157 if ( $blog_id <= 0 ) {
158 return;
159 }
160
161 require_once EASYFONTS_DIR . 'src/Database/Migrator.php';
162
163 switch_to_blog( $blog_id );
164 ( new \EasyFonts\Database\Migrator() )->install();
165 restore_current_blog();
166 },
167 10,
168 1
169 );
170
171 /**
172 * Deactivation — clear scheduled events.
173 */
174 register_deactivation_hook(
175 __FILE__,
176 static function () {
177 wp_clear_scheduled_hook( 'easyfonts_cleanup' );
178 }
179 );
180