0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: WooKomasBCentral Sync
* Plugin URI: https://dottlinks.co.ke/
* Description: Sync all WooCommerce sales data, orders data, inventory data, purchase history, and user data with Microsoft Dynamics NAV/business central.
* Author: Dottlinks Services
* Version: 4.2.0
* Author URI: https://dottlinks.co.ke/
* Text Domain: wookomas_bcentral_sync
*/
defined( 'ABSPATH' ) || exit; // Prevents direct access to the plugin file
// Add plugin menu page
function wpturbo_wookomas_add_menu_page() {
add_menu_page(
esc_html__( 'WooKomas Sync Settings', 'wookomas_bcentral_sync' ),
esc_html__( 'WooKomas Sync', 'wookomas_bcentral_sync' ),
'manage_options',
'wookomas-sync',
'wpturbo_wookomas_display_settings_page',
'dashicons-update',
30
);
}
add_action( 'admin_menu', 'wpturbo_wookomas_add_menu_page' );
// Add plugin settings page
function wpturbo_wookomas_display_settings_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'WooKomas Sync Settings', 'wookomas_bcentral_sync' ); ?></h1>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php settings_fields( 'wookomas_sync_settings' ); ?>
<?php do_settings_sections( 'wookomas-sync' ); ?>
<?php submit_button(); ?>
</form>
<h2><?php esc_html_e( 'Test Connection', 'wookomas_bcentral_sync' ); ?></h2>
<?php wpturbo_wookomas_test_connection_form(); ?>
<div id="wookomas-test-connection-result"></div>
</div>
<?php
}
// Register plugin settings
function wpturbo_wookomas_register_settings() {
// Add 'API Credentials' section to settings page
add_settings_section(
'wookomas_sync_api',
esc_html__( 'API Credentials', 'wookomas_bcentral_sync' ),
'wpturbo_wookomas_output_api_section',
'wookomas-sync'
);
// Add 'Client ID' field to 'API Credentials' section
add_settings_field(
'wookomas_sync_client_id',
esc_html__( 'Client ID', 'wookomas_bcentral_sync' ),
'wpturbo_wookomas_output_client_id_field',
'wookomas-sync',
'wookomas_sync_api'
);
// Add 'Client Secret' field to 'API Credentials' section
add_settings_field(
'wookomas_sync_client_secret',
esc_html__( 'Client Secret', 'wookomas_bcentral_sync' ),
'wpturbo_wookomas_output_client_secret_field',
'wookomas-sync',
'wookomas_sync_api'
);
// Register 'Client ID' and 'Client Secret' settings
register_setting(
'wookomas_sync_settings',
'wookomas_sync_client_id',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
'default' => '',
)
);
register_setting(
'wookomas_sync_settings',
'wookomas_sync_client_secret',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
'default' => '',
)
);
}
add_action( 'admin_init', 'wpturbo_wookomas_register_settings' );
// Output 'API Credentials' section
function wpturbo_wookomas_output_api_section() {
echo '<p>' . esc_html__( 'Enter your Microsoft Dynamics NAV/business central API credentials below.', 'wookomas_bcentral_sync' ) . '</p>';
}
// Output 'Client ID' field
function wpturbo_wookomas_output_client_id_field() {
$client_id = get_option( 'wookomas_sync_client_id', '' );
echo '<input type="text" name="wookomas_sync_client_id" value="' . esc_attr( $client_id ) . '" />';
}
// Output 'Client Secret' field
function wpturbo_wookomas_output_client_secret_field() {
$client_secret = get_option( 'wookomas_sync_client_secret', '' );
echo '<input type="text" name="wookomas_sync_client_secret" value="' . esc_attr( $client_secret ) . '" />';
}
// Output 'Test Connection' form
function wpturbo_wookomas_test_connection_form() {
?>
<button type="button" class="button" id="wookomas-test-connection-button">
<?php esc_html_e( 'Test Connection', 'wookomas_bcentral_sync' ); ?>
</button>
<?php
}
// Handle 'Test Connection' AJAX request
function wpturbo_wookomas_handle_test_connection() {
if ( ! isset( $_POST['client_id'] ) || ! isset( $_POST['client_secret'] ) ) {
wp_send_json_error( __( 'Missing API credentials.', 'wookomas_bcentral_sync' ) );
wp_die();
}
// Retrieve API credentials
$client_id = sanitize_text_field( $_POST['client_id'] );
$client_secret = sanitize_text_field( $_POST['client_secret'] );
// Perform data sync
$url = 'https://api.businesscentral.dynamics.com/v2.0/dottlinks.co.ke/production'; // Replace with actual URL
$data = array(
'action' => 'sync_data',
'client_id' => $client_id,
'client_secret' => $client_secret,
);
$response = wp_remote_request(
$url,
array(
'body' => $data,
'timeout' => 30,
'headers' => array(
'Content-Type' => '