Files
h4b-image-optim/includes/class-plugin.php
Henk 7e1c86f215 feat: initial v0.1.0 MVP
Replaces Smush Pro's optimisation pipeline without the grey-wash bug.

CLI commands working:
  wp h4b-img status
  wp h4b-img optimise --id=<n>
  wp h4b-img bulk
  wp h4b-img rescue

Verified on dev.rds.ink:
- ICC profile preservation works (the Smush-bug fix)
- Bulk: 20 attachments → 487 KB saved (10.4%), 0 errors
- Rescue: end-to-end mechanism verified on WorkingAsOne_horse fixture
- WebP synchronous, AVIF queued via WP-Cron
- Originals backed up to wp-content/h4b-img-originals/

See CHANGELOG.md for details + ../DESIGN-h4b-image-optim.md for architecture.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-19 13:41:03 +10:00

81 lines
2.4 KiB
PHP

<?php
/**
* Plugin loader. Wires hooks; defers heavy work until needed.
*
* @package H4B\ImageOptim
*/
namespace H4B\ImageOptim;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once H4B_IMG_OPTIM_DIR . 'includes/class-tools.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-settings.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-icc-profile.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-attachment-meta.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-optimizer.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-format-generator.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-uploader-hook.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-rescue-detector.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-cli.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-cli-bulk.php';
require_once H4B_IMG_OPTIM_DIR . 'includes/class-cli-rescue.php';
final class Plugin {
private static ?self $instance = null;
public static function instance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function boot(): void {
// Settings load first — everything else reads them.
Settings::register();
// Activation / deactivation
register_activation_hook( H4B_IMG_OPTIM_FILE, [ $this, 'on_activate' ] );
register_deactivation_hook( H4B_IMG_OPTIM_FILE, [ $this, 'on_deactivate' ] );
// Upload pipeline
Uploader_Hook::register();
// Background AVIF queue (WP-Cron)
Format_Generator::register_cron();
// Backup pruning cron
add_action( 'h4b_img_prune_originals', [ Optimizer::class, 'prune_originals' ] );
add_action( 'init', [ $this, 'maybe_schedule_cron' ] );
// WP-CLI
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
CLI::register();
}
}
public function on_activate(): void {
Settings::install_defaults();
if ( ! wp_next_scheduled( 'h4b_img_prune_originals' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'h4b_img_prune_originals' );
}
// Make sure the originals dir + .htaccess exist
Optimizer::ensure_dirs();
}
public function on_deactivate(): void {
wp_clear_scheduled_hook( 'h4b_img_prune_originals' );
wp_clear_scheduled_hook( 'h4b_img_generate_avif' );
}
public function maybe_schedule_cron(): void {
if ( ! wp_next_scheduled( 'h4b_img_prune_originals' ) ) {
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'h4b_img_prune_originals' );
}
}
}