PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 2.0.2
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v2.0.2
2.0.4 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 / src / Cli / Command.php

Command.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 2.0.2, at src/Cli/Command.php

149 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-CLI commands.
4 *
5 * @package EasyFonts
6 */
7
8 namespace EasyFonts\Cli;
9
10 use EasyFonts\Admin\Diagnostics;
11 use EasyFonts\Fonts\Registry;
12 use EasyFonts\Fonts\Storage;
13 use EasyFonts\Fonts\UsageTracker;
14 use EasyFonts\Settings;
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * Manage Easy Fonts from the command line.
20 */
21 class Command {
22
23 /**
24 * Probe one or more URLs to detect + host their fonts.
25 *
26 * ## OPTIONS
27 *
28 * <url>...
29 * : One or more front-end URLs to probe.
30 *
31 * ## EXAMPLES
32 *
33 * wp easyfonts scan https://example.com https://example.com/about
34 *
35 * @param string[] $args Positional args.
36 */
37 public function scan( array $args ): void {
38 foreach ( $args as $url ) {
39 $probe = add_query_arg(
40 array(
41 'easyfonts_probe' => Settings::warm_key(),
42 'efbust' => (string) time(),
43 ),
44 $url
45 );
46
47 $response = wp_remote_get(
48 $probe,
49 array(
50 'timeout' => 60,
51 'cookies' => $this->auth_cookies(),
52 'sslverify' => (bool) apply_filters( 'easyfonts_loopback_sslverify', true ),
53 )
54 );
55
56 if ( is_wp_error( $response ) ) {
57 \WP_CLI::warning( sprintf( '%s — %s', $url, $response->get_error_message() ) );
58 continue;
59 }
60
61 \WP_CLI::log( sprintf( 'Probed %s (HTTP %d)', $url, (int) wp_remote_retrieve_response_code( $response ) ) );
62 }
63
64 $stats = ( new Registry() )->stats();
65 \WP_CLI::success( sprintf( '%d families / %d variants hosted.', $stats['families'], $stats['variants'] ) );
66 }
67
68 /**
69 * List hosted font families.
70 *
71 * @subcommand list
72 */
73 public function list_fonts(): void {
74 $grouped = ( new Registry() )->grouped();
75
76 if ( empty( $grouped ) ) {
77 \WP_CLI::log( 'No fonts hosted yet. Run: wp easyfonts scan <url>' );
78 return;
79 }
80
81 $rows = array();
82
83 foreach ( $grouped as $family ) {
84 $rows[] = array(
85 'family' => $family['family'],
86 'variants' => count( $family['variants'] ),
87 'variable' => $family['is_variable'] ? 'yes' : 'no',
88 'metrics' => $family['has_metrics'] ? 'yes' : 'no',
89 'size_kb' => (int) round( $family['total_size'] / 1024 ),
90 );
91 }
92
93 \WP_CLI\Utils\format_items( 'table', $rows, array( 'family', 'variants', 'variable', 'metrics', 'size_kb' ) );
94 }
95
96 /**
97 * Purge all cached fonts, stylesheets, and learned state.
98 */
99 public function purge(): void {
100 ( new Storage() )->purge();
101 ( new Registry() )->truncate();
102 ( new UsageTracker() )->truncate();
103
104 delete_option( 'easyfonts_processed_local' );
105 delete_option( 'easyfonts_processed_external' );
106
107 $settings = Settings::all();
108 $settings['detectors'] = array();
109 Settings::save( $settings );
110 Settings::bump_buster();
111
112 \WP_CLI::success( 'Cache and learned state cleared.' );
113 }
114
115 /**
116 * Drop and recreate the database tables (fixes a stale/legacy schema).
117 */
118 public function repair(): void {
119 ( new \EasyFonts\Database\Migrator() )->repair();
120 ( new Storage() )->purge();
121
122 delete_option( 'easyfonts_processed_local' );
123 delete_option( 'easyfonts_processed_external' );
124
125 $settings = Settings::all();
126 $settings['detectors'] = array();
127 Settings::save( $settings );
128 Settings::bump_buster();
129
130 \WP_CLI::success( 'Tables rebuilt and cache cleared.' );
131 }
132
133 /**
134 * Print a diagnostics report as JSON.
135 */
136 public function diagnostics(): void {
137 \WP_CLI::log( wp_json_encode( Diagnostics::collect(), JSON_PRETTY_PRINT ) );
138 }
139
140 /**
141 * Build a cookie jar so probes run as an authenticated admin if possible.
142 *
143 * @return array<int,\WP_Http_Cookie>
144 */
145 private function auth_cookies(): array {
146 return array();
147 }
148 }
149