Commit d1441f11 authored by Michael Iseard's avatar Michael Iseard
Browse files

Improve PHP-DI implementation + bug fixes

parent 280f86cd
<?php
namespace Kudos\Admin;
namespace Kudos\Controller;
use Kudos\Admin\Table\CampaignsTable;
use Kudos\Admin\Table\DonorsTable;
use Kudos\Admin\Table\SubscriptionsTable;
use Kudos\Admin\Table\TransactionsTable;
use Kudos\Controller\Table\CampaignsTable;
use Kudos\Controller\Table\DonorsTable;
use Kudos\Controller\Table\SubscriptionsTable;
use Kudos\Controller\Table\TransactionsTable;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
......@@ -15,6 +15,7 @@ use Kudos\Service\ActivatorService;
use Kudos\Service\AdminNotice;
use Kudos\Service\LoggerService;
use Kudos\Service\MapperService;
use Kudos\Service\PaymentService;
use Kudos\Service\RestRouteService;
use Kudos\Service\TwigService;
......@@ -59,11 +60,27 @@ class Admin {
/**
* @var MapperService
*/
private $mapper_service;
private $mapper;
/**
* @var TransactionsTable
*/
private $table;
/**
* @var \Kudos\Service\TwigService
*/
private $twig;
/**
* @var \Kudos\Service\PaymentService
*/
private $payment;
/**
* @var \Kudos\Helpers\Settings
*/
private $settings;
/**
* @var \Kudos\Service\ActivatorService
*/
private $activator;
/**
* Initialize the class and set its properties.
......@@ -74,12 +91,20 @@ class Admin {
public function __construct(
string $plugin_name,
string $version,
MapperService $mapper_service
MapperService $mapper,
TwigService $twig,
PaymentService $payment,
ActivatorService $activator,
Settings $settings
) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->mapper_service = $mapper_service;
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->mapper = $mapper;
$this->twig = $twig;
$this->payment = $payment;
$this->activator = $activator;
$this->settings = $settings;
}
......@@ -127,7 +152,7 @@ class Admin {
'manage_options',
'kudos-transactions',
function () {
include_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-transactions.php';
include_once KUDOS_PLUGIN_DIR . '/app/View/kudos-admin-transactions.php';
}
);
......@@ -144,7 +169,7 @@ class Admin {
'manage_options',
'kudos-subscriptions',
function () {
include_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-subscriptions.php';
include_once KUDOS_PLUGIN_DIR . '/app/View/kudos-admin-subscriptions.php';
}
);
......@@ -162,7 +187,7 @@ class Admin {
'manage_options',
'kudos-donors',
function () {
include_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-donors.php';
include_once KUDOS_PLUGIN_DIR . '/app/View/kudos-admin-donors.php';
}
);
......@@ -180,7 +205,7 @@ class Admin {
'manage_options',
'kudos-campaigns',
function () {
include_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-campaigns.php';
include_once KUDOS_PLUGIN_DIR . '/app/View/kudos-admin-campaigns.php';
}
);
......@@ -197,7 +222,7 @@ class Admin {
'manage_options',
'kudos-debug',
function () {
require_once KUDOS_PLUGIN_DIR . '/app/Admin/partials/kudos-admin-debug.php';
require_once KUDOS_PLUGIN_DIR . '/app/View/kudos-admin-debug.php';
}
);
add_action( "admin_print_scripts-$debug_page_hook_suffix",
......@@ -231,7 +256,7 @@ class Admin {
*/
public function prepare_transactions_page() {
add_action( "admin_enqueue_scripts", [ $this, "transactions_page_assets" ] );
$this->table = new TransactionsTable( $this->mapper_service );
$this->table = new TransactionsTable( $this->mapper );
$this->table->prepare_items();
}
......@@ -240,7 +265,7 @@ class Admin {
*/
public function prepare_subscriptions_page() {
add_action( "admin_enqueue_scripts", [ $this, 'subscriptions_page_assets' ] );
$this->table = new SubscriptionsTable($this->mapper_service);
$this->table = new SubscriptionsTable( $this->mapper, $this->payment );
$this->table->prepare_items();
}
......@@ -249,7 +274,7 @@ class Admin {
*/
public function prepare_donors_page() {
add_action( "admin_enqueue_scripts", [ $this, 'donor_page_assets' ] );
$this->table = new DonorsTable($this->mapper_service);
$this->table = new DonorsTable( $this->mapper );
$this->table->prepare_items();
}
......@@ -258,7 +283,7 @@ class Admin {
*/
public function prepare_campaigns_page() {
add_action( "admin_enqueue_scripts", [ $this, 'campaign_page_assets' ] );
$this->table = new CampaignsTable($this->mapper_service);
$this->table = new CampaignsTable( $this->mapper, $this->settings );
$this->table->prepare_items();
}
......@@ -408,6 +433,8 @@ class Admin {
die();
}
$settings = $this->settings;
switch ( $action ) {
case 'kudos_log_download':
......@@ -422,33 +449,30 @@ class Admin {
break;
case 'kudos_clear_mollie':
$settings = new Settings();
$settings->remove_setting( 'vendor_mollie' );
$settings->add_defaults();
break;
case 'kudos_clear_campaigns':
$settings = new Settings();
$settings->remove_setting( 'campaigns' );
$settings->add_defaults();
break;
case 'kudos_clear_all':
$settings = new Settings();
$settings->remove_settings();
$settings->add_defaults();
break;
case 'kudos_clear_cache':
$twig = TwigService::factory();
if ( $twig->clearCache() ) {
if ( $this->twig->clearCache() ) {
new AdminNotice( __( 'Cache cleared', 'kudos-donations' ) );
}
break;
case 'kudos_clear_transactions':
$mapper = new MapperService( TransactionEntity::class );
$records = $mapper->delete_all();
$records = $this->mapper
->get_repository( TransactionEntity::class )
->delete_all();
if ( $records ) {
new AdminNotice(
sprintf(
......@@ -462,8 +486,9 @@ class Admin {
break;
case 'kudos_clear_donors':
$mapper = new MapperService( DonorEntity::class );
$records = $mapper->delete_all();
$records = $this->mapper
->get_repository( DonorEntity::class )
->delete_all();
if ( $records ) {
new AdminNotice(
sprintf(
......@@ -476,8 +501,9 @@ class Admin {
break;
case 'kudos_clear_subscriptions':
$mapper = new MapperService( SubscriptionEntity::class );
$records = $mapper->delete_all();
$records = $this->mapper
->get_repository( SubscriptionEntity::class )
->delete_all();
if ( $records ) {
new AdminNotice(
sprintf(
......@@ -493,7 +519,6 @@ class Admin {
break;
case 'kudos_recreate_database':
$mapper = new MapperService();
foreach (
[
SubscriptionEntity::get_table_name(),
......@@ -501,9 +526,10 @@ class Admin {
DonorEntity::get_table_name(),
] as $table
) {
$mapper->delete_table( $table );
$this->mapper->delete_table( $table );
}
ActivatorService::activate();
$activator = $this->activator;
$activator->activate();
new AdminNotice( __( 'Database re-created', 'kudos-donations' ) );
}
......@@ -517,7 +543,7 @@ class Admin {
*/
public function register_settings() {
$settings = new Settings();
$settings = $this->settings;
$settings->register_settings();
}
......
<?php
namespace Kudos\Front;
namespace Kudos\Controller;
use Exception;
use Kudos\Entity\DonorEntity;
......@@ -8,15 +8,15 @@ use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Controller\ButtonController;
use Kudos\Controller\FormController;
use Kudos\Controller\MessageController;
use Kudos\Controller\ModalController;
use Kudos\Service\LoggerService;
use Kudos\Service\MapperService;
use Kudos\Service\PaymentService;
use Kudos\Service\RestRouteService;
use Kudos\Service\TwigService;
use Kudos\View\Model\Button;
use Kudos\View\Model\Form;
use Kudos\View\Model\Message;
use Kudos\View\Model\Modal;
class Front {
......@@ -38,19 +38,23 @@ class Front {
/**
* @var LoggerService
*/
private $logger_service;
private $logger;
/**
* @var PaymentService
*/
private $payment_service;
private $payment;
/**
* @var TwigService
*/
private $twig_service;
private $twig;
/**
* @var MapperService
*/
private $mapper_service;
private $mapper;
/**
* @var \Kudos\Helpers\Settings
*/
private $settings;
/**
* Initialize the class and set its properties.
......@@ -62,18 +66,20 @@ class Front {
public function __construct(
string $plugin_name,
string $version,
LoggerService $logger_service,
PaymentService $payment_service,
TwigService $twig_service,
MapperService $mapper_service
LoggerService $logger,
PaymentService $payment,
TwigService $twig,
MapperService $mapper,
Settings $settings
) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->logger_service = $logger_service;
$this->payment_service = $payment_service;
$this->twig_service = $twig_service;
$this->mapper_service = $mapper_service;
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->logger = $logger;
$this->payment = $payment;
$this->twig = $twig;
$this->mapper = $mapper;
$this->settings = $settings;
}
......@@ -254,15 +260,18 @@ class Front {
try {
// Check if the current vendor is connected, otherwise throw an exception.
if ( ! $this->payment_service::is_api_ready() ) {
if ( ! $this->payment::is_api_ready() ) {
/* translators: %s: Payment vendor (e.g. Mollie). */
throw new Exception( sprintf( __( "%s not connected.", 'kudos-donations' ),
$this->payment_service::get_vendor_name() ) );
$this->payment::get_vendor_name() ) );
}
// Twig service.
$twig = $this->twig;
// Create the form based on campaign id.
$form = new FormController( $this->twig_service );
$form->set_campaign( $atts['campaign_id'] );
$form = $this->create_form( $atts['campaign_id'] );
$form = $this->twig->render( Form::TEMPLATE, $form->to_array() );
// If type set and true then return form.
if ( isset( $atts['type'] ) && $atts['type'] === 'form' ) {
......@@ -270,13 +279,14 @@ class Front {
}
// Otherwise create modal and button and return their html.
$modal = new ModalController( $this->twig_service );
$modal->set_content( $form->render() );
$button = new ButtonController( $this->twig_service );
$modal = new Modal();
$modal->set_content( $form );
$button = new Button();
$button->set_atts( $atts );
$button->set_target( $modal->get_container_id() );
return $button->render() . $modal->render();
return $twig->render( Button::TEMPLATE, $button->to_array() ) .
$this->render_modal( $modal );
} catch ( Exception $e ) {
......@@ -292,24 +302,66 @@ class Front {
}
/**
* Wrapper for rendering a modal. Places markup in footer if setting enabled.
*
* @param $modal
*
* @return string|null
*/
private function render_modal( $modal ): ?string {
// Get markup for modal.
$modal = $this->twig->render( Modal::TEMPLATE, $modal->to_array() );
// Place markup in footer if setting enabled.
if ( Settings::get_setting( 'modal_in_footer' ) ) {
add_action( 'wp_footer',
function () use ( $modal ) {
echo $modal;
} );
// No need to return anything.
return null;
} else {
// Otherwise return the modal.
return $modal;
}
}
/**
* Builds the form object from supplied campaign_id.
*
* @throws Exception
*/
private function create_form( $campaign_id ): Form {
$form = new Form();
$form->set_campaign( $campaign_id );
$campaign_stats = $this->settings->get_campaign_stats( $campaign_id );
$form->set_campaign_stats( $campaign_stats );
return $form;
}
/**
* Create message modal with supplied header and body text.
*
* @param string $header The header text.
* @param string $body The body text.
*
* @return ModalController
* @return string
*/
private function create_message_modal( string $header, string $body ): ModalController {
private function create_message_modal( string $header, string $body ): ?string {
$message = new MessageController( $this->twig_service );
$message = new Message();
$message->set_title( $header );
$message->set_body( $body );
$modal = new ModalController( $this->twig_service );
$modal->set_content( $message );
$modal = new Modal();
$modal->set_content( $this->twig->render( Message::TEMPLATE, $message->to_array() ) );
$modal->set_class( 'kudos-message-modal' );
return $modal;
return $this->twig->render(Modal::TEMPLATE, $modal->to_array());
}
/**
......@@ -328,9 +380,10 @@ class Front {
$order_id = sanitize_text_field( $_REQUEST['kudos_order_id'] );
// Return message modal.
if ( ! empty( $order_id ) && ! empty( $token ) ) {
$mapper = $this->mapper_service;
$mapper->set_repository( TransactionEntity::class );
$transaction = $mapper->get_one_by( [ 'order_id' => $order_id ] );
$transaction = $this->mapper
->get_repository( TransactionEntity::class )
->get_one_by( [ 'order_id' => $order_id ] );
if ( $transaction && $transaction->verify_secret( $token ) ) {
$atts = $this->check_transaction( $order_id );
if ( $atts ) {
......@@ -345,11 +398,10 @@ class Front {
// Cancel subscription modal.
if ( ! empty( $token && ! empty( $subscription_id ) ) ) {
$mapper = $this->mapper_service;
$mapper->set_repository( SubscriptionEntity::class );
/** @var SubscriptionEntity $subscription */
$subscription = $mapper->get_one_by( [ 'subscription_id' => $subscription_id ] );
$subscription = $this->mapper
->get_repository( SubscriptionEntity::class )
->get_one_by( [ 'subscription_id' => $subscription_id ] );
// Bail if no subscription found.
if ( null === $subscription ) {
......@@ -357,8 +409,7 @@ class Front {
}
if ( $subscription->verify_secret( $token ) ) {
$payment_service = $this->payment_service;
if ( $payment_service->cancel_subscription( $subscription_id ) ) {
if ( $this->payment->cancel_subscription( $subscription_id ) ) {
echo $this->create_message_modal(
__( 'Subscription cancelled', 'kudos-donations' ),
__( 'We will no longer be taking payments for this subscription. Thank you for your contributions.',
......@@ -379,6 +430,34 @@ class Front {
}
}
/**
* Returns the kudos logo SVG markup.
*
* @param string|null $color
* @param int $width
*
* @return string|null
*/
public function get_kudos_logo_markup( string $color = null, int $width = 24 ): ?string {
if ( $color ) {
$lineColor = $color;
$heartColor = $color;
} else {
$lineColor = '#2ec4b6';
$heartColor = '#ff9f1c';
}
return apply_filters( 'kudos_get_kudos_logo',
$this->twig->render( 'public/logo.html.twig',
[
'width' => $width,
'lineColor' => $lineColor,
'heartColor' => $heartColor,
] ),
$width );
}
/**
* Check payment status based on local order_id
*
......@@ -390,22 +469,26 @@ class Front {
if ( $order_id ) {
$mapper = $this->mapper_service;
$mapper->set_repository( TransactionEntity::class );
$mapper = $this->mapper;
/** @var TransactionEntity $transaction */
$transaction = $mapper->get_one_by( [ 'order_id' => $order_id ] );
$transaction = $mapper
->get_repository( TransactionEntity::class )
->get_one_by( [ 'order_id' => $order_id ] );
if ( null === $transaction ) {
return false;
}
/** @var DonorEntity $donor */
$donor = $transaction->get_donor();
$donor = $mapper
->get_repository( DonorEntity::class )
->get_one_by( [ 'customer_id' => $transaction->customer_id ] );
try {
$campaign = Settings::get_campaign( $transaction->campaign_id );
} catch ( Exception $e ) {
$logger = $this->logger_service;
$logger = $this->logger;
$logger->warning( 'Error checking transaction: ' . $e->getMessage() );
}
......
<?php
namespace Kudos\Admin\Table;
namespace Kudos\Controller\Table;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
......@@ -18,15 +18,19 @@ class CampaignsTable extends WP_List_Table {
* @var MapperService
*/
private $mapper;
/**
* @var \Kudos\Helpers\Settings
*/
private $settings;
/**
* Class constructor.
*/
public function __construct( MapperService $mapper_service ) {
public function __construct( MapperService $mapper_service, Settings $settings) {
$this->mapper = $mapper_service;
$this->mapper->set_repository( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->settings = $settings;
$this->table = TransactionEntity::get_table_name();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -87,7 +91,7 @@ class CampaignsTable extends WP_List_Table {
foreach ( $campaigns as $key => $campaign ) {
$id = $campaign['id'];
$campaign_total = Settings::get_campaign_stats( $id );
$campaign_total = $this->settings->get_campaign_stats( $id );
$campaigns[ $key ]['currency'] = 'EUR';
$campaigns[ $key ]['date'] = $campaign_total['last_donation'] ?? null;
......
<?php
namespace Kudos\Admin\Table;
namespace Kudos\Controller\Table;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\TransactionEntity;
......@@ -27,8 +27,7 @@ class DonorsTable extends WP_List_Table {
public function __construct( MapperService $mapper_service ) {
$this->mapper = $mapper_service;
$this->mapper->set_repository( DonorEntity::class );
$this->table = DonorEntity::get_table_name();
$this->table = DonorEntity::get_table_name();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -83,7 +82,9 @@ class DonorsTable extends WP_List_Table {
$search = $this->get_search_data();
$search_field = ! empty( $search['field'] ) ? $search['field'] : null;
$search_term = ! empty( $search['term'] ) ? $search['term'] : null;
$donors = $this->mapper->get_all_by( [ $search_field => $search_term ] );
$donors = $this->mapper
->get_repository( DonorEntity::class )
->get_all_by( [ $search_field => $search_term ] );
return array_map( function ( $donor ) {
return $donor->to_array();
......@@ -193,7 +194,9 @@ class DonorsTable extends WP_List_Table {
*/
protected function delete_record( string $column, string $id ) {
return $this->mapper->delete( $column, $id );
return $this->mapper
->get_repository(DonorEntity::class)
->