Thời gian tiếp theo cho một sự kiện. Retrieve the next timestamp for an event. wp_next_scheduled (ok
https://developer.wordpress.org/reference/functions/wp_next_scheduled/
if ( ! wp_next_scheduled( 'resend_code_api_error' ) ) {
wp_schedule_event( time(), 'hourly', 'resend_code_api_error' );
}
add_action( 'resend_code_api_error', 'resend_code_api_error' );
function resend_code_api_error() {
global $wpdb;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,API_OEC_ERROR_URL);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$server_output = json_decode($server_output, true);
if($server_output['done']) {
$records = $server_output['records'];
if(count($records) > 0) {
foreach ($records as $record) {
$course = get_post($record['course_id']);
// Kiểm tra xem record đã tạo thành công trước đó chưa
$sql = "SELECT * FROM wp_log_api WHERE course_id = " . $record['course_id'] . " AND order_id = " . $record['order_id'];
$temp = $wpdb->get_row($sql);
if(!$temp || $temp->status != 2) {
// Gen lại code
do {
$code = wccg_get_random_coupon();
// Kiểm tra mã kích hoạt đã được tạo trước đó chưa
$checkSql = "SELECT id FROM wp_codes WHERE code = '" . $code . "'";
$codes = $wpdb->get_results($checkSql);
if(count($codes) > 0) {
$end = false;
} else {
$end = true;
}
} while ($end == false);
// Insert mã kích hoạt vào DB
$today = date('Y-m-d H:i:s');
if($code) {
$data = array(
'code' => $code,
'course_id' => $record['course_id'],
'is_active' => 0,
'status' => 1,
'customer_email' => $record['customer_email'],
'created_at' => $today,
'order_id' => $record['order_id'],
'source' => 'API'
);
$wpdb->insert('wp_codes', $data);
if($wpdb->insert_id) {
if($temp) {
// Cập nhật log
$wpdb->update('wp_log_api', array('api_status' => 1, 'code' => $code), array('id' => $temp['id']));
} else {
// Lưu log TMDT call API
$dataApi = array(
'customer_email' => $record['customer_email'],
'customer_name' => $record['customer_name'],
'course_id' => $record['course_id'],
'order_id' => $record['order_id'],
'created_at' => date('Y-m-d :H:i:s'),
'api_status' => 1,
'source' => 'API',
);
$wpdb->insert('wp_log_api', $dataApi);
}
$subject = 'MÃ KÍCH HOẠT KHÓA HỌC';
$customer_name = $record['customer_name'];
$customer_email = $record['customer_email'];
$body = '<b>MÃ KÍCH HOẠT KHÓA HỌC ' . wc_strtoupper($course->post_title) . '</b><br/>';
$body .= 'Thân gửi học viên <b>' . $customer_name .'</b><br/>';
$body .= 'Vinmec xin xác nhận đã thanh toán thành công khóa học <b>' . $course->post_title . '</b> tại <a href="' . home_url() . '">songkhoe.vinmec.com</a> <br/>';
$body .= 'Mã kích hoạt khóa học của bạn là: <b>' . $code . '</b><br/>';
$body .= 'Hướng dẫn kích hoạt khóa học: <br/>';
$body .= '<b>(Lưu ý: chỉ kích hoạt một lần duy nhất) </b><br/>';
$body .= '<b>Bước 1:</b> Truy cập vào link: <a href="' . home_url() . '">songkhoe.vinmec.com</a> <br/>';
$body .= '<b>Bước 2:</b> Chọn nút "Đăng nhập" và nhập thông tin tài khoản của bạn <br/>';
$body .= '<b>Bước 3:</b> Chọn mục “Khóa học”, xem thông tin khóa học <br/>';
$body .= '<b>Bước 4:</b> Chọn nút “Kích hoạt khóa học” <br/>';
$body .= '<b>Bước 5:</b> Nhập mã kích hoạt thành công và bắt đầu học ngay <br/><br/>';
$body .= 'Cảm ơn sự quan tâm và ủng hộ của bạn. <br/>';
$body .= 'Chúc các bạn có những trải nghiệm thú vị cùng <a href="' . home_url() .'">songkhoe.vinmec.com</a> <br/><br/>';
$body .= '<a href="#">Câu hỏi thường gặp</a> <br/>';
$body .= '<a href="#">Chính sách bảo mật thông tin</a> <br/><br/>';
$body .= '<b>Chú ý:</b> Đây là email tự động từ Hệ thống Y tế Vinmec. Quý khách vui lòng, không trả lời email này. <br/><br/>';
$body .= '<b>CÔNG TY CỔ PHẦN BỆNH VIỆN ĐA KHOA QUỐC TẾ VINMEC </b><br/>';
$body .= 'Địa chỉ: số 458 Minh Khai, phường Vĩnh Tuy, quận Hai Bà Trung, Hà Nội <br/>';
$body .= 'Liên hệ: <a href="mailto: info@vinmec.com">info@vinmec.com</a><br/>';
$headers = array('Content-Type: text/html; charset=UTF-8');
$result = wp_mail($customer_email, $subject, $body, $headers);
if($result) {
// Cập nhật log
$wpdb->update('wp_log_api', array('api_status' => 2), array('id' => $temp['id']));
$response['error'] = 200;
$response['message'] = 'Tạo mã kích hoạt và gửi email thành công';
wp_send_json_success( $response );
} else {
$response['error'] = 400;
$response['message'] = 'Gửi email không thành công';
wp_send_json_success( $response );
}
} else {
$response['error'] = 400;
$response['message'] = 'Tạo mã kích hoạt không thành công';
wp_send_json_success( $response );
}
} else {
$response['error'] = 400;
$response['message'] = 'Không thể tạo mã kích hoạt';
wp_send_json_success( $response );
}
}
}
}
}
}PHP wp_next_scheduled Examples
PreviousWordPress Plugin Development Best Practices: Template FilesNextDisable Sidebar and Dashboard Widgets (ok) full
Last updated
