Commit 280f86cd authored by Michael Iseard's avatar Michael Iseard
Browse files

Initial implementation of PHP-DI

parent 116eb846
......@@ -17,7 +17,6 @@ use Kudos\Service\LoggerService;
use Kudos\Service\MapperService;
use Kudos\Service\RestRouteService;
use Kudos\Service\TwigService;
use WP_List_Table;
/**
* The admin-specific functionality of the plugin.
......@@ -44,7 +43,6 @@ class Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
......@@ -53,13 +51,17 @@ class Admin {
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* @var MapperService
*/
private $mapper_service;
/**
* @var WP_List_Table
* @var TransactionsTable
*/
private $table;
......@@ -68,20 +70,21 @@ class Admin {
*
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*
* @since 1.0.0
*/
public function __construct( string $plugin_name, string $version ) {
public function __construct(
string $plugin_name,
string $version,
MapperService $mapper_service
) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->mapper_service = $mapper_service;
}
/**
* Create the Kudos Donations admin pages.
*
* @since 2.0.0
*/
public function kudos_add_menu_pages() {
......@@ -228,7 +231,7 @@ class Admin {
*/
public function prepare_transactions_page() {
add_action( "admin_enqueue_scripts", [ $this, "transactions_page_assets" ] );
$this->table = new TransactionsTable();
$this->table = new TransactionsTable( $this->mapper_service );
$this->table->prepare_items();
}
......@@ -237,9 +240,8 @@ class Admin {
*/
public function prepare_subscriptions_page() {
add_action( "admin_enqueue_scripts", [ $this, 'subscriptions_page_assets' ] );
$this->table = new SubscriptionsTable();
$this->table = new SubscriptionsTable($this->mapper_service);
$this->table->prepare_items();
}
/**
......@@ -247,7 +249,7 @@ class Admin {
*/
public function prepare_donors_page() {
add_action( "admin_enqueue_scripts", [ $this, 'donor_page_assets' ] );
$this->table = new DonorsTable();
$this->table = new DonorsTable($this->mapper_service);
$this->table->prepare_items();
}
......@@ -256,14 +258,12 @@ class Admin {
*/
public function prepare_campaigns_page() {
add_action( "admin_enqueue_scripts", [ $this, 'campaign_page_assets' ] );
$this->table = new CampaignsTable();
$this->table = new CampaignsTable($this->mapper_service);
$this->table->prepare_items();
}
/**
* Assets specific to the Settings page.
*
* @since 2.0.0
*/
public function settings_page_assets() {
......@@ -287,7 +287,7 @@ class Admin {
$handle,
'kudos',
[
'version' => KUDOS_VERSION,
'version' => $this->version,
'checkApiUrl' => rest_url( RestRouteService::NAMESPACE . RestRouteService::PAYMENT_TEST ),
'sendTestUrl' => rest_url( RestRouteService::NAMESPACE . RestRouteService::EMAIL_TEST ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
......@@ -300,8 +300,6 @@ class Admin {
/**
* Assets common to all Table pages.
*
* @since 2.0.0
*/
private function table_page_assets(): string {
......@@ -320,8 +318,6 @@ class Admin {
/**
* Assets specific to the Kudos Transactions page.
*
* @since 2.0.0
*/
public function transactions_page_assets() {
......@@ -346,8 +342,6 @@ class Admin {
/**
* Assets specific to the Kudos Subscriptions page.
*
* @since 2.0.0
*/
public function subscriptions_page_assets() {
......@@ -365,8 +359,6 @@ class Admin {
/**
* Assets specific to the Kudos Donors page.
*
* @since 2.0.0
*/
public function donor_page_assets() {
......@@ -383,8 +375,6 @@ class Admin {
/**
* Assets specific to the Kudos Campaigns page.
*
* @since 2.0.0
*/
public function campaign_page_assets() {
......@@ -405,8 +395,6 @@ class Admin {
/**
* Actions triggered by request data in the admin.
* Needs to be hooked to admin_init as it modifies headers.
*
* @since 1.0.1
*/
public function admin_actions() {
......@@ -526,8 +514,6 @@ class Admin {
/**
* Register the kudos settings.
*
* @since 2.0.0
*/
public function register_settings() {
......
......@@ -22,10 +22,11 @@ class CampaignsTable extends WP_List_Table {
/**
* Class constructor.
*/
public function __construct() {
public function __construct( MapperService $mapper_service ) {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->mapper = $mapper_service;
$this->mapper->set_repository( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -69,7 +70,7 @@ class CampaignsTable extends WP_List_Table {
$search = $this->get_search_data();
$campaigns = Settings::get_setting('campaigns');
$campaigns = Settings::get_setting( 'campaigns' );
if ( ! $campaigns ) {
return [];
}
......
......@@ -6,7 +6,7 @@ use Kudos\Entity\DonorEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Utils;
use Kudos\Service\MapperService;
use WP_List_Table;
use \WP_List_Table;
class DonorsTable extends WP_List_Table {
......@@ -24,10 +24,11 @@ class DonorsTable extends WP_List_Table {
*
* @since 2.0.0
*/
public function __construct() {
public function __construct( MapperService $mapper_service ) {
$this->mapper = new MapperService( DonorEntity::class );
$this->table = DonorEntity::get_table_name();
$this->mapper = $mapper_service;
$this->mapper->set_repository( DonorEntity::class );
$this->table = DonorEntity::get_table_name();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -276,9 +277,10 @@ class DonorsTable extends WP_List_Table {
$item['country'],
];
$address = array_filter($address, function ($item) {
return !empty($item) ? wp_unslash($item) : null;
});
$address = array_filter( $address,
function ( $item ) {
return ! empty( $item ) ? wp_unslash( $item ) : null;
} );
return implode( '<br/>', $address );
}
......@@ -293,7 +295,8 @@ class DonorsTable extends WP_List_Table {
*/
protected function column_donations( array $item ) {
$mapper = new MapperService( TransactionEntity::class );
$mapper = $this->mapper;
$mapper->set_repository(TransactionEntity::class);
$transactions = $mapper->get_all_by( [ 'customer_id' => $item['customer_id'] ] );
if ( $transactions ) {
......
......@@ -23,9 +23,10 @@ class SubscriptionsTable extends WP_List_Table {
*
* @since 2.0.0
*/
public function __construct() {
public function __construct(MapperService $mapper_service) {
$this->mapper = new MapperService( SubscriptionEntity::class );
$this->mapper = $mapper_service;
$this->mapper->set_repository(SubscriptionEntity::class);
$this->table = SubscriptionEntity::get_table_name();
$this->search_columns = [
......
......@@ -8,7 +8,7 @@ use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\MapperService;
use WP_List_Table;
use \WP_List_Table;
class TransactionsTable extends WP_List_Table {
......@@ -21,13 +21,12 @@ class TransactionsTable extends WP_List_Table {
/**
* Class constructor
*
* @since 1.0.0
*/
public function __construct() {
public function __construct( MapperService $mapper_service ) {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = TransactionEntity::get_table_name();
$this->mapper = $mapper_service;
$this->mapper->set_repository( TransactionEntity::class );
$this->table = TransactionEntity::get_table_name();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -63,8 +62,6 @@ class TransactionsTable extends WP_List_Table {
/**
* Call this function where the table is to be displayed
*
* @since 1.0.0
*/
public function display() {
......@@ -78,7 +75,6 @@ class TransactionsTable extends WP_List_Table {
* Get the table data
*
* @return array
* @since 1.0.0
*/
public function fetch_table_data(): array {
......@@ -122,7 +118,6 @@ class TransactionsTable extends WP_List_Table {
* Returns a list of columns to include in table
*
* @return array
* @since 2.0.0
*/
public function column_names(): array {
......@@ -146,7 +141,6 @@ class TransactionsTable extends WP_List_Table {
* Define which columns are hidden
*
* @return array
* @since 1.0.0
*/
public function get_hidden_columns(): array {
return [
......@@ -160,7 +154,6 @@ class TransactionsTable extends WP_List_Table {
* Define the sortable columns
*
* @return array
* @since 1.0.0
*/
public function get_sortable_columns(): array {
......@@ -187,7 +180,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string
* @since 1.0.0
*/
function column_cb( $item ): string {
......@@ -201,7 +193,6 @@ class TransactionsTable extends WP_List_Table {
/**
* Process delete and bulk-delete actions
*
* @since 1.0.0
*/
public function process_bulk_action() {
......@@ -246,7 +237,6 @@ class TransactionsTable extends WP_List_Table {
* @param string $id Value to search for.
*
* @return false|int
* @since 1.0.0
*/
protected function delete_record( string $column, string $id ) {
......@@ -260,7 +250,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string
* @since 1.0.0
*/
protected function column_created( array $item ): string {
......@@ -298,7 +287,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string|null
* @since 2.0.0
*/
protected function column_name( array $item ): ?string {
......@@ -322,7 +310,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string
* @since 1.0.0
*/
protected function column_email( array $item ): string {
......@@ -343,7 +330,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string|void
* @since 1.0.0
*/
protected function column_value( array $item ): string {
......@@ -382,7 +368,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string|void
* @since 2.0.0
*/
protected function column_type( array $item ): string {
......@@ -396,7 +381,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string|void
* @since 1.0.0
*/
protected function column_status( array $item ): string {
......@@ -434,7 +418,6 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string|void
* @since 2.0.0
*/
protected function column_order_id( array $item ): string {
......@@ -448,13 +431,12 @@ class TransactionsTable extends WP_List_Table {
* @param array $item Array of results.
*
* @return string
* @since 2.0.2
*/
protected function column_campaign_id( array $item ): string {
try {
$campaign = Settings::get_campaign( $item['campaign_id'] );
} catch ( Exception $e) {
} catch ( Exception $e ) {
return $e->getMessage();
}
......@@ -475,7 +457,6 @@ class TransactionsTable extends WP_List_Table {
* Returns an associative array containing the bulk action
*
* @return array
* @since 1.0.0
*/
protected function get_bulk_actions(): array {
......@@ -489,7 +470,6 @@ class TransactionsTable extends WP_List_Table {
* Gets view data
*
* @return array
* @since 1.0.0
*/
protected function get_views(): array {
......
......@@ -43,8 +43,7 @@ $tab = $_GET['tab'] ?? $default_tab;
return;
}
$kudos_logger = LoggerService::factory();
$log_array = $kudos_logger->get_as_array();
$log_array = LoggerService::get_as_array();
?>
<p>This logfile location: <?php echo esc_url( $file ); ?></p>
......
<?php
namespace Kudos\Model;
namespace Kudos\Controller;
use Kudos\Helpers\Utils;
use Kudos\Service\TwigService;
class ButtonModel extends AbstractModel {
class ButtonController extends Controller {
const TEMPLATE = 'public/button/kudos.button.html.twig';
......@@ -35,24 +36,22 @@ class ButtonModel extends AbstractModel {
/**
* Button constructor.
*
* @param array $atts Array of above attributes.
* @param string $target Target modal id.
*
* @since 1.0.0
*/
public function __construct( array $atts, string $target ) {
public function __construct(TwigService $twig_service) {
parent::__construct();
parent::__construct($twig_service);
$this->template = self::TEMPLATE;
$this->logo = apply_filters( 'kudos_get_button_logo', Utils::get_kudos_logo_markup( 'white' ) );
$this->target = $target;
// Assign button atts to properties.
}
public function set_atts( array $atts ) {
$this->alignment = $atts['alignment'] ?? '';
$this->button_label = $atts['button_label'] ?? '';
}
public function set_target( string $target ) {
$this->target = $target;
}
/**
......
<?php
namespace Kudos\Model;
namespace Kudos\Controller;
use Kudos\Helpers\Utils;
use Kudos\Service\TwigService;
abstract class AbstractModel {
class Controller {
/**
* The id of the element.
......@@ -20,14 +20,18 @@ abstract class AbstractModel {
* @var
*/
protected $template;
/**
* @var TwigService
*/
private $twig_service;
/**
* AbstractRender constructor.
*/
public function __construct() {
public function __construct( TwigService $twig_service ) {
$this->twig_service = $twig_service;
$this->container_id = Utils::generate_id();
$this->template = static::TEMPLATE;
}
/**
......@@ -46,8 +50,9 @@ abstract class AbstractModel {
*
* @return string|null
*/
public function render( array $atts = null): ?string {
$twig = TwigService::factory();
public function render( array $atts = null ): ?string {
$twig = $this->twig_service;
return $twig->render( $this->template, $atts ?? $this->to_array() );
}
......
<?php
namespace Kudos\Model;
namespace Kudos\Controller;
use Exception;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\TwigService;
class FormModel extends AbstractModel {
class FormController extends Controller {
const TEMPLATE = 'public/forms/donate.form.html.twig';
......@@ -89,29 +90,30 @@ class FormModel extends AbstractModel {
/**
* KudosForm constructor.
*
* @param string $campaign_id
*
* @throws Exception
*/
public function __construct( string $campaign_id ) {
parent::__construct();
public function __construct( TwigService $twig_service ) {
// Throws exception if campaign not found.
$campaign = Settings::get_campaign( $campaign_id );
parent::__construct( $twig_service );
// Configure global properties.
$this->template = self::TEMPLATE;
$this->campaign_id = $campaign_id;
$this->return_url = Utils::get_return_url();
$this->privacy_link = Settings::get_setting( 'privacy_link' );
$this->terms_link = Settings::get_setting( 'terms_link' );
$this->recurring_allowed = isset( Settings::get_current_vendor_settings()['recurring'] ) ?? false;
$this->vendor_name = Settings::get_setting( 'payment_vendor' );
$this->spinner = Utils::get_kudos_logo_markup( 'black', 30 );
$this->spinner = Utils::get_kudos_logo_markup( 'black', 30 );
}
/**
* @throws Exception
*/
public function set_campaign( $campaign_id ) {
// Throws exception if campaign not found.
$campaign = Settings::get_campaign( $campaign_id );
// Set campaign properties.
$this->campaign_id = $campaign_id;
$this->campaign_stats = Settings::get_campaign_stats( $campaign_id );
$this->button_label = $campaign['button_label'] ?? '';
$this->welcome_title = $campaign['modal_title'] ?? '';
......@@ -126,11 +128,4 @@ class FormModel extends AbstractModel {
$this->message_enabled = $campaign['message_enabled'] ?? '';
}
/**
* @return mixed|string
*/
public function get_button_label() {
return $this->button_label;
}
}
\ No newline at end of file
<?php
namespace Kudos\Model;
namespace Kudos\Controller;
class MessageModel extends AbstractModel {
use Kudos\Service\TwigService;
class MessageController extends Controller {
const TEMPLATE = 'public/modal/embeds/_message.html.twig';
......@@ -17,17 +19,18 @@ class MessageModel extends AbstractModel {
/**
* Creates a message modal.
*
* @param $title
* @param $body
*/
public function __construct($title, $body) {
public function __construct(TwigService $twig_service) {
parent::__construct($twig_service);
parent::__construct();
}
$this->template = self::TEMPLATE;
public function set_title($title) {
$this->header_text = $title;
$this->body_text = $body;
}