| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image Optimizer (Free Version) |
| 4 |
* |
| 5 |
* Shows potential optimization savings in media library with upgrade prompt. |
| 6 |
* Actual optimization functionality is in premium/admin/image-optimizer/ |
| 7 |
* |
| 8 |
* @package MasterAddons\Inc\Admin |
| 9 |
* @since 2.1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace MasterAddons\Inc\Admin; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
class Image_Optimizer |
| 19 |
{ |
| 20 |
private static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get singleton instance |
| 24 |
*/ |
| 25 |
public static function get_instance() |
| 26 |
{ |
| 27 |
if (null === self::$instance) { |
| 28 |
self::$instance = new self(); |
| 29 |
} |
| 30 |
return self::$instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
// Only show for free users (not premium) |
| 39 |
if ($this->is_premium_active()) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
// Enqueue base JS for media library display |
| 44 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']); |
| 45 |
|
| 46 |
// Add display to media library |
| 47 |
add_filter('attachment_fields_to_edit', [$this, 'add_optimization_fields'], 10, 2); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Enqueue admin assets for free version |
| 52 |
*/ |
| 53 |
public function enqueue_admin_assets($hook) |
| 54 |
{ |
| 55 |
// Only load on media pages |
| 56 |
$media_pages = ['upload.php', 'post.php', 'post-new.php', 'media-upload.php']; |
| 57 |
if (!in_array($hook, $media_pages) && strpos($hook, 'media') === false) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Use Assets_Manager for enqueue (single source of truth) |
| 62 |
\MasterAddons\Inc\Classes\Assets_Manager::enqueue('image-optimizer-base'); |
| 63 |
|
| 64 |
// Localize script with settings |
| 65 |
$pricing_url = 'https://master-addons.com/pricing/'; |
| 66 |
|
| 67 |
wp_localize_script('jltma-image-optimizer-base', 'jltmaImageOptimizerBase', [ |
| 68 |
'isPremium' => false, |
| 69 |
'pricingUrl' => $pricing_url, |
| 70 |
'settings' => [], |
| 71 |
'i18n' => [ |
| 72 |
/* translators: %d is the percentage of savings. */ |
| 73 |
'savePercent' => __('Save ~%d%%', 'master-addons'), |
| 74 |
'autoOptimize' => __('Auto-optimize images on upload', 'master-addons'), |
| 75 |
'upgradeToPro' => __('Upgrade to Pro', 'master-addons'), |
| 76 |
], |
| 77 |
]); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Check if premium is active |
| 82 |
*/ |
| 83 |
protected function is_premium_active() |
| 84 |
{ |
| 85 |
return function_exists('ma_el_fs') && ma_el_fs()->can_use_premium_code__premium_only(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add optimization fields to attachment edit screen |
| 90 |
*/ |
| 91 |
public function add_optimization_fields($form_fields, $post) |
| 92 |
{ |
| 93 |
if (!wp_attachment_is_image($post->ID)) { |
| 94 |
return $form_fields; |
| 95 |
} |
| 96 |
|
| 97 |
// Skip on attachment edit page |
| 98 |
global $pagenow; |
| 99 |
if ($pagenow === 'post.php' && isset($_GET['action']) && $_GET['action'] === 'edit' && !wp_doing_ajax()) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin context detection, no data processed |
| 100 |
return $form_fields; |
| 101 |
} |
| 102 |
|
| 103 |
// Get file info for potential savings estimate |
| 104 |
$file_path = get_attached_file($post->ID); |
| 105 |
$file_size = $file_path && file_exists($file_path) ? filesize($file_path) : 0; |
| 106 |
|
| 107 |
// Estimate potential savings (~60-80% for WebP conversion) |
| 108 |
$savings_percent = wp_rand(65, 85); |
| 109 |
$potential_savings = $file_size > 0 ? round($file_size * ($savings_percent / 100)) : 0; |
| 110 |
$potential_size = $file_size - $potential_savings; |
| 111 |
|
| 112 |
// Build upsell card |
| 113 |
$html = '<div class="jltma-optimization-card jltma-upsell-card" style="display: block; margin: 8px 0; padding: 10px 12px; background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%); border: 1px solid #fbbf24; border-radius: 8px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">'; |
| 114 |
|
| 115 |
if ($file_size > 0) { |
| 116 |
$html .= '<div style="display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12px; color: #92400e;">'; |
| 117 |
$html .= '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" style="flex-shrink: 0;"><circle cx="12" cy="12" r="10" stroke="#d97706" stroke-width="2"/><path d="M12 6v6l4 2" stroke="#d97706" stroke-width="2" stroke-linecap="round"/></svg>'; |
| 118 |
/* translators: %d is the percentage of savings. */ |
| 119 |
$html .= '<span style="font-weight: 600;">' . sprintf(__('Save ~%d%%', 'master-addons'), $savings_percent) . '</span>'; |
| 120 |
$html .= '<span style="color: #b45309;">(' . size_format($file_size) . ' → ~' . size_format($potential_size) . ')</span>'; |
| 121 |
$html .= '</div>'; |
| 122 |
} else { |
| 123 |
$html .= '<div style="font-size: 12px; color: #92400e; font-weight: 600;">'; |
| 124 |
$html .= __('Auto-optimize images on upload', 'master-addons'); |
| 125 |
$html .= '</div>'; |
| 126 |
} |
| 127 |
|
| 128 |
// Upgrade link |
| 129 |
$pricing_url = function_exists('ma_el_fs') ? ma_el_fs()->get_upgrade_url() : 'https://master-addons.com/pricing/'; |
| 130 |
$html .= '<a href="' . esc_url($pricing_url) . '" target="_blank" style="display: inline-flex; align-items: center; gap: 4px; margin-top: 6px; font-size: 11px; font-weight: 600; color: #d97706; text-decoration: none;">'; |
| 131 |
$html .= '<span class="dashicons dashicons-star-filled" style="font-size: 12px; width: 12px; height: 12px;"></span>'; |
| 132 |
$html .= __('Upgrade to Pro', 'master-addons'); |
| 133 |
$html .= '</a>'; |
| 134 |
$html .= '</div>'; |
| 135 |
|
| 136 |
$form_fields['jltma_optimization'] = [ |
| 137 |
'label' => __('MA Optimization', 'master-addons'), |
| 138 |
'input' => 'html', |
| 139 |
'html' => $html, |
| 140 |
]; |
| 141 |
|
| 142 |
return $form_fields; |
| 143 |
} |
| 144 |
} |
| 145 |
|