move module.local to module

This commit is contained in:
David Remer 2020-09-01 14:21:58 +03:00
parent c443e4053c
commit f8e82bdfaf
217 changed files with 8 additions and 2 deletions

View file

@ -0,0 +1,12 @@
<?php
/**
* The extensions of the session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
return array();

View file

@ -0,0 +1,16 @@
<?php
/**
* The session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return new SessionModule();
};

View file

@ -0,0 +1,39 @@
<?php
/**
* The services of the session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelView;
return array(
'session.handler' => function ( ContainerInterface $container ) : SessionHandler {
if ( is_null( WC()->session ) ) {
return new SessionHandler();
}
$result = WC()->session->get( SessionHandler::ID );
if ( is_a( $result, SessionHandler::class ) ) {
return $result;
}
$session_handler = new SessionHandler();
WC()->session->set( SessionHandler::ID, $session_handler );
return $session_handler;
},
'session.cancellation.view' => function ( ContainerInterface $container ) : CancelView {
return new CancelView();
},
'session.cancellation.controller' => function ( ContainerInterface $container ) : CancelController {
return new CancelController(
$container->get( 'session.handler' ),
$container->get( 'session.cancellation.view' )
);
},
);

View file

@ -0,0 +1,74 @@
<?php
/**
* Controlls the cancel mechanism to step out of the PayPal order session.
*
* @package Inpsyde\PayPalCommerce\Session\Cancellation
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
/**
* Class CancelController
*/
class CancelController {
/**
* The Session handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* The view.
*
* @var CancelView
*/
private $view;
/**
* CancelController constructor.
*
* @param SessionHandler $session_handler The session handler.
* @param CancelView $view The view object.
*/
public function __construct(
SessionHandler $session_handler,
CancelView $view
) {
$this->view = $view;
$this->session_handler = $session_handler;
}
/**
* Runs the controller.
*/
public function run() {
$param_name = 'ppcp-cancel';
$nonce = 'ppcp-cancel-' . get_current_user_id();
if ( isset( $_GET[ $param_name ] ) && // Input var ok.
wp_verify_nonce(
sanitize_text_field( wp_unslash( $_GET[ $param_name ] ) ), // Input var ok.
$nonce
)
) { // Input var ok.
$this->session_handler->destroy_session_data();
}
if ( ! $this->session_handler->order() ) {
return;
}
$url = add_query_arg( array( $param_name => wp_create_nonce( $nonce ) ), wc_get_checkout_url() );
add_action(
'woocommerce_review_order_after_submit',
function () use ( $url ) {
$this->view->render_session_cancellation( $url );
}
);
}
}

View file

@ -0,0 +1,42 @@
<?php
/**
* Renders the cancel view for the order on the checkout.
*
* @package Inpsyde\PayPalCommerce\Session\Cancellation
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
/**
* Class CancelView
*/
class CancelView {
/**
* Renders the cancel link.
*
* @param string $url The URL.
*/
public function render_session_cancellation( string $url ) {
?>
<p id="ppcp-cancel"
class="has-text-align-center ppcp-cancel"
>
<?php
printf(
// translators: the placeholders are html tags for a link.
esc_html__(
'You are currently paying with PayPal. If you want to cancel
this process, please click %1$shere%2$s.',
'paypal-for-woocommerce'
),
'<a href="' . esc_url( $url ) . '">',
'</a>'
);
?>
</p>
<?php
}
}

View file

@ -0,0 +1,97 @@
<?php
/**
* The Session Handler.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
/**
* Class SessionHandler
*/
class SessionHandler {
const ID = 'ppcp';
/**
* The Order.
*
* @var Order|null
*/
private $order;
/**
* The BN Code.
*
* @var string
*/
private $bn_code = '';
/**
* Returns the order.
*
* @return Order|null
*/
public function order() : ?Order {
return $this->order;
}
/**
* Replaces the current order.
*
* @param Order $order The new order.
*
* @return SessionHandler
*/
public function replace_order( Order $order ) : SessionHandler {
$this->order = $order;
$this->store_session();
return $this;
}
/**
* Returns the BN Code.
*
* @return string
*/
public function bn_code() : string {
return $this->bn_code;
}
/**
* Replaces the BN Code.
*
* @param string $bn_code The new BN Code.
*
* @return SessionHandler
*/
public function replace_bn_code( string $bn_code ) : SessionHandler {
$this->bn_code = $bn_code;
$this->store_session();
return $this;
}
/**
* Destroys the session data.
*
* @return SessionHandler
*/
public function destroy_session_data() : SessionHandler {
$this->order = null;
$this->bn_code = '';
$this->store_session();
return $this;
}
/**
* Stores the session.
*/
private function store_session() {
WC()->session->set( self::ID, $this );
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* The session module.
*
* @package Inpsyde\PayPalCommerce\Session
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Inpsyde\PayPalCommerce\Session\Cancellation\CancelController;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
/**
* Class SessionModule
*/
class SessionModule implements ModuleInterface {
/**
* Sets up the module.
*
* @return ServiceProviderInterface
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* Run the module.
*
* @param ContainerInterface $container The container.
*/
public function run( ContainerInterface $container ) {
add_action(
'woocommerce_init',
function () use ( $container ) {
$controller = $container->get( 'session.cancellation.controller' );
/**
* The Cancel controller.
*
* @var CancelController $controller
*/
$controller->run();
}
);
}
}