577 lines
22 KiB
PHP
577 lines
22 KiB
PHP
<?php
|
||
|
||
/**
|
||
* The admin-specific functionality of the plugin.
|
||
*
|
||
* @link https://polkurier.pl
|
||
* @since 1.0.0
|
||
*
|
||
* @package Polkurier
|
||
* @subpackage Polkurier/admin
|
||
*/
|
||
|
||
/**
|
||
* The admin-specific functionality of the plugin.
|
||
*
|
||
* Defines the plugin name, version, and two examples hooks for how to
|
||
* enqueue the admin-specific stylesheet and JavaScript.
|
||
*
|
||
* @package Polkurier
|
||
* @subpackage Polkurier/admin
|
||
* @author Polkurier <api@polkurier.pl>
|
||
*/
|
||
class Polkurier_Admin
|
||
{
|
||
|
||
private $option_name = 'polkurier';
|
||
/**
|
||
* The ID of this plugin.
|
||
*
|
||
* @since 1.0.0
|
||
* @access private
|
||
* @var string $plugin_name The ID of this plugin.
|
||
*/
|
||
private $plugin_name;
|
||
|
||
/**
|
||
* The version of this plugin.
|
||
*
|
||
* @since 1.0.0
|
||
* @access private
|
||
* @var string $version The current version of this plugin.
|
||
*/
|
||
private $version;
|
||
|
||
/**
|
||
* Initialize the class and set its properties.
|
||
*
|
||
* @param string $plugin_name The name of this plugin.
|
||
* @param string $version The version of this plugin.
|
||
* @since 1.0.0
|
||
*/
|
||
public function __construct($plugin_name, $version)
|
||
{
|
||
|
||
$this->plugin_name = $plugin_name;
|
||
$this->version = $version;
|
||
add_filter('woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2);
|
||
function add_custom_order_status_actions_button($actions, $order)
|
||
{
|
||
$orderStatus = $order->get_status();
|
||
if ($orderStatus != 'pending' && $orderStatus != 'on-hold' && $orderStatus != 'completed' && $orderStatus != 'cancelled' && $orderStatus != 'refunded' && $orderStatus != 'failed') {
|
||
// The key slug defined for your action button
|
||
$action_slug = 'polkurier';
|
||
$status = isset($_GET['status']) ? $_GET['status'] : null;
|
||
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
|
||
require_once dirname(__FILE__) . '/../includes/PolkurierAPI.php';
|
||
if (!PolkurierOrder::getOrderByWCOrderId($order_id)) {
|
||
// Set the action button
|
||
$actions[$action_slug] = array(
|
||
'url' => wp_nonce_url(admin_url('admin.php?page=polkurier_packages&action=create&order_id=' . $order_id), 'polkurier'),
|
||
'name' => __('Zamów usługę Polkurier', 'woocommerce'),
|
||
'action' => $action_slug,
|
||
);
|
||
}
|
||
}
|
||
return $actions;
|
||
}
|
||
|
||
add_action('admin_head', 'add_custom_order_status_actions_button_css');
|
||
function add_custom_order_status_actions_button_css()
|
||
{
|
||
$action_slug = "polkurier"; // The key slug defined for your action button
|
||
|
||
echo '<style>.wc-action-button-' . $action_slug . '::after { font-family: woocommerce !important; content: "\e019" !important; }</style>';
|
||
}
|
||
|
||
function add_polkurier_column_notice()
|
||
{
|
||
?>
|
||
<div id="polkurier-column-notice" class="notice notice-error is-dismissible hidden">
|
||
<p><?php _e('<b>Uwaga</b>, aby skorzystać z możliwości wtyczki Polkurier, włącz kolumnę "Działania" w menu <b>opcje ekranu</b> w prawym górnym narożniku tego widoku. <b>Bez włączenia tej kolumny nie będzie możliwe zamawianie przesyłek przez usługę Polkurier.</b>', 'sample-text-domain'); ?></p>
|
||
</div>
|
||
<script type="text/javascript">
|
||
jQuery(document).ready(function ($) {
|
||
if ($(".column-wc_actions").hasClass("hidden")) {
|
||
$("#polkurier-column-notice").removeClass("hidden");
|
||
}
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
|
||
if (isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order') {
|
||
add_action('admin_notices', 'add_polkurier_column_notice');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Register the stylesheets for the admin area.
|
||
*
|
||
* @since 1.0.0
|
||
*/
|
||
public function enqueue_styles()
|
||
{
|
||
|
||
/**
|
||
* This function is provided for demonstration purposes only.
|
||
*
|
||
* An instance of this class should be passed to the run() function
|
||
* defined in Polkurier_Loader as all of the hooks are defined
|
||
* in that particular class.
|
||
*
|
||
* The Polkurier_Loader will then create the relationship
|
||
* between the defined hooks and the functions defined in this
|
||
* class.
|
||
*/
|
||
|
||
wp_enqueue_style(uniqid('pk', true), plugin_dir_url(__FILE__) . 'css/select2.min.css', array(), $this->version, 'all');
|
||
wp_enqueue_style(uniqid('pk', true), plugin_dir_url( __FILE__ ) . 'css/pk-utils.css', array(), $this->version, 'all' );
|
||
wp_enqueue_style(uniqid('pk', true), plugin_dir_url( __FILE__ ) . 'css/polkurier-admin.css', array(), $this->version, 'all' );
|
||
wp_enqueue_style(uniqid('pk', true), 'https://maps.polkurier.pl/assets/dist/points-map.css', [], $this->version);
|
||
}
|
||
|
||
/**
|
||
* Register the JavaScript for the admin area.
|
||
*
|
||
* @since 1.0.0
|
||
*/
|
||
public function enqueue_scripts()
|
||
{
|
||
|
||
/**
|
||
* This function is provided for demonstration purposes only.
|
||
*
|
||
* An instance of this class should be passed to the run() function
|
||
* defined in Polkurier_Loader as all of the hooks are defined
|
||
* in that particular class.
|
||
*
|
||
* The Polkurier_Loader will then create the relationship
|
||
* between the defined hooks and the functions defined in this
|
||
* class.
|
||
*/
|
||
wp_enqueue_script(uniqid('pk', true), plugin_dir_url(__FILE__) . 'js/polkurier-admin.js', ['jquery'], $this->version, false);
|
||
wp_enqueue_script(uniqid('pk', true), 'https://maps.polkurier.pl/assets/dist/points-map.bundle.js', ['jquery'], $this->version,true);
|
||
|
||
}
|
||
|
||
// Create the Plugin Name menu page with add_menu_page();
|
||
public function add_admin_page()
|
||
{
|
||
if (!class_exists('WooCommerce')) {
|
||
add_action('admin_notices', array($this, 'no_woocommerce_notice'));
|
||
} else {
|
||
add_submenu_page(
|
||
'woocommerce',
|
||
'Ustawienia Polkurier',
|
||
'Ustawienia Polkurier',
|
||
'manage_options',
|
||
$this->plugin_name,
|
||
array($this, 'load_admin_page_content') // Calls function to require the partial
|
||
);
|
||
add_submenu_page(
|
||
'woocommerce',
|
||
'Przesyłki',
|
||
'Przesyłki',
|
||
'manage_woocommerce',
|
||
$this->plugin_name . '_packages',
|
||
array($this, 'load_admin_page_packages') // Calls function to require the partial
|
||
);
|
||
add_submenu_page(
|
||
'woocommerce',
|
||
'Wycena',
|
||
'Wycena',
|
||
'manage_woocommerce',
|
||
$this->plugin_name . '_valuation',
|
||
array($this, 'load_admin_page_valuation') // Calls function to require the partial
|
||
);
|
||
}
|
||
if (!get_option('polkurier_apikey') || !get_option('polkurier_account_id')) {
|
||
add_action('admin_notices', array($this, 'no_settings_notice'));
|
||
}
|
||
}
|
||
|
||
// Load the plugin admin page partial.
|
||
public function load_admin_page_content()
|
||
{
|
||
require_once plugin_dir_path(__FILE__) . 'partials/polkurier-admin-display.php';
|
||
}
|
||
|
||
public function load_admin_page_valuation()
|
||
{
|
||
require_once plugin_dir_path(__FILE__) . 'partials/polkurier-admin-valuation.php';
|
||
}
|
||
|
||
public function load_admin_page_packages()
|
||
{
|
||
require_once plugin_dir_path(__FILE__) . 'partials/polkurier-admin-packages.php';
|
||
}
|
||
|
||
public function register_setting()
|
||
{
|
||
add_settings_section(
|
||
$this->option_name . '_authorization',
|
||
__('Autoryzacja', $this->plugin_name),
|
||
array($this, $this->option_name . '_authorize_cb'),
|
||
$this->plugin_name . '_authorization'
|
||
);
|
||
add_settings_section(
|
||
$this->option_name . '_address',
|
||
__('Domyślne adresy', $this->plugin_name),
|
||
array($this, $this->option_name . '_address_cb'),
|
||
$this->plugin_name . '_address'
|
||
);
|
||
add_settings_section(
|
||
$this->option_name . '_accounts',
|
||
__('Konta bankowe', $this->plugin_name),
|
||
array($this, $this->option_name . '_accounts_cb'),
|
||
$this->plugin_name . '_accounts'
|
||
);
|
||
add_settings_section(
|
||
$this->option_name . '_templates',
|
||
__('Szablony przesyłek', $this->plugin_name),
|
||
array($this, $this->option_name . '_templates_cb'),
|
||
$this->plugin_name . '_templates'
|
||
);
|
||
add_settings_section(
|
||
$this->option_name . '_default_carrier',
|
||
__('Domyślny przewoźnik', $this->plugin_name),
|
||
array($this, $this->option_name . '_default_carrier_section_cb'),
|
||
$this->plugin_name . '_default_carrier'
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_account_id',
|
||
__('ID konta', $this->plugin_name),
|
||
array($this, $this->option_name . '_account_id_cb'),
|
||
$this->plugin_name . '_authorization',
|
||
$this->option_name . '_authorization',
|
||
array('label_for' => $this->option_name . '_account_id')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_apikey',
|
||
__('Token', $this->plugin_name),
|
||
array($this, $this->option_name . '_apikey_cb'),
|
||
$this->plugin_name . '_authorization',
|
||
$this->option_name . '_authorization',
|
||
array('label_for' => $this->option_name . '_apikey')
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_company_name',
|
||
__('Nazwa firmy', $this->plugin_name),
|
||
array($this, $this->option_name . '_company_name_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_company_name')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_name_surname',
|
||
__('Imię i nazwisko', $this->plugin_name),
|
||
array($this, $this->option_name . '_name_surname_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_name_surname_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_street',
|
||
__('Ulica', $this->plugin_name),
|
||
array($this, $this->option_name . '_street_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_street_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_number_1',
|
||
__('Numer budynku', $this->plugin_name),
|
||
array($this, $this->option_name . '_number_1_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_number_1_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_number_2',
|
||
__('Numer lokalu', $this->plugin_name),
|
||
array($this, $this->option_name . '_number_2_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_number_2_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_postal_code',
|
||
__('Kod pocztowy', $this->plugin_name),
|
||
array($this, $this->option_name . '_postal_code_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_postal_code_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_city',
|
||
__('Miasto', $this->plugin_name),
|
||
array($this, $this->option_name . '_city_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_city_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_phone',
|
||
__('Numer telefonu', $this->plugin_name),
|
||
array($this, $this->option_name . '_phone_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_phone_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_email',
|
||
__('Adres e-mail', $this->plugin_name),
|
||
array($this, $this->option_name . '_email_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_email_cb')
|
||
);
|
||
add_settings_field(
|
||
$this->option_name . '_polkurier_account_id',
|
||
__('ID konta Polkurier', $this->plugin_name),
|
||
array($this, $this->option_name . '_polkurier_account_id_cb'),
|
||
$this->plugin_name . '_address',
|
||
$this->option_name . '_address',
|
||
array('label_for' => $this->option_name . '_polkurier_account_id_cb')
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_account_number',
|
||
__('Numer konta bankowego', $this->plugin_name),
|
||
array($this, $this->option_name . '_account_number_cb'),
|
||
$this->plugin_name . '_accounts',
|
||
$this->option_name . '_accounts',
|
||
array('label_for' => $this->option_name . '_account_number_cb')
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_default_carrier_id',
|
||
__('Domyślny przewoźnik', $this->plugin_name),
|
||
array($this, $this->option_name . '_default_carrier_cb'),
|
||
$this->plugin_name . '_default_carrier',
|
||
$this->option_name . '_default_carrier',
|
||
array('label_for' => $this->option_name . '_default_carrier_cb')
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_templates',
|
||
__('Szablony paczek', $this->plugin_name),
|
||
array($this, $this->option_name . '_templates_cb'),
|
||
$this->plugin_name . '_templates',
|
||
$this->option_name . '_templates',
|
||
array('label_for' => $this->option_name . '_templates_cb')
|
||
);
|
||
|
||
add_settings_field(
|
||
$this->option_name . '_adresses',
|
||
__('Domyślne adresy', $this->plugin_name),
|
||
array($this, $this->option_name . '_templates_cb'),
|
||
$this->plugin_name . '_adresses',
|
||
$this->option_name . '_adresses',
|
||
array('label_for' => $this->option_name . '_templates_cb')
|
||
);
|
||
|
||
register_setting($this->plugin_name . '_authorization', $this->option_name . '_apikey', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_authorization', $this->option_name . '_account_id', ['type' => 'string']);
|
||
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_company_name', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_name_surname', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_street', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_number_1', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_number_2', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_postal_code', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_city', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_phone', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_email', ['type' => 'string']);
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_polkurier_account_id', ['type' => 'string']);
|
||
|
||
register_setting($this->plugin_name . '_address', $this->option_name . '_adresses', ['type' => 'string']);
|
||
|
||
register_setting($this->plugin_name . '_accounts', $this->option_name . '_accounts', ['type' => 'string']);
|
||
|
||
register_setting($this->plugin_name . '_templates', $this->option_name . '_templates', ['type' => 'string']);
|
||
|
||
register_setting($this->plugin_name . '_default_carrier', $this->option_name . '_default_carrier_id', ['type' => 'string']);
|
||
}
|
||
|
||
public function polkurier_default_carrier_cb()
|
||
{
|
||
$option_value = get_option($this->option_name . '_default_carrier_id');
|
||
$api = $this->get_polkurier_api();
|
||
foreach ($api->getAvailableCarriers() as $carrier) {
|
||
echo '<input id="polkurier_default_carrier_id_' . $carrier["servicecode"] . '" type="radio" name="polkurier_default_carrier_id" value="' . $carrier["servicecode"] . '" ' . checked($carrier["servicecode"], $option_value, false) . ' />';
|
||
echo '<label for="polkurier_default_carrier_id_' . $carrier["servicecode"] . '">' . $carrier['name'] . '</label><br/>';
|
||
}
|
||
}
|
||
|
||
public function polkurier_authorize_cb()
|
||
{
|
||
echo '<p>' . __('Ustawienia autoryzacji', $this->plugin_name) . '</p>';
|
||
}
|
||
|
||
public function polkurier_default_carrier_section_cb()
|
||
{
|
||
echo '<p>Ustawienia domyślnego przewoźnika usługi Polkurier</p>';
|
||
}
|
||
|
||
public function polkurier_address_cb()
|
||
{
|
||
echo '<p>Ustawienia adresowe nadawcy</p>';
|
||
}
|
||
|
||
protected function polkurier_generic_cb($fieldName, $type = 'text')
|
||
{
|
||
$option_value = get_option($this->option_name . $fieldName);
|
||
echo '<input type="' . $type . '" name="' . $this->option_name . $fieldName . '" id="' . $this->option_name . $fieldName . '" value="' . $option_value . '" > ';
|
||
}
|
||
|
||
public function polkurier_account_number_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_accounts');
|
||
}
|
||
|
||
public function polkurier_account_id_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_account_id');
|
||
}
|
||
|
||
public function polkurier_apikey_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_apikey');
|
||
}
|
||
|
||
public function polkurier_company_name_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_company_name');
|
||
}
|
||
|
||
public function polkurier_name_surname_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_name_surname');
|
||
}
|
||
|
||
public function polkurier_street_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_street');
|
||
}
|
||
|
||
public function polkurier_number_1_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_number_1');
|
||
}
|
||
|
||
public function polkurier_number_2_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_number_2');
|
||
}
|
||
|
||
public function polkurier_postal_code_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_postal_code');
|
||
}
|
||
|
||
public function polkurier_city_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_city');
|
||
}
|
||
|
||
public function polkurier_phone_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_phone');
|
||
}
|
||
|
||
public function polkurier_email_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_email');
|
||
}
|
||
|
||
public function polkurier_polkurier_account_id_cb()
|
||
{
|
||
return $this->polkurier_generic_cb('_polkurier_account_id');
|
||
}
|
||
|
||
// public function polkurier_display_table()
|
||
// {
|
||
// require_once plugin_dir_path(__FILE__) . '../includes/Polkurier_WP_List_Table.php';
|
||
// $table = new Polkurier_WP_List_Table();
|
||
// $table->prepare_items();
|
||
// $table->display();
|
||
// }
|
||
|
||
public function no_woocommerce_notice()
|
||
{
|
||
echo '<div class="notice error polkurier-notice is-dismissible" ><p>Błąd Polkurier: nie znaleziono WooCommerce. Sprawdź konfigurację swojej instancji Wordpress.</p></div>';
|
||
}
|
||
|
||
public function no_settings_notice()
|
||
{
|
||
echo '<div class="notice error polkurier-notice is-dismissible" ><p>Aby użyć wtyczki Polkurier, wprowadź wszystkie <a href="admin.php?page=polkurier">ustawienia</a>.</p></div>';
|
||
}
|
||
|
||
public static function api_error_notice($msg)
|
||
{
|
||
echo '<div class="notice error polkurier-notice is-dismissible" ><p>' . $msg . '</p></div>';
|
||
}
|
||
|
||
protected function get_polkurier_api()
|
||
{
|
||
require_once plugin_dir_path(__FILE__) . '../includes/PolkurierAPI.php';
|
||
return new PolkurierAPI(get_option('polkurier_account_id'), get_option('polkurier_apikey'));
|
||
}
|
||
|
||
public function get_package_details()
|
||
{
|
||
if (isset($_GET['package'])) {
|
||
$api = $this->get_polkurier_api();
|
||
return $api->getPackageDetails($_GET['package']);
|
||
} else {
|
||
return array();
|
||
}
|
||
|
||
}
|
||
|
||
protected function describe_field($field, $value)
|
||
{
|
||
$desc = '';
|
||
switch ($field) {
|
||
case 'orderNumber':
|
||
$desc = 'Numer zamówienia';
|
||
break;
|
||
case 'size':
|
||
$desc = 'Rozmiar paczki';
|
||
break;
|
||
case 'status':
|
||
$desc = 'Status';
|
||
break;
|
||
case 'name':
|
||
$desc = 'Imię';
|
||
break;
|
||
case 'surname':
|
||
$desc = 'Nazwisko';
|
||
break;
|
||
case 'street':
|
||
$desc = 'Ulica';
|
||
break;
|
||
case 'number':
|
||
$desc = 'Adres';
|
||
break;
|
||
case 'zipCode':
|
||
$desc = 'Kod pocztowy';
|
||
break;
|
||
case 'city':
|
||
$desc = 'Miasto';
|
||
break;
|
||
case 'companyName':
|
||
$desc = 'Nazwa firmy';
|
||
break;
|
||
default:
|
||
return false;
|
||
}
|
||
return "<p><b>$desc:</b> $value</p>";
|
||
}
|
||
|
||
}
|