*/ 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 ''; } function add_polkurier_column_notice() { ?> 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 ''; echo '
'; } } public function polkurier_authorize_cb() { echo '

' . __('Ustawienia autoryzacji', $this->plugin_name) . '

'; } public function polkurier_default_carrier_section_cb() { echo '

Ustawienia domyślnego przewoźnika usługi Polkurier

'; } public function polkurier_address_cb() { echo '

Ustawienia adresowe nadawcy

'; } protected function polkurier_generic_cb($fieldName, $type = 'text') { $option_value = get_option($this->option_name . $fieldName); echo ' '; } 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 '

Błąd Polkurier: nie znaleziono WooCommerce. Sprawdź konfigurację swojej instancji Wordpress.

'; } public function no_settings_notice() { echo '

Aby użyć wtyczki Polkurier, wprowadź wszystkie ustawienia.

'; } public static function api_error_notice($msg) { echo '

' . $msg . '

'; } 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 "

$desc: $value

"; } }