205 lines
7.0 KiB
PHP
205 lines
7.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Fired during plugin activation
|
|
*
|
|
* @link https://example.com
|
|
* @since 1.0.0
|
|
*
|
|
* @package Polkurier
|
|
* @subpackage Polkurier/includes
|
|
*/
|
|
|
|
|
|
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
|
|
require_once(__DIR__ . '/PolkurierAPI.php');
|
|
|
|
|
|
/**
|
|
* Fired during plugin activation.
|
|
*
|
|
* This class defines all code necessary to run during the plugin's activation.
|
|
*
|
|
* @since 1.0.0
|
|
* @package Polkurier
|
|
* @subpackage Polkurier/includes
|
|
* @author Polkurier <test@example.com>
|
|
*/
|
|
class Polkurier_Activator
|
|
{
|
|
|
|
const DATABASE_VERSION = 5;
|
|
|
|
|
|
|
|
public static function getDbVersion()
|
|
{
|
|
return (int)get_option('pk_database_version');
|
|
}
|
|
|
|
|
|
/**
|
|
* Short Description. (use period)
|
|
*
|
|
* Long Description.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public static function activate()
|
|
{
|
|
self::updateDatabaseVersionVersion();
|
|
add_option( 'pk_plugin_version', POLKURIER_PLUGIN_VERSION);
|
|
}
|
|
|
|
|
|
private static function updateDatabaseVersionVersion()
|
|
{
|
|
|
|
self::ensureOrdersTable();
|
|
|
|
if (self::getDbVersion() <= 0) {
|
|
self::convertDatabaseVersionFrom0To1();
|
|
update_option( 'pk_database_version', 1);
|
|
}
|
|
|
|
if (self::getDbVersion() === 1) {
|
|
self::convertDatabaseVersionFrom1To2();
|
|
update_option( 'pk_database_version', 2);
|
|
}
|
|
|
|
if (self::getDbVersion() === 2) {
|
|
self::convertDatabaseVersionFrom2To3();
|
|
update_option( 'pk_database_version', 3);
|
|
}
|
|
|
|
if (self::getDbVersion() === 3) {
|
|
self::convertDatabaseVersionFrom3To4();
|
|
update_option( 'pk_database_version', 4);
|
|
}
|
|
|
|
if (self::getDbVersion() === 4) {
|
|
self::convertDatabaseVersionFrom4To5();
|
|
update_option( 'pk_database_version', 5);
|
|
}
|
|
}
|
|
|
|
private static function ensureOrdersTable()
|
|
{
|
|
global $wpdb;
|
|
$tableName = $wpdb->prefix . 'polkurier_orders';
|
|
$charsetCollate = $wpdb->get_charset_collate();
|
|
dbDelta("
|
|
CREATE TABLE $tableName (
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
wp_order_id int(11) DEFAULT NULL,
|
|
order_number varchar(55) NULL,
|
|
label text NULL,
|
|
price_gross decimal(20,2) DEFAULT '0.00' NOT NULL,
|
|
price_net decimal(20,2) DEFAULT '0.00' NOT NULL,
|
|
carrier varchar(255) DEFAULT '' NOT NULL,
|
|
address text NULL,
|
|
address_to text NULL,
|
|
pobranie decimal(20,2) DEFAULT '0.00' NOT NULL,
|
|
status_code varchar(10) DEFAULT '' NOT NULL,
|
|
delivered_date datetime NULL DEFAULT NULL,
|
|
extra text NULL,
|
|
date_created DATETIME NULL,
|
|
KEY wp_order_id (wp_order_id),
|
|
KEY order_number (order_number),
|
|
PRIMARY KEY (`id`)
|
|
) $charsetCollate;
|
|
");
|
|
}
|
|
|
|
private static function convertDatabaseVersionFrom0To1()
|
|
{
|
|
global $wpdb;
|
|
$orders = get_option('polkurier_orders');
|
|
if (!empty($orders)) {
|
|
foreach ((array)$orders as $order) {
|
|
|
|
$order->address->fromArray(array_merge($order->address->address, [
|
|
'id' => $order->address->id,
|
|
]));
|
|
|
|
$order->address_to->fromArray(array_merge($order->address_to->address, [
|
|
'id' => $order->address->id,
|
|
]));
|
|
|
|
$wpdb->insert($wpdb->prefix . 'polkurier_orders', [
|
|
'wp_order_id' => $order->params['wp_order_id'],
|
|
'order_number' => $order->params['order_number'],
|
|
'label' => json_encode($order->params['label']),
|
|
'price_gross' => (string)$order->params['price_gross'],
|
|
'price_net' => (string)$order->params['price_net'],
|
|
'carrier' => $order->params['carrier'],
|
|
'address' => json_encode($order->params['address'], JSON_FORCE_OBJECT),
|
|
'address_to' => json_encode($order->params['address_to'], JSON_FORCE_OBJECT),
|
|
'pobranie' => $order->params['pobranie'],
|
|
'extra' => json_encode($order->params['extra'], JSON_FORCE_OBJECT),
|
|
'date_created' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private static function convertDatabaseVersionFrom1To2()
|
|
{
|
|
$settings = get_option('woocommerce_pk_paczka_w_ruchu_settings');
|
|
if (!empty($settings) && isset($settings['method_title']) && $settings['method_title'] === 'Paczka w RUCHu') {
|
|
$settings['method_title'] = 'ORLEN Paczka';
|
|
update_option('woocommerce_pk_paczka_w_ruchu_settings', $settings);
|
|
}
|
|
}
|
|
|
|
|
|
private static function convertDatabaseVersionFrom2To3()
|
|
{
|
|
global $wpdb;
|
|
$tableName = $wpdb->prefix . 'polkurier_orders';
|
|
$wpdb->query("UPDATE $tableName SET date_created = NOW() WHERE date_created IS NULL");
|
|
}
|
|
|
|
|
|
private static function convertDatabaseVersionFrom3To4()
|
|
{
|
|
global $wpdb;
|
|
$shippings = [
|
|
new \Polkurier\ShippingMethods\InpostParcelMachineShippingMethodMethod(0),
|
|
new \Polkurier\ShippingMethods\PaczkaWRuchuShippingMethodMethod(0),
|
|
];
|
|
$zones = (array)WC_Shipping_Zones::get_zones();
|
|
$zones[] = ['id' => 0];
|
|
foreach ($shippings as $shipping) {
|
|
if ($shipping->is_enabled() && (float)$shipping->get_option('fee') > 0.0) {
|
|
foreach ($zones as $zone) {
|
|
$position = (int)$wpdb->get_var("SELECT MAX(method_order) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = {$zone['id']};");
|
|
$wpdb->insert($wpdb->prefix . 'woocommerce_shipping_zone_methods', [
|
|
'zone_id' => $zone['id'],
|
|
'method_id' => $shipping->getId(),
|
|
'method_order' => $position + 1,
|
|
'is_enabled' => 1,
|
|
]);
|
|
$shippingMethod = new \Polkurier\ShippingMethods\InpostParcelMachineShippingMethodMethod($wpdb->insert_id);
|
|
update_option($shippingMethod->get_instance_option_key(), [
|
|
"enabled" => "yes",
|
|
"title" => $shipping->get_option('method_title'),
|
|
"cost" => $shipping->get_option('fee'),
|
|
"cost_cod" => $shipping->get_option('fee_cod'),
|
|
"free_shipping_from" => $shipping->get_option('free_shipping_from'),
|
|
]);
|
|
}
|
|
update_option('pk-update-notice-1.10.0', 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function convertDatabaseVersionFrom4To5()
|
|
{
|
|
update_option('polkurier_default_point_id_POCZTEX_PUNKT', get_option('polkurier_default_point_id_KURIER48'));
|
|
update_option('polkurier_default_point_label_POCZTEX_PUNKT', get_option('polkurier_default_point_label_KURIER48'));
|
|
}
|
|
|
|
}
|