-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoocommerce-image-protector.php
More file actions
271 lines (233 loc) · 7.83 KB
/
woocommerce-image-protector.php
File metadata and controls
271 lines (233 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* Plugin Name: WooCommerce Image Watermark & Protector
* Plugin URI: https://wordpress.org/plugins/woocommerce-image-protector
* Description: Protect WooCommerce product images with dynamic watermarks and right-click protection. Configure position, opacity, and scale from admin panel.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://yourwebsite.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: woo-image-protector
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
* WC requires at least: 5.0
* WC tested up to: 8.0
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('WIP_VERSION', '1.0.0');
define('WIP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WIP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WIP_CACHE_DIR', WP_CONTENT_DIR . '/uploads/watermarked-cache/');
define('WIP_CACHE_URL', content_url('/uploads/watermarked-cache/'));
/**
* Main Plugin Class
*/
class WooCommerce_Image_Protector {
private static $instance = null;
/**
* Get singleton instance
*/
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
// Check if WooCommerce is active
if (!$this->is_woocommerce_active()) {
add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
return;
}
// Check PHP GD Library
if (!extension_loaded('gd')) {
add_action('admin_notices', array($this, 'gd_library_missing_notice'));
return;
}
// Load plugin files
$this->load_dependencies();
// Initialize hooks
$this->init_hooks();
}
/**
* Check if WooCommerce is active
*/
private function is_woocommerce_active() {
return in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')));
}
/**
* Load plugin dependencies
*/
private function load_dependencies() {
// Core includes
require_once WIP_PLUGIN_DIR . 'includes/image-detector.php';
require_once WIP_PLUGIN_DIR . 'includes/watermark-handler.php';
require_once WIP_PLUGIN_DIR . 'includes/cache-manager.php';
require_once WIP_PLUGIN_DIR . 'includes/image-filter.php';
// Admin includes
if (is_admin()) {
require_once WIP_PLUGIN_DIR . 'admin/admin-settings.php';
require_once WIP_PLUGIN_DIR . 'admin/settings-handler.php';
require_once WIP_PLUGIN_DIR . 'admin/preview-generator.php';
}
}
/**
* Initialize hooks
*/
private function init_hooks() {
// Activation and deactivation hooks
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// Frontend scripts and styles
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
// Admin scripts and styles
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets'));
// Initialize components
add_action('init', array($this, 'init_components'));
}
/**
* Initialize plugin components
*/
public function init_components() {
if (is_admin()) {
new WIP_Admin_Settings();
new WIP_Settings_Handler();
new WIP_Preview_Generator();
}
new WIP_Image_Detector();
new WIP_Image_Filter();
}
/**
* Plugin activation
*/
public function activate() {
// Create cache directory
if (!file_exists(WIP_CACHE_DIR)) {
wp_mkdir_p(WIP_CACHE_DIR);
}
// Create .htaccess file to protect cache directory
$htaccess_file = WIP_CACHE_DIR . '.htaccess';
if (!file_exists($htaccess_file)) {
$htaccess_content = "# Protect cache directory\n";
$htaccess_content .= "<FilesMatch \"\\.(jpg|jpeg|png)$\">\n";
$htaccess_content .= " Order Allow,Deny\n";
$htaccess_content .= " Allow from all\n";
$htaccess_content .= "</FilesMatch>\n";
file_put_contents($htaccess_file, $htaccess_content);
}
// Set default options
$defaults = array(
'wip_enable_watermark' => 0,
'wip_enable_right_click_protection' => 0,
'wip_watermark_image_id' => 0,
'wip_watermark_position' => 'bottom-right',
'wip_watermark_opacity' => 70,
'wip_watermark_scale' => 100
);
foreach ($defaults as $key => $value) {
if (get_option($key) === false) {
add_option($key, $value);
}
}
// Set activation flag for admin notice
set_transient('wip_activation_notice', true, 30);
}
/**
* Plugin deactivation
*/
public function deactivate() {
// Optional: Clear cache on deactivation
// WIP_Cache_Manager::clear_all_cache();
}
/**
* Enqueue frontend assets
*/
public function enqueue_frontend_assets() {
// Only load on product pages if protection is enabled
if (!is_product()) {
return;
}
$enable_protection = get_option('wip_enable_right_click_protection', 0);
if ($enable_protection) {
wp_enqueue_style(
'wip-protection-css',
WIP_PLUGIN_URL . 'assets/css/protection.css',
array(),
WIP_VERSION
);
wp_enqueue_script(
'wip-protection-js',
WIP_PLUGIN_URL . 'assets/js/image-protection.js',
array(),
WIP_VERSION,
true
);
}
}
/**
* Enqueue admin assets
*/
public function enqueue_admin_assets($hook) {
// Only load on our settings page
if ($hook !== 'woocommerce_page_wip-settings') {
return;
}
wp_enqueue_media();
wp_enqueue_style(
'wip-admin-css',
WIP_PLUGIN_URL . 'assets/css/admin-style.css',
array(),
WIP_VERSION
);
wp_enqueue_script(
'wip-admin-preview-js',
WIP_PLUGIN_URL . 'assets/js/admin-preview.js',
array('jquery'),
WIP_VERSION,
true
);
// Localize script
wp_localize_script('wip-admin-preview-js', 'wipSettings', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('wip_preview_nonce'),
'pluginUrl' => WIP_PLUGIN_URL
));
}
/**
* WooCommerce missing notice
*/
public function woocommerce_missing_notice() {
?>
<div class="error">
<p><?php _e('WooCommerce Image Watermark & Protector requires WooCommerce to be installed and active.', 'woo-image-protector'); ?></p>
</div>
<?php
}
/**
* GD Library missing notice
*/
public function gd_library_missing_notice() {
?>
<div class="error">
<p><?php _e('WooCommerce Image Watermark & Protector requires PHP GD Library extension to be installed.', 'woo-image-protector'); ?></p>
</div>
<?php
}
}
/**
* Initialize the plugin
*/
function wip_init() {
return WooCommerce_Image_Protector::get_instance();
}
// Start the plugin
add_action('plugins_loaded', 'wip_init');