-
Notifications
You must be signed in to change notification settings - Fork 43
/
woocommerce-1c.php
133 lines (102 loc) · 4.41 KB
/
woocommerce-1c.php
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
<?php
/*
Plugin Name: WooCommerce and 1C:Enterprise/1С:Предприятие Data Exchange
Version: 0.9.20
Description: Provides data exchange between eCommerce plugin WooCommerce and business application "1C:Enterprise 8. Trade Management".
Author: Danil Semelenov
Author URI: mailto:[email protected]
Plugin URI:
Text Domain: woocommerce-1c
Domain Path: /languages
*/
if (!defined('ABSPATH')) exit;
require_once ABSPATH . "wp-admin/includes/plugin.php";
if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
define('WC1C_PLUGIN_DIR', __DIR__ . '/');
define('WC1C_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('WC1C_PLUGIN_BASEDIR', dirname(WC1C_PLUGIN_BASENAME) . '/');
$upload_dir = wp_upload_dir();
define('WC1C_DATA_DIR', "{$upload_dir['basedir']}/woocommerce-1c/");
function wc1c_init() {
if (!is_plugin_active("woocommerce/woocommerce.php")) {
function wc1c_woocommerce_admin_notices() {
$plugin_data = get_plugin_data(__FILE__);
$message = sprintf(__("Plugin <strong>%s</strong> requires plugin <strong>WooCommerce</strong> to be installed and activated.", 'woocommerce-1c'), $plugin_data['Name']);
printf('<div class="updated"><p>%s</p></div>', $message);
}
add_action('admin_notices', 'wc1c_woocommerce_admin_notices');
}
}
add_action('init', 'wc1c_init');
function wc1c_plugins_loaded() {
$plugin_data = get_plugin_data(__FILE__);
$languages_dir = WC1C_PLUGIN_BASEDIR . $plugin_data['DomainPath'];
load_plugin_textdomain('woocommerce-1c', false, $languages_dir);
$revision = trim(str_replace('Revision', '', '$Revision$'), "$: ");
define('WC1C_VERSION', sprintf("%sr%s", $plugin_data['Version'], $revision));
}
add_action('plugins_loaded', 'wc1c_plugins_loaded');
function wc1c_activate() {
global $wpdb;
$index_table_names = array(
$wpdb->postmeta,
$wpdb->termmeta,
$wpdb->usermeta,
);
foreach ($index_table_names as $index_table_name) {
$index_name = 'wc1c_meta_key_meta_value';
$result = $wpdb->get_var("SHOW INDEX FROM $index_table_name WHERE Key_name = '$index_name';");
if ($result) continue;
$wpdb->query("ALTER TABLE $index_table_name ADD INDEX $index_name (meta_key, meta_value(36))");
}
if (!is_dir(WC1C_DATA_DIR)) mkdir(WC1C_DATA_DIR);
file_put_contents(WC1C_DATA_DIR . ".htaccess", "Deny from all");
file_put_contents(WC1C_DATA_DIR . "index.html", '');
wc1c_add_rewrite_rules();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'wc1c_activate');
register_deactivation_hook(__FILE__, 'flush_rewrite_rules');
function wc1c_add_rewrite_rules() {
add_rewrite_rule("wc1c/exchange", "index.php?wc1c=exchange", 'top');
add_rewrite_rule("wc1c/clean", "index.php?wc1c=clean");
}
function wc1c_delete_term($term_id, $tt_id, $taxonomy, $deleted_term) {
global $wpdb;
if ($taxonomy != 'product_cat' && strpos($taxonomy, 'pa_') !== 0) return;
$wpdb->delete($wpdb->termmeta, array('term_id' => $term_id));
if (function_exists('wc1c_check_wpdb_error')) wc1c_check_wpdb_error();
}
add_action('delete_term', 'wc1c_delete_term', 10, 4);
function wc1c_woocommerce_attribute_by_id($attribute_id) {
global $wpdb;
$cache_key = "wc1c_woocomerce_attribute_by_id-$attribute_id";
$attribute = wp_cache_get($cache_key);
if ($attribute === false) {
$attribute = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $attribute_id), ARRAY_A);
if (function_exists('wc1c_check_wpdb_error')) wc1c_check_wpdb_error();
if ($attribute) {
$attribute['taxonomy'] = wc_attribute_taxonomy_name($attribute['attribute_name']);
wp_cache_set($cache_key, $attribute);
}
}
return $attribute;
}
function wc1c_delete_woocommerce_attribute($attribute_id) {
global $wpdb;
$attribute = wc1c_woocommerce_attribute_by_id($attribute_id);
if (!$attribute) return false;
delete_option("{$attribute['taxonomy']}_children");
$terms = get_terms($attribute['taxonomy'], "hide_empty=0");
foreach ($terms as $term) {
wp_delete_term($term->term_id, $attribute['taxonomy']);
}
$wpdb->delete("{$wpdb->prefix}woocommerce_attribute_taxonomies", compact('attribute_id'));
if (function_exists('wc1c_check_wpdb_error')) wc1c_check_wpdb_error();
}
function wc1c_parse_decimal($number) {
$number = str_replace(array(',', ' '), array('.', ''), $number);
return (float) $number;
}
require_once WC1C_PLUGIN_DIR . "admin.php";
require_once WC1C_PLUGIN_DIR . "exchange.php";