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

496 lines
16 KiB
PHP

<?php
/**
* Shipping method.
*
* @package WooCommerce DPD
*/
use DPDVendor\Psr\Log\NullLogger;
use DPDVendor\WPDesk\Logger\WPDeskLoggerFactory;
use DPDVendor\WPDesk\PickupPoints\WooCommerceSettings\RefreshPickupPointsTrait;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\Fields\SenderAddress\SettingsField;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\Fields\SenderAddress\ShippingMethodRenderSettingsTrait;
use DPDVendor\WpDesk\WooCommerce\ShippingMethod\SenderAddress;
use DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\ApiConnectionRenderer;
use DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\HasApiConnectionStatusField;
/**
* DPD Shipping method.
*/
class WPDesk_WooCommerce_DPD_Shipping_Method extends WC_Shipping_Method implements HasApiConnectionStatusField {
use ApiConnectionRenderer;
use ShippingMethodRenderSettingsTrait;
use RefreshPickupPointsTrait;
const SHIPPING_METHOD_ID = 'dpd';
const ADDITIONAL_SENDER_ADDRESSES = 'additional_sender_addresses';
const ADDITIONAL_SENDER_ADDRESSES_ENABLED = 'additional_sender_addresses_enabled';
const OPTION_LABEL_PAGE_FORMAT = 'label_page_format';
const OPTION_LABEL_FORMAT = 'label_format';
const OPTION_LABEL_TYPE = 'label_type';
const OPTION_SENDER_COMPANY = 'sender_company';
const OPTION_SENDER_NAME = 'sender_name';
const OPTION_SENDER_ADDRESS = 'sender_address';
const OPTION_SENDER_POSTAL_CODE = 'sender_postal_code';
const OPTION_SENDER_CITY = 'sender_city';
const OPTION_SENDER_PHONE = 'sender_phone';
const OPTION_SENDER_EMAIL = 'sender_email';
const OPTION_LOGGING = 'logging';
/**
* API login.
*
* @var string
*/
private $login;
/**
* API password.
*
* @var string
*/
private $password;
/**
* API fid.
*
* @var string
*/
private $fid;
/**
* API test mode setting.
*
* @var string
*/
private $test_mode;
/**
* DPD API.
*
* @var bool|WPDesk_WooCommerce_DPD_API
*/
public $api = false;
/**
* @param int $instance_id .
*/
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
$this->id = self::SHIPPING_METHOD_ID;
$this->method_title = __( 'DPD', 'woocommerce-dpd' );
$this->method_description = sprintf(
// Translators: docs link.
__( 'Integracja WooCommerce z DPD. %1$sZapoznaj się z instrukcją obsługi &rarr;%2$s', 'woocommerce-dpd' ),
'<a href="https://www.wpdesk.pl/docs/woocommerce-dpd-docs/" target="_blank">',
'</a>'
);
$this->enabled = 'yes';
$this->title = __( 'DPD', 'woocommerce-dpd' );
$this->settings['enabled'] = 'yes';
$this->init();
add_action( 'woocommerce_update_options_shipping_' . $this->id, [ $this, 'process_admin_options' ] );
}
/**
* .
*/
public function init() {
$this->init_settings();
$this->init_form_fields();
$this->login = $this->get_option( 'login' );
$this->password = $this->get_option( 'password' );
$this->fid = $this->get_option( 'fid' );
$this->test_mode = $this->get_option( 'test_mode', 'no' );
if ( 'yes' === $this->test_mode ) {
$this->login = $this->get_option( 'login_testmode' );
$this->password = $this->get_option( 'password_testmode' );
$this->fid = $this->get_option( 'fid_testmode' );
}
}
/**
* Get DPD API.
*
* @return bool|WPDesk_WooCommerce_DPD_API
*/
public function get_api() {
if ( ! $this->api ) {
$disable_ssl_verification = apply_filters( 'woocommerce_dpd_disable_ssl_verification', false );
$disable_cache_wsdl = apply_filters( 'woocommerce_dpd_disable_cache_wsdl', false );
if ( $this->get_option( self::OPTION_LOGGING, 'no' ) === 'yes' ) {
$logger = ( new WPDeskLoggerFactory() )->createWPDeskLogger();
} else {
$logger = new NullLogger();
}
$this->api = new WPDesk_WooCommerce_DPD_API(
new WPDesk_WooCommerce_DPD_Auth_Data(
$this->login,
$this->password,
$this->fid
),
$logger,
'yes' === $this->test_mode,
$disable_ssl_verification,
$disable_cache_wsdl
);
}
return $this->api;
}
/**
* Initialise Settings Form Fields
*/
public function init_form_fields() {
$order_statuses = wc_get_order_statuses();
$flexible_printing = apply_filters( 'flexible_printing', false );
$auto_print_custom_attributes = [];
if ( $flexible_printing ) {
if ( 'yes' === $this->get_option( 'auto_print', '' ) ) {
$flexible_printing_integration_url = apply_filters( 'flexible_printing_integration_url', self::SHIPPING_METHOD_ID );
// Translators: printing settings.
$auto_print_description = sprintf(
// Translators: link.
__( 'Aby przejść do ustawień wydruku kliknij %1$stutaj%2$s.', 'woocommerce-dpd' ),
'<a target="_blank" href="' . $flexible_printing_integration_url . '">',
'</a>'
);
} else {
$auto_print_description = __( 'Konfiguracja wydruku dostępna po włączeniu opcji i zapisaniu ustawień.', 'woocommerce-dpd' );
}
} else {
$this->settings['auto_print'] = 'no';
$flexible_printing_buy_url = 'https://www.wpdesk.pl/sklep/flexible-printing/';
$auto_print_description = sprintf(
// Translators: buy link.
__( 'Drukuj etykiety bezpośrednio na drukarce (bez pobierania pliku) lub zapisuj automatycznie na dysku. %1$sKup Flexible Printing &rarr;%2$s.', 'woocommerce-dpd' ),
'<a href="' . $flexible_printing_buy_url . '" target="_blank">',
'</a>'
);
$auto_print_custom_attributes = [ 'disabled' => 'disabled' ];
}
$settings = [
[
'title' => __( 'Logowanie', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
'login' => [
'title' => __( 'Login', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'description' => __( 'Login do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
],
],
'password' => [
'title' => __( 'Hasło', 'woocommerce-dpd' ) . '*',
'type' => 'password',
'description' => __( 'Hasło do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
'autocomplete' => 'new-password',
],
],
'fid' => [
'title' => __( 'Nr klienta', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'desc_tip' => false,
'custom_attributes' => [
'required' => 'required',
],
],
'test_mode' => [
'title' => __( 'Tryb testowy', 'woocommerce-dpd' ),
'label' => __( 'Włącz tryb testowy', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => __( 'Jeśli włączasz tryb testowy - wpisz Login, Hasło i Nr. klienta do API testowego, a nie serwisu produkcyjnego.', 'woocommerce-dpd' ),
'default' => 'no',
'desc_tip' => false,
],
'login_testmode' => [
'title' => __( 'Login dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'text',
'description' => __( 'Login do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
],
'password_testmode' => [
'title' => __( 'Hasło dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'password',
'description' => __( 'Hasło do API DPD.', 'woocommerce-dpd' ),
'default' => '',
'desc_tip' => true,
],
'fid_testmode' => [
'title' => __( 'Nr klienta dla trybu testowego', 'woocommerce-dpd' ),
'type' => 'text',
'default' => '',
'desc_tip' => false,
'custom_attributes' => [],
],
'api_status' => [
'title' => __( 'Status połączenia', 'woocommerce-dhl' ),
'type' => \DPDVendor\WPDesk\WooCommerceShippingMethod\Field\ApiConnectionStatus\ApiConnectionStatus::FIELD_TYPE,
'default' => '',
'desc_tip' => true,
'custom_attributes' => [
'required' => 'required',
'autocomplete' => 'new-password'
]
],
[
'title' => __( 'Tworzenie przesyłek', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
'auto_create' => [
'title' => __( 'Tworzenie przesyłek', 'woocommerce-dpd' ),
'type' => 'select',
'default' => 'manual',
'options' => [
'manual' => __( 'Ręczne', 'woocommerce-dpd' ),
'auto' => __( 'Automatyczne', 'woocommerce-dpd' ),
],
],
'order_status' => [
'title' => __( 'Status zamówienia', 'woocommerce-dpd' ),
'type' => 'select',
'default' => 'wc-completed',
'options' => $order_statuses,
'description' => __( 'Status zamówienia, przy którym do zamówienia zostanie automatycznie utworzona przesyłka.', 'woocommerce-dpd' ),
],
'complete_order' => [
'title' => __( 'Zrealizuj zamówienie', 'woocommerce-dpd' ),
'type' => 'checkbox',
'label' => __( 'Włącz zmianę statusu zamówienia na Zrealizowane', 'woocommerce-dpd' ),
'default' => 'no',
'description' => __( 'Po nadaniu przesyłek status zamówienia zostanie automatycznie zmieniony na Zrealizowane.', 'woocommerce-dpd' ),
],
[
'title' => __( 'Etykiety', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_LABEL_FORMAT => [
'title' => __( 'Format etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::PDF => __( 'PDF', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::ZPL => __( 'ZPL', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::EPL => __( 'EPL', 'woocommerce-dpd' ),
],
'default' => 'PDF',
],
self::OPTION_LABEL_PAGE_FORMAT => [
'title' => __( 'Format strony etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::A4 => __( 'A4 - jedna etykieta na stronę', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE => __( 'A4 - wiele etykiet na stronę', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::LBL_PRINTER => __( 'Etykieciarka', 'woocommerce-dpd' ),
],
'default' => WPDesk_WooCommerce_DPD_Label_Settings::A4_MULTIPLE,
'desc_tip' => true,
'description' => sprintf(
// Translators: strong.
__( 'Dla opcji %1$sA4 - wiele etykiet na stronę%2$s, etykiety przesyłek zagranicznych drukowane są w osobnym pliku.', 'woocommerce-dpd' ),
'<strong>',
'</strong>'
),
],
self::OPTION_LABEL_TYPE => [
'title' => __( 'Typ etykiety', 'woocommerce-dpd' ),
'type' => 'select',
'options' => [
WPDesk_WooCommerce_DPD_Label_Settings::BIC3 => __( '15x10 cm (BIC3)', 'woocommerce-dpd' ),
WPDesk_WooCommerce_DPD_Label_Settings::BIC3_EXTENDED1 => __( '17x10 cm (BIC3_EXT)', 'woocommerce-dpd' ),
],
'default' => WPDesk_WooCommerce_DPD_Label_Settings::BIC3,
],
'auto_print' => [
'title' => __( 'Drukowanie', 'woocommerce-dpd' ),
'label' => __( 'Włącz automatyczne drukowanie', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => $auto_print_description,
'custom_attributes' => $auto_print_custom_attributes,
'default' => 'no',
'desc_tip' => false,
],
[
'title' => __( 'Dane nadawcy (adres główny)', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_SENDER_COMPANY => [
'title' => __( 'Firma', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_NAME => [
'title' => __( 'Imię i nazwisko', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_ADDRESS => [
'title' => __( 'Adres', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_POSTAL_CODE => [
'title' => __( 'Kod pocztowy', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_CITY => [
'title' => __( 'Miasto', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_PHONE => [
'title' => __( 'Telefon', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => '',
'custom_attributes' => [
'required' => 'required',
],
],
self::OPTION_SENDER_EMAIL => [
'title' => __( 'E-mail', 'woocommerce-dpd' ) . '*',
'type' => 'text',
'default' => get_option( 'admin_email' ),
'custom_attributes' => [
'required' => 'required',
],
],
self::ADDITIONAL_SENDER_ADDRESSES_ENABLED => [
'title' => __( 'Dodatkowe adresy', 'woocommerce-dpd' ),
'label' => __( 'Włącz konfigurację dodatkowych adresów nadania', 'woocommerce-dpd' ),
'type' => 'checkbox',
'description' => __( 'Adres nadawcy możesz wybrać w metabox DPD w edycji zamówienia.', 'woocommerce-dpd' ),
'default' => 'no',
'desc_tip' => false,
],
self::ADDITIONAL_SENDER_ADDRESSES => [
'title' => '',
'type' => SettingsField::FIELD_TYPE,
'default' => '{}',
],
[
'title' => __( 'Opcje zaawansowane', 'woocommerce-dpd' ),
'type' => 'title',
'description' => '',
],
self::OPTION_LOGGING => [
'title' => __( 'Zapis zdarzeń', 'woocommerce-dpd' ),
'label' => __( 'Włącz zapis zdarzeń do logów', 'woocommerce-dpd' ),
'type' => 'checkbox',
// Translators: WC logger URL.
'description' => sprintf( __( 'Zapis zdarzeń należy włączać tylko podczas diagnozowania problemów. Zdarzenia zapisywane są w %1$sdzienniku WooCommerce%2$s.', 'woocommerce-dpd' ), '<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '" target="_blank">', '</a>' ),
'custom_attributes' => [],
'default' => 'no',
'desc_tip' => false,
],
'refresh_points' => [
'type' => 'refresh_pickup_points',
'service' => 'dpd',
],
];
$this->form_fields = $settings;
}
/**
* @param array $form_fields .
* @param bool $echo .
*
* @return string|void .
*/
public function generate_settings_html( $form_fields = [], $echo = false ) {
parent::generate_settings_html( $form_fields );
$lbl_printer = WPDesk_WooCommerce_DPD_Label_Settings::LBL_PRINTER;
include __DIR__ . '/views/shipping-method-settings-script.php';
}
/**
* Check API connection.
*/
public function check_connection() {
$api = $this->get_api();
$api->ping();
}
/**
* @param array $package .
*/
public function calculate_shipping( $package = [] ) {
}
/**
* @return bool|void
*/
public function process_admin_options() {
parent::process_admin_options();
$api = $this->get_api();
$api->clear_cache();
}
/**
* Return array of SenderAddress.
*
* @return SenderAddress[]
*/
public function get_additional_sender_addresses() {
return SenderAddress::create_sender_addresses_from_settings( $this->get_option( self::ADDITIONAL_SENDER_ADDRESSES, '{}' ) );
}
/**
* @return WPDesk_WooCommerce_DPD_Label_Settings
*/
public function get_label_settings() {
return new WPDesk_WooCommerce_DPD_Label_Settings(
$this->get_option( self::OPTION_LABEL_FORMAT ),
$this->get_option( self::OPTION_LABEL_PAGE_FORMAT ),
$this->get_option( self::OPTION_LABEL_TYPE )
);
}
}