Skip to content
Snippets Groups Projects
Commit 4b6b30c9 authored by Dani Quilez's avatar Dani Quilez
Browse files

change odoo post using native wp_post

parent 796bc6f9
No related branches found
No related tags found
No related merge requests found
<?php <?php
function wpct_oc_post_odoo($data,$endpoint) { function wpct_oc_post_odoo($data,$endpoint) {
$post_data = json_encode($data);
$post_url = wpct_oc_get_odoo_base_url().$endpoint; $post_url = wpct_oc_get_odoo_base_url().$endpoint;
// Prepare new cURL resource $post_data = json_encode($data);
$crl = curl_init(); $args = array(
curl_setopt($crl,CURLOPT_URL, $post_url); 'headers' => array(
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 'Content-Type' => 'application/json',
curl_setopt($crl, CURLINFO_HEADER_OUT, true); 'Connection' => 'keep-alive',
curl_setopt($crl, CURLOPT_POST, true); 'accept' => 'application/json',
curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data); 'API-KEY' => wpct_oc_get_api_key()
// Set HTTP Header for POST request ),
curl_setopt($crl, CURLOPT_HTTPHEADER, 'body' => $post_data
array(
'Content-Type: application/json',
'Connection: keep-alive',
'accept: application/json',
'API-KEY: '.wpct_oc_get_api_key()
)
); );
// Submit the POST request $response = wp_remote_post( $post_url, $args);
$result = curl_exec($crl); if ( !is_wp_error( $response ) ) {
$success = true; return $response;
// handle curl error
if ($result === false) {
$success = false;
}
// handle http error code
if( $success ){
$response = curl_getinfo($crl, CURLINFO_HTTP_CODE);
if($response != 200) $success = false;
} }
curl_close($crl); return false;
if($success) return json_decode($result, true);
return $success;
} }
...@@ -73,6 +73,23 @@ function wpct_oc_settings_init(){ ...@@ -73,6 +73,23 @@ function wpct_oc_settings_init(){
'wpct_oc_options', 'wpct_oc_options',
'wpct_oc_api_settings' 'wpct_oc_api_settings'
); );
// Admin notification receiver
register_setting(
'wpct_oc_options',
'wpct_oc_admin_notification_receiver',
array(
'type' => 'string',
'description' => 'Admin notification receiver',
'show_in_rest' => false,
)
);
add_settings_field(
'wpct_oc_admin_notification_receiver',
__('Admin notification receiver', 'wpct_odoo_connect'),
'wpct_oc_admin_notification_receiver_render',
'wpct_oc_options',
'wpct_oc_api_settings'
);
} }
/** /**
...@@ -86,6 +103,10 @@ function wpct_oc_odoo_base_url_render(){ ...@@ -86,6 +103,10 @@ function wpct_oc_odoo_base_url_render(){
function wpct_oc_api_key_render(){ function wpct_oc_api_key_render(){
echo "<input type='text' name='wpct_oc_api_key' value='" . wpct_oc_get_api_key() . "'> "; echo "<input type='text' name='wpct_oc_api_key' value='" . wpct_oc_get_api_key() . "'> ";
} }
// Admin notification receiver
function wpct_oc_admin_notification_receiver_render(){
echo "<input type='text' name='wpct_oc_admin_notification_receiver' value='" . wpct_oc_get_admin_notification_receiver() . "'> ";
}
/** /**
...@@ -106,3 +127,7 @@ function wpct_oc_get_odoo_base_url(){ ...@@ -106,3 +127,7 @@ function wpct_oc_get_odoo_base_url(){
function wpct_oc_get_api_key(){ function wpct_oc_get_api_key(){
return get_option('wpct_oc_api_key') ? get_option('wpct_oc_api_key') : ''; return get_option('wpct_oc_api_key') ? get_option('wpct_oc_api_key') : '';
} }
// Admin notification receiver
function wpct_oc_get_admin_notification_receiver(){
return get_option('wpct_oc_admin_notification_receiver') ? get_option('wpct_oc_admin_notification_receiver') : '';
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment