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

151 lines 3.6 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 delete_option( 'easyfonts_warm_key' ); // Rotate the loopback secret.
107
108 $settings = Settings::all();
109 $settings['detectors'] = array();
110 Settings::save( $settings );
111 Settings::bump_buster();
112
113 \WP_CLI::success( 'Cache and learned state cleared.' );
114 }
115
116 /**
117 * Drop and recreate the database tables (fixes a stale/legacy schema).
118 */
119 public function repair(): void {
120 ( new \EasyFonts\Database\Migrator() )->repair();
121 ( new Storage() )->purge();
122
123 delete_option( 'easyfonts_processed_local' );
124 delete_option( 'easyfonts_processed_external' );
125 delete_option( 'easyfonts_warm_key' ); // Rotate the loopback secret.
126
127 $settings = Settings::all();
128 $settings['detectors'] = array();
129 Settings::save( $settings );
130 Settings::bump_buster();
131
132 \WP_CLI::success( 'Tables rebuilt and cache cleared.' );
133 }
134
135 /**
136 * Print a diagnostics report as JSON.
137 */
138 public function diagnostics(): void {
139 \WP_CLI::log( wp_json_encode( Diagnostics::collect(), JSON_PRETTY_PRINT ) );
140 }
141
142 /**
143 * Build a cookie jar so probes run as an authenticated admin if possible.
144 *
145 * @return array<int,\WP_Http_Cookie>
146 */
147 private function auth_cookies(): array {
148 return array();
149 }
150 }
151