Commit 34520ea6 authored by Michael Iseard's avatar Michael Iseard
Browse files

Getting close

parent 71558db9
......@@ -19,6 +19,10 @@ class CampaignsTable extends WP_List_Table {
* @var MapperService
*/
private $mapper;
/**
* @var Campaigns
*/
private $campaigns;
/**
* Class constructor
......@@ -29,20 +33,21 @@ class CampaignsTable extends WP_List_Table {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = $this->mapper->get_table_name();
$this->campaigns = new Campaigns();
$this->search_columns = [
'label' => __( 'Label', 'kudos-donations' ),
'name' => __( 'Name', 'kudos-donations' ),
];
$this->export_columns = [
'label' => __( 'Email', 'kudos-donations' ),
'name' => __( 'Email', 'kudos-donations' ),
'transactions' => __( 'Street', 'kudos-donations' ),
'total' => __( 'Total', 'kudos-donations' ),
];
parent::__construct(
[
'orderBy' => 'slug',
'orderBy' => 'date',
'singular' => __( 'Campaign', 'kudos-donations' ),
'plural' => __( 'Campaigns', 'kudos-donations' ),
'ajax' => false,
......@@ -75,17 +80,15 @@ class CampaignsTable extends WP_List_Table {
$mapper = $this->mapper;
$search = $this->get_search_data();
$campaigns = Settings::get_setting( 'campaigns' );
$campaigns = $this->campaigns->get_all();
if ( ! $campaigns ) {
return [];
}
// Add search query if exist.
if ( $search ) {
$campaigns = array_filter(
$campaigns,
function ( $value ) use ( $search ) {
return $value[ $search['field'] ] === $search['term'];
$campaigns = array_filter( $campaigns, function ( $value ) use ( $search ) {
return (strtolower($value[ $search['field'] ]) == strtolower($search['term']));
}
);
}
......@@ -95,7 +98,7 @@ class CampaignsTable extends WP_List_Table {
$transactions = $mapper->get_all_by( [ 'campaign_label' => $id ] );
// $campaigns[ $key ]['date'] = date("r",hexdec(substr($id,3,8)));
$campaigns[ $key ]['date'] = date("r",hexdec(substr($id,3,8)));
$campaigns[ $key ]['transactions'] = 0;
$campaigns[ $key ]['total'] = 0;
if ( $transactions ) {
......@@ -129,7 +132,8 @@ class CampaignsTable extends WP_List_Table {
*/
public function column_names(): array {
return [
'label' => __( 'Label', 'kudos-donations' ),
'date' => __('Date', 'kudos-donations'),
'name' => __( 'Name', 'kudos-donations' ),
'transactions' => __( 'Transactions', 'kudos-donations' ),
'total' => __( 'Total', 'kudos-donations' ),
'last_donation' => __( 'Last Donation', 'kudos-donations' ),
......@@ -144,6 +148,7 @@ class CampaignsTable extends WP_List_Table {
*/
public function get_hidden_columns(): array {
return [
'date',
'subscription_id',
'id',
];
......@@ -172,46 +177,6 @@ class CampaignsTable extends WP_List_Table {
];
}
/**
* Process cancel and bulk-cancel actions
*
* @since 2.0.4
*/
public function process_bulk_action() {
// Detect when a bulk action is being triggered.
switch ( $this->current_action() ) {
case 'delete':
// In our file that handles the request, verify the nonce.
if ( isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ),
'bulk-' . $this->_args['singular'] ) ) {
die();
}
if ( isset( $_GET['label'] ) ) {
self::delete_record( sanitize_text_field( wp_unslash( $_GET['label'] ) ) );
}
break;
case 'bulk-delete':
// In our file that handles the request, verify the nonce.
if ( isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ),
'bulk-' . $this->_args['plural'] ) ) {
die();
}
if ( isset( $_REQUEST['bulk-action'] ) ) {
$labels = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['bulk-action'] ) );
foreach ( $labels as $label ) {
self::delete_record( sanitize_text_field( $label ) );
}
}
break;
}
}
/**
* Delete a campaign.
*
......@@ -272,7 +237,7 @@ class CampaignsTable extends WP_List_Table {
* @return string
* @since 2.0.4
*/
protected function column_label( array $item ): string {
protected function column_name( array $item ): string {
return $item['name'];
......@@ -290,7 +255,7 @@ class CampaignsTable extends WP_List_Table {
return sprintf(
'<a href=%1$s>%2$s</a>',
sprintf( admin_url( 'admin.php?page=kudos-transactions&search-field=campaign_label&s=%s' ), rawurlencode( $item['slug'] ) ),
sprintf( admin_url( 'admin.php?page=kudos-transactions&search-field=campaign_label&s=%s' ), rawurlencode( $item['id'] ) ),
strtoupper( $item['transactions'] )
);
......@@ -327,16 +292,4 @@ class CampaignsTable extends WP_List_Table {
strtotime( $item['last_donation'] ) ) : '';
}
/**
* Returns an associative array containing the bulk action
*
* @return array|string
* @since 2.0.4
*/
protected function get_bulk_actions() {
return [
'bulk-delete' => __( 'Delete', 'kudos-donations' ),
];
}
}
......@@ -17,6 +17,10 @@ class TransactionsTable extends WP_List_Table {
* @var MapperService
*/
private $mapper;
/**
* @var Campaigns
*/
private $campaigns;
/**
* Class constructor
......@@ -27,6 +31,7 @@ class TransactionsTable extends WP_List_Table {
$this->mapper = new MapperService( TransactionEntity::class );
$this->table = TransactionEntity::get_table_name();
$this->campaigns = new Campaigns();
$this->search_columns = [
'name' => __( 'Name', 'kudos-donations' ),
......@@ -443,11 +448,15 @@ class TransactionsTable extends WP_List_Table {
*/
protected function column_campaign_label( array $item ): string {
$campaign_name = $this->campaigns->get_campaign(
$item['campaign_label'] )['name']
?? $item['campaign_label'];
return sprintf(
'<a href=%1$s>%2$s</a>',
sprintf( admin_url( 'admin.php?page=kudos-campaigns&search-field=label&s=%s' ),
rawurlencode( $item['campaign_label'] ) ),
Campaigns::get_campaign( $item['campaign_label'], 'id' )['name'] ?? $item['campaign_label']
sprintf( admin_url( 'admin.php?page=kudos-campaigns&search-field=name&s=%s' ),
rawurlencode( $campaign_name ) ),
$campaign_name
);
}
......
......@@ -5,6 +5,7 @@ namespace Kudos\Front;
use Kudos\Entity\DonorEntity;
use Kudos\Entity\SubscriptionEntity;
use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Campaigns;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\LoggerService;
......@@ -329,8 +330,7 @@ class Front {
'amount_type' => 'open',
'donation_type' => 'both',
'fixed_amounts' => '5, 10, 20, 50',
'campaign_label' => '',
'campaign_id' => '',
'campaign' => 'default',
'alignment' => 'none',
],
$atts,
......@@ -352,9 +352,9 @@ class Front {
'type' => 'string',
'default' => __( 'Donate now', 'kudos-donations' ),
],
'campaign_id' => [
'campaign' => [
'type' => 'string',
'default' => '',
'default' => 'default',
],
'alignment' => [
'type' => 'string',
......@@ -375,19 +375,19 @@ class Front {
/**
* Renders the kudos button and donation modals
*
* @param array $attr Array of kudos button/modal attributes.
* @param array $atts Array of kudos button/modal attributes.
*
* @return string|null
* @since 2.0.0
*/
public function kudos_render_callback( array $attr ): ?string {
public function kudos_render_callback( array $atts ): ?string {
// Check if ready to go and if not return error messages
$status = self::ready();
if ( isset($status['error']) ) {
if ( isset($status['error_messages']) ) {
if ( is_user_logged_in() && ! is_admin() ) {
$out = '';
foreach ( $status['messages'] as $message ) {
foreach ( $status['error_messages'] as $message ) {
$out .= "<p>$message</p>";
}
......@@ -397,8 +397,19 @@ class Front {
return null;
}
// Set campaign according to atts and if none found then set as default
$campaigns = new Campaigns();
if(!empty($atts['campaign'])) {
$campaign = $campaigns->get_campaign($atts['campaign']);
}
if(empty($campaign)) {
$campaign = $campaigns->get_campaign('default');
}
$atts = wp_parse_args($campaign, $atts);
// Create button and modal.
$button = new KudosButton( $attr );
$button = new KudosButton( $atts );
$modal = $button->get_donate_modal();
......@@ -426,10 +437,8 @@ class Front {
$return = [];
if(!$api_key || !$api_connected || !$campaigns) $return['error'] = true;
if(!$api_connected) $return['messages'][] = __( 'Mollie not connected', 'kudos-donations' );
if(!$campaigns) $return['messages'][] = __( 'No campaigns found', 'kudos-donations' );
if(!$api_connected || !$api_key) $return['error_messages'][] = __( 'Mollie not connected', 'kudos-donations' );
if(!$campaigns) $return['error_messages'][] = __( 'No campaigns found', 'kudos-donations' );
return $return;
......
......@@ -2,7 +2,6 @@
namespace Kudos\Front;
use Kudos\Helpers\Campaigns;
use Kudos\Service\TwigService;
class KudosButton {
......@@ -24,7 +23,7 @@ class KudosButton {
*
* @var bool|mixed|void
*/
private $label;
private $button_label;
/**
* Button id
*
......@@ -36,13 +35,13 @@ class KudosButton {
*
* @var string
*/
private $title;
private $modal_title;
/**
* Modal text
*
* @var string
*/
private $text;
private $welcome_text;
/**
* Donation amount selection type
*
......@@ -65,11 +64,14 @@ class KudosButton {
* @var string
*/
private $donation_type;
/**
* @var array
* @var bool
*/
private $address_enabled;
/**
* @var bool
*/
private $address;
private $address_required;
/**
* KudosButton constructor.
......@@ -80,26 +82,14 @@ class KudosButton {
*/
public function __construct( array $atts ) {
$this->twig = TwigService::factory();
$this->id = uniqid( 'kudos_modal-' );
$this->label = $atts['button_label'];
$this->alignment = $atts['alignment'];
// Set campaign according to atts and if none found or empty then set as default
if(!empty($atts['campaign_id'])) $campaign = Campaigns::get_campaign($atts['campaign_id']);
if(empty($campaign)) $campaign = Campaigns::get_campaign('default');
$this->twig = TwigService::factory();
$this->id = uniqid( 'kudos_modal-' );
$this->address = [
'enabled' => !empty($campaign['address_enabled']) ?? false,
'required' => !empty($campaign['address_required']) ?? false
];
$this->campaign_label = !empty($campaign['id']) ? $campaign['id'] : 'default';
$this->donation_type = !empty($campaign['donation_type']) ? $campaign['donation_type'] : 'both';
$this->title = !empty($campaign['modal_title']) ? $campaign['modal_title'] : '' ;
$this->text = !empty($campaign['welcome_text']) ? $campaign['welcome_text'] : '' ;
$this->amount_type = !empty($campaign['amount_type']) ? $campaign['amount_type'] : '' ;
$this->fixed_amounts = !empty($campaign['fixed_amounts']) ? $campaign['fixed_amounts'] : '' ;
foreach ( $atts as $property => $value ) {
if ( property_exists( static::class, $property ) ) {
$this->$property = $value;
}
}
}
......@@ -115,7 +105,7 @@ class KudosButton {
$data = [
'alignment' => $this->alignment,
'label' => $this->label,
'label' => $this->button_label,
'target' => $this->id,
];
......@@ -140,17 +130,16 @@ class KudosButton {
$allowed_types = ['fixed', 'open', 'both'];
$data = [
'modal_id' => $this->id,
'modal_title' => $this->title,
'modal_text' => $this->text,
'amount' => [
'type' => in_array( $this->amount_type, $allowed_types, true ) ? $this->amount_type : 'open',
'fixed_amounts' => array_slice(explode( ',', $this->fixed_amounts ),0, 4),
],
'address' => $this->address,
'donation_type' => $this->donation_type,
'campaign_label' => $this->campaign_label,
'payment_by' => __( 'Secure payment by', 'kudos-donations' ),
'modal_id' => $this->id,
'modal_title' => $this->modal_title,
'modal_text' => $this->welcome_text,
'amount_type' => in_array( $this->amount_type, $allowed_types, true ) ? $this->amount_type : 'open',
'fixed_amounts' => array_slice(explode( ',', $this->fixed_amounts ),0, 4),
'address_enabled' => $this->address_enabled,
'address_required' => $this->address_required,
'donation_type' => $this->donation_type,
'campaign_label' => $this->campaign_label,
'payment_by' => __( 'Secure payment by', 'kudos-donations' ),
];
return $modal->get_donate_modal( $data );
......
......@@ -5,15 +5,25 @@ namespace Kudos\Helpers;
class Campaigns {
/**
* Adds default campaign
* @var mixed
*/
private $campaigns;
public function __construct() {
$this->campaigns = Settings::get_setting('campaigns');
}
/**
* Adds default campaign is no campaigns found
*
* @since 2.3.0
*/
public static function add_default() {
public function add_default() {
$default_campaign[0] = [
'id' => 'default',
'slug' => 'default',
'name' => 'Default',
'modal_title' => 'Hello',
'welcome_text' => 'Welcome text',
......@@ -24,7 +34,7 @@ class Campaigns {
'protected' => true
];
if(empty(Settings::get_setting('campaigns'))) update_option(Settings::PREFIX . 'campaigns', $default_campaign);
if(empty($this->campaigns)) update_option(Settings::PREFIX . 'campaigns', $default_campaign);
}
......@@ -32,23 +42,72 @@ class Campaigns {
* Gets the campaign by specified column (e.g slug)
*
* @param string $value
* @param string $column
*
* @return array|null
* @since 2.3.0
*/
public static function get_campaign( string $value, $column = 'slug' ): ?array {
public function get_campaign( string $value ): ?array {
$forms = Settings::get_setting('campaigns');
$key = array_search($value, array_column($forms, $column));
$campaigns = $this->campaigns;
$key = array_search($value, array_column($campaigns, 'id'));
// Check if key is an index and if so return index from forms
if(is_int($key)) {
return $forms[$key];
return $campaigns[$key];
}
return null;
}
/**
* Returns all campaigns
*
* @return null|array
* @since 2.3.0
*/
public function get_all(): ?array {
return (array) $this->campaigns;
}
/**
* Sanitize the various setting fields in the donation form array
*
* @param $campaigns
*
* @return array
* @since 2.3.0
*/
public static function sanitize_campaigns($campaigns): array {
//Define the array for the updated options
$output = [];
// Loop through each of the options sanitizing the data
foreach ($campaigns as $key=>$form) {
if(!array_search('id', $form)) $output[$key]['id'] = strtoupper(uniqid('kc_'));
foreach ($form as $option=>$value) {
switch ($option) {
case 'modal_title':
case 'welcome_text':
$output[$key][$option] = sanitize_text_field($value);
break;
case 'amount_type':
case 'donation_type':
$output[$key][$option] = sanitize_key($value);
break;
default:
$output[$key][$option] = $value;
}
}
}
return $output;
}
}
......@@ -167,9 +167,6 @@ class Settings {
'id' => [
'type' => 'int'
],
'slug' => [
'type' => 'string'
],
'name' => [
'type' => 'string'
],
......@@ -201,53 +198,12 @@ class Settings {
],
],
],
'sanitize_callback' => [$this, 'sanitize_campaigns'],
'sanitize_callback' => [Campaigns::class, 'sanitize_campaigns'],
],
]
);
}
/**
* Sanitize the various setting fields in the donation form array
*
* @param $forms
*
* @return array
* @since 2.3.0
*/
public static function sanitize_campaigns($forms): array {
//Define the array for the updated options
$output = [];
// Loop through each of the options sanitizing the data
foreach ($forms as $key=>$form) {
if(!array_search('id', $form)) $output[$key]['id'] = uniqid('kc_');
foreach ($form as $option=>$value) {
switch ($option) {
case 'modal_title':
case 'welcome_text':
$output[$key][$option] = sanitize_text_field($value);
break;
case 'amount_type':
case 'donation_type':
$output[$key][$option] = sanitize_key($value);
break;
case 'slug':
$output[$key][$option] = sanitize_title($value);
break;
default:
$output[$key][$option] = $value;
}
}
}
return $output;
}
/**
* Returns setting value
*
......
......@@ -219,7 +219,9 @@ class ActivatorService {
$settings = new Settings();
$settings->add_defaults();
Campaigns::add_default();
$campaigns = new Campaigns();
$campaigns->add_default();
}
}
import { CampaignPanel } from "./CampaignPanel"
import {CampaignPanel} from "./CampaignPanel"
const { __, sprintf } = wp.i18n;
const { PanelBody, TextControl, Button } = wp.components;
const { useState } = wp.element;
const AddCampaignPanel = ( props ) => {
const AddCampaignPanel = ({settings, showNotice, updateSetting, handleInputChange} ) => {
const [ addFormValue, setAddFormValue ] = useState('');
const [ buttonDisabled, setButtonDisabled ] = useState(true);
let current = props.settings._kudos_campaigns;
const isValid = ( value ) => {
if( '' === value.trim() || current.find( x => x.name.toLowerCase() === value.toLowerCase().trim() ) ) {
setButtonDisabled(true)
return false;
}
return true
let current = settings._kudos_campaigns;