Files
2026-04-28 15:13:50 +02:00

638 lines
20 KiB
PHP

<?php
/**
* Plugin
*
* @package PaczkomatyInpost
*/
use FS\InPost\CheckoutBlocks\DefaultOptions;
use FS\InPost\PickupPoints\PickupPointsData;
use FS\InPost\PickupPoints\PickupPointsManager;
use FS\InPost\PickupPoints\RefreshPoints;
use FS\InPost\Plugin\UpgradeOnboarding;
use FS\InPost\Product\PaczkomatDisabledField;
use FS\InPost\Returns\FastReturns;
use FS\InPost\Shipment\LabelBuilder;
use FS\InPost\Shipment\SubscriptionsIntegration;
use FSVendor\WPDesk\FS\Shipment\Label\LabelsBulkActionHandler;
use VendorInPost\Octolize\Blocks\PickupPoint\IntegrationData;
use VendorInPost\Octolize\Blocks\PickupPoint\PickupPointValidatorForFsIntegration;
use VendorInPost\Octolize\Blocks\PickupPoint\PickupPointValidatorForShippingMethod;
use VendorInPost\Octolize\Blocks\PickupPoint\Registrator;
use VendorInPost\Octolize\PluginUpdateReminder\RemindersFactory;
use VendorInPost\Octolize\Tracker\TrackerInitializer;
use VendorInPost\WPDesk\PickupPoints\Cache\CacheManager;
use VendorInPost\WPDesk\PickupPoints\PickupPoints;
use VendorInPost\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
use VendorInPost\WPDesk\Beacon\BeaconPro;
use VendorInPost\WPDesk\Beacon\BeaconGetShouldShowStrategy;
use VendorInPost\WPDesk\PluginBuilder\Plugin\HookableCollection;
use VendorInPost\WPDesk\PluginBuilder\Plugin\HookableParent;
use VendorInPost\WPDesk\PluginBuilder\Plugin\TemplateLoad;
use VendorInPost\WPDesk\View\Renderer\Renderer;
use VendorInPost\WPDesk\View\Renderer\SimplePhpRenderer;
use VendorInPost\WPDesk\View\Resolver\ChainResolver;
use VendorInPost\WPDesk\View\Resolver\DirResolver;
use VendorInPost\WPDesk\View\Resolver\WPThemeResolver;
/**
* Class WPDesk_Paczkomaty_Plugin
*/
class WPDesk_Paczkomaty_Plugin extends AbstractPlugin implements HookableCollection {
use TemplateLoad;
use HookableParent;
/**
* @var WPDesk_Paczkomaty
*/
public $paczkomaty;
/**
* Script version
*
* @var string
*/
public $scripts_version = WOOCOMMERCE_PACZKOMATY_INPOST_VERSION . '.134';
/**
* Flexible Printing Integration
*
* @var null
*/
public $flexible_printing_integration = null;
/**
* Logger
*
* @var bool
*/
private $logger = false;
/**
* Plugin namespace
*
* @var string
*/
public $plugin_namespace;
/**
* @var WPDesk_Paczkomaty_ShipX
*/
private $ship_x;
/**
* @var WC_Paczkomaty_Shipping_Method
*/
private $paczkomaty_shipping_method;
/**
* Renderer.
*
* @var Renderer
*/
private $renderer;
/**
* Pickup points cache manager.
*
* @var CacheManager
*/
private CacheManager $pickup_points;
/**
* Pickup points manager.
*
* @var PickupPointsManager
*/
private PickupPointsManager $pickup_points_manager;
/**
* Flexible_Invoices_Reports_Plugin constructor.
*
* @param VendorInPost\WPDesk_Plugin_Info $plugin_info Plugin info.
*/
public function __construct( VendorInPost\WPDesk_Plugin_Info $plugin_info ) {
$this->plugin_info = $plugin_info;
parent::__construct( $this->plugin_info );
}
public function get_scrips_version() {
return $this->scripts_version;
}
/**
* 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->plugin_namespace = $this->plugin_info->get_text_domain();
$this->template_path = basename( $this->plugin_info->get_plugin_dir() );
}
/**
* Init plugin
*/
public function init() {
parent::init();
$this->init_logger();
$this->init_renderer();
$this->init_base_variables();
$this->load_dependencies();
$this->init_tracker();
}
private function init_logger() {
$settings = $this->get_shipping_method_settings();
if ( isset( $settings['logging'] ) && 'yes' === $settings['logging'] ) {
$this->logger = ( new \VendorInPost\WPDesk\Logger\SimpleLoggerFactory( 'inpost' ) )->getLogger();
} else {
$this->logger = new \VendorInPost\Psr\Log\NullLogger();
}
}
/**
* @return void
*/
private function init_tracker() {
( new WPDesk_Paczkomaty_Tracker() )->hooks();
TrackerInitializer::create_from_plugin_info_for_shipping_method( $this->plugin_info, WC_Paczkomaty_Shipping_Method::SHIPPING_METHOD_ID )->hooks();
}
/**
* Fires hooks.
*/
public function hooks(): void {
parent::hooks();
add_filter( 'woocommerce_shipping_methods', [ $this, 'paczkomaty_shipping_method' ] );
add_filter( 'flexible_printing_integrations', [ $this, 'flexible_printing_integrations' ] );
add_action( 'paczkomaty_shipping_method_log', [ $this, 'paczkomaty_shipping_method_log' ] );
add_action( 'plugins_loaded', [ $this, 'init_labels_builder' ] );
add_action( 'woocommerce_init', [ $this, 'woocommerce_init' ] );
$this->add_hookable(
new UpgradeOnboarding(
$this->plugin_info->get_plugin_name(),
$this->plugin_info->get_version(),
$this->plugin_info->get_plugin_file_name(),
$this->plugin_info->get_plugin_url()
)
);
$this->add_hookable( new PaczkomatDisabledField() );
$this->add_hookable( new RemindersFactory( $this->plugin_info->get_plugin_dir(), $this->plugin_info->get_plugin_file_name(), $this->plugin_info->get_plugin_name() ) );
$this->hooks_on_hookable_objects();
$this->init_checkout_blocks();
}
/**
* @param array $methods .
*
* @return string[]
*/
public function paczkomaty_shipping_method( $methods ) { // phpcs:ignore
$methods['paczkomaty_shipping_method'] = WC_Paczkomaty_Shipping_Method::class;
return $methods;
}
public function woocommerce_init(): void {
$this->init_paczkomaty();
$this->init_api_xml();
$this->init_manifest();
$this->init_returns();
}
private function init_checkout_blocks(): void {
$default_options = new DefaultOptions(
$this->plugin_namespace,
$this->get_pickup_points_manager(),
$this->get_shipping_method_settings()
);
$default_options->hooks();
$integration_data = ( new IntegrationData() )
->set_integration_name( $this->plugin_namespace )
->set_meta_data_name( 'paczkomat_id' )
->set_ajax_action( WPDesk_Paczkomaty_Select_Machines_Via_Ajax::AJAX_ACTION )
->set_ajax_url( admin_url( 'admin-ajax.php' ) )
->set_nonce_name( $this->plugin_namespace )
->set_flexible_shipping_integration( 'paczkomaty' );
( new Registrator(
$integration_data,
$this->plugin_path,
$this->get_plugin_file_path(),
[
new PickupPointValidatorForFsIntegration(
'paczkomaty',
'paczkomaty_usluga',
'paczkomaty',
__( 'Proszę wybrać paczkomat!', 'woocommerce-paczkomaty-inpost' )
),
]
) )->hooks();
add_action(
'init',
function () {
// Dummy texts.
__( 'Select pickup point', 'woocommerce-paczkomaty-inpost' );
__( 'Please enter at least 3 characters', 'woocommerce-paczkomaty-inpost' );
}
);
}
/**
* Get shipX API.
*
* @return WPDesk_Paczkomaty_ShipX
*/
public function get_shipX() {
if ( empty( $this->ship_x ) ) {
$settings = $this->get_shipping_method_settings();
$test_mode = 'yes' === ( $settings['test_mode'] ?? '' );
$this->ship_x = new WPDesk_Paczkomaty_ShipX(
$settings['token'] ?? '',
$settings['token_test_mode'] ?? '',
$test_mode
);
$this->ship_x->set_organization_id( $settings['organization_id'] ?? '' );
}
return $this->ship_x;
}
/**
* @return internal
*/
public function init_manifest() {
\FS\InPost\Manifest\Manifest::set_api( $this->get_shipX() );
\FS\InPost\Manifest\Manifest::set_shipping_method( $this->get_paczkomaty_shipping_method() );
class_alias( \FS\InPost\Manifest\Manifest::class, 'WPDesk_Flexible_Shipping_Manifest_paczkomaty' );
add_filter( 'flexible_shipping_has_manifests', '__return_true' );
}
/**
* @return internal
*/
public function init_returns() {
$shipping_method = $this->get_paczkomaty_shipping_method();
( new FastReturns(
$shipping_method->get_option( $shipping_method::FAST_RETURNS_URL, '' ),
$shipping_method->get_option( $shipping_method::FAST_RETURNS_BUTTON_LABEL, __( 'Zwrot', 'woocommerce-paczkomaty-inpost' ) ),
$this->renderer
) )->hooks();
}
/**
* Init labels builder.
*/
public function init_labels_builder() {
if ( class_exists( '\FSVendor\WPDesk\FS\Shipment\Label\LabelsBulkActionHandler' ) ) {
$labels_bulk_actions_handler = LabelsBulkActionHandler::get_labels_bulk_actions_handler();
$labels_bulk_actions_handler->add_builder( new LabelBuilder( WC_Paczkomaty_Shipping_Method::SHIPPING_INTEGRATION_ID, $this->get_logger() ) );
}
}
/**
* @param array $integrations Integrations.
*
* @return array
*/
public function flexible_printing_integrations( array $integrations ) {
$this->flexible_printing_integration = new WPDesk_Paczkomaty_Flexible_Printing_Integration( $this );
$integrations[ $this->flexible_printing_integration->id ] = $this->flexible_printing_integration;
return $integrations;
}
/**
* Load some dependencies
*/
public function load_dependencies() {
require_once $this->plugin_path . '/api/functions.php';
require_once $this->plugin_path . '/api/inpost.php';
}
private function get_pickup_points_service(): string {
return 'inpost';
}
private function get_pickup_points_manager(): PickupPointsManager {
if ( empty( $this->pickup_points_manager ) ) {
$this->pickup_points_manager = new PickupPointsManager( $this->get_pickup_points() );
}
return $this->pickup_points_manager;
}
private function get_pickup_points(): CacheManager {
if ( empty( $this->pickup_points ) ) {
$this->pickup_points = new CacheManager( new PickupPointsData( $this ), $this->get_pickup_points_service() );
}
return $this->pickup_points;
}
/**
* Init plugin classes
*/
private function init_paczkomaty(): void {
( new RefreshPoints( $this->get_pickup_points_service(), $this->get_pickup_points() ) )->hooks();
( new PickupPoints( $this->get_pickup_points_service(), $this->get_logger() ) )->hooks();
$pickup_points_manager = $this->get_pickup_points_manager();
$this->paczkomaty = new WPDesk_Paczkomaty( $this, $pickup_points_manager );
WC_Paczkomaty_Shipping_Method::$pickup_points_manager = $pickup_points_manager;
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::$pickup_points_manager = $pickup_points_manager;
new WPDesk_Paczkomaty_FS( $this->paczkomaty );
$settings_filters = new WPDesk_Paczkomaty_Settings_Filters( $this );
$settings_filters->hooks();
$paczkomaty_ajax = new WPDesk_Paczkomaty_Select_Machines_Via_Ajax( $this->paczkomaty, $this->plugin_namespace, $pickup_points_manager );
$paczkomaty_ajax->hooks();
$checkout = new WPDesk_Paczkomaty_Checkout( $this, $pickup_points_manager );
$checkout->hooks();
$allegro = new WPDesk_Paczkomaty_Allegro( $this );
$allegro->hooks();
$open_ssl = new WPDesk_Paczkomaty_Open_SSL();
$open_ssl->hooks();
$shipment_status_scheduler = new WPDesk_Paczkomaty_Shipment_Status_Scheduler( $this->get_logger(), WC_Queue::instance() );
$shipment_status_scheduler->hooks();
$beacon = new BeaconPro(
'121326c5-8c89-4adb-b982-f151af67182f',
new BeaconGetShouldShowStrategy(
[
[
'page' => 'wc-settings',
'tab' => 'shipping',
'section' => 'paczkomaty_shipping_method',
],
]
),
$this->get_plugin_url() . 'vendor_prefixed/wpdesk/wp-helpscout-beacon/assets/'
);
$beacon->hooks();
( new SubscriptionsIntegration() )->hooks();
WPDesk_Flexible_Shipping_Shipment_Paczkomaty::$_plugin = $this;
}
/**
* @return array
*/
public function get_shipping_method_settings() {
$settings = get_option( 'woocommerce_paczkomaty_shipping_method_settings', [] );
if ( ! is_array( $settings ) ) {
$settings = [];
} else {
$settings['select_type'] = 'select2_ajax'; // Only select2_ajax is supported.
}
return $settings;
}
/**
* @return WC_Paczkomaty_Shipping_Method
*/
public function get_paczkomaty_shipping_method() {
if ( ! $this->paczkomaty_shipping_method instanceof WC_Paczkomaty_Shipping_Method ) {
if ( class_exists( WC_Shipping_Method::class ) ) {
$this->paczkomaty_shipping_method = new WC_Paczkomaty_Shipping_Method();
}
}
return $this->paczkomaty_shipping_method;
}
/**
* @return bool|\VendorInPost\Psr\Log\LoggerInterface
*/
public function get_logger() {
return $this->logger;
}
/**
* @param string $message .
*/
public function paczkomaty_shipping_method_log( $message ) {
$this->get_logger()->debug( $message );
}
/**
* Init XML API if enabled.
*
* @return void
*/
public function init_api_xml() {
$paczkomaty_shipping_method = $this->get_paczkomaty_shipping_method();
if ( $paczkomaty_shipping_method instanceof WC_Paczkomaty_Shipping_Method
&& $paczkomaty_shipping_method->get_parcel_lockers_api() === WC_Paczkomaty_Shipping_Method::API_TYPE_XML
) {
$wp_upload_dir = wp_upload_dir();
$inpost_data_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'woocommerce-inpost/api/data';
$inpost_api_url = 'http://api.paczkomaty.pl';
$settings = get_option( 'woocommerce_paczkomaty_shipping_method_settings' );
if ( isset( $settings['test_mode'] ) && 'yes' === $settings['test_mode'] ) {
$inpost_data_dir = trailingslashit( $wp_upload_dir['basedir'] ) . 'woocommerce-inpost/api/data/test';
$inpost_api_url = 'https://sandbox-api.paczkomaty.pl';
}
$this->set_global_inpost_config( $inpost_data_dir, $inpost_api_url );
$this->prepare_cache( $inpost_data_dir );
if ( 1 !== inpost_check_environment() ) {
add_action( 'admin_notices', [ $this, 'paczkomaty_check_notice' ] );
}
}
}
public function paczkomaty_check_notice() {
ob_start();
inpost_check_environment( 1 );
$out = ob_get_clean();
if ( $out ) {
$class = 'error';
echo "<div class=\"$class\"> <p>$out</p></div>"; // phpcs:ignore
}
}
/**
* @param string $inpost_data_dir .
*/
private function prepare_cache( $inpost_data_dir ) {
if ( ! file_exists( $inpost_data_dir ) ) {
mkdir( $inpost_data_dir, 0777, true ); // phpcs:ignore
}
$this->create_file_if_not_exists( $inpost_data_dir . '/time1.dat' );
$this->create_file_if_not_exists( $inpost_data_dir . '/time2.dat' );
$this->create_file_if_not_exists( $inpost_data_dir . '/cache1.dat' );
$this->create_file_if_not_exists( $inpost_data_dir . '/cache2.dat' );
}
/**
* @param string $data_dir .
* @param string $api_url .
*/
private function set_global_inpost_config( $data_dir, $api_url ) {
global $inpost_data_dir, $inpost_api_url;
$inpost_data_dir = $data_dir;
$inpost_api_url = $api_url;
}
/**
* @param string $file_name .
*/
private function create_file_if_not_exists( $file_name ) {
if ( ! file_exists( $file_name ) ) {
$file = fopen( $file_name, "w" ); // phpcs:ignore
if ( false !== $file ) {
fclose( $file ); // phpcs:ignore
}
}
}
/**
* Returns data for script localization
*
* @return array .
*/
public function get_localization_script_data() {
$settings = $this->get_shipping_method_settings();
if ( ! isset( $settings['select_type'] ) ) {
$settings['select_type'] = 'select2_ajax';
}
$scripts = wp_scripts();
return [
'lang' => [
'loading_more' => __( 'Więcej...', 'woocommerce-paczkomaty-inpost' ),
'no_results' => __( 'Brak paczkomatów.', 'woocommerce-paczkomaty-inpost' ),
'searching' => __( 'Szukam paczkomatów...', 'woocommerce-paczkomaty-inpost' ),
'error_loading' => __( 'Wyszukiwanie w toku...', 'woocommerce-paczkomaty-inpost' ),
'placeholder' => __( 'Wpisz miasto lub miasto, ulica.', 'woocommerce-paczkomaty-inpost' ),
'min_chars' => __( 'Wpisz minimum % znaki.', 'woocommerce-paczkomaty-inpost' ),
'nomachine' => __( 'Paczkomat nie istnieje', 'woocommerce-paczkomaty-inpost' ),
],
'settings' => [
'select_type' => $settings['select_type'],
],
'ajax_url' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce( $this->plugin_namespace ),
'api_endpoint' => ( $settings['test_mode'] ?? 'no' ) === 'no' ? WPDesk_Paczkomaty_ShipX::MAP_ENDPOINT_URL : WPDesk_Paczkomaty_ShipX::MAP_ENDPOINT_URL_SANDBOX,
];
}
/**
* Admin enqueue scripts
*
* @param string $hook_suffix .
*/
public function admin_enqueue_scripts( $hook_suffix = '' ) {
$current_screen = get_current_screen();
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$is_settings_page = ( 'woocommerce_page_wc-settings' === $current_screen->id && isset( $_GET['section'] ) && 'paczkomaty_shipping_method' === $_GET['section'] );
$is_shop_order = in_array( $current_screen->id, [ 'shop_order', 'woocommerce_page_wc-orders' ], true );
$is_shop_subscription = ( 'shop_subscription' === $current_screen->id );
if ( $is_settings_page ) {
wp_register_script( 'paczkomaty_admin_settings', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin_settings' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
wp_localize_script( 'paczkomaty_admin_settings', 'paczkomaty_admin_settings', [ 'ajax_url' => admin_url( 'admin-ajax.php' ) ] );
wp_enqueue_script( 'paczkomaty_admin_settings' );
}
if ( $is_shop_order || $is_shop_subscription ) {
wp_register_script( 'paczkomaty_admin_order', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin_order' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
wp_localize_script( 'paczkomaty_admin_order', 'paczkomaty_admin_order', [ 'ajax_url' => admin_url( 'admin-ajax.php' ) ] );
wp_enqueue_script( 'paczkomaty_admin_order' );
}
if ( $is_settings_page || $is_shop_order || $is_shop_subscription ) {
wp_register_script( 'paczkomaty_select2_ajax', trailingslashit( $this->get_plugin_assets_url() ) . 'js/select2_ajax_paczkomaty' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
$localize_data = $this->get_localization_script_data();
wp_localize_script( 'paczkomaty_select2_ajax', 'paczkomaty', $localize_data );
wp_enqueue_script( 'paczkomaty_select2_ajax' );
}
wp_register_script( 'paczkomaty_admin', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin' . $suffix . '.js', [ 'jquery' ], $this->scripts_version, true );
wp_enqueue_script( 'paczkomaty_admin' );
wp_register_style( 'paczkomaty_admin', trailingslashit( $this->get_plugin_assets_url() ) . 'css/admin' . $suffix . '.css', [], $this->scripts_version );
wp_enqueue_style( 'paczkomaty_admin' );
}
/**
* 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->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();
}
/**
* Plugin links
*
* @param array $links Links.
*
* @return array
*/
public function links_filter( $links ) {
$plugin_links = [
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping&section=paczkomaty_shipping_method' ) . '">' . __( 'Ustawienia', 'woocommerce-paczkomaty-inpost' ) . '</a>',
'<a target="_blank" href="https://octol.io/inpost-docs-pl">' . __( 'Dokumentacja', 'woocommerce-paczkomaty-inpost' ) . '</a>',
'<a target="_blank" href="https://octol.io/inpost-support-pl">' . __( 'Wsparcie', 'woocommerce-paczkomaty-inpost' ) . '</a>',
];
return array_merge( $plugin_links, $links );
}
/**
* Init renderer.
*
* @return void
*/
private function init_renderer() {
$resolver = new ChainResolver();
$resolver->appendResolver( new WPThemeResolver( $this->get_template_path() ) );
$resolver->appendResolver( new DirResolver( trailingslashit( $this->plugin_path ) . 'templates' ) );
$this->renderer = new SimplePhpRenderer( $resolver );
}
}