From 1d4096bc13517a5541b174d178f49ecdb8185320 Mon Sep 17 00:00:00 2001 From: David Remer Date: Tue, 1 Sep 2020 10:02:47 +0300 Subject: [PATCH] move session repo to local modules --- composer.json | 1 - inc/autoload.php | 5 +- modules.local/ppcp-session/extensions.php | 8 +++ modules.local/ppcp-session/module.php | 10 ++++ modules.local/ppcp-session/services.php | 33 ++++++++++++ .../src/Cancellation/CancelController.php | 46 +++++++++++++++++ .../src/Cancellation/CancelView.php | 30 +++++++++++ .../ppcp-session/src/SessionHandler.php | 50 +++++++++++++++++++ .../ppcp-session/src/SessionModule.php | 36 +++++++++++++ 9 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 modules.local/ppcp-session/extensions.php create mode 100644 modules.local/ppcp-session/module.php create mode 100644 modules.local/ppcp-session/services.php create mode 100644 modules.local/ppcp-session/src/Cancellation/CancelController.php create mode 100644 modules.local/ppcp-session/src/Cancellation/CancelView.php create mode 100644 modules.local/ppcp-session/src/SessionHandler.php create mode 100644 modules.local/ppcp-session/src/SessionModule.php diff --git a/composer.json b/composer.json index e1d3b9194..aaede2279 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/inc/autoload.php b/inc/autoload.php index eccbb6777..a39b0b883 100644 --- a/inc/autoload.php +++ b/inc/autoload.php @@ -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] ) ) { diff --git a/modules.local/ppcp-session/extensions.php b/modules.local/ppcp-session/extensions.php new file mode 100644 index 000000000..280e6c73c --- /dev/null +++ b/modules.local/ppcp-session/extensions.php @@ -0,0 +1,8 @@ + 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') + ); + }, +]; diff --git a/modules.local/ppcp-session/src/Cancellation/CancelController.php b/modules.local/ppcp-session/src/Cancellation/CancelController.php new file mode 100644 index 000000000..89ee3eb79 --- /dev/null +++ b/modules.local/ppcp-session/src/Cancellation/CancelController.php @@ -0,0 +1,46 @@ +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); + } + ); + } +} diff --git a/modules.local/ppcp-session/src/Cancellation/CancelView.php b/modules.local/ppcp-session/src/Cancellation/CancelView.php new file mode 100644 index 000000000..4f4ec6027 --- /dev/null +++ b/modules.local/ppcp-session/src/Cancellation/CancelView.php @@ -0,0 +1,30 @@ + +

+ ', + '' + ); + ?> +

+ 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); + } +} diff --git a/modules.local/ppcp-session/src/SessionModule.php b/modules.local/ppcp-session/src/SessionModule.php new file mode 100644 index 000000000..0f398b39a --- /dev/null +++ b/modules.local/ppcp-session/src/SessionModule.php @@ -0,0 +1,36 @@ +get('session.cancellation.controller'); + /** + * @var CancelController $controller + */ + $controller->run(); + } + ); + } +}