<?php

/**
 * The plugin bootstrap file
 *
 * @link              https://www.wpsaad.com
 * @since             1.0.0
 * @package           ODDW
 *
 * @wordpress-plugin
 * Plugin Name:       Orders Delivery Drivers for WooCommerce
 * Plugin URI:        https://wpsaad.com/wp-plugins/orders-delivery-drivers-for-woocommerce-pro/
 * Description:       Orders Delivery Drivers for WooCommerce allows you to manage your delivery drivers, track their performance, and optimize your delivery operations. With features like driver scheduling, route optimization, and real-time tracking, you can streamline your mobile workforce and increase your bottom line.
 * Version:           1.3.0
 * Requires at least: 5.5
 * Requires PHP:      7.4
 * Tested up to:      6.9
 * WC requires at least: 5.0
 * WC tested up to:    8.0
 * Requires Plugins: woocommerce
 * Author:            WPSAAD
 * Author URI:        https://wpsaad.com
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       orders-delivery-drivers-for-woocommerce
 * Domain Path:       /languages
 *
 */
defined( 'ABSPATH' ) or die;
/**
 * Current plugin version.
 */
define( 'ODDW_VERSION', '1.3.0' );
if ( function_exists( 'oddfw_fs' ) ) {
    oddfw_fs()->set_basename( false, __FILE__ );
} else {
    // Create a helper function for easy SDK access.
    function oddfw_fs() {
        global $oddfw_fs;
        if ( !isset( $oddfw_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $oddfw_fs = fs_dynamic_init( array(
                'id'               => '24772',
                'slug'             => 'orders-delivery-drivers-for-woocommerce',
                'type'             => 'plugin',
                'public_key'       => 'pk_0b6e9a5ab68e188773449f6d31584',
                'is_premium'       => false,
                'premium_suffix'   => 'Premium',
                'navigation'       => 'tabs',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug' => 'oddw-settings',
                ),
                'is_live'          => true,
            ) );
        }
        return $oddfw_fs;
    }

    // Init Freemius.
    oddfw_fs();
    // Signal that SDK was initiated.
    do_action( 'oddfw_fs_loaded' );
}
/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-oddw-activator.php
 */
function oddw_activate() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-oddw-activator.php';
    ODDW_Activator::activate();
}

/**
 * Backward-compatible wrapper.
 *
 * @deprecated 2.4.3 Use oddw_activate() instead.
 */
function activate_oddw() {
    oddw_activate();
}

// Include driver profile admin fields
if ( is_admin() ) {
    require_once plugin_dir_path( __FILE__ ) . 'admin/class-oddw-driver-profile.php';
}
/**
 * Helper: Get order meta (compatible with HPOS and legacy).
 */
function oddw_get_order_meta(  $order_id, $meta_key, $single = true  ) {
    $order = wc_get_order( $order_id );
    if ( $order && method_exists( $order, 'get_meta' ) ) {
        return $order->get_meta( $meta_key, $single );
    }
    return get_post_meta( $order_id, $meta_key, $single );
}

/**
 * Helper: Update order meta (compatible with HPOS and legacy).
 */
function oddw_update_order_meta(  $order_id, $meta_key, $meta_value  ) {
    $order = wc_get_order( $order_id );
    if ( $order && method_exists( $order, 'update_meta_data' ) ) {
        $order->update_meta_data( $meta_key, $meta_value );
        $order->save();
        return true;
    }
    return update_post_meta( $order_id, $meta_key, $meta_value );
}

// Declare compatibility with Custom Order Tables.
add_action( 'before_woocommerce_init', function () {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    }
} );
/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-oddw-deactivator.php
 */
function oddw_deactivate() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-oddw-deactivator.php';
    ODDW_Deactivator::deactivate();
}

/**
 * Backward-compatible wrapper.
 *
 * @deprecated 2.4.3 Use oddw_deactivate() instead.
 */
function deactivate_oddw() {
    oddw_deactivate();
}

register_activation_hook( __FILE__, 'oddw_activate' );
register_deactivation_hook( __FILE__, 'oddw_deactivate' );
/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-oddw.php';
/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function oddw_run() {
    $plugin = new ODDW();
    $plugin->run();
}

/**
 * Backward-compatible wrapper.
 *
 * @deprecated 2.4.3 Use oddw_run() instead.
 */
function run_oddw() {
    oddw_run();
}

oddw_run();
// Create variable for settings link filter.
$plugin_name = plugin_basename( __FILE__ );
/**
 * Add settings link on plugin page
 *
 * @since 1.0.3
 * @param array $links an array of links related to the plugin.
 * @return array updatead array of links related to the plugin.
 */
function oddw_settings_link(  $links  ) {
    $settings_link = '<a href="admin.php?page=oddw-settings">' . __( 'Settings', 'orders-delivery-drivers-for-woocommerce' ) . '</a>';
    array_unshift( $links, $settings_link );
    return $links;
}

add_filter( "plugin_action_links_{$plugin_name}", 'oddw_settings_link' );