| 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( 'easyfonts_probe', '1', $url ); |
| 40 |
|
| 41 |
$response = wp_remote_get( |
| 42 |
$probe, |
| 43 |
array( |
| 44 |
'timeout' => 60, |
| 45 |
'cookies' => $this->auth_cookies(), |
| 46 |
'sslverify' => (bool) apply_filters( 'easyfonts_loopback_sslverify', true ), |
| 47 |
) |
| 48 |
); |
| 49 |
|
| 50 |
if ( is_wp_error( $response ) ) { |
| 51 |
\WP_CLI::warning( sprintf( '%s — %s', $url, $response->get_error_message() ) ); |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
\WP_CLI::log( sprintf( 'Probed %s (HTTP %d)', $url, (int) wp_remote_retrieve_response_code( $response ) ) ); |
| 56 |
} |
| 57 |
|
| 58 |
$stats = ( new Registry() )->stats(); |
| 59 |
\WP_CLI::success( sprintf( '%d families / %d variants hosted.', $stats['families'], $stats['variants'] ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* List hosted font families. |
| 64 |
* |
| 65 |
* @subcommand list |
| 66 |
*/ |
| 67 |
public function list_fonts(): void { |
| 68 |
$grouped = ( new Registry() )->grouped(); |
| 69 |
|
| 70 |
if ( empty( $grouped ) ) { |
| 71 |
\WP_CLI::log( 'No fonts hosted yet. Run: wp easyfonts scan <url>' ); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$rows = array(); |
| 76 |
|
| 77 |
foreach ( $grouped as $family ) { |
| 78 |
$rows[] = array( |
| 79 |
'family' => $family['family'], |
| 80 |
'variants' => count( $family['variants'] ), |
| 81 |
'variable' => $family['is_variable'] ? 'yes' : 'no', |
| 82 |
'metrics' => $family['has_metrics'] ? 'yes' : 'no', |
| 83 |
'size_kb' => (int) round( $family['total_size'] / 1024 ), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
\WP_CLI\Utils\format_items( 'table', $rows, array( 'family', 'variants', 'variable', 'metrics', 'size_kb' ) ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Purge all cached fonts, stylesheets, and learned state. |
| 92 |
*/ |
| 93 |
public function purge(): void { |
| 94 |
( new Storage() )->purge(); |
| 95 |
( new Registry() )->truncate(); |
| 96 |
( new UsageTracker() )->truncate(); |
| 97 |
|
| 98 |
delete_option( 'easyfonts_processed_local' ); |
| 99 |
delete_option( 'easyfonts_processed_external' ); |
| 100 |
|
| 101 |
$settings = Settings::all(); |
| 102 |
$settings['detectors'] = array(); |
| 103 |
Settings::save( $settings ); |
| 104 |
Settings::bump_buster(); |
| 105 |
|
| 106 |
\WP_CLI::success( 'Cache and learned state cleared.' ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Drop and recreate the database tables (fixes a stale/legacy schema). |
| 111 |
*/ |
| 112 |
public function repair(): void { |
| 113 |
( new \EasyFonts\Database\Migrator() )->repair(); |
| 114 |
( new Storage() )->purge(); |
| 115 |
|
| 116 |
delete_option( 'easyfonts_processed_local' ); |
| 117 |
delete_option( 'easyfonts_processed_external' ); |
| 118 |
|
| 119 |
$settings = Settings::all(); |
| 120 |
$settings['detectors'] = array(); |
| 121 |
Settings::save( $settings ); |
| 122 |
Settings::bump_buster(); |
| 123 |
|
| 124 |
\WP_CLI::success( 'Tables rebuilt and cache cleared.' ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Print a diagnostics report as JSON. |
| 129 |
*/ |
| 130 |
public function diagnostics(): void { |
| 131 |
\WP_CLI::log( wp_json_encode( Diagnostics::collect(), JSON_PRETTY_PRINT ) ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Build a cookie jar so probes run as an authenticated admin if possible. |
| 136 |
* |
| 137 |
* @return array<int,\WP_Http_Cookie> |
| 138 |
*/ |
| 139 |
private function auth_cookies(): array { |
| 140 |
return array(); |
| 141 |
} |
| 142 |
} |
| 143 |
|