Files
torebki-fabiola.pl/wp-content/plugins/woocommerce-dhl/classes/class-shipping-method.php
2026-03-05 13:07:40 +01:00

215 lines
5.2 KiB
PHP

<?php
use VendorDHL\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\ApiConnectionRenderer;
use VendorDHL\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\HasApiConnectionStatusField;
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'WPDesk_WooCommerce_DHL_Shipping_Method' ) ) {
/**
* Shipping Method class.
*/
class WPDesk_WooCommerce_DHL_Shipping_Method extends WC_Shipping_Method implements HasApiConnectionStatusField {
use ApiConnectionRenderer;
const OPTION_LOGGING = 'logging';
const METHOD_ID = 'dhl';
/**
* @var bool|WPDesk_WooCommerce_DHL_API
*/
public $api = false;
/**
* @var bool|WPDesk_WooCommerce_DHL_Parcelshop_API
*/
public $parcelshop_api = false;
/**
* @var mixed
*/
private $login;
/**
* @var mixed
*/
private $password;
/**
* @var mixed
*/
private $api_key;
/**
* @var mixed
*/
private $test_mode;
/**
* @var mixed
*/
private $login_parcelshop;
/**
* @var mixed
*/
private $password_parcelshop;
/**
* @var mixed
*/
private $cost;
/**
* @var mixed
*/
private $cost_cod;
/**
* Constructor for your shipping class.
*
* @return void
*/
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
$this->id = self::METHOD_ID;
$this->method_title = __( 'DHL', 'woocommerce-dhl' );
$this->method_description = __( 'Integracja WooCommerce z DHL. <a href="https://www.wpdesk.pl/docs/woocommerce-dhl-docs/" target="_blank">Zapoznaj się z instrukcją obsługi &rarr;</a>', 'woocommerce-dhl' );
$this->enabled = 'yes';
$this->title = __( 'DHL', 'woocommerce-dhl' );
$this->settings['enabled'] = 'yes';
$this->init();
add_action( 'woocommerce_update_options_shipping_' . $this->id, [ $this, 'process_admin_options' ] );
}
/**
* Init your settings.
*
* @return void
*/
public function init() {
// Load the settings API.
$this->init_settings();
$this->init_form_fields();
// Define user set variables.
$this->login = $this->get_option( 'login' );
$this->password = $this->get_option( 'password' );
$this->api_key = $this->get_option( 'api_key' );
$this->test_mode = $this->get_option( 'test_mode', 'no' );
$this->login_parcelshop = $this->get_option( 'login_parcelshop' );
$this->password_parcelshop = $this->get_option( 'password_parcelshop' );
$this->cost = $this->get_option( 'cost' );
$this->cost_cod = $this->get_option( 'cost_cod' );
}
/**
* @return WPDesk_WooCommerce_DHL_API
*/
public function get_api() {
if ( ! $this->api ) {
$this->api = new WPDesk_WooCommerce_DHL_API( $this->login, $this->password, $this->get_logger(), $this->test_mode === 'yes' );
}
return $this->api;
}
/**
*
*/
private function get_logger() {
if ( $this->get_option( self::OPTION_LOGGING, 'no' ) === 'yes' ) {
$logger = ( new \VendorDHL\WPDesk\Logger\WPDeskLoggerFactory() )->createWPDeskLogger();
} else {
$logger = new \VendorDHL\Psr\Log\NullLogger();
}
return $logger;
}
/**
* @return WPDesk_WooCommerce_DHL_Parcelshop_API
*/
public function get_parcelshop_api() {
if ( ! $this->parcelshop_api && ! empty( $this->login_parcelshop ) && ! empty( $this->password_parcelshop ) ) {
$this->parcelshop_api = new WPDesk_WooCommerce_DHL_Parcelshop_API( $this->login_parcelshop, $this->password_parcelshop, $this->get_logger(), $this->test_mode === 'yes' );
}
return $this->parcelshop_api;
}
/**
* Initialise Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = include( 'settings-dhl.php' );
}
/**
* @param array $form_fields .
* @param false $echo .
*
* @return void
*/
public function generate_settings_html( $form_fields = [], $echo = false ) {
parent::generate_settings_html( $form_fields );
?>
<script type="text/javascript">
jQuery( document ).ready( function () {
function dhl_order_status() {
if ( jQuery( '#woocommerce_dhl_auto_create' ).val() === 'auto' ) {
jQuery( '#woocommerce_dhl_order_status' ).closest( 'tr' ).show();
} else {
jQuery( '#woocommerce_dhl_order_status' ).closest( 'tr' ).hide();
}
}
function dhl_permanent_collection() {
if ( jQuery( '#woocommerce_dhl_permanent_collection' ).is( ':checked' ) ) {
jQuery( '#woocommerce_dhl_date_change_hour' ).closest( 'tr' ).show();
} else {
jQuery( '#woocommerce_dhl_date_change_hour' ).closest( 'tr' ).hide();
}
}
dhl_permanent_collection();
dhl_order_status();
jQuery( '#woocommerce_dhl_permanent_collection' ).click( function () {
dhl_permanent_collection();
} );
jQuery( '#woocommerce_dhl_auto_create' ).change( function () {
dhl_order_status();
} );
} )
</script>
<?php
}
/**
* @param array $package .
*
* @return void
*/
public function calculate_shipping( $package = [] ) {
}
/**
* @return bool|void
*/
public function process_admin_options() {
parent::process_admin_options();
$api = $this->get_api();
$api->clear_cache();
}
}
}