-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathopen-graph-for-woocommerce.php
More file actions
469 lines (399 loc) · 14 KB
/
open-graph-for-woocommerce.php
File metadata and controls
469 lines (399 loc) · 14 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* Plugin Name: Open Graph for WooCommerce
* Plugin URI: https://wbcomdesigns.com/downloads/woo-open-graph/
* Description: Comprehensive Schema.org markup, Open Graph optimization, and social sharing for WooCommerce. Fill the gaps that free SEO plugins miss.
* Version: 2.0.1
* Author: Wbcom Designs
* Author URI: https://wbcomdesigns.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: woo-open-graph
* Domain Path: /languages
* Requires at least: 5.0
* Tested up to: 6.8.1
* Requires PHP: 7.4
* WC requires at least: 4.0
* WC tested up to: 8.5
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// WooCommerce compatibility declarations
add_action('before_woocommerce_init', function() {
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
'custom_order_tables',
__FILE__,
true
);
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
'cart_checkout_blocks',
__FILE__,
true
);
}
});
// Define plugin constants
define('WOG_VERSION', '2.0.1');
define('WOG_PLUGIN_FILE', __FILE__);
define('WOG_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WOG_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* Main plugin class
*/
class Woo_Open_Graph {
private static $instance = null;
private $settings;
/**
* Get singleton instance
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize plugin
*/
private function __construct() {
$this->init_hooks();
$this->load_dependencies();
}
/**
* Set up WordPress hooks
*/
private function init_hooks() {
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
add_action('plugins_loaded', array($this, 'init'));
add_action('init', array($this, 'load_textdomain'));
add_filter('robots_txt', array($this, 'add_sitemap_to_robots'), 10, 2);
add_action('wp_loaded', array($this, 'flush_rewrite_rules_maybe'));
}
/**
* Load required class files
*/
private function load_dependencies() {
$includes_dir = WOG_PLUGIN_DIR . 'includes/';
$admin_dir = WOG_PLUGIN_DIR . 'admin/';
$required_files = array(
$includes_dir . 'class-wog-settings.php',
$includes_dir . 'class-wog-meta-tags.php',
$includes_dir . 'class-wog-schema.php',
$includes_dir . 'class-wog-sitemap.php',
$includes_dir . 'class-wog-social-share.php',
$includes_dir . 'class-wog-meta-boxes.php',
$admin_dir . 'class-wog-admin.php'
);
foreach ($required_files as $file) {
if (file_exists($file)) {
require_once $file;
}
}
}
/**
* Initialize plugin components after WordPress and WooCommerce are loaded
*/
public function init() {
// Check WooCommerce dependency
if (!$this->is_woocommerce_active()) {
add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
return;
}
// Initialize components
if (class_exists('WOG_Settings')) {
$this->settings = WOG_Settings::get_instance();
}
if (class_exists('WOG_Meta_Tags')) {
WOG_Meta_Tags::get_instance();
}
if (class_exists('WOG_Schema')) {
WOG_Schema::get_instance();
}
if (class_exists('WOG_Sitemap')) {
WOG_Sitemap::get_instance();
}
if (class_exists('WOG_Social_Share')) {
WOG_Social_Share::get_instance();
}
if (class_exists('WOG_Meta_Boxes')) {
WOG_Meta_Boxes::get_instance();
}
if (is_admin() && class_exists('WOG_Admin')) {
WOG_Admin::get_instance();
}
$this->add_custom_hooks();
}
/**
* Set up custom hooks for cache clearing and extensibility
*/
private function add_custom_hooks() {
do_action('wog_init', $this);
// Clear WordPress object cache when products/terms are updated
add_action('woocommerce_update_product', array($this, 'clear_product_object_cache'));
add_action('woocommerce_new_product', array($this, 'clear_product_object_cache'));
add_action('woocommerce_delete_product', array($this, 'clear_product_object_cache'));
add_action('edited_product_cat', array($this, 'clear_category_object_cache'));
add_action('created_product_cat', array($this, 'clear_category_object_cache'));
add_action('delete_product_cat', array($this, 'clear_category_object_cache'));
}
/**
* Load translation files
*/
public function load_textdomain() {
load_plugin_textdomain(
'woo-open-graph',
false,
dirname(plugin_basename(__FILE__)) . '/languages/'
);
}
/**
* Check if WooCommerce is installed and active
*/
private function is_woocommerce_active() {
return class_exists('WooCommerce');
}
/**
* Show admin notice when WooCommerce is missing
*/
public function woocommerce_missing_notice() {
?>
<div class="notice notice-error">
<p>
<?php
echo sprintf(
__('Open Graph for WooCommerce requires WooCommerce to be installed and active. %s', 'woo-open-graph'),
'<a href="' . admin_url('plugin-install.php?s=woocommerce&tab=search&type=term') . '">' . __('Install WooCommerce', 'woo-open-graph') . '</a>'
);
?>
</p>
</div>
<?php
}
/**
* Plugin activation setup
*/
public function activate() {
if (!$this->is_woocommerce_active()) {
deactivate_plugins(plugin_basename(__FILE__));
wp_die(
__('Open Graph for WooCommerce requires WooCommerce to be installed and active.', 'woo-open-graph'),
__('Plugin Activation Error', 'woo-open-graph'),
array('back_link' => true)
);
}
// Set default plugin options
$default_options = array(
'enable_schema' => true,
'enable_enhanced_schema' => true,
'enable_breadcrumb_schema' => true,
'enable_organization_schema' => true,
'enable_facebook' => true,
'enable_twitter' => true,
'enable_linkedin' => true,
'enable_pinterest' => true,
'enable_whatsapp' => true,
'disable_title_description' => false,
'image_size' => 'large',
'fallback_image' => '',
'facebook_app_id' => '',
'twitter_username' => '',
'enable_product_sitemap' => true,
'sitemap_products_per_page' => 500,
'enable_social_share' => true,
'share_button_style' => 'modern',
'share_button_position' => 'after_add_to_cart',
'debug_mode' => false
);
add_option('wog_settings', $default_options);
add_option('wog_version', WOG_VERSION);
add_option('wog_flush_rewrite_rules', true);
// Schedule daily sitemap generation
if (!wp_next_scheduled('wog_generate_sitemaps')) {
wp_schedule_event(time(), 'daily', 'wog_generate_sitemaps');
}
}
/**
* Plugin deactivation cleanup
*/
public function deactivate() {
wp_clear_scheduled_hook('wog_generate_sitemaps');
flush_rewrite_rules();
$this->cleanup_transients();
}
/**
* Flush rewrite rules if needed after activation
*/
public function flush_rewrite_rules_maybe() {
if (get_option('wog_flush_rewrite_rules')) {
flush_rewrite_rules();
delete_option('wog_flush_rewrite_rules');
}
}
/**
* Add sitemap URL to robots.txt
*/
public function add_sitemap_to_robots($output, $public) {
if ('1' == $public) {
$settings = get_option('wog_settings', array());
if (!empty($settings['enable_product_sitemap'])) {
$output .= "\nSitemap: " . home_url('/wog-sitemap.xml') . "\n";
}
}
return $output;
}
/**
* Clear WordPress object cache for a product
*/
public function clear_product_object_cache($product_id) {
if (!$product_id) {
return;
}
wp_cache_delete($product_id, 'posts');
wp_cache_delete($product_id, 'post_meta');
if (function_exists('wc_delete_product_transients')) {
wc_delete_product_transients($product_id);
}
delete_transient('wog_product_meta_' . $product_id);
delete_transient('wog_product_schema_' . $product_id);
do_action('wog_product_cache_cleared', $product_id);
}
/**
* Clear WordPress object cache for a category
*/
public function clear_category_object_cache($term_id) {
if (!$term_id) {
return;
}
wp_cache_delete($term_id, 'terms');
delete_transient('wog_category_meta_' . $term_id);
do_action('wog_category_cache_cleared', $term_id);
}
/**
* Clean up plugin transients
*/
private function cleanup_transients() {
global $wpdb;
$wpdb->query(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE '_transient_wog_%'
OR option_name LIKE '_transient_timeout_wog_%'"
);
}
/**
* Get plugin settings
*/
public function get_settings() {
return $this->settings ? $this->settings->get_all_settings() : array();
}
/**
* Get plugin version
*/
public static function get_version() {
return WOG_VERSION;
}
/**
* Check if debug mode is enabled
*/
public function is_debug_mode() {
$settings = get_option('wog_settings', array());
return !empty($settings['debug_mode']);
}
/**
* Log debug messages to error log
*/
public function debug_log($message, $data = null) {
if ($this->is_debug_mode() && function_exists('error_log')) {
$log_message = '[Open Graph for WooCommerce] ' . $message;
if ($data !== null) {
$log_message .= ' | Data: ' . print_r($data, true);
}
error_log($log_message);
}
}
/**
* Get system information for debugging
*/
public function get_system_info() {
global $wpdb;
$info = array(
'plugin_version' => WOG_VERSION,
'wordpress_version' => get_bloginfo('version'),
'woocommerce_version' => defined('WC_VERSION') ? WC_VERSION : 'Not installed',
'php_version' => PHP_VERSION,
'mysql_version' => $wpdb->db_version(),
'server_info' => isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : 'Unknown',
'memory_limit' => ini_get('memory_limit'),
'max_execution_time' => ini_get('max_execution_time'),
'upload_max_filesize' => ini_get('upload_max_filesize'),
'active_plugins' => get_option('active_plugins'),
'active_theme' => get_template(),
'multisite' => is_multisite() ? 'Yes' : 'No',
'settings' => get_option('wog_settings', array())
);
return apply_filters('wog_system_info', $info);
}
/**
* Export plugin settings as base64 encoded JSON
*/
public function export_settings() {
$settings = get_option('wog_settings', array());
$export_data = array(
'version' => WOG_VERSION,
'timestamp' => current_time('mysql'),
'settings' => $settings
);
return base64_encode(json_encode($export_data));
}
/**
* Import plugin settings from base64 encoded JSON
*/
public function import_settings($import_data) {
try {
$data = json_decode(base64_decode($import_data), true);
if (!$data || !isset($data['settings'])) {
return new WP_Error('invalid_data', __('Invalid import data', 'woo-open-graph'));
}
if (class_exists('WOG_Admin')) {
$admin = WOG_Admin::get_instance();
$sanitized_settings = $admin->sanitize_settings($data['settings']);
} else {
$sanitized_settings = array_map('sanitize_text_field', $data['settings']);
}
update_option('wog_settings', $sanitized_settings);
return true;
} catch (Exception $e) {
return new WP_Error('import_error', $e->getMessage());
}
}
// Legacy method names for backward compatibility
public function clear_product_cache($product_id) {
$this->clear_product_object_cache($product_id);
}
public function clear_category_cache($term_id) {
$this->clear_category_object_cache($term_id);
}
}
// Initialize the plugin
Woo_Open_Graph::get_instance();
/**
* Get plugin instance
*/
function wog() {
return Woo_Open_Graph::get_instance();
}
/**
* Get plugin settings
*/
function wog_get_settings() {
return wog()->get_settings();
}
/**
* Log debug message
*/
function wog_debug_log($message, $data = null) {
wog()->debug_log($message, $data);
}