| 1 |
<?php |
| 2 |
/** |
| 3 |
* MetaSync - Media Optimization Module Loader |
| 4 |
* |
| 5 |
* @package Search Atlas SEO |
| 6 |
* @copyright Copyright (C) 2021-2025, Search Atlas Group - support@searchatlas.com |
| 7 |
* @since 2.6.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
define('METASYNC_MEDIA_OPT_PATH', plugin_dir_path(__FILE__)); |
| 15 |
define('METASYNC_MEDIA_OPT_URL', plugin_dir_url(__FILE__)); |
| 16 |
|
| 17 |
// Load classes |
| 18 |
require_once METASYNC_MEDIA_OPT_PATH . 'class-media-settings.php'; |
| 19 |
require_once METASYNC_MEDIA_OPT_PATH . 'class-image-converter.php'; |
| 20 |
require_once METASYNC_MEDIA_OPT_PATH . 'class-smart-lazy-loader.php'; |
| 21 |
require_once METASYNC_MEDIA_OPT_PATH . 'class-dimension-injector.php'; |
| 22 |
require_once METASYNC_MEDIA_OPT_PATH . 'class-media-batch-optimizer.php'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize media optimization features based on settings. |
| 26 |
* Only activates when the plugin is active and settings are enabled. |
| 27 |
*/ |
| 28 |
function metasync_media_optimization_init() { |
| 29 |
$settings = Metasync_Media_Settings::get_settings(); |
| 30 |
|
| 31 |
// Frontend features - only load when enabled |
| 32 |
if (!empty($settings['enable_conversion'])) { |
| 33 |
new Metasync_Image_Converter($settings); |
| 34 |
} |
| 35 |
|
| 36 |
if (!empty($settings['enable_lazy_loading'])) { |
| 37 |
new Metasync_Smart_Lazy_Loader($settings); |
| 38 |
} |
| 39 |
|
| 40 |
if (!empty($settings['enable_dimension_injection'])) { |
| 41 |
new Metasync_Dimension_Injector(); |
| 42 |
} |
| 43 |
} |
| 44 |
add_action('init', 'metasync_media_optimization_init'); |
| 45 |
|
| 46 |
// Clean up converted sibling files (.webp/.avif) when an attachment is deleted. |
| 47 |
// Registered unconditionally so orphaned files are removed even if conversion |
| 48 |
// has since been disabled in settings. |
| 49 |
add_action('delete_attachment', ['Metasync_Image_Converter', 'cleanup_on_delete']); |
| 50 |
|
| 51 |
// Drop cached media-library stats when an attachment is deleted so the |
| 52 |
// admin stats card never counts removed images. |
| 53 |
add_action('delete_attachment', ['Metasync_Media_Batch_Optimizer', 'flush_stats_cache']); |
| 54 |
|
| 55 |
// Invalidate cached image dimensions when an attachment is deleted or its |
| 56 |
// metadata is (re)generated, so stale width/height are never injected after an |
| 57 |
// image is removed, replaced under the same filename, or its thumbnails are |
| 58 |
// regenerated to new sizes. Registered unconditionally so the cache cannot |
| 59 |
// outlive the image even when dimension injection is disabled. |
| 60 |
add_action('delete_attachment', ['Metasync_Dimension_Injector', 'purge_attachment_cache']); |
| 61 |
add_filter('wp_update_attachment_metadata', ['Metasync_Dimension_Injector', 'purge_on_metadata_update'], 10, 2); |
| 62 |
|
| 63 |
/** |
| 64 |
* Enqueue media optimization admin assets on the correct page only. |
| 65 |
* |
| 66 |
* @param string $hook_suffix The current admin page hook suffix. |
| 67 |
*/ |
| 68 |
function metasync_media_optimization_enqueue_assets($hook_suffix) { |
| 69 |
// Only load on the media optimization admin page. |
| 70 |
// The hook suffix varies based on the parent menu slug (which may be white-labeled). |
| 71 |
if (strpos($hook_suffix, 'media-optimization') === false) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$version = defined('METASYNC_VERSION') ? METASYNC_VERSION : '1.0.0'; |
| 76 |
$assets_url = METASYNC_MEDIA_OPT_URL . 'assets/'; |
| 77 |
|
| 78 |
// ── CSS ── |
| 79 |
wp_enqueue_style( |
| 80 |
'metasync-media-optimization', |
| 81 |
$assets_url . 'css/media-optimization.css', |
| 82 |
array(), |
| 83 |
$version |
| 84 |
); |
| 85 |
|
| 86 |
// ── Settings JS ── |
| 87 |
wp_enqueue_script( |
| 88 |
'metasync-media-opt-settings', |
| 89 |
$assets_url . 'js/media-optimization-settings.js', |
| 90 |
array(), |
| 91 |
$version, |
| 92 |
true |
| 93 |
); |
| 94 |
|
| 95 |
wp_localize_script('metasync-media-opt-settings', 'metasyncMediaOpt', array( |
| 96 |
'resetConfirm' => __('Are you sure you want to reset all media optimization settings to their defaults? This cannot be undone.', 'metasync'), |
| 97 |
)); |
| 98 |
|
| 99 |
// ── Image Library JS ── |
| 100 |
wp_enqueue_script( |
| 101 |
'metasync-media-opt-library', |
| 102 |
$assets_url . 'js/media-optimization-library.js', |
| 103 |
array(), |
| 104 |
$version, |
| 105 |
true |
| 106 |
); |
| 107 |
|
| 108 |
$batch_progress = Metasync_Media_Batch_Optimizer::get_progress(); |
| 109 |
|
| 110 |
wp_localize_script('metasync-media-opt-library', 'metasyncMediaLib', array( |
| 111 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 112 |
'nonce' => wp_create_nonce('metasync_media_opt_nonce'), |
| 113 |
'batchRunning' => ($batch_progress['status'] === 'running'), |
| 114 |
'i18n' => array( |
| 115 |
'optimizing' => __('Optimizing...', 'metasync'), |
| 116 |
'optimize' => __('Optimize', 'metasync'), |
| 117 |
'revert' => __('Revert', 'metasync'), |
| 118 |
'revertConfirm' => __('Revert this image to its original format?', 'metasync'), |
| 119 |
'optimizeFailed' => __('Optimization failed.', 'metasync'), |
| 120 |
'revertFailed' => __('Revert failed.', 'metasync'), |
| 121 |
'revertDisabled' => __('Original image unavailable — revert is disabled', 'metasync'), |
| 122 |
'startBatch' => __('Start optimizing all unoptimized images?', 'metasync'), |
| 123 |
'batchFailed' => __('Failed to start batch optimization.', 'metasync'), |
| 124 |
'batchStalled' => __('Batch optimization stopped after repeated errors. Please reload the page and try again.', 'metasync'), |
| 125 |
'batchComplete' => __('Batch optimization complete!', 'metasync'), |
| 126 |
'imagesProcessed' => __('images processed', 'metasync'), |
| 127 |
'failed' => __('failed', 'metasync'), |
| 128 |
'optimizingOf' => __('Optimizing', 'metasync'), |
| 129 |
'of' => __('of', 'metasync'), |
| 130 |
'images' => __('images...', 'metasync'), |
| 131 |
'unoptimized' => __('Unoptimized', 'metasync'), |
| 132 |
'selectImages' => __('Please select at least one image.', 'metasync'), |
| 133 |
'bulkFailed' => __('Bulk optimization failed.', 'metasync'), |
| 134 |
'apply' => __('Apply', 'metasync'), |
| 135 |
'unoptimizing' => __('Unoptimizing...', 'metasync'), |
| 136 |
'unoptimizeConfirm' => __('Revert selected images to their original format?', 'metasync'), |
| 137 |
'bulkUnoptimizeFailed' => __('Bulk unoptimize failed.', 'metasync'), |
| 138 |
'alreadyUnoptimized' => __('All selected images are already unoptimized.', 'metasync'), |
| 139 |
'deleteOrphanConfirm' => __('The file for this image is missing on disk. Delete this orphaned media record? This cannot be undone.', 'metasync'), |
| 140 |
'deleteOrphanFailed' => __('Failed to delete the orphaned media record.', 'metasync'), |
| 141 |
), |
| 142 |
)); |
| 143 |
} |
| 144 |
add_action('admin_enqueue_scripts', 'metasync_media_optimization_enqueue_assets'); |
| 145 |
|