first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,175 @@
<?php
/**
* File that define P24_Config class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Methods for Przelewy 24 plugin to display on admin order.
*/
class P24_MC_Admin_Order {
const META_ACTIVE_MULTIPLIER = '_p24_mc_active_multiplier';
/**
* Instance of core of plugin.
*
* @var P24_Multi_Currency
*/
private $mc;
/**
* Instance of core of plugin.
*
* @var P24_Core
*/
private $plugin_core;
/**
* Construct class instance.
*
* @param P24_Multi_Currency $mc The multi currency part of plugin.
* @param P24_Core $plugin_core The core class of plugin.
*/
public function __construct( P24_Multi_Currency $mc, P24_Core $plugin_core ) {
$this->mc = $mc;
$this->plugin_core = $plugin_core;
}
/**
* Add box to set currency for order.
*
* This box is used on admin panel.
*
* @param WP_Post $post The post object.
*/
public function add_admin_order_change_currency( WP_Post $post ) {
$currency_options = $this->plugin_core->get_multi_currency_instance()->get_available_currencies();
$params = compact( 'post', 'currency_options' );
$this->plugin_core->render_template( 'multi-currency-order-edit', $params );
}
/**
* Add all meta boxes for different pages.
*/
public function add_meta_boxes() {
foreach ( wc_get_order_types( 'order-meta-boxes' ) as $type ) {
add_meta_box( 'p24_admin_order_multi_currency', __( 'Aktywna waluta', 'przelewy24' ), array( $this, 'add_admin_order_change_currency' ), $type, 'side', 'high' );
}
}
/**
* Set currency of order created by admin.
*/
public function admin_order_edit() {
if ( is_admin() ) {
wp_verify_nonce( null );
if ( isset( $_POST['order_id'] ) && isset( $_POST['currency_code'] ) ) {
$order_id = (int) $_POST['order_id'];
$currency = sanitize_text_field( wp_unslash( $_POST['currency_code'] ) );
update_post_meta( $order_id, '_order_currency', $currency );
wp_die( 'ok' );
} else {
wp_die( 'fail' );
}
}
wp_die( 'fail' );
}
/**
* Fix currencies if mismatched.
*
* @param WP_Error $error Error, may be empty.
* @param WC_Product $product A product.
* @param WC_Order $order An order.
* @param int $qty Quantity.
* @return WP_Error
*/
public function fix_currencies( WP_Error $error, WC_Product $product, WC_Order $order, $qty ) {
if ( $product instanceof P24_MC_Product_Common_Interface ) {
$order_currency = $order->get_currency();
$product_currency = $product->get_currency();
if ( $product_currency !== $order_currency ) {
$product->overwrite_currency( $order_currency );
}
}
/* Assume other products have correct price already. */
return $error;
}
/**
* Fix currencies if mismatched in additional place.
*
* @param array $items Items to analyse.
* @param WC_Abstract_Order $order An order.
* @param array $type Array with types.
* @return array
*/
public function fix_currencies_again( $items, WC_Abstract_Order $order, $type ) {
if ( ! in_array( 'line_item', $type, true ) ) {
return $items;
}
$currency = $order->get_currency();
$multipliers = $this->mc->get_multipliers();
$multiplier = (float) $multipliers[ $currency ];
foreach ( $items as $product ) {
if ( ! $product instanceof WC_Order_Item_Product ) {
/* Only products need changes. */
continue;
}
$active_multiplier = (float) $product->get_meta( self::META_ACTIVE_MULTIPLIER );
if ( $active_multiplier === $multiplier ) {
/* Nothing to change. */
continue;
} elseif ( $active_multiplier ) {
/* The product has wrong multiplier. Fix it. */
$subtotal = $product->get_subtotal() / $active_multiplier * $multiplier;
$product->set_subtotal( $subtotal );
$total = $product->get_total() / $active_multiplier * $multiplier;
$product->set_total( $total );
$subtotal_tax = $product->get_subtotal_tax() / $active_multiplier * $multiplier;
$product->set_subtotal_tax( $subtotal_tax );
$total_tax = $product->get_total_tax() / $active_multiplier * $multiplier;
$product->set_total_tax( $total_tax );
}
$product->update_meta_data( self::META_ACTIVE_MULTIPLIER, $multiplier, true );
$product->save_meta_data();
}
return $items;
}
/**
* Hide additional metas.
*
* @param array $hidden Provided hidden metas.
* @return array
*/
public function hide_meta( $hidden ) {
if ( ! in_array( self::META_ACTIVE_MULTIPLIER, $hidden, true ) ) {
$hidden[] = self::META_ACTIVE_MULTIPLIER;
}
return $hidden;
}
/**
* Bind multi currency events.
*/
public function bind_common_events() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'wp_ajax_p24_mc_admin_order_edit', array( $this, 'admin_order_edit' ) );
add_filter( 'woocommerce_ajax_add_order_item_validation', array( $this, 'fix_currencies' ), 10, 4 );
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hide_meta' ), 10, 1 );
add_filter( 'woocommerce_order_get_items', array( $this, 'fix_currencies_again' ), 10, 4 );
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* File that define P24_External_Multi_Currency class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Class with support for external multi currency.
*/
class P24_MC_External_None implements P24_MC_Interface {
/**
* Check if external multi currency is activated.
*
* This one is always inactive.
*
* @return bool
*/
public function is_multi_currency_active() {
return false;
}
/**
* Check if multi currency is internal.
*
* @return bool
*/
public function is_internal() {
return false;
}
/**
* Get list of available currencies.
*
* @return array
*/
public function get_available_currencies() {
return array();
}
/**
* Get name.
*
* @return string
*/
public function get_name() {
return '';
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* File that define P24_External_Multi_Currency class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Class with support for external multi currency.
*/
class P24_MC_External_WPML implements P24_MC_Interface {
/**
* Get wpml settings.
*
* @return array
*/
private static function get_wpml_settings() {
global $woocommerce_wpml;
if ( $woocommerce_wpml ) {
if ( isset( $woocommerce_wpml->settings['currencies_order'] ) ) {
return $woocommerce_wpml->settings['currencies_order'];
}
}
return array();
}
/**
* Try create instance.
*
* @return null|P24_MC_External_WPML
*/
public static function try_create() {
$settings = self::get_wpml_settings();
if ( $settings ) {
return new self();
} else {
return null;
}
}
/**
* Check if external multi currency is activated.
*
* @return bool
*/
public function is_multi_currency_active() {
$settings = self::get_wpml_settings();
return (bool) $settings;
}
/**
* Check if multi currency is internal.
*
* @return bool
*/
public function is_internal() {
return false;
}
/**
* Get list of available currencies.
*
* @return array
*/
public function get_available_currencies() {
$settings = self::get_wpml_settings();
return array_combine( $settings, $settings );
}
/**
* Get name.
*
* @return string
*/
public function get_name() {
return 'WPML';
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* File that define P24_External_Multi_Currency class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Class with support for external multi currency.
*/
interface P24_MC_Interface {
/**
* Check if external multi currency is activated.
*
* @return bool
*/
public function is_multi_currency_active();
/**
* Check if multi currency is internal.
*
* @return bool
*/
public function is_internal();
/**
* Get list of available currencies.
*
* @return array
*/
public function get_available_currencies();
/**
* Get name of multi currency implementation.
*
* @return string
*/
public function get_name();
}

View File

@@ -0,0 +1,152 @@
<?php
/**
* File that define P24_MC_Pool class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Class that pool all multi currency support.
*/
class P24_MC_Pool {
/**
* Instance of core of plugin.
*
* @var P24_Core
*/
private $plugin_core;
/**
* External multi currency proxy.
*
* @var P24_MC_Interface|null
*/
private $external = null;
/**
* Active currency.
*
* @var string|null
*/
private $active_currency = null;
/**
* P24_MC_Pool constructor.
*
* @param P24_Core $plugin_core Instance of main plugin.
*/
public function __construct( P24_Core $plugin_core ) {
$this->plugin_core = $plugin_core;
}
/**
* Set_active_currency.
*
* @param string $currency Currency.
*/
public function set_active_currency( $currency ) {
$this->active_currency = $currency;
}
/**
* Get external Multi Currency proxy.
*
* @return P24_MC_Interface
*/
public function get_external_mc() {
if ( ! $this->external ) {
$wpml = P24_MC_External_WPML::try_create();
if ( $wpml && $wpml->is_multi_currency_active() ) {
$this->external = $wpml;
} else {
$this->external = new P24_MC_External_None();
}
}
return $this->external;
}
/**
* Get preffered Multi Currency proxy.
*
* @return P24_MC_Interface|null
*/
public function get_preffered_mc() {
if ( $this->plugin_core->is_internal_multi_currency_active() ) {
$mc = $this->plugin_core->get_multi_currency_instance();
} else {
$mc = $this->get_external_mc();
}
return $mc;
}
/**
* Method for filter that change default currency.
*
* The default value is ignored.
* The wrapped function should always return something.
*
* @param string|null $default Default value provided by filter.
* @return string|null
*/
public function check_admin_currency( $default ) {
if ( ! $this->active_currency ) {
$mc = $this->get_preffered_mc();
$available = $mc->get_available_currencies();
wp_verify_nonce( null ); /* There is no noce. */
if ( $this->plugin_core->is_in_json_mode() ) {
if ( array_key_exists( P24_Request_Support::WP_JSON_GET_KEY_CURRENCY, $_GET ) ) {
$this->active_currency = sanitize_text_field( wp_unslash( $_GET[ P24_Request_Support::WP_JSON_GET_KEY_CURRENCY ] ) );
}
} else {
if ( isset( $_COOKIE['admin_p24_currency'] ) ) {
$this->active_currency = sanitize_text_field( wp_unslash( $_COOKIE['admin_p24_currency'] ) );
}
}
if ( ! $this->active_currency || ! array_key_exists( $this->active_currency, $available ) ) {
$this->active_currency = P24_Woo_Commerce_Low_Level_Getter::get_unhooked_currency_form_woocommerce();
}
}
return $this->active_currency ? $this->active_currency : $default;
}
/**
* Get list of available currencies.
*
* It is based on multipliers.
*
* @return array
*/
public function get_available_currencies() {
return $this->get_preffered_mc()->get_available_currencies();
}
/**
* An AJAX method to change currency for admin.
*/
public function admin_ajax_change_currency() {
$mc = $this->get_preffered_mc();
if ( $mc->is_multi_currency_active() ) {
header( 'Content-Type: text/plain; charset=utf-8' );
wc_setcookie( 'admin_p24_currency', $this->check_admin_currency( '' ) );
echo 'Ok';
wp_die();
}
}
/**
* Bind events to use multi currency.
*/
public function bind_events() {
add_action( 'wp_ajax_p24_change_currency', array( $this, 'admin_ajax_change_currency' ) );
add_filter( 'przelewy24_multi_currency_admin_currency', array( $this, 'check_admin_currency' ) );
add_filter( 'przelewy24_multi_currency_options', array( $this, 'get_available_currencies' ) );
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* File that define P24_External_Multi_Currency class.
*
* @package Przelewy24
*/
defined( 'ABSPATH' ) || exit;
/**
* Class with support for external multi currency.
*/
interface P24_MC_Product_Common_Interface {
/**
* Get currency of the object.
*
* @param string $context The name of context.
* @return mixed
*/
public function get_currency( $context = 'view' );
/**
* Overwrite currency of product.
*
* @param string $new_currency New currency.
* @return void
*/
public function overwrite_currency( $new_currency );
}