PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.0.9
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.0.9
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / admin / image-optimizer.php

image-optimizer.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.0.9, at inc/admin/image-optimizer.php

143 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'savePercent' => __('Save ~%d%%', 'master-addons'),
73 'autoOptimize' => __('Auto-optimize images on upload', 'master-addons'),
74 'upgradeToPro' => __('Upgrade to Pro', 'master-addons'),
75 ],
76 ]);
77 }
78
79 /**
80 * Check if premium is active
81 */
82 protected function is_premium_active()
83 {
84 return function_exists('ma_el_fs') && ma_el_fs()->can_use_premium_code__premium_only();
85 }
86
87 /**
88 * Add optimization fields to attachment edit screen
89 */
90 public function add_optimization_fields($form_fields, $post)
91 {
92 if (!wp_attachment_is_image($post->ID)) {
93 return $form_fields;
94 }
95
96 // Skip on attachment edit page
97 global $pagenow;
98 if ($pagenow === 'post.php' && isset($_GET['action']) && $_GET['action'] === 'edit' && !wp_doing_ajax()) {
99 return $form_fields;
100 }
101
102 // Get file info for potential savings estimate
103 $file_path = get_attached_file($post->ID);
104 $file_size = $file_path && file_exists($file_path) ? filesize($file_path) : 0;
105
106 // Estimate potential savings (~60-80% for WebP conversion)
107 $savings_percent = rand(65, 85);
108 $potential_savings = $file_size > 0 ? round($file_size * ($savings_percent / 100)) : 0;
109 $potential_size = $file_size - $potential_savings;
110
111 // Build upsell card
112 $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;">';
113
114 if ($file_size > 0) {
115 $html .= '<div style="display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12px; color: #92400e;">';
116 $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>';
117 $html .= '<span style="font-weight: 600;">' . sprintf(__('Save ~%d%%', 'master-addons'), $savings_percent) . '</span>';
118 $html .= '<span style="color: #b45309;">(' . size_format($file_size) . ' → ~' . size_format($potential_size) . ')</span>';
119 $html .= '</div>';
120 } else {
121 $html .= '<div style="font-size: 12px; color: #92400e; font-weight: 600;">';
122 $html .= __('Auto-optimize images on upload', 'master-addons');
123 $html .= '</div>';
124 }
125
126 // Upgrade link
127 $pricing_url = function_exists('ma_el_fs') ? ma_el_fs()->get_upgrade_url() : 'https://master-addons.com/pricing/';
128 $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;">';
129 $html .= '<span class="dashicons dashicons-star-filled" style="font-size: 12px; width: 12px; height: 12px;"></span>';
130 $html .= __('Upgrade to Pro', 'master-addons');
131 $html .= '</a>';
132 $html .= '</div>';
133
134 $form_fields['jltma_optimization'] = [
135 'label' => __('MA Optimization', 'master-addons'),
136 'input' => 'html',
137 'html' => $html,
138 ];
139
140 return $form_fields;
141 }
142 }
143