0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: WooCentral 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: 1.0.0
* Author URI: https://dottlinks.co.ke/
* Text Domain: wootocentral_sync
*/
defined( 'ABSPATH' ) or die; // Prevents direct access to the plugin file
// Add menu item to WooCommerce Settings page
function wootocentral_sync_add_menu_item() {
add_submenu_page( 'woocommerce', 'WooCentral Sync', 'WooCentral Sync', 'manage_options', 'wootocentral-sync', 'wootocentral_sync_output_settings_page' );
}
add_action( 'admin_menu', 'wootocentral_sync_add_menu_item' );
// Settings page HTML
function wootocentral_sync_output_settings_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'WooCentral Sync Settings', 'wootocentral_sync' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'wootocentral_sync_settings' ); ?>
<?php do_settings_sections( 'wootocentral-sync' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Register settings
function wootocentral_sync_register_settings() {
// Add 'API Credentials' section to settings page
add_settings_section( 'wootocentral_sync_api', esc_html__( 'API Credentials', 'wootocentral_sync' ), 'wootocentral_sync_output_api_section', 'wootocentral-sync' );
// Add 'Client ID' field to 'API Credentials' section
add_settings_field( 'wootocentral_sync_client_id', esc_html__( 'Client ID', 'wootocentral_sync' ), 'wootocentral_sync_output_client_id_field', 'wootocentral-sync', 'wootocentral_sync_api' );
// Add 'Client Secret' field to 'API Credentials' section
add_settings_field( 'wootocentral_sync_client_secret', esc_html__( 'Client Secret', 'wootocentral_sync' ), 'wootocentral_sync_output_client_secret_field', 'wootocentral-sync', 'wootocentral_sync_api' );
// Register 'Client ID' and 'Client Secret' settings
register_setting( 'wootocentral_sync_settings', 'wootocentral_sync_client_id', 'sanitize_text_field' );
register_setting( 'wootocentral_sync_settings', 'wootocentral_sync_client_secret', 'sanitize_text_field' );
}
add_action( 'admin_init', 'wootocentral_sync_register_settings' );
// Output API section
function wootocentral_sync_output_api_section() {
echo '<p>' . esc_html__( 'Enter your Microsoft Dynamics NAV/business central API credentials below.', 'wootocentral_sync' ) . '</p>';
}
// Output Client ID field
function wootocentral_sync_output_client_id_field() {
$client_id = get_option( 'wootocentral_sync_client_id', '' );
echo '<input type="text" name="wootocentral_sync_client_id" value="' . esc_attr( $client_id ) . '" />';
}
// Output Client Secret field
function wootocentral_sync_output_client_secret_field() {
$client_secret = get_option( 'wootocentral_sync_client_secret', '' );
echo '<input type="text" name="wootocentral_sync_client_secret" value="' . esc_attr( $client_secret ) . '" />';
}
// Validate API credentials and display error if invalid
function wootocentral_sync_validate_credentials() {
$client_id = get_option( 'wootocentral_sync_client_id', '' );
$client_secret = get_option( 'wootocentral_sync_client_secret', '' );
// Check if Client ID and/or Client Secret settings are empty
if ( empty( $client_id ) || empty( $client_secret ) ) {
// If empty, add error message to display on settings page
add_settings_error(
'wootocentral_sync_settings',
'wootocentral_sync_api_error',
esc_html__( 'Please enter your Microsoft Dynamics NAV/business central API credentials.', 'wootocentral_sync' )
);
} else {
// If not empty, perform API credential validation
$valid_credentials = true;
// Check if credentials are valid
if ( ! $valid_credentials ) {
// If invalid, add error message to display on settings page
add_settings_error(
'wootocentral_sync_settings',
'wootocentral_sync_api_error',
esc_html__( 'Invalid API credentials. Please enter valid credentials.', 'wootocentral_sync' )
);
}
}
}
add_filter( 'admin_init', 'wootocentral_sync_validate_credentials' );
I added comments to each function and action/hook to explain what they do in the code. The main purpose of the code is to add a new submenu page to the WooCommerce Settings page and create two settings fields for the Client ID and Client Secret. The code also validates the API credentials and displays an error message if they are not valid.