336 lines
10 KiB
PHP
336 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* Plugin.
|
|
*
|
|
* @package Woocommerce Dhl
|
|
*/
|
|
|
|
use VendorDHL\Octolize\Tracker\TrackerInitializer;
|
|
use VendorDHL\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
|
|
|
/**
|
|
* Main class for WordPress plugin
|
|
*/
|
|
class WooCommerce_DHL_Plugin extends AbstractPlugin {
|
|
|
|
const SELECT_TYPE = 'select_type';
|
|
|
|
/**
|
|
* Plugin path.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $plugin_path;
|
|
|
|
/**
|
|
* Template path.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $template_path;
|
|
|
|
/**
|
|
* Plugin text domain.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $plugin_text_domain;
|
|
|
|
/**
|
|
* Scripts version.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $scripts_version = '67';
|
|
|
|
/**
|
|
* DHL.
|
|
*
|
|
* @var WPDesk_WooCommerce_DHL
|
|
*/
|
|
public $dhl = null;
|
|
|
|
/**
|
|
* Flexible printing integration.
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $flexible_printing_integration = false;
|
|
|
|
/**
|
|
* WooCommerce_DHL_Plugin constructor.
|
|
*
|
|
* @param \VendorDHL\WPDesk_Plugin_Info $plugin_info .
|
|
*/
|
|
public function __construct( \VendorDHL\WPDesk_Plugin_Info $plugin_info ) {
|
|
$this->plugin_info = $plugin_info;
|
|
|
|
parent::__construct( $this->plugin_info );
|
|
}
|
|
|
|
/**
|
|
* Init base variables for plugin
|
|
*/
|
|
public function init_base_variables() {
|
|
$this->plugin_url = $this->plugin_info->get_plugin_url();
|
|
|
|
$this->plugin_path = $this->plugin_info->get_plugin_dir();
|
|
$this->template_path = $this->plugin_info->get_text_domain();
|
|
|
|
$this->plugin_text_domain = $this->plugin_info->get_text_domain();
|
|
$this->plugin_namespace = $this->plugin_info->get_text_domain();
|
|
$this->template_path = $this->plugin_info->get_text_domain();
|
|
$this->default_settings_tab = 'main';
|
|
|
|
$this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=dhl' );
|
|
$this->docs_url = get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/docs/faktury-zaawansowane-raporty-docs' : 'https://www.wpdesk.net/docs/flexible-invoices-advanced-reports-docs/';
|
|
|
|
$this->default_view_args = [
|
|
'plugin_url' => $this->get_plugin_url(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Init plugin
|
|
*/
|
|
public function init() {
|
|
parent::init();
|
|
|
|
add_action(
|
|
'plugins_loaded',
|
|
function () {
|
|
if ( class_exists( 'WPDesk_Flexible_Shipping_Shipment_dhl' ) ) {
|
|
WPDesk_Flexible_Shipping_Shipment_dhl::$_plugin = $this;
|
|
|
|
$this->dhl = WPDesk_WooCommerce_DHL::get_instance( $this );
|
|
new WPDesk_WooCommerce_DHL_FS_Hooks( $this );
|
|
|
|
$dhl_select_ajax = new WPDesk_WooCommerce_DHL_Select_Parcels_Via_Ajax( $this->dhl, $this->plugin_namespace );
|
|
$dhl_select_ajax->hooks();
|
|
}
|
|
}
|
|
);
|
|
|
|
add_action(
|
|
'woocommerce_init',
|
|
function () {
|
|
$shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
if ( isset( $shipping_methods['dhl'] ) ) {
|
|
$api_status_ajax = new \WPDesk\DHL\ApiConnectionStatusAjax( $shipping_methods['dhl'] );
|
|
$api_status_ajax->hooks();
|
|
}
|
|
}
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* Hooks.
|
|
*/
|
|
public function hooks() {
|
|
parent::hooks();
|
|
|
|
TrackerInitializer::create_from_plugin_info_for_shipping_method( $this->plugin_info, WPDesk_WooCommerce_DHL_Shipping_Method::METHOD_ID )->hooks();
|
|
|
|
add_action( 'admin_notices', [ $this, 'admin_notices' ] );
|
|
add_filter( 'flexible_printing_integrations', [ $this, 'flexible_printing_integrations' ] );
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param array $integrations .
|
|
*
|
|
* @return array
|
|
*/
|
|
public function flexible_printing_integrations( array $integrations ) {
|
|
include_once 'class-flexible-printing-integration.php';
|
|
|
|
$this->flexible_printing_integration = new WPDesk_WooCommerce_DHL_Flexible_Printing_Integration( $this );
|
|
$integrations[ $this->flexible_printing_integration->id ] = $this->flexible_printing_integration;
|
|
|
|
return $integrations;
|
|
}
|
|
|
|
/**
|
|
* Enqueue scripts.
|
|
*/
|
|
public function wp_enqueue_scripts() {
|
|
parent::wp_enqueue_scripts();
|
|
|
|
if ( is_checkout() ) {
|
|
|
|
$shipping_methods = WC()->shipping()->get_shipping_methods();
|
|
$settings = $shipping_methods['dhl']->settings;
|
|
$select_type = isset( $settings[ self::SELECT_TYPE ] ) ? $settings[ self::SELECT_TYPE ] : 'select2';
|
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
|
wp_register_style( 'dhl_checkout_css', $this->get_plugin_assets_url() . 'css/checkout' . $suffix . '.css', [], $this->scripts_version );
|
|
wp_enqueue_style( 'dhl_checkout_css' );
|
|
wp_register_style( 'dhl_admin_css', $this->get_plugin_assets_url() . 'css/admin' . $suffix . '.css', [], $this->scripts_version );
|
|
wp_enqueue_style( 'dhl_admin_css' );
|
|
wp_enqueue_script( 'dhl_checkout_js', $this->get_plugin_assets_url() . 'js/checkout' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
|
|
wp_localize_script(
|
|
'dhl_checkout_js',
|
|
'dhl_checkout_js',
|
|
[
|
|
'map_url' => site_url( '?dhl_parcelshop_map=1' ),
|
|
'parcelshop_not_exists' => __( 'Wskazany punkt odbioru nie istnieje!', 'woocommerce-dhl' ),
|
|
'select_type' => $select_type,
|
|
'lang' => [
|
|
'loadingMore' => __( 'Więcej...', 'woocommerce-dhl' ),
|
|
'noResults' => __( 'Brak punktów.', 'woocommerce-dhl' ),
|
|
'searching' => __( 'Szukam punktów...', 'woocommerce-dhl' ),
|
|
'errorLoading' => __( 'Wyszukiwanie w toku...', 'woocommerce-dhl' ),
|
|
'placeholder' => __( 'Wpisz miasto lub miasto, ulica.', 'woocommerce-dhl' ),
|
|
'min_chars' => __( 'Wpisz minimum % znaki.', 'woocommerce-dhl' ),
|
|
'nomachine' => __( 'Punkt nie istnieje', 'woocommerce-dhl' ),
|
|
],
|
|
'ajax_nonce' => wp_create_nonce( 'woocommerce-dhl' ),
|
|
'l10n' => [
|
|
'close_popup' => __( 'Zamknij mapę', 'woocommerce-dhl' ),
|
|
],
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Admin enqueue scripts.
|
|
*/
|
|
public function admin_enqueue_scripts() {
|
|
$current_screen = get_current_screen();
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
|
if ( $current_screen && in_array( $current_screen->id, [ 'shop_order', 'edit-shop_order', 'woocommerce_page_wc-orders' ], true ) ) {
|
|
wp_register_style( 'dhl_admin_css', $this->get_plugin_assets_url() . 'css/admin' . $suffix . '.css', [], $this->scripts_version );
|
|
wp_enqueue_style( 'dhl_admin_css' );
|
|
|
|
wp_enqueue_script( 'dhl_admin_order_js', $this->get_plugin_assets_url() . 'js/admin-order' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
|
|
|
|
wp_localize_script(
|
|
'dhl_admin_order_js',
|
|
'dhl_ajax_object',
|
|
[
|
|
'ajax_url' => admin_url( 'admin-ajax.php' ),
|
|
]
|
|
);
|
|
|
|
/**
|
|
* JavaScript i18n support in WordPress 5.0
|
|
*
|
|
* @link https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/
|
|
*/
|
|
wp_enqueue_script( 'i18n_dhl_support', $this->get_plugin_assets_url() . 'js/i18n-support.js', [ 'wp-i18n' ], $this->scripts_version, true );
|
|
|
|
wp_set_script_translations( 'i18n_dhl_support', 'woocommerce-dhl' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Admin notices.
|
|
*/
|
|
public function admin_notices() {
|
|
if ( defined( 'FLEXIBLE_SHIPPING_VERSION' ) ) {
|
|
$version_compare = version_compare( FLEXIBLE_SHIPPING_VERSION, '1.9.4' );
|
|
|
|
if ( $version_compare < 0 ) {
|
|
$class = 'notice notice-error';
|
|
$message = __( 'Wtyczka WooCommerce DHL wymaga wtyczki Flexible Shipping w wersji nie niższej niż 1.9.4.', 'woocommerce-dhl' );
|
|
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message ); // wpcs: XSS ok.
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
|
|
|
|
$slug = 'flexible-shipping';
|
|
$install_url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $slug ), 'install-plugin_' . $slug );
|
|
$activate_url = 'plugins.php?action=activate&plugin=' . urlencode( 'flexible-shipping/flexible-shipping.php' ) . '&plugin_status=all&paged=1&s&_wpnonce=' . urlencode( wp_create_nonce( 'activate-plugin_flexible-shipping/flexible-shipping.php' ) );
|
|
|
|
$message = sprintf(
|
|
wp_kses(
|
|
// Translators: url.
|
|
__(
|
|
'Wtyczka WooCommerce DHL wymaga wtyczki Flexible Shipping. <a href="%s">Zainstaluj Flexible Shipping →</a>',
|
|
'woocommerce-dhl'
|
|
),
|
|
[ 'a' => [ 'href' => [] ] ]
|
|
),
|
|
esc_url( $install_url )
|
|
);
|
|
$is_downloaded = false;
|
|
$plugins = array_keys( get_plugins() );
|
|
foreach ( $plugins as $plugin ) {
|
|
if ( strpos( $plugin, 'flexible-shipping/flexible-shipping.php' ) === 0 ) {
|
|
$is_downloaded = true;
|
|
$message = sprintf(
|
|
wp_kses(
|
|
// Translators: url.
|
|
__( 'Wtyczka WooCommerce DHL wymaga wtyczki Flexible Shipping. <a href="%s">Włącz Flexible Shipping →</a>', 'woocommerce-dhl' ),
|
|
[ 'a' => [ 'href' => [] ] ]
|
|
),
|
|
esc_url( admin_url( $activate_url ) )
|
|
);
|
|
}
|
|
}
|
|
echo '<div class="error fade"><p>' . $message . '</p></div>' . "\n"; // WPCS: XSS ok.
|
|
}
|
|
|
|
/**
|
|
* Plugin actions links.
|
|
*
|
|
* @param array $links .
|
|
*
|
|
* @return array
|
|
*/
|
|
public function links_filter( $links ) {
|
|
$plugin_links = [
|
|
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=dhl' ) . '">' . __( 'Ustawienia', 'woocommerce-dhl' ) . '</a>',
|
|
'<a target="_blank" href="https://www.wpdesk.pl/docs/woocommerce-dhl-docs/">' . __( 'Dokumentacja', 'woocommerce-dhl' ) . '</a>',
|
|
'<a target="_blank" href="https://www.wpdesk.pl/support/">' . __( 'Wsparcie', 'woocommerce-dhl' ) . '</a>',
|
|
];
|
|
|
|
return array_merge( $plugin_links, $links );
|
|
}
|
|
|
|
/**
|
|
* Renders end returns selected template
|
|
*
|
|
* @param string $name name of the template .
|
|
* @param string $path additional inner path to the template .
|
|
* @param array $args args accesible from template .
|
|
*
|
|
* @return string
|
|
*/
|
|
public function load_template( $name, $path = '', array $args = [] ) {
|
|
$plugin_template_path = trailingslashit( $this->plugin_path ) . 'templates/';
|
|
|
|
// Look within passed path within the theme - this is priority.
|
|
$template = locate_template(
|
|
[
|
|
trailingslashit( $this->get_template_path() ) . trailingslashit( $path ) . $name . '.php',
|
|
]
|
|
);
|
|
|
|
if ( ! $template ) {
|
|
$template = $plugin_template_path . trailingslashit( $path ) . $name . '.php';
|
|
}
|
|
|
|
extract( $args ); // phpcs:ignore
|
|
ob_start();
|
|
include $template;
|
|
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Get template path.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_template_path() {
|
|
return trailingslashit( $this->template_path );
|
|
}
|
|
}
|