Merge pull request #884 from woocommerce/PCP-905-tracking-integration-compatibility-with-third-party-plugins

Tracking integration compatibility with Germanized plugin
This commit is contained in:
Emili Castells 2022-10-17 14:16:02 +02:00 committed by GitHub
commit 763e1e287c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 2602 additions and 20 deletions

55
.psalm/gzd.php Normal file
View file

@ -0,0 +1,55 @@
<?php
namespace Vendidero\Germanized\Shipments {
use WC_Data;
use WC_Order;
abstract class Shipment extends WC_Data {
/**
* Return the shipment statuses without gzd- internal prefix.
*
* @param string $context View or edit context.
* @return string
*/
public function get_status( $context = 'view' ) {
}
/**
* Tries to fetch the order for the current shipment.
*
* @return bool|WC_Order|null
*/
abstract public function get_order();
/**
* Returns the shipment shipping provider.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_shipping_provider( $context = 'view' ) {
}
/**
* @return bool
*/
public function has_tracking() {
}
/**
* Returns the shipment tracking id.
*
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_tracking_id( $context = 'view' ) {
}
public function add_note( $note, $added_by_user = false ) {
}
}
}

View file

@ -0,0 +1,11 @@
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": "3.25.0"
}
]
]
}

2
modules/ppcp-compat/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
/assets

View file

@ -0,0 +1,32 @@
{
"name": "ppcp-compat",
"version": "1.0.0",
"license": "GPL-3.0-or-later",
"main": "resources/js/compat.js",
"browserslist": [
"> 0.5%",
"Safari >= 8",
"Chrome >= 41",
"Firefox >= 43",
"Edge >= 14"
],
"dependencies": {
"core-js": "^3.25.0"
},
"devDependencies": {
"@babel/core": "^7.19",
"@babel/preset-env": "^7.19",
"babel-loader": "^8.2",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
"sass": "^1.42.1",
"sass-loader": "^12.1.0",
"webpack": "^5.74",
"webpack-cli": "^4.10"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"watch": "cross-env BABEL_ENV=default NODE_ENV=production webpack --watch",
"dev": "cross-env BABEL_ENV=default webpack --watch"
}
}

View file

@ -0,0 +1,32 @@
document.addEventListener(
'DOMContentLoaded',
() => {
const orderTrackingContainerId = "ppcp_order-tracking";
const orderTrackingContainerSelector = "#ppcp_order-tracking";
const gzdSaveButton = document.getElementById('order-shipments-save');
const loadLocation = location.href + " " + orderTrackingContainerSelector + ">*";
const setEnabled = function (enabled) {
let childNodes = document.getElementById(orderTrackingContainerId).getElementsByTagName('*');
for (let node of childNodes) {
node.disabled = !enabled;
}
}
const waitForTrackingUpdate = function () {
if (jQuery('#order-shipments-save').css('display') !== 'none') {
setEnabled(false);
setTimeout(waitForTrackingUpdate, 100)
} else {
jQuery(orderTrackingContainerSelector).load(loadLocation,"");
}
}
if (typeof(gzdSaveButton) != 'undefined' && gzdSaveButton != null) {
gzdSaveButton.addEventListener('click', function (event) {
waitForTrackingUpdate();
setEnabled(true);
})
}
},
);

View file

@ -10,10 +10,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Compat;
use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
'compat.ppec.mock-gateway' => static function( $container ) {
'compat.ppec.mock-gateway' => static function( $container ) {
$settings = $container->get( 'wcgateway.settings' );
$title = $settings->has( 'title' ) ? $settings->get( 'title' ) : __( 'PayPal', 'woocommerce-paypal-payments' );
$title = sprintf(
@ -25,20 +27,20 @@ return array(
return new PPEC\MockGateway( $title );
},
'compat.ppec.subscriptions-handler' => static function ( ContainerInterface $container ) {
'compat.ppec.subscriptions-handler' => static function ( ContainerInterface $container ) {
$ppcp_renewal_handler = $container->get( 'subscription.renewal-handler' );
$gateway = $container->get( 'compat.ppec.mock-gateway' );
return new PPEC\SubscriptionsHandler( $ppcp_renewal_handler, $gateway );
},
'compat.ppec.settings_importer' => static function( ContainerInterface $container ) : PPEC\SettingsImporter {
'compat.ppec.settings_importer' => static function( ContainerInterface $container ) : PPEC\SettingsImporter {
$settings = $container->get( 'wcgateway.settings' );
return new PPEC\SettingsImporter( $settings );
},
'compat.plugin-script-names' => static function( ContainerInterface $container ) : array {
'compat.plugin-script-names' => static function( ContainerInterface $container ) : array {
return array(
'ppcp-smart-button',
'ppcp-oxxo',
@ -50,4 +52,47 @@ return array(
);
},
'compat.gzd.is_supported_plugin_version_active' => function (): bool {
return function_exists( 'wc_gzd_get_shipments_by_order' ); // 3.0+
},
'compat.gzd.tracking_statuses_map' => function ( ContainerInterface $container ): array {
return array(
'draft' => 'ON_HOLD',
'processing' => 'SHIPPED',
'shipped' => 'SHIPPED',
'delivered' => 'DELIVERED',
);
},
'compat.module.url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
* @psalm-suppress PossiblyFalseArgument
*/
return plugins_url(
'/modules/ppcp-compat/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'compat.assets' => function( ContainerInterface $container ) : CompatAssets {
return new CompatAssets(
$container->get( 'compat.module.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'order-tracking.is-paypal-order-edit-page' ) && $container->get( 'compat.should-initialize-gzd-compat-layer' )
);
},
'compat.should-initialize-gzd-compat-layer' => function( ContainerInterface $container ) : bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$tracking_enabled = $settings->has( 'tracking_enabled' ) && $settings->get( 'tracking_enabled' );
$is_gzd_active = $container->get( 'compat.gzd.is_supported_plugin_version_active' );
return $tracking_enabled && $is_gzd_active;
},
);

View file

@ -0,0 +1,79 @@
<?php
/**
* Register and configure assets for Compat module.
*
* @package WooCommerce\PayPalCommerce\Compat\Assets
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Compat\Assets;
/**
* Class OrderEditPageAssets
*/
class CompatAssets {
/**
* The URL to the module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* Whether Germanized synchronization scripts should be loaded.
*
* @var bool
*/
protected $should_enqueue_gzd_scripts;
/**
* Compat module assets constructor.
*
* @param string $module_url The URL to the module.
* @param string $version The assets version.
* @param bool $should_enqueue_gzd_scripts Whether Germanized synchronization scripts should be loaded.
*/
public function __construct( string $module_url, string $version, bool $should_enqueue_gzd_scripts ) {
$this->module_url = $module_url;
$this->version = $version;
$this->should_enqueue_gzd_scripts = $should_enqueue_gzd_scripts;
}
/**
* Registers the scripts and styles.
*
* @return void
*/
public function register(): void {
$gzd_sync_enabled = apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true );
if ( $this->should_enqueue_gzd_scripts && $gzd_sync_enabled ) {
wp_register_script(
'ppcp-gzd-compat',
untrailingslashit( $this->module_url ) . '/assets/js/gzd-compat.js',
array( 'jquery' ),
$this->version,
true
);
}
}
/**
* Enqueues the necessary scripts.
*
* @return void
*/
public function enqueue(): void {
$gzd_sync_enabled = apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true );
if ( $this->should_enqueue_gzd_scripts && $gzd_sync_enabled ) {
wp_enqueue_script( 'ppcp-gzd-compat' );
}
}
}

View file

@ -11,9 +11,15 @@ namespace WooCommerce\PayPalCommerce\Compat;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Exception;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Compat\PPEC\PPECHelper;
use Psr\Log\LoggerInterface;
use Vendidero\Germanized\Shipments\Shipment;
use WC_Order;
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class CompatModule
@ -34,10 +40,19 @@ class CompatModule implements ModuleInterface {
/**
* {@inheritDoc}
*
* @throws NotFoundException
*/
public function run( ContainerInterface $c ): void {
$this->initialize_ppec_compat_layer( $c );
$this->fix_site_ground_optimizer_compatibility( $c );
$this->initialize_gzd_compat_layer( $c );
$asset_loader = $c->get( 'compat.assets' );
assert( $asset_loader instanceof CompatAssets );
add_action( 'init', array( $asset_loader, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
}
/**
@ -91,4 +106,77 @@ class CompatModule implements ModuleInterface {
}
);
}
/**
* Sets up the <a href="https://wordpress.org/plugins/woocommerce-germanized/">Germanized for WooCommerce</a>
* plugin compatibility layer.
*
* @link https://wordpress.org/plugins/woocommerce-germanized/
*
* @param ContainerInterface $c The Container.
* @return void
*/
protected function initialize_gzd_compat_layer( ContainerInterface $c ): void {
if ( ! $c->get( 'compat.should-initialize-gzd-compat-layer' ) ) {
return;
}
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
assert( $endpoint instanceof OrderTrackingEndpoint );
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$status_map = $c->get( 'compat.gzd.tracking_statuses_map' );
add_action(
'woocommerce_gzd_shipment_after_save',
static function( Shipment $shipment ) use ( $endpoint, $logger, $status_map ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) {
return;
}
$gzd_shipment_status = $shipment->get_status();
if ( ! array_key_exists( $gzd_shipment_status, $status_map ) ) {
return;
}
$wc_order = $shipment->get_order();
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$transaction_id = $wc_order->get_transaction_id();
if ( empty( $transaction_id ) ) {
return;
}
$tracking_data = array(
'transaction_id' => $transaction_id,
'status' => (string) $status_map[ $gzd_shipment_status ],
);
$provider = $shipment->get_shipping_provider();
if ( ! empty( $provider ) && $provider !== 'none' ) {
$tracking_data['carrier'] = 'DHL_DEUTSCHE_POST';
}
try {
$tracking_information = $endpoint->get_tracking_information( $wc_order->get_id() );
$tracking_data['tracking_number'] = $tracking_information['tracking_number'] ?? '';
if ( $shipment->has_tracking() ) {
$tracking_data['tracking_number'] = $shipment->get_tracking_id();
}
! $tracking_information ? $endpoint->add_tracking_information( $tracking_data, $wc_order->get_id() ) : $endpoint->update_tracking_information( $tracking_data, $wc_order->get_id() );
} catch ( Exception $exception ) {
$logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
$shipment->add_note( "Couldn't sync tracking information: " . $exception->getMessage() );
throw $exception;
}
}
);
}
}

View file

@ -0,0 +1,35 @@
const path = require('path');
const isProduction = process.env.NODE_ENV === 'production';
module.exports = {
devtool: isProduction ? 'source-map' : 'eval-source-map',
mode: isProduction ? 'production' : 'development',
target: 'web',
entry: {
'gzd-compat': path.resolve('./resources/js/gzd-compat.js'),
},
output: {
path: path.resolve(__dirname, 'assets/'),
filename: 'js/[name].js',
},
module: {
rules: [{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: 'css/[name].css',
}
},
{loader:'sass-loader'}
]
}]
}
};

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,3 @@
import {PaymentMethods} from "../../../ppcp-button/resources/js/modules/Helper/CheckoutMethodState";
document.addEventListener(
'DOMContentLoaded',
() => {
@ -9,14 +7,14 @@ document.addEventListener(
return;
}
const transactionId = document.querySelector('.ppcp-tracking-transaction_id');
const trackingNumber = document.querySelector('.ppcp-tracking-tracking_number');
const status = document.querySelector('.ppcp-tracking-status');
const carrier = document.querySelector('.ppcp-tracking-carrier');
const orderId = document.querySelector('.ppcp-order_id');
const submitButton = document.querySelector('.submit_tracking_info');
jQuery(document).on('click', '.submit_tracking_info', function () {
const transactionId = document.querySelector('.ppcp-tracking-transaction_id');
const trackingNumber = document.querySelector('.ppcp-tracking-tracking_number');
const status = document.querySelector('.ppcp-tracking-status');
const carrier = document.querySelector('.ppcp-tracking-carrier');
const orderId = document.querySelector('.ppcp-order_id');
const submitButton = document.querySelector('.submit_tracking_info');
submitButton.addEventListener('click', function (event) {
submitButton.setAttribute('disabled', 'disabled');
fetch(config.ajax.tracking_info.endpoint, {
method: 'POST',
@ -34,6 +32,9 @@ document.addEventListener(
return res.json();
}).then(function (data) {
if (!data.success) {
jQuery( "<span class='error tracking-info-message'>" + data.data.message + "</span>" ).insertAfter(submitButton);
setTimeout(()=> jQuery('.tracking-info-message').remove(),3000);
submitButton.removeAttribute('disabled');
console.error(data);
throw Error(data.data.message);
}

View file

@ -90,7 +90,7 @@ class OrderTrackingEndpoint {
$action = $data['action'];
$request_body = $this->extract_tracking_information( $data );
$order_id = (int) $data['order_id'];
$action === 'create' ? $this->add_tracking_information( $request_body, $order_id ) : $this->update_tracking_information( $data, $order_id );
$action === 'create' ? $this->add_tracking_information( $request_body, $order_id ) : $this->update_tracking_information( $request_body, $order_id );
$action_message = $action === 'create' ? 'created' : 'updated';
$message = sprintf(
@ -101,7 +101,7 @@ class OrderTrackingEndpoint {
wp_send_json_success( array( 'message' => $message ) );
} catch ( Exception $error ) {
wp_send_json_error( $error->getMessage(), 500 );
wp_send_json_error( array( 'message' => $error->getMessage() ), 500 );
}
}

View file

@ -85,8 +85,9 @@ class MetaBoxRenderer {
$status_value = $tracking_info['status'] ?? 'SHIPPED';
$carrier_value = $tracking_info['carrier'] ?? '';
$carriers = (array) apply_filters( 'ppcp_tracking_carriers', $this->carriers );
$statuses = (array) apply_filters( 'ppcp_tracking_statuses', $this->allowed_statuses );
$carriers = (array) apply_filters( 'woocommerce_paypal_payments_tracking_carriers', $this->carriers, $wc_order->get_id() );
$statuses = (array) apply_filters( 'woocommerce_paypal_payments_tracking_statuses', $this->allowed_statuses, $wc_order->get_id() );
$tracking_number = (string) apply_filters( 'woocommerce_paypal_payments_tracking_number', $tracking_number, $wc_order->get_id() );
$action = ! $tracking_info ? 'create' : 'update';
?>

View file

@ -11,15 +11,17 @@
"install:modules:ppcp-webhooks": "cd modules/ppcp-webhooks && yarn install && cd -",
"install:modules:ppcp-vaulting": "cd modules/ppcp-vaulting && yarn install && cd -",
"install:modules:ppcp-order-tracking": "cd modules/ppcp-order-tracking && yarn install && cd -",
"install:modules:ppcp-compat": "cd modules/ppcp-compat && yarn install && cd -",
"install:modules:ppcp-onboarding": "cd modules/ppcp-onboarding && yarn install && cd -",
"install:modules": "yarn run install:modules:ppcp-button && yarn run install:modules:ppcp-wc-gateway && yarn run install:modules:ppcp-webhooks && yarn run install:modules:ppcp-vaulting && yarn run install:modules:ppcp-order-tracking && yarn run install:modules:ppcp-onboarding",
"install:modules": "yarn run install:modules:ppcp-button && yarn run install:modules:ppcp-wc-gateway && yarn run install:modules:ppcp-webhooks && yarn run install:modules:ppcp-vaulting && yarn run install:modules:ppcp-order-tracking && yarn run install:modules:ppcp-compat && yarn run install:modules:ppcp-onboarding",
"build:modules:ppcp-button": "cd modules/ppcp-button && yarn run build && cd -",
"build:modules:ppcp-wc-gateway": "cd modules/ppcp-wc-gateway && yarn run build && cd -",
"build:modules:ppcp-webhooks": "cd modules/ppcp-webhooks && yarn run build && cd -",
"build:modules:ppcp-vaulting": "cd modules/ppcp-vaulting && yarn run build && cd -",
"build:modules:ppcp-order-tracking": "cd modules/ppcp-order-tracking && yarn run build && cd -",
"build:modules:ppcp-compat": "cd modules/ppcp-compat && yarn run build && cd -",
"build:modules:ppcp-onboarding": "cd modules/ppcp-onboarding && yarn run build && cd -",
"build:modules": "yarn run build:modules:ppcp-button && yarn build:modules:ppcp-wc-gateway && yarn build:modules:ppcp-webhooks && yarn build:modules:ppcp-vaulting && yarn build:modules:ppcp-order-tracking && yarn build:modules:ppcp-onboarding",
"build:modules": "yarn run build:modules:ppcp-button && yarn build:modules:ppcp-wc-gateway && yarn build:modules:ppcp-webhooks && yarn build:modules:ppcp-vaulting && yarn build:modules:ppcp-order-tracking && yarn build:modules:ppcp-compat && yarn build:modules:ppcp-onboarding",
"build:dev": "yarn run install:modules && yarn run build:modules",
"ddev:setup": "ddev start && ddev orchestrate",

View file

@ -30,6 +30,7 @@
<stubs>
<file name=".psalm/stubs.php"/>
<file name=".psalm/wcs.php"/>
<file name=".psalm/gzd.php"/>
<file name="vendor/php-stubs/wordpress-stubs/wordpress-stubs.php"/>
<file name="vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php"/>
</stubs>