Skip to content

Commit

Permalink
Merge branch 'dev/sofort-deprecation' of github.com:Automattic/woocom…
Browse files Browse the repository at this point in the history
…merce-payments into dev/sofort-deprecation
  • Loading branch information
dmallory42 committed Oct 19, 2023
2 parents 3993240 + 5348010 commit 6a94393
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 84 deletions.
4 changes: 4 additions & 0 deletions changelog/add-7428-internal-logger
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Added an internal logger class, meant for use by classes within src.
4 changes: 4 additions & 0 deletions changelog/fix-7461-documents-page-api-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix Documents page loading on WooCommerce 8.2.0.
4 changes: 4 additions & 0 deletions changelog/fix-woopay-session-compat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Limit early WooPay session requests based on feature flag.
40 changes: 21 additions & 19 deletions client/checkout/woopay/email-input-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,27 @@ export const handleWooPayEmailInput = async (
// Set the initial value.
iframeHeaderValue = true;

request(
buildAjaxURL( getConfig( 'wcAjaxUrl' ), 'get_woopay_session' ),
{
_ajax_nonce: getConfig( 'woopaySessionNonce' ),
order_id: getConfig( 'order_id' ),
key: getConfig( 'key' ),
billing_email: getConfig( 'billing_email' ),
}
).then( ( response ) => {
if ( response?.data?.session ) {
iframe.contentWindow.postMessage(
{
action: 'setSessionData',
value: response,
},
getConfig( 'woopayHost' )
);
}
} );
if ( getConfig( 'isWoopayFirstPartyAuthEnabled' ) ) {
request(
buildAjaxURL( getConfig( 'wcAjaxUrl' ), 'get_woopay_session' ),
{
_ajax_nonce: getConfig( 'woopaySessionNonce' ),
order_id: getConfig( 'order_id' ),
key: getConfig( 'key' ),
billing_email: getConfig( 'billing_email' ),
}
).then( ( response ) => {
if ( response?.data?.session ) {
iframe.contentWindow.postMessage(
{
action: 'setSessionData',
value: response,
},
getConfig( 'woopayHost' )
);
}
} );
}

getWindowSize();
window.addEventListener( 'resize', getWindowSize );
Expand Down
40 changes: 21 additions & 19 deletions client/checkout/woopay/express-button/express-checkout-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,27 @@ export const expressCheckoutIframe = async ( api, context, emailSelector ) => {
// Set the initial value.
iframeHeaderValue = true;

request(
buildAjaxURL( getConfig( 'wcAjaxUrl' ), 'get_woopay_session' ),
{
_ajax_nonce: getConfig( 'woopaySessionNonce' ),
order_id: getConfig( 'order_id' ),
key: getConfig( 'key' ),
billing_email: getConfig( 'billing_email' ),
}
).then( ( response ) => {
if ( response?.data?.session ) {
iframe.contentWindow.postMessage(
{
action: 'setSessionData',
value: response,
},
getConfig( 'woopayHost' )
);
}
} );
if ( getConfig( 'isWoopayFirstPartyAuthEnabled' ) ) {
request(
buildAjaxURL( getConfig( 'wcAjaxUrl' ), 'get_woopay_session' ),
{
_ajax_nonce: getConfig( 'woopaySessionNonce' ),
order_id: getConfig( 'order_id' ),
key: getConfig( 'key' ),
billing_email: getConfig( 'billing_email' ),
}
).then( ( response ) => {
if ( response?.data?.session ) {
iframe.contentWindow.postMessage(
{
action: 'setSessionData',
value: response,
},
getConfig( 'woopayHost' )
);
}
} );
}

getWindowSize();
window.addEventListener( 'resize', getWindowSize );
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-wc-payments-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public function register_payments_scripts() {
return;
}

WC_Payments::register_script_with_dependencies( 'WCPAY_DASH_APP', 'dist/index' );
WC_Payments::register_script_with_dependencies( 'WCPAY_DASH_APP', 'dist/index', [ 'wp-api-request' ] );

wp_set_script_translations( 'WCPAY_DASH_APP', 'woocommerce-payments' );

Expand Down
41 changes: 3 additions & 38 deletions includes/class-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,14 @@

namespace WCPay;

use WC_Logger;
use WC_Payments;
use WCPay\Internal\Logger as InternalLogger;

defined( 'ABSPATH' ) || exit; // block direct access.

/**
* A wrapper class for interacting with WC_Logger.
*/
class Logger {
/**
* The holding property for our WC_Logger instance.
*
* @var WC_Logger $logger
*/
private static $logger;

const LOG_FILENAME = 'woocommerce-payments';

/**
* Add a log entry.
Expand All @@ -45,21 +36,7 @@ class Logger {
* 'debug': Debug-level messages.
*/
public static function log( $message, $level = 'info' ) {
if ( ! self::can_log() ) {
return;
}

self::init_logger();
self::$logger->log( $level, $message, [ 'source' => self::LOG_FILENAME ] );
}

/**
* Initiate logger property with the WooCommerce core logger only if it's not set already
*/
public static function init_logger() {
if ( ! isset( self::$logger ) && ! is_object( self::$logger ) ) {
self::$logger = wc_get_logger();
}
wcpay_get_container()->get( InternalLogger::class )->log( $message );
}

/**
Expand All @@ -68,19 +45,7 @@ public static function init_logger() {
* @return bool Depending on the enable_logging setting.
*/
public static function can_log() {
if ( ! function_exists( 'wc_get_logger' ) ) {
return false;
}

if ( is_null( WC_Payments::get_gateway() ) ) {
return false;
}

if ( WC_Payments::mode()->is_dev() ) {
return true;
}

return 'yes' === WC_Payments::get_gateway()->get_option( 'enable_logging' );
return wcpay_get_container()->get( InternalLogger::class )->can_log();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

use WC_Payments_Account;
use WC_Payments_Order_Service;
use WC_Payment_Gateway_WCPay;
use WCPay\Core\Mode;
use WCPay\Internal\DependencyManagement\AbstractServiceProvider;
use WCPay\Internal\Logger;
use WCPay\Internal\Proxy\HooksProxy;
use WCPay\Internal\Proxy\LegacyProxy;
use WCPay\Internal\Service\Level3Service;
Expand All @@ -25,6 +28,7 @@ class GenericServiceProvider extends AbstractServiceProvider {
* @var string[]
*/
protected $provides = [
Logger::class,
OrderService::class,
Level3Service::class,
];
Expand All @@ -33,8 +37,15 @@ class GenericServiceProvider extends AbstractServiceProvider {
* Registers all provided classes.
*/
public function register(): void {

$container = $this->getContainer();

$container->add( 'wc_get_logger', 'wc_get_logger' );
$container->addShared( Logger::class )
->addArgument( 'wc_get_logger' )
->addArgument( Mode::class )
->addArgument( WC_Payment_Gateway_WCPay::class );

$container->addShared( OrderService::class )
->addArgument( WC_Payments_Order_Service::class )
->addArgument( LegacyProxy::class )
Expand Down
169 changes: 169 additions & 0 deletions src/Internal/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* Class Logger
*
* @package WooCommerce\Payments
*/

namespace WCPay\Internal;

use Exception;
use WC_Logger;
use WC_Log_Levels;
use WC_Payment_Gateway_WCPay;
use WCPay\Core\Mode;

/**
* A wrapper class for interacting with WC_Logger.
*/
class Logger {

const LOG_FILENAME = 'woocommerce-payments';

/**
* The holding property for our WC_Logger instance.
*
* @var WC_Logger $logger
*/
private $wc_logger;

/**
* Mode
*
* @var Mode
*/
private $mode;

/**
* WC_Payment_Gateway_WCPay
*
* @var WC_Payment_Gateway_WCPay
*/
private $gateway;

/**
* Logger constructor.
*
* @param WC_Logger $wc_logger WC_Logger.
* @param Mode $mode Mode.
* @param WC_Payment_Gateway_WCPay $gateway WC_Payment_Gateway_WCPay.
*/
public function __construct( WC_Logger $wc_logger, Mode $mode, WC_Payment_Gateway_WCPay $gateway ) {
$this->wc_logger = $wc_logger;
$this->mode = $mode;
$this->gateway = $gateway;
}

/**
* Add a log entry.
*
* Note that this depends on WC_Payments gateway property to be initialized as
* we need this to access the plugins debug setting to figure out if the setting
* is turned on.
*
* @param string $message Log message.
* @param string $level One of the following:
* 'emergency': System is unusable.
* 'alert': Action must be taken immediately.
* 'critical': Critical conditions.
* 'error': Error conditions.
* 'warning': Warning conditions.
* 'notice': Normal but significant condition.
* 'info': Informational messages.
* 'debug': Debug-level messages.
*/
public function log( $message, $level = 'info' ) : void {
if ( ! $this->can_log() ) {
return;
}
$this->wc_logger->log( $level, $message, [ 'source' => self::LOG_FILENAME ] );
}

/**
* Checks if the gateway setting logging toggle is enabled.
*
* @return bool Depending on the enable_logging setting.
*/
public function can_log() {
try {
if ( $this->mode->is_dev() ) {
return true;
}
} catch ( Exception $e ) {
return false;
}
return 'yes' === $this->gateway->get_option( 'enable_logging' );
}

/**
* Creates a log entry of type emergency
*
* @param string $message To send to the log file.
*/
public function emergency( $message ) : void {
$this->log( $message, WC_Log_Levels::EMERGENCY );
}

/**
* Creates a log entry of type alert
*
* @param string $message To send to the log file.
*/
public function alert( $message ) : void {
$this->log( $message, WC_Log_Levels::ALERT );
}

/**
* Creates a log entry of type critical
*
* @param string $message To send to the log file.
*/
public function critical( $message ) : void {
$this->log( $message, WC_Log_Levels::CRITICAL );
}

/**
* Creates a log entry of type error
*
* @param string $message To send to the log file.
*/
public function error( $message ) : void {
$this->log( $message, WC_Log_Levels::ERROR );
}

/**
* Creates a log entry of type warning
*
* @param string $message To send to the log file.
*/
public function warning( $message ) : void {
$this->log( $message, WC_Log_Levels::WARNING );
}

/**
* Creates a log entry of type notice
*
* @param string $message To send to the log file.
*/
public function notice( $message ) : void {
$this->log( $message, WC_Log_Levels::NOTICE );
}

/**
* Creates a log entry of type info
*
* @param string $message To send to the log file.
*/
public function info( $message ) : void {
$this->log( $message, WC_Log_Levels::INFO );
}

/**
* Creates a log entry of type debug
*
* @param string $message To send to the log file.
*/
public function debug( $message ) : void {
$this->log( $message, WC_Log_Levels::DEBUG );
}
}
Loading

0 comments on commit 6a94393

Please sign in to comment.