| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin orchestrator. |
| 4 |
* |
| 5 |
* @package EasyFonts |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace EasyFonts; |
| 9 |
|
| 10 |
use EasyFonts\Admin\AdminPage; |
| 11 |
use EasyFonts\Admin\RestApi; |
| 12 |
use EasyFonts\Database\Migrator; |
| 13 |
use EasyFonts\Fonts\UsageTracker; |
| 14 |
use EasyFonts\Frontend\Beacon; |
| 15 |
use EasyFonts\Frontend\OutputBuffer; |
| 16 |
use EasyFonts\Fonts\MetricsBackfill; |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Singleton that wires the plugin together. |
| 22 |
*/ |
| 23 |
class Plugin { |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Plugin|null |
| 27 |
*/ |
| 28 |
private static ?Plugin $instance = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Singleton accessor. |
| 32 |
* |
| 33 |
* @return Plugin |
| 34 |
*/ |
| 35 |
public static function instance(): Plugin { |
| 36 |
if ( null === self::$instance ) { |
| 37 |
self::$instance = new self(); |
| 38 |
self::$instance->boot(); |
| 39 |
} |
| 40 |
|
| 41 |
return self::$instance; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Wire subsystems. |
| 46 |
*/ |
| 47 |
private function boot(): void { |
| 48 |
$this->maybe_migrate(); |
| 49 |
|
| 50 |
( new OutputBuffer() )->boot(); |
| 51 |
( new Beacon() )->boot(); |
| 52 |
( new RestApi() )->boot(); |
| 53 |
|
| 54 |
if ( is_admin() ) { |
| 55 |
( new AdminPage() )->boot(); |
| 56 |
( new MetricsBackfill() )->boot(); |
| 57 |
} |
| 58 |
|
| 59 |
// Weekly housekeeping. |
| 60 |
add_action( 'easyfonts_cleanup', array( $this, 'cleanup' ) ); |
| 61 |
|
| 62 |
if ( ! wp_next_scheduled( 'easyfonts_cleanup' ) ) { |
| 63 |
wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'easyfonts_cleanup' ); |
| 64 |
} |
| 65 |
|
| 66 |
load_plugin_textdomain( 'easyfonts', false, dirname( EASYFONTS_BASENAME ) . '/languages' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Install / repair tables on load. |
| 71 |
* |
| 72 |
* Triggers when the version moved OR the live schema is wrong — the latter |
| 73 |
* is what heals a site carrying an incompatible legacy table even though the |
| 74 |
* version option already reads as current. The check is cached for the day |
| 75 |
* so it costs one tiny query at most once per 24h on healthy sites. |
| 76 |
*/ |
| 77 |
private function maybe_migrate(): void { |
| 78 |
if ( get_option( 'easyfonts_db_version' ) !== Migrator::DB_VERSION ) { |
| 79 |
( new Migrator() )->install(); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if ( get_transient( 'easyfonts_schema_checked' ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
set_transient( 'easyfonts_schema_checked', 1, DAY_IN_SECONDS ); |
| 88 |
|
| 89 |
$migrator = new Migrator(); |
| 90 |
|
| 91 |
if ( ! $migrator->tables_ok() ) { |
| 92 |
$migrator->install(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Scheduled cleanup — prune stale usage/decisions rows so the tables stay |
| 98 |
* bounded on long-running and high-traffic sites. |
| 99 |
*/ |
| 100 |
public function cleanup(): void { |
| 101 |
$tracker = new UsageTracker(); |
| 102 |
|
| 103 |
/** |
| 104 |
* Retention window (days) for usage/decision rows. |
| 105 |
* |
| 106 |
* @param int $days |
| 107 |
*/ |
| 108 |
$days = (int) apply_filters( 'easyfonts_gc_retention_days', 30 ); |
| 109 |
|
| 110 |
/** |
| 111 |
* Hard cap on usage rows after time-based pruning. |
| 112 |
* |
| 113 |
* @param int $max |
| 114 |
*/ |
| 115 |
$max = (int) apply_filters( 'easyfonts_gc_max_usage_rows', 5000 ); |
| 116 |
|
| 117 |
$tracker->gc( $days, $max ); |
| 118 |
|
| 119 |
/** |
| 120 |
* Fires on the weekly Easy Fonts cleanup. |
| 121 |
* |
| 122 |
* @param UsageTracker $tracker |
| 123 |
*/ |
| 124 |
do_action( 'easyfonts_run_cleanup', $tracker ); |
| 125 |
} |
| 126 |
} |
| 127 |
|