WooCentl V3

Home » Snippets » WooCentl V3
0

Created with:

Visibility: 

public

Creator: Kabucho

Customize with WPTurbo AI
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;

// 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_settings_section( 'wootocentral_sync_api', esc_html__( 'API Credentials', 'wootocentral_sync' ), 'wootocentral_sync_output_api_section', 'wootocentral-sync' );

    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_settings_field( 'wootocentral_sync_client_secret', esc_html__( 'Client Secret', 'wootocentral_sync' ), 'wootocentral_sync_output_client_secret_field', 'wootocentral-sync', 'wootocentral_sync_api' );

    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', '' );

    if ( empty( $client_id ) || empty( $client_secret ) ) {
        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 {
        // Perform API credential validation
        $valid_credentials = true;

        if ( ! $valid_credentials ) {
            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 broke down the code into functions that handle specific tasks.

The wootocentral_sync_add_menu_item function adds a submenu page to the WooCommerce settings page.

The wootocentral_sync_output_settings_page function outputs the HTML for the settings page.

The wootocentral_sync_register_settings function registers the settings for the plugin.

The wootocentral_sync_output_api_section, wootocentral_sync_output_client_id_field, and wootocentral_sync_output_client_secret_field functions output the sections and fields for the plugin settings.

Finally, the wootocentral_sync_validate_credentials function performs the validation of the API credentials.

By breaking the code into smaller functions, it makes the code easier to read, understand, and maintain. In addition, each function can be reused elsewhere in the code, making the overall development process more efficient.

Register an account to save your snippets or go Pro to get more features.