move session repo to local modules

This commit is contained in:
David Remer 2020-09-01 10:02:47 +03:00
parent 81637ebe6d
commit 1d4096bc13
9 changed files with 216 additions and 3 deletions

View file

@ -24,7 +24,6 @@
"dhii/module-interface": "0.2.x-dev",
"psr/container": "^1.0",
"inpsyde/ppcp-admin-notices": "dev-master",
"inpsyde/ppcp-session": "dev-master",
"oomphinc/composer-installers-extender": "^1.1",
"container-interop/service-provider": "^0.4.0",
"dhii/containers": "dev-develop",

View file

@ -28,13 +28,14 @@ function autoload() {
$class_parts = explode( '\\', $class_name );
$module_dir = dirname( __DIR__ ) . '/modules.local/';
$modules = array(
'ApiClient' => $module_dir . 'ppcp-api-client/src/',
'ApiClient' => $module_dir . 'ppcp-api-client/src/',
'Button' => $module_dir . 'ppcp-button/src/',
'Logging' => $module_dir . 'woocommerce-logging/src/',
'Onboarding' => $module_dir . 'ppcp-onboarding/src/',
'Session' => $module_dir . 'ppcp-sesion/src/',
'Subscription' => $module_dir . 'ppcp-subscription/src/',
'WcGateway' => $module_dir . 'ppcp-wc-gateway/src/',
'Webhooks' => $module_dir . 'ppcp-webhooks/src/',
'Logging' => $module_dir . 'woocommerce-logging/src/',
);
if ( isset( $class_parts[2] ) && ! isset( $class_parts[3] ) ) {

View file

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
return [
];

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return new SessionModule();
};

View file

@ -0,0 +1,33 @@
<?php
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 [
'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;
}
$sessionHandler = new SessionHandler();
WC()->session->set(SessionHandler::ID, $sessionHandler);
return $sessionHandler;
},
'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,46 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
use Inpsyde\PayPalCommerce\Session\SessionHandler;
class CancelController
{
private $sessionHandler;
private $view;
public function __construct(
SessionHandler $sessionHandler,
CancelView $view
) {
$this->view = $view;
$this->sessionHandler = $sessionHandler;
}
public function run()
{
$paramName = 'ppcp-cancel';
$nonce = 'ppcp-cancel-' . get_current_user_id();
if (isset($_GET[$paramName]) && // Input var ok.
wp_verify_nonce(
sanitize_text_field(wp_unslash($_GET[$paramName])), // Input var ok.
$nonce
)
) { // Input var ok.
$this->sessionHandler->destroySessionData();
}
if (! $this->sessionHandler->order()) {
return;
}
$url = add_query_arg([$paramName => wp_create_nonce($nonce)], wc_get_checkout_url());
add_action(
'woocommerce_review_order_after_submit',
function () use ($url) {
$this->view->renderSessionCancelation($url);
}
);
}
}

View file

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session\Cancellation;
class CancelView
{
public function renderSessionCancelation(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.',
'woocommerce-paypal-commerce-gateway'
),
'<a href="'. esc_url($url) . '">',
'</a>'
);
?>
</p>
<?php
}
}

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Session;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
class SessionHandler
{
const ID = 'ppcp';
private $order;
private $bnCode = '';
public function order() : ?Order
{
return $this->order;
}
public function replaceOrder(Order $order) : SessionHandler
{
$this->order = $order;
$this->storeSession();
return $this;
}
public function bnCode() : string
{
return $this->bnCode;
}
public function replaceBnCode(string $bnCode) : SessionHandler
{
$this->bnCode = $bnCode;
$this->storeSession();
return $this;
}
public function destroySessionData() : SessionHandler
{
$this->order = null;
$this->bnCode = '';
$this->storeSession();
return $this;
}
private function storeSession()
{
WC()->session->set(self::ID, $this);
}
}

View file

@ -0,0 +1,36 @@
<?php
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 implements ModuleInterface
{
public function setup(): ServiceProviderInterface
{
return new ServiceProvider(
require __DIR__.'/../services.php',
require __DIR__.'/../extensions.php'
);
}
public function run(ContainerInterface $container)
{
add_action(
'woocommerce_init',
function () use ($container) {
$controller = $container->get('session.cancellation.controller');
/**
* @var CancelController $controller
*/
$controller->run();
}
);
}
}