-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdidar.php
204 lines (167 loc) · 5.99 KB
/
didar.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
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
<?php
/*
Plugin Name: Didar
Plugin URI: https://didar.me/
Description: Connect your Woocommerce website to Didar CRM
Version: 1.0
Author: Didar
Author URI: https://didar.me/
Text Domain: didar
Domain Path: /lang
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
function isHPOSenabled(){
if (OrderUtil::custom_orders_table_usage_is_enabled()) {
return true;
} else {
return false;
}
}
if(!defined('ds'))
define('ds',DIRECTORY_SEPARATOR);
define('did_path',dirname(__file__).ds);
include_once(did_path.'inc'.ds.'functions.php');
include_once(did_path.'inc'.ds.'didar_api.php');
include_once(did_path.'inc'.ds.'hooks.php');
include_once(did_path.'inc'.ds.'paging.php');
new did_hooks();
function did_load_textdomain() {
load_plugin_textdomain('didar', false, dirname(plugin_basename(__FILE__)) . '/lang/');
}
add_action('plugins_loaded', 'did_load_textdomain');
function did_enqueue_admin_css_for_wc_orders() {
//$screen = get_current_screen();
//if ($screen && $screen->id === 'woocommerce_page_wc-orders') {
wp_enqueue_style('didar-admin-css', plugins_url( 'assets/css/admin.css', __FILE__ ) );
//}
}
add_action('admin_enqueue_scripts', 'did_enqueue_admin_css_for_wc_orders');
add_action( 'admin_enqueue_scripts', 'did_plugin_enqueue_scripts' );
function did_plugin_enqueue_scripts() {
wp_enqueue_style( 'did-plugin-style', plugins_url( 'css/style.css', __FILE__ ), array(), '1.0' );
$inline_css = "#toplevel_page_did_managment img {width: 20px;height: auto;}";
wp_add_inline_style( 'did-plugin-style', $inline_css );
}
add_action( 'add_meta_boxes', 'did_custom_order_meta_box' );
function did_custom_order_meta_box() {
add_meta_box(
'did-order-meta-box',
esc_html__('Didar', 'didar'),
'did_custom_order_meta_box_callback',
wc_get_page_screen_id( 'shop-order' ),
'side',
'core'
);
}
function did_custom_order_meta_box_callback( $post ){
$order = wc_get_order($post->ID);
$opt = get_option( 'did_option', [] );
?>
<div class="order_data_column">
<?php
//var_dump($opt['status']);
if(isset( $opt['status']['wc-'.$order->get_status()] )){ ?>
<div class="didar_status">
<?php
if($code=get_post_meta($order->get_id(),'didar_id',true))
echo "<span class='sent'>" . esc_attr('Sent', 'didar'). "</span>
<div>" . esc_attr('Registered code', 'didar'). ":<kbd class='dcode'>". esc_attr( get_post_meta($order->get_id(),'didar_id',true) ) ."</kbd></div>";
else
echo "<span class='waiting'>" . esc_attr('Waiting', 'didar') . "</span>";
?>
</div>
<div class="save_order_didar_wrapper">
<input type="checkbox" name="didar" value="1"/> <label><?php esc_attr_e('Send to Didar', 'didar'); ?></label>
</div>
<?php
}else
echo esc_html__('The current invoice is not ready to be sent.', 'didar');
?>
</div>
<?php
}
add_action('woocommerce_process_shop_order_meta', 'did_save_admin_order', 10, 2);
function did_save_admin_order($order_id, $order){
if(!is_admin() or !isset($_POST['didar']))
return;
$didar = didar_api::save_order($order_id);
if(isset($didar->Message) or isset($didar->Error))
update_post_meta($order_id,'didar_msg',(isset($didar->Message)?ucfirst(wp_strip_all_tags($didar->Message) ) . " [" . $didar->Code . "]" :ucfirst(wp_strip_all_tags($didar->Error) )));
else
update_post_meta($order_id,'didar_id',$didar->Id);
}
/**
* schedular task
*/
// Schedule the function to run hourly
add_action('didar_send_all_order_cron', 'didar_send_all_order_function');
function didar_send_all_order_function() {
global $wpdb;
$opt = get_option( 'did_option', [] );
if( $opt['send_type'] == 1){
$from = isset($opt['order_start'])?$opt['order_start']:0;
$to = isset($opt['order_count'])?$opt['order_count']:20;
$status = implode("','",$opt['status']);
if( isHPOSenabled() ){
$rows = $wpdb->get_results( $wpdb->prepare("
select * from {$wpdb->prefix}wc_orders o
where o.type='shop_order' and o.status in(%s)
and o.id> %d and NOT EXISTS(select post_id from $wpdb->postmeta where post_id=o.id and meta_key='didar_id' and meta_value<>'')
order by id limit %s",
$status, $from, $to), ARRAY_A);
if(!empty($rows)){
foreach($rows as $row){
$didar = didar_api::save_order($row['id']);
if(isset($didar->Message)or isset($didar->Error)){
update_post_meta($row['id'],'didar_msg',(isset($didar->Message)?$didar->Message:$didar->Error));
}
if(isset($didar->Id)){
update_post_meta($row['id'],'didar_id',$didar->Id);
}
}
}
}else{
$rows = $wpdb->get_results( $wpdb->prepare("
select * from {$wpdb->prefix}posts o
where o.post_type='shop_order' and o.post_status in(%s)
and o.ID> %d and NOT EXISTS(select post_id from $wpdb->postmeta where post_id=o.ID and meta_key='didar_id' and meta_value<>'')
order by ID limit %d",
$status, $from, $to), ARRAY_A);
if(!empty($rows)){
foreach($rows as $row){
$didar = didar_api::save_order($row['ID']);
/*if(isset($didar->Message)or isset($didar->Error)){
update_post_meta($row['ID'],'didar_msg',(isset($didar->Message)?$didar->Message:$didar->Error));
}*/
if(isset($didar->Id)){
update_post_meta($row['ID'],'didar_id',$didar->Id);
}
}
}
}
}
}
// Schedule the cron event on plugin activation
register_activation_hook(__FILE__, 'didar_send_all_order_schedule_cron');
function didar_send_all_order_schedule_cron() {
if (!wp_next_scheduled('didar_send_all_order_cron')) {
wp_schedule_event(time(), 'hourly', 'didar_send_all_order_cron');
}
global $wpdb;
$sql = "CREATE TABLE {$wpdb->prefix}didar_error (
id INT NOT NULL AUTO_INCREMENT,
order_id INT NOT NULL,
date datetime NOT NULL,
error text NOT NULL,
path varchar(30) NOT NULL,
params text NOT NULL,
PRIMARY KEY (id)
) DEFAULT CHARACTER SET utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Remove the cron event on plugin deactivation
register_deactivation_hook(__FILE__, 'didar_send_all_order_remove_cron');
function didar_send_all_order_remove_cron() {
wp_clear_scheduled_hook('didar_send_all_order_cron');
}